Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional volume/secret volume in kubernetes?

Tags:

kubernetes

I'd like to mount volume if it exists. For example:

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: mypod
    image: redis
    volumeMounts:
    - name: foo
      mountPath: "/etc/foo"
  volumes:
  - name: foo
    secret:
      secretName: mysecret

is the example from the documentation. However if the secret mysecret doesn't exist I'd like to skip mounting. That is optimistic/optional mount point.

Now it stalls until the secret is created.

like image 324
nmiculinic Avatar asked Jan 12 '18 09:01

nmiculinic


2 Answers

secret and configmap volumes can be marked optional, and result in empty directories if the associated secret or configmap doesn't exist, rather than blocking pod startup

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
    - name: mypod
      image: redis
      volumeMounts:
        - name: foo
          mountPath: /etc/foo
  volumes:
    - name: foo
      secret:
        secretName: mysecret
        optional: true
like image 83
Jordan Liggitt Avatar answered Sep 25 '22 05:09

Jordan Liggitt


While this optional logic exists for env variables, it's not available for volumes as far as I am aware. It also seems a bit problematic as your infrastructure stops being immutable, depending on sequence for creation in kube you get a different application state. Rather then looking for this I woud suggest utilising a higher level templating features like the ones available in Helm so that you can do :

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
  - name: mypod
    image: redis
{{- if .Values.mysecret.enabled }}
    volumeMounts:
    - name: foo
      mountPath: "/etc/foo"
  volumes:
  - name: foo
    secret:
      secretName: mysecret
{{- end }}

And then if you provision with --set mysecret.enabled=true you will get the volume declared and with --set mysecret.enabled=false it will not be declared so it will not attempt to mount it at all

like image 25
Radek 'Goblin' Pieczonka Avatar answered Sep 26 '22 05:09

Radek 'Goblin' Pieczonka