0
votes

My Scenario is I'm creating rabbmitmq cluster deployment file in which i set replica set 3 because I'm creating 3 node cluster. the goal is when pod 1 created then after the second and third pod created then run the command on only on pod 2 and 3 . the command is "rabbitmqctl join_cluster command [email protected]" command to join with pod 1. which is i don't understand how can I achieve it.

I've attached my statefulset yaml file. Please guide as I'm new learner in Kubernetes.

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: rabbitmq
spec:
  serviceName: rabbitmq-internal
  revisionHistoryLimit: 3
  updateStrategy:
    type: RollingUpdate
  replicas: 3
  selector: 
    matchLabels:
       app: rabbitmq
  template:
    metadata:
      name: rabbitmq
      labels:
        app: rabbitmq
    spec:
      imagePullSecrets:
      - name: dockerhublogin
      - name: rabbitmq
      serviceAccountName: rabbitmq
      terminationGracePeriodSeconds: 10
      containers:        
      - name: rabbitmq
        image: constellationdealer/ptlids:rabbitmq
        lifecycle:
          postStart:
            exec:
              command:             
                - /bin/sh
                - -c
                - >
                  until rabbitmqctl --erlang-cookie ${RABBITMQ_ERLANG_COOKIE} await_startup; do sleep 1; done;
                  rabbitmqctl --erlang-cookie ${RABBITMQ_ERLANG_COOKIE} set_policy ha-two "" '{"ha-mode":"exactly", "ha-params": 2, "ha-sync-mode": "automatic"}'
                  #rabbitmqctl stop_app  &&  join_cluster rabbit@(HOSTNAME}.rabbitmq-internal.default.svc.cluster.local  && rabbitmqctl start_app

        ports:
        - containerPort: 4369
        - containerPort: 5672
        - containerPort: 25672
        - containerPort: 15672
        resources:
          requests:
            memory: "300Mi"
            cpu: "0.4"
          limits:
            memory: "500Mi"
            cpu: "0.6"
        livenessProbe:
          exec:
            command: ["rabbitmq-diagnostics", "status", "--erlang-cookie", "$(RABBITMQ_ERLANG_COOKIE)"]
          initialDelaySeconds: 60
          periodSeconds: 60
          timeoutSeconds: 15
        readinessProbe:
          exec:
            command: ["rabbitmq-diagnostics", "status", "--erlang-cookie", "$(RABBITMQ_ERLANG_COOKIE)"]
            # command: ["rabbitmq-diagnostics", "check_port_connectivity", "--erlang-cookie", "$(RABBITMQ_ERLANG_COOKIE)"]
          initialDelaySeconds: 20
          periodSeconds: 60
          timeoutSeconds: 10
        envFrom:
         - configMapRef:
             name: rabbitmq-cfg
        env:
          - name: HOSTNAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
          - name: RABBITMQ_USE_LONGNAME
            value: "true"
          - name: RABBITMQ_NODENAME
            value: "rabbit@$(HOSTNAME).rabbitmq-internal.$(NAMESPACE).svc.cluster.local"
          - name: K8S_SERVICE_NAME
            value: "rabbitmq-internal"
          - name: RABBITMQ_DEFAULT_USER
            value: admin
          - name: RABBITMQ_DEFAULT_PASS
            value: password
          - name: RABBITMQ_ERLANG_COOKIE
            value: secret_cookie
          - name: NODE_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name

1

1 Answers

0
votes

which is i don't understand how can I achieve it.

While it is possible to form cluster Manually with rabbitmqctl, the RabbitMQ official documentation says that "a RabbitMQ cluster can be formed in a number of ways", one of which is declaratively using Kubernetes (K8s) discovery (via a plugin).

In case of K8s that is the preferred one.

The peer discovery on K8s is explained here.

Additionally, there is a concept of the Init Container in K8s. That is the container that runs to completion before the application container starts. It can create anything that can later be used by the "main" container. Even a set of commands to be ran by main container.