How to merge kubectl config file with ~/.kube/config?

前端 未结 6 913
你的背包
你的背包 2020-12-07 16:27

Is there a simple kubectl command to take a kubeconfig file (that contains a cluster+context+user) and merge it into the ~/.kube/config file as an

相关标签:
6条回答
  • 2020-12-07 16:58

    If you use bash you can use this to simply add the configs:

    function kmerge() {
     DATE=$(date +"%Y%m%d%H%M")
     KUBECONFIG=~/.kube/config:$1 kubectl config view --flatten > ~/.kube/mergedkub && mv ~/.kube/config ~/.kube/config-$DATE && mv ~/.kube/mergedkub ~/.kube/config
    }
    

    Then just use "kmerge $newConfigfile" to add this. Be aware the clusternames etc. should be different from existing config entries!

    0 讨论(0)
  • 2020-12-07 17:03

    If you want to merge two config files in a single one

    I found this way (not sure if this is the simplest)

    # Add the two config files to the env var
    export KUBECONFIG=~/.kube/config:~/Desktop/configFile2.yaml
    
    # Review that you have two configurations in one view
    kubectl config view
    
    # View the raw config and output to a new file
    kubectl config view --raw > /tmp/config
    

    Then copy the new config file where you want, also do not forget to unset KUBECONFIG the env variable

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

    If you find yourself doing this a lot... There is now also the krew plugin package manager for kubectl.

    The krew plugin "konfig" can help you manage your ~/.kube/config file.

    Using the konfig plugin the syntax will be:

    kubectl konfig import -s new.yaml

    To install krew: https://github.com/kubernetes-sigs/krew

    To install konfig: kubectl krew install konfig

    0 讨论(0)
  • 2020-12-07 17:15

    Using multiple kubeconfigs at once

    Sometimes you have a bunch of small kubeconfig files (e.g. one per cluster) but you want to use them all at once, with tools like kubectl or kubectx that work with multiple contexts at once.

    To do that, you need a “merged” kubeconfig file. In the section "Merging kubeconfig files" below, we explain how you can merge the kubeconfigs into a single file, but you can also merge them in-memory.

    By specifying multiple files in KUBECONFIG environment variable, you can temporarily stitch kubeconfig files together and use them all in kubectl .

    #
    # Kubeconfig in-memory merge
    #
    export KUBECONFIG=file1:file2
    kubectl get pods --context=cluster-1
    kubectl get pods --context=cluster-2
    
    #
    # For your example
    # merging your kubeconfig file w/ $HOME/.kube/config (w/ cp backup)
    #
    cp $HOME/.kube/config $HOME/.kube/config.backup.$(date +%Y-%m-%d.%H:%M:%S)
    KUBECONFIG= $HOME/.kube/config:file2: kubectl config view --merge --flatten > \
    ~/.kube/merged_kubeconfig && mv ~/.kube/merged_kubeconfig ~/.kube/config
    kubectl get pods --context=cluster-1
    kubectl get pods --context=cluster-2
    

    Merging kubeconfig files

    Since kubeconfig files are structured YAML files, you can’t just append them to get one big kubeconfig file, but kubectl can help you merge these files:

    #
    # Merging your kubeconfig file w/ $HOME/.kube/config (w/ cp backup)
    #
    cp $HOME/.kube/config $HOME/.kube/config.backup.$(date +%Y-%m-%d.%H:%M:%S)
    KUBECONFIG=$HOME/.kube/config:file2:file3 kubectl config view --merge --flatten > \
    ~/.kube/merged_kubeconfig && mv ~/.kube/merged_kubeconfig ~/.kube/config
    kubectl get pods --context=cluster-1
    kubectl get pods --context=cluster-2
    

    Extracting a context from a kubeconfig file

    Let’s say you followed the before merging kubeconfig files and have a merged kubeconfig file in $HOME/.kube/config. Now you want to extract a cluster’s information to a portable kubeconfig file that only has the parts you need to connect to that cluster.

    Run:

    KUBECONFIG=$HOME/.kube/config kubectl config view \
        --minify --flatten --context=context-1 > $HOME/.kube/config-context-1
    
    #
    # using --kubeconfig flag
    #
    kubectl get pods --kubeconfig=$HOME/.kube/config-context-1
    
    #
    # or 
    # using `KUBECONFIG` environment variable
    #
    KUBECONFIG=$HOME/.kube/config-context-1 kubectl get pods
    
    #
    # or 
    # keep using kubeconfig file at $HOME/.kube/config (which has the merged context)
    #
    kubectl get pods --context=cluster-1
    

    In this command, we extract data about context-1 from $HOME/.kube/config to config-context-1 file. The --minify flag allows us to extract only info about that context, and the --flatten flag allows us to keep the credentials unredacted.

    ref article: https://ahmet.im/blog/mastering-kubeconfig/

    0 讨论(0)
  • 2020-12-07 17:16

    Do this:

    export KUBECONFIG=~/.kube/config:~/someotherconfig 
    kubectl config view --flatten
    

    You can then pipe that out to a new file if needed.

    0 讨论(0)
  • 2020-12-07 17:20

    To dynamically merge multiple config files in you .bashrc:

    export KUBECONFIG=/Users/<user>/.kube/config:/Users/<user>/.kube/other.config
    source <(kubectl completion bash)
    

    After fresh source, verify:

    kubectl config view
    
    0 讨论(0)
提交回复
热议问题