<p>I am trying to run zookeeper as cluster in Azure Kubernetes Service. All the instances are staring with <strong>myid:1</strong>, not sure what configuration I need to change. Any help is appreciated.</p>
<p>Here's my configuration file,</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: zk-hs
  labels:
    app: zk
spec:
  ports:
  - port: 2888
    name: server
  - port: 3888
    name: leader-election
  clusterIP: None
  selector:
    app: zk
---
apiVersion: v1
kind: Service
metadata:
  name: zk-cs
  labels:
    app: zk
spec:
  ports:
  - port: 2181
    name: client
  selector:
    app: zk
---
apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
  name: zk-pdb
spec:
  selector:
    matchLabels:
      app: zk
  maxUnavailable: 1
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: zookeeper-sc
  selfLink: /apis/storage.k8s.io/v1/storageclasses/zookeeper-sc
  labels:
    addonmanager.kubernetes.io/mode: EnsureExists
    kubernetes.io/cluster-service: 'true'
provisioner: kubernetes.io/azure-disk
parameters:
  cachingmode: ReadOnly
  kind: Managed
  storageaccounttype: StandardSSD_LRS
reclaimPolicy: Delete
allowVolumeExpansion: true
volumeBindingMode: WaitForFirstConsumer
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: zk
spec:
  selector:
    matchLabels:
      app: zk
  serviceName: zk-hs
  replicas: 3
  updateStrategy:
    type: RollingUpdate
  podManagementPolicy: OrderedReady
  template:
    metadata:
      labels:
        app: zk
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: &quot;app&quot;
                    operator: In
                    values:
                    - zk
              topologyKey: &quot;kubernetes.io/hostname&quot;
      containers:
      - name: zookeeper        
        image: &quot;zookeeper:3.6.2&quot;
        env:       
        - name: ZOO_MY_ID
          valueFrom:
            fieldRef:
              fieldPath: metadata.annotations['spec.pod.beta.kubernetes.io/statefulset-index']
        - name: ZOO_SERVERS
          value: &quot;server.1=zk-0:2888:3888;2181 server.2=zk-1:2888:3888;2181 server.3=zk-2:2888:3888;2181&quot;
        - name: ZOO_STANDALONE_ENABLED
          value: &quot;false&quot;
        - name: ZOO_4LW_COMMANDS_WHITELIST
          value: &quot;srvr,mntr&quot;
        resources:
          requests:
            memory: &quot;1Gi&quot;            
        ports:
        - containerPort: 2181
          name: client
        - containerPort: 2888
          name: server
        - containerPort: 3888
          name: leader-election
        volumeMounts:        
        - name: zk-data
          mountPath: &quot;/data&quot;
      securityContext:
        runAsUser: 1000
        runAsGroup: 1000
        fsGroup: 1000
  volumeClaimTemplates:
  - metadata:
      name: zk-data
    spec:
      storageClassName: &quot;zookeeper-sc&quot;
      accessModes: [ &quot;ReadWriteOnce&quot; ]
      resources:
        requests:
          storage: 10Gi
</code></pre>

<p>After a week I came up with the below configuration that worked,</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: zk-hs
  labels:
    app: zk
spec:
  ports:
  - port: 2888
    name: server
  - port: 3888
    name: leader-election
  clusterIP: None
  selector:
    app: zk
---
apiVersion: v1
kind: Service
metadata:
  name: zk-cs
  labels:
    app: zk
spec:
  ports:
  - port: 2181
    name: client
  selector:
    app: zk
---
apiVersion: policy/v1beta1
kind: PodDisruptionBudget
metadata:
  name: zk-pdb
spec:
  selector:
    matchLabels:
      app: zk
  maxUnavailable: 1
---
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: zookeeper-sc
  selfLink: /apis/storage.k8s.io/v1/storageclasses/zookeeper-sc
  labels:
    addonmanager.kubernetes.io/mode: EnsureExists
    kubernetes.io/cluster-service: 'true'
provisioner: kubernetes.io/azure-disk
parameters:
  cachingmode: ReadOnly
  kind: Managed
  storageaccounttype: StandardSSD_LRS
reclaimPolicy: Delete
allowVolumeExpansion: true
volumeBindingMode: WaitForFirstConsumer
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: zk
spec:
  selector:
    matchLabels:
      app: zk
  serviceName: zk-hs
  replicas: 3
  updateStrategy:
    type: RollingUpdate
  podManagementPolicy: OrderedReady
  template:
    metadata:
      labels:
        app: zk
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            - labelSelector:
                matchExpressions:
                  - key: &quot;app&quot;
                    operator: In
                    values:
                    - zk
              topologyKey: &quot;kubernetes.io/hostname&quot;
      initContainers:
      - command:
        - /bin/bash
        - -c
        - |-
          set -ex;
          mkdir -p /data;         
          if [[ ! -f &quot;/data/myid&quot; ]]; then
            hostindex=$HOSTNAME; let zooid=${hostindex: -1: 1}+1; echo $zooid &gt; &quot;/data/myid&quot;
            echo &quot;Zookeeper MyId: &quot; $zooid
          fi
        env:
        - name: HOSTNAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        image: zookeeper:3.6.2
        name: zookeeper-init
        securityContext:
          runAsUser: 1000
        volumeMounts:
        - name: zk-data
          mountPath: &quot;/data&quot;
      containers:
      - name: zookeeper        
        image: &quot;zookeeper:3.6.2&quot;
        env:           
        - name: ZOO_SERVERS
          value: &quot;server.1=zk-0.zk-hs.default.svc.cluster.local:2888:3888;2181 server.2=zk-1.zk-hs.default.svc.cluster.local:2888:3888;2181 server.3=zk-2.zk-hs.default.svc.cluster.local:2888:3888;2181&quot;
        - name: ZOO_STANDALONE_ENABLED
          value: &quot;false&quot;
        - name: ZOO_4LW_COMMANDS_WHITELIST
          value: &quot;srvr,mntr&quot;           
        ports:
        - containerPort: 2181
          name: client
        - containerPort: 2888
          name: server
        - containerPort: 3888
          name: leader-election
        volumeMounts:        
        - name: zk-data
          mountPath: &quot;/data&quot;
      securityContext:
        runAsUser: 1000
        runAsGroup: 1000
        fsGroup: 1000
  volumeClaimTemplates:
  - metadata:
      name: zk-data
    spec:
      storageClassName: &quot;zookeeper-sc&quot;
      accessModes: [ &quot;ReadWriteOnce&quot; ]
      resources:
        requests:
          storage: 10Gi
</code></pre>
