How would I assign ConfigMap to a pod that is already running?

8/17/2019

I cannot get a ConfigMap loaded into a pod that is currently running nginx.

I tried by creating a simple pod definition and added to it a simple read ConfigMap shown below:

apiVersion: v1
kind: Pod
metadata:
  name: testpod
spec:
  containers:
  - name: testcontainer
    image: nginx
    env:
    - name: MY_VAR
      valueFrom:
        configMapKeyRef:
          name: configmap1
          key: data1

This ran successfully and its YAML file was saved and then deleted.

Here's what I got:

apiVersion: v1
kind: Pod
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","kind":"Pod","metadata":{"annotations":{},"name":"testpod","namespace":"default"},"spec":{"containers":[{"env":[{"name":"MY_VAR","valueFrom":{"configMapKeyRef":{"key":"data1","name":"configmap1"}}}],"image":"nginx","name":"testcontainer"}]}}
  creationTimestamp: null
  name: testpod
  selfLink: /api/v1/namespaces/default/pods/testpod
spec:
  containers:
  - env:
    - name: MY_VAR
      valueFrom:
        configMapKeyRef:
          key: data1
          name: configmap1
    image: nginx
    imagePullPolicy: Always
    name: testcontainer
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-27x4x
      readOnly: true
  dnsPolicy: ClusterFirst
  enableServiceLinks: true
  nodeName: ip-10-0-1-103
  priority: 0
  restartPolicy: Always
  schedulerName: default-scheduler
  securityContext: {}
  serviceAccount: default
  serviceAccountName: default
  terminationGracePeriodSeconds: 30
  tolerations:
  - effect: NoExecute
    key: node.kubernetes.io/not-ready
    operator: Exists
    tolerationSeconds: 300
  - effect: NoExecute
    key: node.kubernetes.io/unreachable
    operator: Exists
    tolerationSeconds: 300
  volumes:
  - name: default-token-27x4x
    secret:
      defaultMode: 420
      secretName: default-token-27x4x
status:
  phase: Pending
  qosClass: BestEffort

I then tried copying its syntax into what was another pod which was running.

This is what I got using kubectl edit pod po?

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2019-08-17T18:15:22Z"
  labels:
    run: pod1
  name: pod1
  namespace: default
  resourceVersion: "12167"
  selfLink: /api/v1/namespaces/default/pods/pod1
  uid: fa297c13-c11a-11e9-9a5f-02ca4f0dcea0
spec:
  containers:
  - image: nginx
    imagePullPolicy: Always
    name: pod1
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-27x4x
      readOnly: true
  dnsPolicy: ClusterFirst
  enableServiceLinks: true
  nodeName: ip-10-0-1-102
  priority: 0
  restartPolicy: Never
  schedulerName: default-scheduler
  securityContext: {}
  serviceAccount: default
  serviceAccountName: default
  terminationGracePeriodSeconds: 30
  tolerations:
  - effect: NoExecute
    key: node.kubernetes.io/not-ready
    operator: Exists
    tolerationSeconds: 300
  - effect: NoExecute
    key: node.kubernetes.io/unreachable
    operator: Exists
    tolerationSeconds: 300
  volumes:
  - name: default-token-27x4x
    secret:
      defaultMode: 420
      secretName: default-token-27x4x
status:
  conditions:
  - lastProbeTime: null
    lastTransitionTime: "2019-08-17T18:15:22Z"
    status: "True"
    type: Initialized
  - lastProbeTime: null
    lastTransitionTime: "2019-08-17T18:15:27Z"
    status: "True"
    type: Ready
  - lastProbeTime: null
    lastTransitionTime: "2019-08-17T18:15:27Z"
    status: "True"
    type: ContainersReady
  - lastProbeTime: null
    lastTransitionTime: "2019-08-17T18:15:22Z"
    status: "True"
    type: PodScheduled
  containerStatuses:
  - containerID: docker://99bfded0d69f4ed5ed854e59b458acd8a9197f9bef6d662a03587fe2ff61b128
    image: nginx:latest
    imageID: docker-pullable://nginx@sha256:53ddb41e46de3d63376579acf46f9a41a8d7de33645db47a486de9769201fec9
    lastState: {}
    name: pod1
    ready: true
    restartCount: 0
    state:
      running:
        startedAt: "2019-08-17T18:15:27Z"
  hostIP: 10.0.1.102
  phase: Running
  podIP: 10.244.2.2
  qosClass: BestEffort
  startTime: "2019-08-17T18:15:22Z"

And also k get po pod1 -o yaml --export

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: null
  labels:
    run: pod1
  name: pod1
  selfLink: /api/v1/namespaces/default/pods/pod1
spec:
  containers:
  - image: nginx
    imagePullPolicy: Always
    name: pod1
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-27x4x
      readOnly: true
  dnsPolicy: ClusterFirst
  enableServiceLinks: true
  nodeName: ip-10-0-1-102
  priority: 0
  restartPolicy: Never
  schedulerName: default-scheduler
  securityContext: {}
  serviceAccount: default
  serviceAccountName: default
  terminationGracePeriodSeconds: 30
  tolerations:
  - effect: NoExecute
    key: node.kubernetes.io/not-ready
    operator: Exists
    tolerationSeconds: 300
  - effect: NoExecute
    key: node.kubernetes.io/unreachable
    operator: Exists
    tolerationSeconds: 300
  volumes:
  - name: default-token-27x4x
    secret:
      defaultMode: 420
      secretName: default-token-27x4x
status:
  phase: Pending
  qosClass: BestEffort

What am I doing wrong or have I missed something?

-- mac
configmap
docker
google-kubernetes-engine
kubernetes
kubernetes-pod

1 Answer

8/17/2019

You can't add configuration to a running pod, that's something inherent to containers.

To put it simply: a container is running with a service, the state of the service defines the state of the container. As you know, nginx needs to reload it's configuration if you change it, but that's not really a good idea in this context, so you need to stop/start the container with the new configuration.

So what you are getting is normal, the service state is still running so it's keeping the old file configuration it has from before even if you make change inside the file.

If you need the service to be reloading without downtime, set multiple replicas and create a rolling update rule for no downtime during update.

There are some special cases to this, like grafana, where it can go check if files have been changed from the last modification.

-- night-gold
Source: StackOverflow