Kubernetes ships with a ConfigMap
called coredns
that lets you specify DNS settings. I want to modify or patch a small piece of this configuration by adding:
apiVersion: v1 kind: ConfigMap data: upstreamNameservers: | ["1.1.1.1", "1.0.0.1"]
I know I can use kubectrl edit
to edit the coredns
ConfigMap
is there some way I can take the above file containing only the settings I want to insert or update and have it merged on top of or patched over the existing ConfigMap
?
The reason for this is that I want my deployment to be repeatable using CI/CD. So, even if I ran my Helm chart on a brand new Kubernetes cluster, the settings above would be applied.
Kubernetes: Updating an existing ConfigMap using kubectl replace Creating a ConfigMap using ‘kubectl create configmap’ is a straightforward operation. However, there is not a corresponding ‘kubectl apply’ that can easily update that ConfigMap.
Similarly, to update a ConfigMap: $ kubectl create configmap my-config --from-literal=foo=bar --dry-run -o yaml | kubectl apply -f - It is best to create your Secrets and ConfigMaps using the above approach so kubectl can record its annotation for tracking changes to the resource in the spec.
kubectl replace -f ./pod.json # Replace a pod based on the JSON passed into stdin. kubectl replace fails if a configmap does not already exist: The best solution is to use kubectl apply which would create configmap if not present else update configmap if it is present:
ConfigMaps in Kubernetes (K8s). ConfigMaps are Kubernetes objects that… | by Kubernetes Advocate | AVM Consulting Blog | Medium ConfigMaps are Kubernetes objects that allow you to separate configuration data/files from image content to keep containerized applications portable.
This will apply the same patch to that single field:
kubectl patch configmap/coredns \ -n kube-system \ --type merge \ -p '{"data":{"upstreamNameservers":"[\"1.1.1.1\", \"1.0.0.1\"]"}}'
you should try something like this:
kubectl get cm some-config -o yaml | run 'sed' commands to make updates | kubectl create cm some-config -o yaml --dry-run | kubectl apply -f -
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With