K8S Dashboard配置使用kubeconfig文件登陆

时间:2023-02-20 14:22:21

1、查看相应的secret

[root@k8s-master dashboard]# kubectl -n kubernetes-dashboard get secrets|grep kubernetes-dashboard-token
kubernetes-dashboard-token-kmcrd kubernetes.io/service-account-token 3 34d
[root@k8s-master dashboard]#
[root@k8s-master dashboard]# kubectl -n kubernetes-dashboard get secrets kubernetes-dashboard-token-kmcrd
NAME TYPE DATA AGE
kubernetes-dashboard-token-kmcrd kubernetes.io/service-account-token 3 34d

2、通过kubernetes-dashboard-token获取对应的token

[root@k8s-master dashboard]# DASH_TOCKEN=$(kubectl -n kubernetes-dashboard get secrets kubernetes-dashboard-token-kmcrd  -o jsonpath={.data.token}) 
[root@k8s-master dashboard]#

3、设置kubeconfig文件中的一个集群条目

[root@k8s-master dashboard]# kubectl config set-cluster kubernetes --server=172.16.4.169:6443 --kubeconfig=/root/dashbord-admin.conf
Cluster "kubernetes" set.

4、设置kubeconfig文件中的一个用户条目

[root@k8s-master dashboard]# kubectl config set-credentials kubernetes-dashboard --token=$DASH_TOCKEN --kubeconfig=/root/dashbord-admin.conf 
User "kubernetes-dashboard" set.

5、设置kubeconfig文件中的一个上下文条目

[root@k8s-master dashboard]# kubectl config set-context kubernetes-dashboard@kubernetes --cluster=kubernetes --user=kubernetes-dashboard --kubeconfig=/root/dashbord-admin.conf 
Context "kubernetes-dashboard@kubernetes" created.

6、设置kubeconfig文件中的当前上下文

[root@k8s-master dashboard]# kubectl config use-context kubernetes-dashboard@kubernetes --kubeconfig=/root/dashbord-admin.conf 
Switched to context "kubernetes-dashboard@kubernetes".

7、保存本地用于登陆