Managing Application Configuration in Kubernetes
/ 3 min read
kubernetes , k8s , configmaps , secrets , devops , cloud-native , containers , security , series:kubernetes:9
Understanding ConfigMaps and Secrets
ConfigMaps and Secrets are Kubernetes objects used to decouple configuration details from container images, with Secrets specifically designed for sensitive data.
ConfigMaps
Basic ConfigMap Creation
apiVersion: v1kind: ConfigMapmetadata: name: game-configdata: game.properties: | enemies=aliens lives=3 enemies.cheat=true enemies.cheat.level=noGoodRotten ui.properties: | color.good=purple color.bad=yellow allow.textmode=trueUsing ConfigMaps
1. Environment Variables
apiVersion: v1kind: Podmetadata: name: config-env-podspec: containers: - name: test-container image: busybox command: [ "/bin/sh", "-c", "env" ] env: - name: SPECIAL_LEVEL valueFrom: configMapKeyRef: name: game-config key: special.how2. Volume Mounts
apiVersion: v1kind: Podmetadata: name: config-volume-podspec: containers: - name: test-container image: busybox command: [ "/bin/sh","-c","cat /etc/config/game.properties" ] volumeMounts: - name: config-volume mountPath: /etc/config volumes: - name: config-volume configMap: name: game-configSecrets
Types of Secrets
- Opaque (generic)
- kubernetes.io/tls
- kubernetes.io/dockerconfigjson
- kubernetes.io/basic-auth
- kubernetes.io/ssh-auth
Creating Secrets
1. Generic Secret
apiVersion: v1kind: Secretmetadata: name: mysecrettype: Opaquedata: username: YWRtaW4= # base64 encoded password: MWYyZDFlMmU2N2Rm # base64 encoded2. TLS Secret
apiVersion: v1kind: Secretmetadata: name: tls-secrettype: kubernetes.io/tlsdata: tls.crt: <base64-encoded-cert> tls.key: <base64-encoded-key>Using Secrets
1. As Environment Variables
apiVersion: v1kind: Podmetadata: name: secret-env-podspec: containers: - name: mycontainer image: redis env: - name: SECRET_USERNAME valueFrom: secretKeyRef: name: mysecret key: username - name: SECRET_PASSWORD valueFrom: secretKeyRef: name: mysecret key: password2. As Files in a Volume
apiVersion: v1kind: Podmetadata: name: secret-volume-podspec: containers: - name: mycontainer image: redis volumeMounts: - name: secret-volume mountPath: /etc/secrets readOnly: true volumes: - name: secret-volume secret: secretName: mysecretBest Practices
1. ConfigMap Best Practices
- Keep configurations small
- Use meaningful names
- Version control configurations
- Consider using Helm for templating
2. Secret Best Practices
- Enable encryption at rest
- Rotate secrets regularly
- Use RBAC to restrict access
- Consider external secret stores
Real-World Examples
1. Database Configuration
apiVersion: v1kind: ConfigMapmetadata: name: mysql-configdata: my.cnf: | [mysqld] max_connections=250 max_allowed_packet=32M sql_mode=STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION---apiVersion: v1kind: Secretmetadata: name: mysql-secretstype: Opaquedata: root-password: cm9vdHBhc3N3b3Jk user-password: dXNlcnBhc3N3b3Jk2. Web Application Configuration
apiVersion: v1kind: ConfigMapmetadata: name: nginx-configdata: nginx.conf: | server { listen 80; server_name example.com; location / { root /usr/share/nginx/html; index index.html; } }---apiVersion: v1kind: Secretmetadata: name: app-tlstype: kubernetes.io/tlsdata: tls.crt: <base64-encoded-cert> tls.key: <base64-encoded-key>Advanced Usage
1. Dynamic Updates
apiVersion: v1kind: Podmetadata: name: config-env-podspec: containers: - name: test-container image: k8s.gcr.io/busybox command: [ "/bin/sh", "-c", "while true; do echo $(cat /etc/config/game.properties); sleep 10; done" ] volumeMounts: - name: config-volume mountPath: /etc/config volumes: - name: config-volume configMap: name: game-config2. Immutable ConfigMaps and Secrets
apiVersion: v1kind: ConfigMapmetadata: name: immutable-configimmutable: truedata: config.json: | { "environment": "production" }Security Considerations
1. Encryption at Rest
apiVersion: apiserver.config.k8s.io/v1kind: EncryptionConfigurationresources: - resources: - secrets providers: - aescbc: keys: - name: key1 secret: <base64-encoded-key> - identity: {}2. RBAC Configuration
apiVersion: rbac.authorization.k8s.io/v1kind: Rolemetadata: name: secret-readerrules:- apiGroups: [""] resources: ["secrets"] verbs: ["get", "list"]Troubleshooting
Common issues and solutions:
-
ConfigMap Not Updating
- Check volume mounts
- Verify pod recreation
- Review configuration
- Check permissions
-
Secret Access Issues
- Verify base64 encoding
- Check RBAC permissions
- Review mount paths
- Check secret existence
Integration with External Tools
1. External Secrets Operator
apiVersion: external-secrets.io/v1beta1kind: ExternalSecretmetadata: name: vault-examplespec: refreshInterval: "15s" secretStoreRef: name: vault-backend kind: ClusterSecretStore target: name: secret-to-be-created data: - secretKey: password remoteRef: key: secret/data/myapp property: password2. Sealed Secrets
apiVersion: bitnami.com/v1alpha1kind: SealedSecretmetadata: name: mysecretspec: encryptedData: password: AgBy8hCi4...Series Navigation
- Previous: Jobs and CronJobs in Kubernetes
- Next: Kubernetes Services and Ingress
Conclusion
ConfigMaps and Secrets are essential for managing application configuration and sensitive data in Kubernetes. Understanding their proper usage and best practices is crucial for building secure and maintainable applications.