kuberneteskubectlconfigmapkubernetes-deployment

How to update a config map in kubernetes?


Hi I am using a config map which is created from the files in a directory. The config map is mounted to the volume in the pods container. sometimes i need to update my config map . Any idea how can i do it ?. currently i am deleting the config map and recreating it and then restarting the pods. not sure if it is the best way because, here we delete and create a config map.

here is the shell script code

kubectl create ns k6
kubectl create configmap k6-scripts-configmap -n k6 --from-file /test/scripts/k6/
kubectl apply  -f /test/config/k6/k6-deployment.yaml 

later in the script i need to update the config map as some files in the directory (/test/scripts/k6/) needs to be changed.

kubectl delete configmap k6-scripts-configmap -n k6
kubectl create configmap k6-scripts-configmap -n k6 --from-file /test/scripts/k6/
kubectl rollout restart deployment k6 -n k6

could you let me know if i am doing it correctly , or is it possible i can update the config map without deleting it ? Also after an update should i restart the deployment ?

thank you


Solution

  • We can use four methods to update configmaps.

    Method : 1 You can use below command to update configmaps

    kubectl create configmap k6-scripts-configmap -n k6 --from-file /test/scripts/k6/ -o yaml --dry-run=client | kubectl apply -f -
    

    Method : 2 As explained in the document you can use Kubectl replace also.

    Note : kubectl replace fails if a configmap does not already exist.

    Method : 3

    As Chris Gillatt suggested you can use kubectl edit configmap <cfg-name>

    Method : 4

    Refer this blog by Harsh Manvor for information on auto updating configmap without restarting POD.