How to combine kubectl configuration file with ~ / .kube / config?

Is there a simple command kubectlto take a file kubeconfig(containing a cluster + context + user) and merge it into a ~ / .kube / config file as an additional context?

+20
source share
4 answers

Do it:

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

You can then transfer this to a new file if necessary.

+43
source

If you find yourself doing this a lot ... Now there is also a 'krew' plugin package manager for kubectl. And there is a plugin for managing yours. / kube / config file.

Using the plugin 'konfig', the syntax will be:

kubectl konfig import -s new.yaml

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

+3

kubeconfigs

kubeconfig (, ), , , kubectl kubectx, .

kubeconfig . " kubeconfig" , kubeconfigs , .

KUBECONFIG, kubeconfig 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

kubeconfig

kubeconfig YAML, , kubeconfig, kubectl :

#
# 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

kubeconfig

, kubeconfig kubeconfig $HOME/.kube/config. kubeconfig, , .

:

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

context-1 $HOME/.kube/config config-context-1. --minify , --flatten .

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

+2

( , )

# 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

, , unset KUBECONFIG env

0

Source: https://habr.com/ru/post/1685554/


All Articles