本案例,k8s版本为v1.22.17,所以安装v2.7.0版本的dashboard
1、下载dashboard的yaml文件
curl -O https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml
2、修改recommended.yaml文件
修改定义的Service,将type由ClusterIP修改为NodePort,以便我们直接通过地址+端口进行访问,并配置访问端口。添加的内如为:
type: NodePort
nodePort: 31111
如下图 :
3、kubectl create 生成pod
执行
kubectl create -f recommended.yaml
查看 pod
kubectl get pods --all-namespaces
4、访问https://192.168.0.107:31111
5、生成token
1)创建dashboard-adminuser.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: admin-user
namespace: kubernetes-dashboard
2)执行 kubectl apply -f dashboard-adminuser.yaml
kubectl apply -f dashboard-adminuser.yaml
3)创建admin-user-role-binding.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: admin-user
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: admin-user
namespace: kubernetes-dashboard
4)执行kubectl create -f admin-user-role-binding.yaml
5)生成token
kubectl -n kubernetes-dashboard get secret $(kubectl -n kubernetes-dashboard get sa/admin-user -o jsonpath="{.secrets[0].name}") -o go-template="{{.data.token | base64decode}}"
root@k8s-master:/usr/java/k8s-dashboard# kubectl -n kubernetes-dashboard get secret $(kubectl -n kubernetes-dashboard get sa/admin-user -o jsonpath="{.secrets[0].name}") -o go-template="{{.data.token | base64decode}}"
eyJhbGciOiJSUzI1NiIsImtpZCI6Il9IS0x1SE9WOUhrcExlcS1oeWF5azNoMEhlT3M3bzVqZlRwN3Q0eU9meWMifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi11c2VyLXRva2VuLXc2N2IyIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImFkbWluLXVzZXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiJkYzg2NTM3OS02MDFjLTRmYWEtYTQzOS01N2I1ZTUxMThlMzIiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZXJuZXRlcy1kYXNoYm9hcmQ6YWRtaW4tdXNlciJ9.Hf9srfqYATgpYDlhkIdwRanFaOj2KID6P3iDzJwTINJpG2dJaROkbQql29Iier8Qp1yu0xCLjWve-9e3Zn_GY8d8r_bzqkvrckXkXwfWLoDmUJPlhewRa0EQxzQm9Pua2Ym0UcsJqGNFRtJeAFYHaR-3BvkXQ1DMnOT_slEWqOrvnoNN1PDyd6ooFdc4PTJ787FbQPO5_5geUeZn2DvA1-dHcMMADcqxW_f1rSP6fJeEVGUgwWjBt_8UyX0c9D5wjCOq7OpitS2tLZbvw2nr_BsmcOyal4G0EV0NFogADxclRUi21o5am1wjqvWsvq-LJiRKfnd6OJsCahISumyz2A
6)在页面上输入生成的token