Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update k8s ConfigMap or Secret without deleting the existing one

People also ask

How do I update Kubernetes ConfigMap?

Updating the config map in Kubernetes POD requires the restart of POD. A possible approach to auto-update configmap inside pod without restart is mounting it's content as volume. Kubernetes auto-updates the config map into POD if mounted as a volume, however, it has a limitation like it won't work if subpath used.

What happens when ConfigMap is updated?

When the config map is updated, a new version is created for that configMap. All the deployments that were attached to the configMap get a rolling update with the latest configMap version tied to it.

Can you update a Kubernetes secret?

Secrets can also be used by Kubernetes and the apps that run in the cluster to take extra measures. When you alter a secret's value, the value which is used by an already operating pod does not change dynamically. You must remove the original pod and build a new pod to update a secret.

Can we edit ConfigMap in Kubernetes?

Method to edit ConfigMaps in Kubernetes using kubectlYou can alter the file as per your work needs.


You can get YAML from the kubectl create configmap command and pipe it to kubectl replace, like this:

kubectl create configmap foo --from-file foo.properties -o yaml --dry-run | kubectl replace -f -

For future reference, kubectl replace is now a very handy way to achieve this

kubectl replace -f some_spec.yaml Let you update a complete configMap (or other objects)

See doc and examples directly here

Copy/pasted from the help:

# Replace a pod using the data in pod.json.
kubectl replace -f ./pod.json

# Replace a pod based on the JSON passed into stdin.
cat pod.json | kubectl replace -f -

# Update a single-container pod's image version (tag) to v4
kubectl get pod mypod -o yaml | sed 's/\(image: myimage\):.*$/\1:v4/' | kubectl replace -f -

# Force replace, delete and then re-create the resource
kubectl replace --force -f ./pod.json

For small changes in configMap, use edit

kubectl edit configmap <cfg-name>

This will open configMap in vi editor. Make the changes and save it.


kubectl replace fails if a configmap does not already exist:

$ kubectl create configmap foo --from-file foo.properties -o yaml --dry-run=client | kubectl replace -f -

Error from server (NotFound): error when replacing "STDIN": configmaps "falco-config" not found

Best solution is to use kubectl apply which would create configmap if not present else update configmap if it is present:

$ kubectl create configmap foo --from-file foo.properties -o yaml --dry-run=client | kubectl apply -f -

configmap/falco-config configured


Take a copy of the existing configmap:

kubectl get configmap foo -o yaml > foo.yaml

And then do the modifications and use apply command, this should work.

kubectl apply -f foo.yaml

Note: Incase if you see any of the following issue, then include latest "resourceVersion" from the existing config map and try again.

" Operation cannot be fulfilled on configmaps "foo": the object has been modified; please apply your changes to the latest version and try again"