Kubernetes puzzle: Populate environment variable from file (mounted volume) Kubernetes puzzle: Populate environment variable from file (mounted volume) kubernetes kubernetes

Kubernetes puzzle: Populate environment variable from file (mounted volume)


You can create deployment with kubectl endless loop which will constantly poll volume and update configmap from it. After that you can mount created configmap into your pod. It's a little bit hacky but will work and update your configmap automatically. The only requirement is that PV must be ReadWriteMany or ReadOnlyMany (but in that case you can mount it in read-only mode to all pods).

apiVersion: v1kind: ServiceAccountmetadata:  name: cm-creator  namespace: default---apiVersion: rbac.authorization.k8s.io/v1kind: Rolemetadata:  namespace: default  name: cm-creatorrules:- apiGroups: [""]  resources: ["configmaps"]  verbs: ["create", "update", "get"]---apiVersion: rbac.authorization.k8s.io/v1kind: RoleBindingmetadata:  name: cm-creator  namespace: defaultsubjects:- kind: User  name: system:serviceaccount:default:cm-creator  apiGroup: rbac.authorization.k8s.ioroleRef:  kind: Role  name: cm-creator  apiGroup: rbac.authorization.k8s.io---apiVersion: apps/v1kind: Deploymentmetadata:  name: cm-creator  namespace: default  labels:    app: cm-creatorspec:  replicas: 1  serviceAccountName: cm-creator  selector:    matchLabels:      app: cm-creator  template:    metadata:      labels:        app: cm-creator    spec:      containers:      - name: cm-creator        image: bitnami/kubectl        command:        - /bin/bash        - -c        args:        - while true;             kubectl create cm myconfig --from-file=my_var=/mnt/my_var_value.txt --dry-run -o yaml | kubectl apply -f-;            sleep 60;          done        volumeMounts:        - name: my-vol          path: /mnt          readOnly: true      volumes:      - name: my-vol        persistentVolumeClaim:          claimName: my-pvc