One of the K8S utilities - How to merge multiple kubeconfig?
This article was last updated on: July 24, 2024 am
Opening
📜 introduction:
- Sharpen knives and do not chop wood by mistake
- Better tools make good work
K8S cluster size, some companies tend to have a small number of large-scale K8S clusters, and some companies tend to have a large number of small-scale K8S clusters.
If it is the second case, is there a simple one kubectl command to get a kubeconfig file and merge it into ~/.kube/config File as an extra on context?
🔥 prompt:
The Kubeconfig file contains the following information for the Kubernetes cluster:
- cluster
- Context
- user
There are the following solutions:
solution
Option 1:KUBECONFIG Environment variables point to multiple files
YOU CAN TEMPORARILY GROUP KUBECONFIG FILES TOGETHER BY SPECIFYING MULTIPLE FILES IN THE KUBECONFIG ENVIRONMENT VARIABLE AND CREATE A kubectl .
As follows, then the merge in kubeconfig is done in memory:
1 | |
Option 2:flatten
Directly as follows:
1 | |
If needed, you can also pipe the output to another new file.
--flatten: Flattens the generated kubeconfig file to self-contained output (used to create portable
kubeconfig file)
Option 3:kubectl Plugins konfig
kubectl There is one krew Plug-in package manager, which can be accessed by krew Installation konfig Utility plugins to manage kubeconfig.
Utilities:krew
What is krew:
Krew is kubectl Plug-in manager for command-line tools.
Krew can help you:
- Discover the kubectl plugin
- Install them on your machine
- Keep the installed plugins up to date
Currently in krew Yes 164 pcs kubectl Plugins。
Krew works on all major platforms, such as macOS, Linux, and Windows.
Krew can also help kubectl Plugin developers: You can easily package and publish your plugins on multiple platforms, and you can do it through krew Centralized plugin library to discover them.
Installation
Krew itself is a kubectl plugin that is installed and updated via Krew (yes, krew is self-hosted).
Bash or ZSH shell installation:
-
If you need to use a proxy, configure proxy first, operation guide:Advanced Configuration · Krew
-
Confirm that it is installed
git -
Download and install
krew:1
2
3
4
5
6
7
8
9(
set -x; cd "$(mktemp -d)" &&
OS="$(uname | tr '[:upper:]' '[:lower:]')" &&
ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')" &&
KREW="krew-${OS}_${ARCH}" &&
curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/latest/download/${KREW}.tar.gz" &&
tar zxvf "${KREW}.tar.gz" &&
./"${KREW}" install krew
) -
Add to
krewarrivePATH:export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"and restart the shell -
run
kubectl krewto verify -
To see the full list of plugins, run:
kubectl krew search
Utilities:konfig
Installation:kubectl krew install konfig
krew Plugins konfig Can help you manage ~/.kube/config。
use konfig The syntax of the plugin is as follows:
1 | |
summary
2 useful plugins are shared today:
- krew:
kubectlPlugin Manager - konfig: kubeconfig configuration management plugin
🎉🎉🎉