Ask Your Question

Revision history [back]

Certainly! Here's a straightforward YAML configuration for a Kubernetes RabbitMQ cluster that includes the Erlang cookie:

apiVersion: v1
kind: Service
metadata:
  name: rabbitmq
spec:
  type: NodePort
  ports:
    - name: amqp
      port: 5672
      targetPort: 5672
    - name: management
      port: 15672
      targetPort: 15672
  selector:
    app: rabbitmq
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: rabbitmq
spec:
  selector:
    matchLabels:
      app: rabbitmq
  serviceName: rabbitmq
  replicas: 3
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: rabbitmq
    spec:
      terminationGracePeriodSeconds: 60
      containers:
        - name: rabbitmq
          image: rabbitmq:3.8.16
          env:
            - name: RABBITMQ_ERLANG_COOKIE
              valueFrom:
                secretKeyRef:
                  name: rabbitmq-erlang-cookie
                  key: erlang-cookie
          ports:
            - name: amqp
              containerPort: 5672
            - name: management
              containerPort: 15672
          volumeMounts:
            - name: rabbitmq-data
              mountPath: /var/lib/rabbitmq
          readinessProbe:
            tcpSocket:
              port: 5672
            initialDelaySeconds: 30
            periodSeconds: 10
          livenessProbe:
            tcpSocket:
              port: 5672
            initialDelaySeconds: 30
            periodSeconds: 10
      volumes:
        - name: rabbitmq-data
          persistentVolumeClaim:
            claimName: rabbitmq-pvc
  volumeClaimTemplates:
    - metadata:
        name: rabbitmq-pvc
      spec:
        accessModes: [ "ReadWriteOnce" ]
        resources:
          requests:
            storage: 10Gi

This configuration includes a YAML definition for a Kubernetes service that exposes the RabbitMQ port and management port to allow external access. The StatefulSet definition then creates a RabbitMQ cluster with three replicas, each of which has the Erlang cookie passed in as an environment variable.

To create the secret containing the Erlang cookie, you can use the following command:

kubectl create secret generic rabbitmq-erlang-cookie --from-literal=erlang-cookie=$(openssl rand -hex 32)

This will generate a random string of 32 hexadecimal characters and store it in the rabbitmq-erlang-cookie secret. The RABBITMQ_ERLANG_COOKIE environment variable in the YAML definition then reads this secret to set the Erlang cookie.

With this configuration, the Erlang cookie will be seamlessly transferred between all replicas in the RabbitMQ cluster, ensuring that they can communicate with each other without any user-related problems.