Kubernetes provides two resources, Secrets and ConfigMaps, to store and manage configuration data that can be injected into containers as environment variables, command-line arguments, or mounted files.
Secrets in Kubernetes
Secrets are Kubernetes objects that store sensitive information, such as passwords, authentication tokens, or SSL certificates, in an encrypted form. Secrets can be created manually or generated from existing files using the kubectl create secret command. There are four types of Secrets:
generic: stores arbitrary key-value pairs
docker-registry: stores authentication credentials for a Docker registry
tls: stores a pair of SSL certificates and their private key
ssh: stores an SSH private key and its public key
Secrets are base64-encoded when stored, but they can be decoded when mounted into containers. Secrets can be mounted as environment variables or as files inside a container. To mount a Secret as an environment variable, define a container environment variable and set its value to the secret key, like this:
env:
- name: DB\_PASSWORD
valueFrom:
secretKeyRef:
name: db-credentials
key: password
To mount a Secret as a file, define a volume that references the Secret and mount the volume into the container, like this:
volumes:
- name: db-secrets
secret:
secretName: db-credentials
items:
- key: username
path: db-username
- key: password
path: db-password
containers:
- name: app
image: myapp:latest
volumeMounts:
- name: db-secrets
mountPath: /etc/db-secrets
ConfigMaps in Kubernetes
ConfigMaps are Kubernetes objects that store non-sensitive configuration data, such as configuration files, command-line arguments, or environment variables, in a key-value pair format. ConfigMaps can be created manually or generated from existing files or directories using the kubectl create configmap command. ConfigMaps can be used in a similar way to Secrets, but they are not encrypted and can be used to store non-sensitive data.
ConfigMaps can be mounted as environment variables or as files inside a container. To mount a ConfigMap as an environment variable, define a container environment variable and set its value to the ConfigMap key, like this:
env:
- name: APP\_CONFIG
valueFrom:
configMapKeyRef:
name: app-config
key: config-file
To mount a ConfigMap as a file, define a volume that references the ConfigMap and mount the volume into the container, like this:
volumes:
- name: app-config
configMap:
name: app-config
containers:
- name: app
image: myapp:latest
volumeMounts:
- name: app-config
mountPath: /etc/app-config
Injecting Secrets and ConfigMaps into a Pod
Both Secrets and ConfigMaps can be injected into a Pod as environment variables or as files. To inject a Secret or a ConfigMap into a Pod as an environment variable, use the envFrom field and set its value to a list of secretRef or configMapRef objects, like this:
envFrom:
- secretRef:
name: db-credentials
- configMapRef:
name: app-config
To inject a Secret or a ConfigMap into a Pod as a file, define a volume that references the Secret or the ConfigMap and mount the volume into the container, like this:
volumes:
- name: db-secrets
secret:
secretName: