1、下载recommended.yaml配置文件
1)根据自己安装的kubernetes版本安装适配的dashboard
我安装的kubernetes是1.24.2版本的,需要安装v:2.6.1版本的dashboard
2)下载地址:https://raw.githubusercontent.com/kubernetes/dashboard/v2.6.1/aio/deploy/recommended.yaml
3)可以直接用wget方式下载,也可以手动下载上传到服务器中;
2、修改配置文件
1)将名称空间改为kube-system(改不改都可,只要自己知道在哪个命名空间即可)
[root@kub-k8s-master ~] # sed -i '/namespace/ s/kubernetes-dashboard/kube-system/g' recommended.yaml
2)修改Service的类型
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
spec:
type: NodePort #修改类型为NodePort,大概在40行
ports:
- port: 443
nodePort: 31260 #添加nodePort端口
targetPort: 8443
selector:
k8s-app: kubernetes-dashboard
3、拉取镜像
1)查看要拉取的镜像
[root@k8s-master ~]# cat recommended.yaml |grep image
image: kubernetesui/dashboard:v2.6.1
imagePullPolicy: Always #提前拉取镜像需要改成ifNotPresent,不会重复拉取
image: kubernetesui/metrics-scraper:v1.0.8
2)提前拉取镜像,可节省时间
4、应用配置文件并创建dashboard
1)应用recommended.yaml
[root@k8s-master ~]# 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
2)查看pod的状态确保dashboard已经部署成功
[root@k8s-master ~]# kubectl get pod -n kubernetes-dashboard
NAME READY STATUS RESTARTS AGE
dashboard-metrics-scraper-7c5f6dcf49-gsvxr 1/1 Running 0 33s
kubernetes-dashboard-78fdc69869-hqzzk 1/1 Running 0 33s
5、访问dashboard
1)查看service,访问端口为31260
[root@k8s-master ~]# kubectl get service -n kubernetes-dashboard
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
dashboard-metrics-scraper ClusterIP 10.107.136.107 <none> 8000/TCP 61m
kubernetes-dashboard NodePort 10.102.22.176 <none> 443:31260/TCP 61m
2)浏览器输入https://masterIP:端口号访问即可;我的是https://192.168.22.139:31260,然后选择token登录
6、编写 dashboard-adminuser.yaml文件并应用
[root@k8s-master ~]# vim dashboard-adminuser.yaml
[root@k8s-master ~]# cat dashboard-adminuser.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: admin-user
namespace: kube-system
---
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: kube-system[root@k8s-master ~]# kubectl apply -f dashboard-adminuser.yaml
Warning: resource serviceaccounts/admin-user is missing the kubectl.kubernetes.io/last-applied-configuration annotation which is required by kubectl apply. kubectl apply should only be used on resources created declaratively by either kubectl create --save-config or kubectl apply. The missing annotation will be patched automatically.
serviceaccount/admin-user configured
Warning: resource clusterrolebindings/admin-user is missing the kubectl.kubernetes.io/last-applied-configuration annotation which is required by kubectl apply. kubectl apply should only be used on resources created declaratively by either kubectl create --save-config or kubectl apply. The missing annotation will be patched automatically.
clusterrolebinding.rbac.authorization.k8s.io/admin-user configured#我已经创建过一次了,所以是configured
7、获取token并登录
[root@k8s-master ~]# kubectl create token admin-user --namespace kube-system
eyJhbGciOiJSUzI1NiIsImtpZCI6IldYTHM4VXdhclB0MWozZFNyS2xMc2VWVjZGNXdtVXNjNTUtLU1hZVlXSncifQ.eyJhdWQiOlsiaHR0cHM6Ly9rdWJlcm5ldGVzLmRlZmF1bHQuc3ZjLmNsdXN0ZXIubG9jYWwiXSwiZXhwIjoxNzIyNzU3MjU0LCJpYXQiOjE3MjI3NTM2NTQsImlzcyI6Imh0dHBzOi8va3ViZXJuZXRlcy5kZWZhdWx0LnN2Yy5jbHVzdGVyLmxvY2FsIiwia3ViZXJuZXRlcy5pbyI6eyJuYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsInNlcnZpY2VhY2NvdW50Ijp7Im5hbWUiOiJhZG1pbi11c2VyIiwidWlkIjoiMzQzM2VhZGYtNDkzMi00ZjE3LTk3ZmMtZDdkYWIyZThiZTVmIn19LCJuYmYiOjE3MjI3NTM2NTQsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDprdWJlLXN5c3RlbTphZG1pbi11c2VyIn0.nRpshRe0t06fW1QCMtvIvRMhCOe9UTWW-GW7rFNgxCq1G0G15VAk7KRn5dha0CY1x1IUoZJwfay7HQwyP3W4LCUyjcJTwjUQnl7tgeHA4I_NgF_Qw21WFT77tJGCAa6Z6rY9IfrfdkVuA_Rbci0hdzKpstzZSb_mV6mTxkU9UrMzu0krGKTkewdCvq9cO9H9tIkljFzEJ7a-y4BONneHUn9qY5eEEcvFSfM7eT1dPP43oUrTLdEAgx5NZaafHT88J-bhyFqQSvJfO4vktjCGqyw5k87xxtbKnh45QrVO_j6cvhJHIq_MO8gWNQE0z-URfi2BFZC-C8lz3Akx3szW5A