How to mount a configMap as a volume mount in a Stateful Set

2/14/2020

I don't see an option to mount a configMap as volume in the statefulset , as per https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.17/#statefulset-v1-apps only PVC can be associated with "StatefulSet" . But PVC does not have option for configMaps.

-- SKS
configmap
kubernetes
kubernetes-pvc
kubernetes-statefulset

1 Answer

2/14/2020

Here is a minimal example:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: example
spec:
  selector:
    matchLabels:
      app: example
  serviceName: example
  template:
    metadata:
      labels:
        app: example
    spec:
      containers:
        - name: example
          image: nginx:stable-alpine
          volumeMounts:
            - mountPath: /config
              name: example-config
      volumes:
        - name: example-config
          configMap:
            name: example-configmap
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: example-configmap
data:
  a: "1"
  b: "2"

In the container, you can find the files a and b under /config, with the contents 1 and 2, respectively.

Some explanation: You do not need a PVC to mount the configmap as a volume to your pods. PersistentVolumeClaims are persistent drives, which you can read from/write to. An example for their usage is a database, such as Postgres.

ConfigMaps on the other hand are read-only key-value structures that are stored inside Kubernetes (in its etcd store), which are to store the configuration for your application. Their values can be mounted as environment variables or as files, either individually or altogether.

-- Utku Ă–zdemir
Source: StackOverflow