<pre><code>apiVersion: apps/v1 # for versions before 1.8.0 use apps/v1beta1
    kind: Deployment
    metadata:
      name: consoleservice1
    spec:
      selector:
        matchLabels:
          app: consoleservice1
      replicas: 3 # tells deployment to run 3 pods matching the template
      strategy:
        type: RollingUpdate
        rollingUpdate:
          maxSurge: 1
          maxUnavailable: 1
      minReadySeconds: 5
      template: # create pods using pod definition in this template
        metadata:
          labels:
            app: consoleservice1
        spec:
          containers:
          - name: consoleservice
            image: chintamani/insightvu:ms-console1
            readinessProbe:
              httpGet:
                path: /
                port: 8385
              initialDelaySeconds: 5
              periodSeconds: 5
              successThreshold: 1
            ports:
            - containerPort: 8384
            imagePullPolicy: Always
            volumeMounts:
              - mountPath: /deploy/config
                name: config
          volumes:
            - name: config
              configMap:
                name: console-config
</code></pre>

<p>For creating configmap I am using this command:</p>

<pre><code>kubectl create configmap console-config --from-file=deploy/config
</code></pre>

<p>While changing in configmap it doesn't reflect automatically, every time I have to restart the pod. How can I do it automatically?</p>

<p>Pod and configmap are completely separate in Kubernetes and pods don't automatically restart itself if there is a configmap change. </p>

<p>There are few alternatives to achieve this.</p>

<ol>
<li><p>Use wave, it's a Kubernetes controller which look for specific annotation and update the deployment if there is any change in configmap <a href="https://github.com/pusher/wave" rel="nofollow noreferrer">https://github.com/pusher/wave</a></p></li>
<li><p>Use of <a href="https://github.com/stakater/Reloader" rel="nofollow noreferrer">https://github.com/stakater/Reloader</a>, reloader can watch configmap changes and can update the pod to pick the new configuration.</p>

<pre><code>    kind: Deployment
    metadata:
      annotations:
        reloader.stakater.com/auto: "true"
    spec:
      template:
        metadata:
</code></pre></li>
<li><p>You can add a customize configHash annotation in deployment and in CI/CD or while deploying the application use <code>yq</code> to replace that value with hash of configmap, so in case of any change in configmap. Kubernetes will detect the change in annotation of deployment and reload the pods with new configuration.</p></li>
</ol>

<p><code>yq w --inplace deployment.yaml spec.template.metadata.annotations.configHash $(kubectl get cm/configmap -oyaml | sha256sum)</code></p>

<pre><code>        apiVersion: apps/v1 # for versions before 1.8.0 use apps/v1beta1
        kind: Deployment
        metadata:
          name: application
        spec:
          selector:
            matchLabels:
              app: consoleservice1
          replicas: 3              
          template:
            metadata:
              labels:
                app: consoleservice1
              annotations:
                configHash: ""
</code></pre>

<p>Reference: <a href="https://blog.questionable.services/article/kubernetes-deployments-configmap-change/" rel="nofollow noreferrer">here</a></p>
