Byebye
Byebye

Reputation: 1103

How to add a Configmap to a specific Pod In a StatefulSet?

I'd like to know how I can add a ConfigMap to a specific pod spawned by a StatefulSet.

I've looked at the spec and I can't figure out how to ensure that a pod only mounts a ConfigMap with a specific label. I've already got a workaround ready in the form of a container. However I'd still like to know if this is possible out of the box.

Upvotes: 4

Views: 3565

Answers (2)

whites11
whites11

Reputation: 13260

You can't treat a pod inside the same StatefulSet as a special pod. Simply because you can only specify a single PodSpec that's valid for the whole set.

A workaround is to mount all versions of the config files in every pod and run an entrypoint script that uses different config files based on the pod name. I have no example to show but it should be quite easy

Upvotes: 3

Simon Weng
Simon Weng

Reputation: 161

A more elegant solution, based on Mutating Webhook, is available https://github.com/spoditor/spoditor to solve this exact problem.

Essentially, it uses a custom annotation on the PodSpec template, like:

      annotations:
        spoditor.io/mount-volume: |
          {
            "volumes": [
              {
                "name": "my-volume",
                "secret": {
                  "secretName": "my-secret"
                }
              }
            ],
            "containers": [
              {
                "name": "nginx",
                "volumeMounts": [
                  {
                    "name": "my-volume",
                    "mountPath": "/etc/secrets/my-volume"
                  }
                ]
              }
            ]
          }

Now, nginx container in each Pod of the StatefulSet will try to mount its own dedicated secret in the pattern of my-secret-{pod ordinal}.

You will just need to make sure my-secret-0, my-secret-1, so on and so forth exists in the same namespace of the StatefulSet.

There're more advanced usage of the annotation in the documentation of the project.

Upvotes: 1

Related Questions