多k8s叢集管理
阿新 • • 發佈:2020-12-01
多叢集的切換是K8s運維中比不可少的問題,常見的基於多個叢集進行切換的方法有三種:
- 切換config檔案
- 通過context進行叢集切換
- 用kubecm進行叢集切換
切換config檔案
我們先看看放在.kube
下預設的config叢集:
$ kubectl config view apiVersion: v1 clusters: - cluster: certificate-authority-data: DATA+OMITTED server: name: kubernetes contexts: - context: cluster: kubernetes user: "2xxxxxxxxx7" name: 2xxxxxxxxxxxxxxxx8 current-context: 2xxxxxxxxxxxx8 kind: Config preferences: {} users: - name: "2xxxxxxxxxxx27" user: client-certificate-data: client-key-data:
通過export
更改預設叢集配置:
$ export KUBECONFIG=$HOME/.kube/rancher-config
檢視:
$ kubectl config view apiVersion: v1 clusters: - cluster: certificate-authority-data: DATA+OMITTED server: name: stage contexts: - context: cluster: stage user: stage name: stage current-context: stage kind: Config preferences: {} users: - name: stage user: token:
這種方法就是特別麻煩,每次切換都要通過export設定,非常麻煩,同時也不方便。
通過context進行叢集切換
把要切換的所有config檔案新增到KUBECONFIG
環境變數中
$ export KUBECONFIG=$HOME/.kube/config:$HOME/.kube/rancher-config
或者可以將命令直接寫到.bashrc
檔案中,這樣就不需要每次手動設定。
通過kubectl config view
可以看到這兩個檔案已經合併到一起了:
$ kubectl config view apiVersion: v1 clusters: - cluster: certificate-authority-data: DATA+OMITTED server: name: kubernetes contexts: - context: cluster: kubernetes user: "2xxxxxxxxx7" name: 2xxxxxxxxxxxxxxxx8 current-context: 2xxxxxxxxxxxx8 kind: Config preferences: {} users: - name: "2xxxxxxxxxxx27" user: client-certificate-data: client-key-data:
合併一起後就可以通過use-context
切換上下文來切換叢集:
# 切換stage叢集
$ kubectl config use-context stage
Switched to context "stage".
切換到第二個叢集:
$ kubectl config use-context 2xxxxxxxxxxx8
Switched to context "21xxxxxxxxxxxxx9648".
但這種方法不方便的地方就在如果context很多的時候,不方便檢視。
用kubecm進行叢集切換
安裝篇
基礎環境
mac
k8s 1.16.9
# wget https://github.com.cnpmjs.org/sunny0826/kubecm/releases/download/v0.10.3/kubecm_0.10.3_Darwin_x86_64.tar.gz
# tar xf kubecm_0.10.3_Darwin_x86_64.tar.gz
# mv kubecm /usr/local/bin
# kubecm --help
KubeConfig Manager
_ _
| | ___ _| |__ ___ ___ _ __ ___
| |/ / | | | '_ \ / _ \/ __| '_ \ _ \
| <| |_| | |_) | __/ (__| | | | | |
|_|\_\\__,_|_.__/ \___|\___|_| |_| |_|
Find more information at: https://github.com/sunny0826/kubecm
Usage:
kubecm [command]
Available Commands:
add Merge configuration file with $HOME/.kube/config
alias Generate alias for all contexts
completion Generates bash/zsh completion scripts
delete Delete the specified context from the kubeconfig
help Help about any command
ls List kubeconfig
merge Merge the kubeconfig files in the specified directory
namespace Switch or change namespace interactively
rename Rename the contexts of kubeconfig
switch Switch Kube Context interactively
version Print version info
Flags:
--config string path of kubeconfig (default "/var/root/.kube/config")
-h, --help help for kubecm
Use "kubecm [command] --help" for more information about a command.
補全
bash
# ource <(kubecm completion bash)
# echo "source <(kubecm completion bash)" >> ~/.bashrc
# source ~/.bashrc
zsh
# source <(kubecm completion zsh)
# echo "source <(kubecm completion zsh)" >> ~/.zshrc
# source ~/.zshrc
基本使用
檢視
# kubecm ls
+------------+-----------------------------------+---------------+-----------------------+--------------------------------+--------------+
| CURRENT | NAME | CLUSTER | USER | SERVER | Namespace |
+============+===================================+===============+=======================+================================+==============+
| * | 283387751775489594-c02e82b6ada | kubernetes | 283387751775489594 | https://3.3.3.3:6443 | default |
| | f044bd94d251fea1279648 | | | | |
+------------+-----------------------------------+---------------+-----------------------+--------------------------------+--------------+
Cluster check succeeded!
Kubernetes master is running at https://3.3.3.3:6443
Contains components: [scheduler controller-manager etcd-5 etcd-0 etcd-4 etcd-1 etcd-3 etcd-2]
新增
# ll
-rw-r--r-- 1 root wheel 5.9K 8 26 15:39 config.shengchan
-rw-r--r-- 1 root wheel 5.9K 11 18 15:59 config.yufa
# kubecm add -f config.yufa -c -n stage
# kubecm add -f config.shengchan -c -n production
# kubecm ls
+------------+---------------+-----------------------+--------------------+--------------------------------+--------------+
| CURRENT | NAME | CLUSTER | USER | SERVER | Namespace |
+============+===============+=======================+====================+================================+==============+
| * | production | cluster-kg7h48f8tb | user-kg7h48f8tb | https://3.3.3.3:6443 | default |
+------------+---------------+-----------------------+--------------------+--------------------------------+--------------+
| | stage | cluster-kfc6fbfcdt | user-kfc6fbfcdt | https://4.4.4.4:6443 | default |
+------------+---------------+-----------------------+--------------------+--------------------------------+--------------+
刪除
# kubecm delete