多k8s集羣管理

多集羣的切換是K8s運維中比不可少的問題,常見的基於多個集羣進行切換的方法有三種:git

  • 切換config文件
  • 經過context進行集羣切換
  • 用kubecm進行集羣切換

切換config文件

咱們先看看放在.kube下默認的config集羣:github

$ 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更改默認集羣配置:npm

$ export  KUBECONFIG=$HOME/.kube/rancher-config

查看:api

$ 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設置,很是麻煩,同時也不方便。bash

經過context進行集羣切換

把要切換的全部config文件添加到KUBECONFIG環境變量中運維

$ export  KUBECONFIG=$HOME/.kube/config:$HOME/.kube/rancher-config

或者能夠將命令直接寫到.bashrc文件中,這樣就不須要每次手動設置。spa

經過kubectl config view能夠看到這兩個文件已經合併到一塊兒了:code

$ 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切換上下文來切換集羣:component

# 切換stage集羣
$ kubectl config use-context stage
Switched to context "stage".

切換到第二個集羣:orm

$ 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
😸 Select:production
👻 True
Context Delete:「production」
「/var/root/.kube/config」 write successful!
+------------+---------------+-----------------------+--------------------+--------------------------------+--------------+
|   CURRENT  |      NAME     |        CLUSTER        |        USER        |             SERVER             |   Namespace  |
+============+===============+=======================+====================+================================+==============+
|     *      |     stage     |   cluster-kfc6fbfcdt  |   user-kfc6fbfcdt  |   https://4.4.4.4:6443  |    default   |
+------------+---------------+-----------------------+--------------------+--------------------------------+--------------+

切換

# kubecm switch
😸 Select:production
「/var/root/.kube/config」 write successful!
+------------+---------------+-----------------------+--------------------+--------------------------------+--------------+
|   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   |
+------------+---------------+-----------------------+--------------------+--------------------------------+--------------+

Switched to context 「production」
相關文章
相關標籤/搜索