Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to patch a ConfigMap in Kubernetes

Tags:

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.

like image 251
Muhammad Rehan Saeed Avatar asked Feb 07 '19 10:02

Muhammad Rehan Saeed


People also ask

How do I update a configmap in Kubernetes?

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.

How to update a config map in kubectl?

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.

How do I replace a pod in Kubernetes?

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:

What are ConfigMaps in Kubernetes (K8s)?

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.


2 Answers

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\"]"}}' 
like image 200
Jordan Liggitt Avatar answered Sep 19 '22 13:09

Jordan Liggitt


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 -  
like image 23
P Ekambaram Avatar answered Sep 18 '22 13:09

P Ekambaram