Update k8s ConfigMap or Secret without deleting th

2019-02-01 18:37发布

问题:

I've been using K8S ConfigMap and Secret to manage our properties. My design is pretty simple, that keeps properties files in a git repo and use build server such as Thoughtworks GO to automatically deploy them to be ConfigMaps or Secrets (on choice condition) to my k8s cluster.

Currently, I found it's not really efficient that I have to always delete the existing ConfigMap and Secret and create the new one to update as below:

  1. kubectl delete configmap foo

  2. kubectl create configmap foo --from-file foo.properties

Is there a nice and simple way to make above one step and more efficient than deleting current? potentially what I'm doing now may compromise the container that uses these configmaps if it tries to mount while the old configmap is deleted and the new one hasn't been created.

Thanks in advance.

回答1:

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 -


回答2:

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 in https://kubernetes.io/docs/user-guide/kubectl/kubectl_replace/

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


回答3:

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.