前言
Kubectl 是一个命令行接口,用于对 Kubernetes 集群运行命令。
Kubectl 在 $HOME/.kube 目录中寻找一个名为 config 的文件。
你可以通过设置环境变量 KUBECONFIG 或设置 --kubeconfig 参数指定其它 kubeconfig 文件。
基本配置
当集群初始化成功后,要使 kubectl 适用于非 root 用户,则需要运行以下命令,这些命令也是 kubeadm init
输出的一部分:
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
# 即创建.kube目录 -> 拷贝配置文件 -> 用户授权
当然,如果你是 root 用户,则可以直接添加环境变量:
export KUBECONFIG=/etc/kubernetes/admin.conf
本地配置
① 打开CMD,在用户目录下新建.kube文件夹【mkdir .kube】并拷贝k8s集群config文件到.kube目录。
② 下载 kubectl.exe 文件放置任意文件夹,添加到环境变量Path中,打开CMD即可执行kubectl命令。
多集群配置
1)同样需要新建.kube文件夹,拷贝不同集群的config,例如重命名为config-jhmy和config-ymt,分别查看配置文件。
[root@jenkins .kube]# cat config-jhmy apiVersion: v1 clusters: - cluster: certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSU****** server: https://10.88.88.147:6443 name: kubernetes contexts: - context: cluster: kubernetes user: kubernetes-admin name: kubernetes-admin@kubernetes current-context: kubernetes-admin@kubernetes kind: Config preferences: {} users: - name: kubernetes-admin user: client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZ****** client-key-data: LS0tLS1CRUdJTiBSU0E******
2)两份配置文件中context、cluster、users名称必须不一样(同集群设置不同环境除外),否则调用将会出错,所以我们可以适当修改配置文件。
[root@jenkins .kube]# cat config-jhmy apiVersion: v1 clusters: - cluster: certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSU****** server: https://10.88.88.147:6443 name: k8s-jhmy contexts: - context: cluster: k8s-jhmy user: k8s-jhmy name: k8s-jhmy current-context: k8s-jhmy kind: Config preferences: {} users: - name: k8s-jhmy user: client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZ****** client-key-data: LS0tLS1CRUdJTiBSU0EgU******
[root@jenkins .kube]# cat config-ymt apiVersion: v1 clusters: - cluster: certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSU****** server: https://10.88.88.148:6443 name: k8s-ymt contexts: - context: cluster: k8s-ymt user: k8s-ymt name: k8s-ymt current-context: k8s-ymt kind: Config preferences: {} users: - name: k8s-ymt user: client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZ****** client-key-data: LS0tLS1CRUdJTiBSU0EgU******
3)然后执行以下命令生成新的config文件即可
[root@jenkins8 .kube]# KUBECONFIG=config-ymt:config-jhmy kubectl config view --flatten > $HOME/.kube/config
[root@jenkins8 .kube]# cat config apiVersion: v1 clusters: - cluster: certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0F****** server: https://10.88.88.147:6443 name: k8s-jhmy - cluster: certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0F****** server: https://10.88.88.148:6443 name: k8s-ymt contexts: - context: cluster: k8s-jhmy user: k8s-jhmy name: k8s-jhmy - context: cluster: k8s-ymt user: k8s-ymt name: k8s-ymt current-context: k8s-jhmy kind: Config preferences: {} users: - name: k8s-jhmy user: client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJ****** client-key-data: LS0tLS1CRUdJTiBSU****** - name: k8s-ymt user: client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJ****** client-key-data: LS0tLS1CRUdJTiBSU******
4)现在我们来测试一下吧
[root@jenkins8 .kube]# kubectl config current-context k8s-jhmy [root@jenkins8 .kube]# kubectl get nodes NAME STATUS ROLES AGE VERSION k8s116 Ready master 67d v1.17.5 k8s117 Ready master 67d v1.17.5 k8s134 Ready <none> 67d v1.17.5 k8s147 Ready master 67d v1.17.5 k8s178 Ready <none> 67d v1.17.5 [root@jenkins8 .kube]# kubectl config use-context k8s-ymt Switched to context "k8s-ymt".
作者:Leozhanggg
出处:https://www.cnblogs.com/leozhanggg/p/13223951.html
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。