<p>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 &quot;rabbitmqctl join_cluster command rabbit@pod1.svc&quot; command to join with pod 1. which is i don't understand how can I achieve it.</p>
<p>I've attached my statefulset yaml file. Please guide as I'm new learner in Kubernetes.</p>
<pre>
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

</pre>

rizwan nadeem
<blockquote>
<p>which is i don't understand how can I achieve it.</p>
</blockquote>
<p>While it is possible to form cluster <em>Manually</em> with <code>rabbitmqctl</code>, the <a href="https://www.rabbitmq.com/clustering.html#cluster-formation" rel="nofollow noreferrer"> RabbitMQ official documentation</a> says that &quot;a RabbitMQ cluster can be formed in a number of ways&quot;, one of which is declaratively using <a href="https://github.com/rabbitmq/rabbitmq-peer-discovery-k8s" rel="nofollow noreferrer">Kubernetes (K8s) discovery</a> (via a plugin).</p>
<p>In case of K8s that is the preferred one.</p>
<p>The peer discovery on K8s is explained <a href="https://www.rabbitmq.com/cluster-formation.html#peer-discovery-k8s" rel="nofollow noreferrer">here</a>.</p>
<p>Additionally, there is a concept of the <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-initialization/" rel="nofollow noreferrer">Init Container</a> 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 &quot;main&quot; container. Even a set of commands to be ran by main container.</p>
