证书拷贝到当前目录,kubectl命令加入环境变量
[root@k8s-node1 ~]# cd kubectl/
[root@k8s-node1 kubectl]# ll
total 12
-rw------- 1 root root 1679 Oct 6 14:07 admin-key.pem
-rw-r--r-- 1 root root 1399 Oct 6 14:07 admin.pem
-rw-r--r-- 1 root root 1359 Oct 6 14:07 ca.pem
[root@k8s-node1 kubectl]# pwd
/root/kubectl
[root@k8s-node1 kubectl]#
# 设置集群项中名为kubernetes的apiserver地址与根证书
kubectl config set-cluster kubernetes --server=https://192.168.1.195:6443 --certificate-authority=ca.pem
# 设置用户项中cluster-admin用户证书认证字段
kubectl config set-credentials cluster-admin --certificate-authority=ca.pem --client-key=admin-key.pem --client-certificate=admin.pem
# 设置环境项中名为default的默认集群和用户
kubectl config set-context default --cluster=kubernetes --user=cluster-admin
# 设置默认环境项为default
kubectl config use-context default
会在root目录下生成配置文件
[root@k8s-node1 kubectl]# cat /root/.kube/config apiVersion: v1 clusters: - cluster: certificate-authority: /root/kubectl/ca.pem server: https://192.168.56.10:6443 name: kubernetes contexts: - context: cluster: kubernetes user: cluster-admin nam e: default current-context: default kind: Config preferences: {} users: - name: cluster-admin user: as-user-extra: {} client-certificate: /root/kubectl/admin.pem client-key: /root/kubectl/admin-key.pem [root@k8s-node1 kubectl]#
这样kubectl 就可以操作集群信息了
[root@k8s-node1 kubectl]# kubectl get node
NAME STATUS ROLES AGE VERSION
192.168.56.11 Ready <none> 5h v1.9.0
192.168.56.12 Ready <none> 5h v1.9.0