Update k8s ConfigMap or Secret without deleting the existing one

后端 未结 5 925
梦如初夏
梦如初夏 2020-12-07 11:39

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 Though

相关标签:
5条回答
  • 2020-12-07 12:04

    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 -
    
    0 讨论(0)
  • 2020-12-07 12:09

    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"

    0 讨论(0)
  • 2020-12-07 12:18

    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.

    0 讨论(0)
  • 2020-12-07 12:19

    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
    
    0 讨论(0)
  • 2020-12-07 12:22

    kubectl replace fails if a configmap already exists :

    $ kubectl create configmap foo --from-file foo.properties -o yaml --dry-run | 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 | kubectl apply -f -
    
    configmap/falco-config configured
    
    
    0 讨论(0)
提交回复
热议问题