ConfigMaps and Secrets
ConfigMaps and Secrets
Section titled “ConfigMaps and Secrets”Hard-coding configuration in container images makes them environment-specific and forces rebuilds for config changes. ConfigMaps and Secrets externalise this configuration.
ConfigMaps
Section titled “ConfigMaps”ConfigMaps hold non-sensitive key-value pairs.
Creating a ConfigMap
Section titled “Creating a ConfigMap”From literals:
kubectl create configmap app-config \ --from-literal=LOG_LEVEL=info \ --from-literal=MAX_CONNECTIONS=100From a file:
kubectl create configmap app-config --from-file=config.propertiesFrom YAML:
apiVersion: v1kind: ConfigMapmetadata: name: app-configdata: LOG_LEVEL: "info" MAX_CONNECTIONS: "100" APP_CONFIG: | key1=value1 key2=value2Using ConfigMaps in Pods
Section titled “Using ConfigMaps in Pods”As environment variables:
env: - name: LOG_LEVEL valueFrom: configMapKeyRef: name: app-config key: LOG_LEVEL
# Or inject all keys at once:envFrom: - configMapRef: name: app-configAs a volume (file mount):
volumes: - name: config-volume configMap: name: app-config
containers: - name: my-app volumeMounts: - name: config-volume mountPath: /etc/configEach key becomes a file at /etc/config/<key>.
Secrets
Section titled “Secrets”Secrets hold sensitive data — passwords, API keys, tokens. Values are base64-encoded (not encrypted by default in etcd without additional setup).
Creating a Secret
Section titled “Creating a Secret”kubectl create secret generic db-credentials \ --from-literal=username=admin \ --from-literal=password=s3cur3p@ssFrom YAML (values must be base64-encoded):
echo -n 'admin' | base64 # YWRtaW4=echo -n 's3cur3p@ss' | base64apiVersion: v1kind: Secretmetadata: name: db-credentialstype: Opaquedata: username: YWRtaW4= password: czNjdXIzcEBzcw==Using Secrets in Pods
Section titled “Using Secrets in Pods”env: - name: DB_USERNAME valueFrom: secretKeyRef: name: db-credentials key: username - name: DB_PASSWORD valueFrom: secretKeyRef: name: db-credentials key: passwordAs a volume:
volumes: - name: secret-volume secret: secretName: db-credentials
volumeMounts: - name: secret-volume mountPath: /etc/secrets readOnly: trueSecret Types
Section titled “Secret Types”| Type | Use |
|---|---|
Opaque | Arbitrary user-defined data (most common) |
kubernetes.io/dockerconfigjson | Docker registry credentials |
kubernetes.io/tls | TLS certificate and key |
kubernetes.io/service-account-token | Service account tokens |
Common Commands
Section titled “Common Commands”kubectl get configmapskubectl describe configmap app-configkubectl get secret db-credentials -o yaml
# Decode a secret valuekubectl get secret db-credentials -o jsonpath='{.data.password}' | base64 --decodeImportant Notes
Section titled “Important Notes”- Secrets are base64-encoded, not encrypted. Use tools like Sealed Secrets, Vault, or External Secrets Operator for proper secret management at scale.
- When a ConfigMap or Secret is updated, pods using env variables are not automatically restarted. Pods using volume mounts get updates eventually (kubelet sync period).
- Annotate deployments to force restarts on config change:
kubectl rollout restart deployment/my-app.