Kubernetes RabbitMQ Init script

5/27/2021

I want init exchange, queue, and binding after running RabbitMQ in a Kubernetes pod.

However, my commands are not executing.

Here is my deployment config:

...
      containers:
        - image: rabbitmq:3-management
          startupProbe:
            failureThreshold: 10
            exec:
              command: 
                - /opt/rabbitmq/sbin/rabbitmq-diagnostics
                - -q
                - ping
            initialDelaySeconds: 10
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 20
          lifecycle:
            postStart:
              exec:
                command: 
                  - /bin/bash
                  - -c
                  - |
                    /bin/chown rabbitmq:rabbitmq /var/lib/rabbitmq/.erlang.cookie ;
                    /bin/chmod 400 /var/lib/rabbitmq/.erlang.cookie ;
                    /bin/echo "--------------------------- Run Init RabbitMQ Script. Waiting... ----" ;
                    /bin/echo "--------------------------- Run commands ----" ;
                    /opt/rabbitmq/sbin/rabbitmqctl set_permissions -p / rabbitmq ".*" ".*" ".*" ;
                    /bin/echo "--------------------------- Comand 1 ----" ;
                    /usr/local/bin/rabbitmqadmin -u rabbitmq -p nZUjrgCp88ZLdXPW declare exchange --vhost=/ name=edi1c-sources-production type=direct ;
                    /bin/echo "--------------------------- Comand 2 ----" ;
                    /usr/local/bin/rabbitmqadmin -u rabbitmq -p nZUjrgCp88ZLdXPW declare queue --vhost=/ name=edi1c-sources-production durable=true ;
                    /bin/echo "--------------------------- Comand 3 ----" ;
                    /usr/local/bin/rabbitmqadmin -u rabbitmq -p nZUjrgCp88ZLdXPW --vhost="/" declare binding source="edi1c-sources-production" destination_type="queue" destination="edi1c-sources-production" routing_key="" ; 
                    /bin/echo "--------------------------- Init script Finished! ----" ;
...

I can't see any logs about my script running after the pod starts.

Maybe this can be done with a bash sleep command?

-- Maksim Borodov
bash
kubernetes
rabbitmq
rabbitmqctl

1 Answer

6/6/2021

the best way to do that is to use the rabbitmq operator and import the definition. See the RabbitMQ operator page and the definition example

-- Gabriele Santomaggio
Source: StackOverflow