背景:由于需要管理多个 K8s 集群,如果每个集群都登录到 k8s 的 master 上去管理,非常不方便,最好是能从本地个人计算机连接

1 kubectl的安装步骤
https://cloud.baidu.com/doc/CCE/s/6jxpotcn5
2 将集群的 config 信息存放到一个文件中

bj-k8s 集群中执行 cat ~/.kube/config 获取,放到本地个人计算机相同的目录

MacBook-Pro .kube % cat bj-k8s
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: xxxxx # 此处是自己集群的内容
    server: https://IP:6443
  name: bj-k8s
contexts:
- context:
    cluster: bj-k8s
    user: kubernetes-admin
  name: bj-k8s
current-context: bj-k8s
kind: Config
preferences: {}
users:
- name: kubernetes-admin
  user:
    client-certificate-data: xxxxx  # 此处是自己集群的内容
    client-key-data: xxxxx          # 此处是自己集群的内容
MacBook-Pro .kube %

parking-k8s 集群中执行 cat ~/.kube/config 获取,放到本地个人计算机相同的目录下

MacBook-Pro .kube % cat parking-k8s-ali-hz
apiVersion: v1
clusters:
- cluster:
    server: https://172.25.4.95:6443
    certificate-authority-data: xxxxx  # 此处是自己集群的内容
  name: parking-k8s
contexts:
- context:
    cluster: parking-k8s
    user: "245675735389451415"
  name: parking-k8s
current-context: parking-k8s
kind: Config
preferences: {}
users:
- name: "245675735389451415"
  user:
    client-certificate-data: xxxxx  # 此处是自己集群的内容
    client-key-data: xxxxx          # 此处是自己集群的内容
MacBook-Pro .kube %

通过 config 信息,可以看到两个集群的 cluster name、context name 等信息

将配置好的配置文件合成为一个 config

MacBook-Pro .kube % cd $HOME/.kube/
MacBook-Pro .kube % KUBECONFIG=bj-k8s:ks-k8s:parking-k8s-ali-hz kubectl config view --flatten > $HOME/.kube/config

kubectl config 帮助文件

MacBook-Pro .kube % kubectl config --help
Modify kubeconfig files using subcommands like "kubectl config set current-context my-context"

 The loading order follows these rules:

  1.  If the --kubeconfig flag is set, then only that file is loaded. The flag may only be set once and no merging takes
place.
  2.  If $KUBECONFIG environment variable is set, then it is used as a list of paths (normal path delimiting rules for
your system). These paths are merged. When a value is modified, it is modified in the file that defines the stanza. When
a value is created, it is created in the first file that exists. If no files in the chain exist, then it creates the
last file in the list.
  3.  Otherwise, ${HOME}/.kube/config is used and no merging takes place.

Available Commands:
  current-context Display the current-context
  delete-cluster  删除 kubeconfig 文件中指定的集群
  delete-context  删除 kubeconfig 文件中指定的 context
  delete-user     Delete the specified user from the kubeconfig
  get-clusters    显示 kubeconfig 文件中定义的集群
  get-contexts    描述一个或多个 contexts
  get-users       Display users defined in the kubeconfig
  rename-context  Rename a context from the kubeconfig file
  set             Set an individual value in a kubeconfig file
  set-cluster     Set a cluster entry in kubeconfig
  set-context     Set a context entry in kubeconfig
  set-credentials Set a user entry in kubeconfig
  unset           Unset an individual value in a kubeconfig file
  use-context     Set the current-context in a kubeconfig file
  view            显示合并的 kubeconfig 配置或一个指定的 kubeconfig 文件

Usage:
  kubectl config SUBCOMMAND [options]

Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).
MacBook-Pro .kube %
3 常用示例:

显示 kubeconfig 文件中定义的集群

MacBook-Pro .kube % kubectl config get-clusters
NAME
parking-k8s
bj-k8s
ks-k8s
MacBook-Pro .kube %

描述一个或多个 contexts

MacBook-Pro .kube % kubectl config get-contexts
CURRENT   NAME          CLUSTER       AUTHINFO             NAMESPACE
          bj-k8s        bj-k8s        kubernetes-admin
          ks-k8s        ks-k8s        kubernetes-admin
*         parking-k8s   parking-k8s   245675735389451415

显示当前正在使用的集群

MacBook-Pro .kube % kubectl config current-context
parking-k8s
# 显示当前集群的 node信息
MacBook-Pro .kube % kubectl get node
NAME                       STATUS   ROLES    AGE     VERSION
cn-hangzhou.172.25.6.208   Ready    <none>   6d15h   v1.20.11-aliyun.1
cn-hangzhou.172.25.6.209   Ready    <none>   6d15h   v1.20.11-aliyun.1
MacBook-Pro .kube %

切换集群,即修改需要使用的集群

MacBook-Pro .kube % kubectl config use-context bj-k8s
Switched to context "bj-k8s".
MacBook-Pro .kube % kubectl config current-context
bj-k8s
MacBook-Pro .kube %
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐