环境 Ubuntu 20.4
root@ek8s-master01:~# kubectl get nodes
NAME STATUS ROLES AGE VERSION
ek8s-master01 Ready control-plane 218d v1.27.0
ek8s-node01 Ready <none> 218d v1.27.0
ek8s-node02 Ready <none> 218d v1.27.0
先创建dashboard的文件夹
root@ek8s-master01:~# mkdir -p /etc/kubernetes/dashboard
root@ek8s-master01:~# cd /etc/kubernetes/dashboard/
root@ek8s-master01:/etc/kubernetes/dashboard#
然后下载dashboard的YAML文件
wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml
为了方便访问,编辑recommended.yaml 将类型添加为NodePort
vim recommended.yaml
修改完成后 :wq保存退出 运行recommended.yaml
root@ek8s-master01:/etc/kubernetes/dashboard# kubectl apply -f recommended.yaml
namespace/kubernetes-dashboard created
serviceaccount/kubernetes-dashboard created
service/kubernetes-dashboard created
secret/kubernetes-dashboard-certs created
secret/kubernetes-dashboard-csrf created
secret/kubernetes-dashboard-key-holder created
configmap/kubernetes-dashboard-settings created
role.rbac.authorization.k8s.io/kubernetes-dashboard created
clusterrole.rbac.authorization.k8s.io/kubernetes-dashboard created
rolebinding.rbac.authorization.k8s.io/kubernetes-dashboard created
clusterrolebinding.rbac.authorization.k8s.io/kubernetes-dashboard created
deployment.apps/kubernetes-dashboard created
service/dashboard-metrics-scraper created
deployment.apps/dashboard-metrics-scraper created
创建一个管理员用户
首先新建dashboard-adminuser.yaml 文件 然后 vi dashboard-adminuser.yaml 进入编辑
root@ek8s-master01:/etc/kubernetes/dashboard# touch dashboard-adminuser.yaml
root@ek8s-master01:/etc/kubernetes/dashboard# vi dashboard-adminuser.yaml
输入如下内容
################################
### Service Accounts
### 创建管理员用户,namespace为kubernetes-dashboard
################################
apiVersion: v1
kind: ServiceAccount
metadata:
name: admin-user
namespace: kubernetes-dashboard
---
### 创建 ClusterRoleBinding
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
---
### Getting a long-lived Bearer Token for ServiceAccount
apiVersion: v1
kind: Secret
metadata:
name: admin-user
namespace: kubernetes-dashboard
annotations:
kubernetes.io/service-account.name: "admin-user"
type: kubernetes.io/service-account-token
最后:wq保存并退出
应用文件
root@ek8s-master01:/etc/kubernetes/dashboard# kubectl apply -f dashboard-adminuser.yaml
查看dashboard是否正常运行,以及查看NodePort端口,此处为31874
root@ek8s-master01:~# kubectl -n kubernetes-dashboard get pod
NAME READY STATUS RESTARTS AGE
dashboard-metrics-scraper-5cb4f4bb9c-l6dkg 1/1 Running 0 23m
kubernetes-dashboard-6967859bff-2pnhr 1/1 Running 0 23m
root@ek8s-master01:~# kubectl -n kubernetes-dashboard get service kubernetes-dashboard
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes-dashboard NodePort 10.102.125.72 <none> 443:31874/TCP 24m
打开浏览器,输入https://192.168.56.111:31874/ 如果访问失败,注意查看端口号是否正确,以及是否有写https://
回到终端,获取admin-user 的 token
输入如下命令
kubectl get secret admin-user -n kubernetes-dashboard -o jsonpath={".data.token"} | base64 -d
将终端输出的token复制粘贴到浏览器后点击登录,看到此界面即为成功