Kubernetes Hacks and Tricks – #2 Optional ConfigMaps and Secrets in Pods
In some applications, the application configurations are set by default in the codebase (hard-coded). If you want to override them, you can provide your override values through environment variables. Sometimes and for some configurations, it doesn’t matter if the override values exist. However, you want to make sure the application runs without any issues. If ConfigMaps/Secrets are available, use them and override default values, and if there are no ConfigMaps/Secrets for the application, run the application with its default configuration. In such a case, Kubernetes provides us optional
field for using ConfigMaps/Secrets as environment variables in Pods.
Follow our social media:
https://www.linkedin.com/in/ssbostan
https://www.linkedin.com/company/kubedemy
https://www.youtube.com/@kubedemy
If you run the pod with a regular manifest and the ConfigMap/Secret does not exist, it will face the following CreateContainerConfigError
error.
apiVersion: v1
kind: Pod
metadata:
name: optional-configmap-secret
spec:
containers:
- name: registry
image: registry:latest
envFrom:
- configMapRef:
name: registry-config
- secretRef:
name: registry-secret
Now we can use the optional field to run the pod even if ConfigMap/Secret is not available to be used by the pod container without that error.
If you like this series of articles, please share them and write your thoughts as comments here. Your feedback encourages me to complete this massively planned program.
Follow my LinkedIn https://www.linkedin.com/in/ssbostan
Follow Kubedemy LinkedIn https://www.linkedin.com/company/kubedemy
Follow Kubedemy Telegram https://telegram.me/kubedemy