4
votes

I have created new app on OpenShift using this image: https://hub.docker.com/r/luiscoms/openshift-rabbitmq/

It runs successfully and I can use it. I have added a persistent volume to it. However, every time a POD is restarted, I loos all my data. This is because RabbitMq uses a hostname to create database directory.

For example:

node           : rabbit@openshift-rabbitmq-11-9b6p7
home dir       : /var/lib/rabbitmq
config file(s) : /etc/rabbitmq/rabbitmq.config
cookie hash    : BsUC9W6z5M26164xPxUTkA==
log            : tty
sasl log       : tty
database dir   : /var/lib/rabbitmq/mnesia/rabbit@openshift-rabbitmq-11-9b6p7

How can I set RabbitMq to always use same database dir?

3

3 Answers

6
votes

You should be able to set an environment variable RABBITMQ_MNESIA_DIR to override the default configuration. This can be done via the OpenShift console by add an entry to environment in the deployment config or via the oc tool, for example:

oc set env dc/my-rabbit RABBITMQ_MNESIA_DIR=/myDir

You would then need to mount the persistent volume inside the Pod at the required path. Since you have said it is already created, then you just need to update it, example:

oc volume dc/my-rabbit --add --overwrite --name=my-pv-name --mount-path=/myDir

You will need to make sure you have correct r/w access on the provided mount path

EDIT: Some additional workarounds based on issues in comments

The issues caused by the dynamic hostname could be solved in a number of ways:

1.(Preferred IMO) Move the deployment to a StatefulSet. StatefulSet will provide stability in the naming and hence network identifier of the Pod, which must be fronted by a headless service. This feature is out of beta as of Kubernetes 1.9 and tech preview in OpenShift since version 3.5

  1. Set the hostname for the Pod if Statefulsets are not an option. This can be done by adding the environment variable oc set env dc/example HOSTNAME=example to make the hostname static and setting RABBITMQ_NODENAME to do likewise.
1
votes

I was able to get it to work by setting the HOSTNAME environment variable. OSE normally sets that value to the pod name, so it changes everytime the pod restarts. By setting it the pod's hostname doesn't change when the pod restarts.

Combined with a Persistent Volume the the queues, messages users and i assume whatever other configuration is persisted through pod restarts.

This was done on an OSE 3.2 server. I just added an environment variable to the deployment config. You can do it through the UI or with the OC CLI:

oc set env dc/my-rabbit HOSTNAME=some-static-name

This will probably be an issue if you run multiple pods for the service, but in that case you would need to setup proper RabbitMq clustering, which is a whole different beast.

0
votes

The easiest and production-safest way to run RabbitMQ on K8s including OpenShift is the RabbitMQ Cluster Operator.

See this video on how to deploy RabbitMQ on OpenShift.