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
export KUBECONFIG=~/.kube/config:~/anotherconfig 

Option 2:flatten

Directly as follows:

1
2
export KUBECONFIG=~/.kube/config:~/anotherconfig 
kubectl config view --flatten

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:

  1. If you need to use a proxy, configure proxy first, operation guide:Advanced Configuration · Krew

  2. Confirm that it is installed git

  3. 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
    )
  4. Add to krew arrive PATH: export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH" and restart the shell

  5. run kubectl krew to verify

  6. 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
kubectl konfig import -s new.yaml

summary

2 useful plugins are shared today:

  1. krewkubectl Plugin Manager
  2. konfig: kubeconfig configuration management plugin

🎉🎉🎉


One of the K8S utilities - How to merge multiple kubeconfig?
https://e-whisper.com/posts/46789/
Author
east4ming
Posted on
November 9, 2021
Licensed under