在 Kubernetes 社区中,有一个很受欢迎的 Dashboard 项目,它可以给用户提供一个可视化的 Web 界面来查看当前集群的各种信息。用户可以用 Kubernetes Dashboard 部署容器化的应用、监控应用的状态、执行故障排查任务以及管理 Kubernetes 各种资源。
kubernetes集群准备
集群部署参考:
https://blog.csdn.net/qq_37382077/article/details/98632379
官方参考文档:
https://kubernetes.io/docs/tasks/access-application-cluster/web-ui-dashboard/#deploying-the-dashboard-ui
github项目地址:
https://github.com/kubernetes/dashboard
这里部署dashboard v1.10.1版本。
先下载yaml文件到本地
wget https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml
由于yaml配置文件中指定镜像从google拉取,修改yaml配置文件image部分,指定镜像从阿里云镜像仓库拉取:
[kubeadm@server1 ~]$ vim kubernetes-dashboard.yaml
......
containers:
- name: kubernetes-dashboard
#image: k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1 #如果本地已存在镜像
image: registry.cn-hangzhou.aliyuncs.com/google_containers/kubernetes-dashboard-amd64:v1.10.1
ports:
......
各个节点加载镜像
[root@server1 ~]# docker load -i kubernetes-dashboard.tar
master节点执行以下命令部署dashboard服务:
[root@server1 ~]# su - kubeadm
[kubeadm@server1 ~]$ ls
dashboard-admin.yaml kube-flannel.yml kubernetes-dashboard.tar kubernetes-dashboard.yaml
[kubeadm@server1 ~]$ kubectl create -f kubernetes-dashboard.yaml # 以普通用户身份执行
查看Pod 的状态为running说明dashboard已经部署成功:
[kubeadm@server1 ~]$ kubectl get pod --namespace=kube-system -o wide | grep dashboard
Dashboard 会在 kube-system namespace 中创建自己的 Deployment 和 Service:
[kubeadm@server1 ~]$ kubectl get deployment kubernetes-dashboard --namespace=kube-system
[kubeadm@server1 ~]$ kubectl get service kubernetes-dashboard --namespace=kube-system
有以下几种方式访问dashboard:
为了便于本地访问,修改yaml文件,将service改为NodePort 类型:
[kubeadm@server1 ~]$ vim kubernetes-dashboard.yaml
......
---
# ------------------- Dashboard Service ------------------- #
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kube-system
spec:
type: NodePort #增加type: NodePort
ports:
- port: 443
targetPort: 8443
nodePort: 31620 #增加nodePort: 31620
selector:
k8s-app: kubernetes-dashboard
重新应用yaml文件
kubectl apply -f kubernetes-dashboard.yaml
查看service,TYPE类型已经变为NodePort,端口为31620
[kubeadm@server1 ~]$ kubectl get service -n kube-system | grep dashboard
kubernetes-dashboard NodePort 10.105.43.244 443:31620/TCP 36m
通过浏览器访问:https://172.25.25.1:31620/, 登录界面如下:
Dashboard 支持 Kubeconfig 和 Token 两种认证方式,我们这里选择Token认证方式登录:
创建登录用户
官方参考文档:
https://github.com/kubernetes/dashboard/wiki/Creating-sample-user
创建dashboard-adminuser.yaml:
[kubeadm@server1 ~]$ vim 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
执行yaml文件:
kubectl create -f dashboard-admin.yaml
说明:上面创建了一个叫admin-user的服务账号,并放在kube-system命名空间下,并将cluster-admin角色绑定到admin-user账户,这样admin-user账户就有了管理员的权限。默认情况下,kubeadm创建集群时已经创建了cluster-admin角色,我们直接绑定即可。
查看admin-user账户的token
[kubeadm@server1 ~]$ kubectl get secrets -n kube-system | grep admin
admin-user-token-ks8c4 kubernetes.io/service-account-token 3 2m23s
[kubeadm@server1 ~]$ kubectl describe secrets admin-user-token-ks8c4 -n kube-system
Name: admin-user-token-ks8c4
Namespace: kube-system
Labels:
Annotations: kubernetes.io/service-account.name: admin-user
kubernetes.io/service-account.uid: ae3f4fcb-9e7b-4c8b-b500-3ab9d81e0628
Type: kubernetes.io/service-account-token
Data
====
ca.crt: 1025 bytes
namespace: 11 bytes
token: eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi11c2VyLXRva2VuLWtzOGM0Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImFkbWluLXVzZXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiJhZTNmNGZjYi05ZTdiLTRjOGItYjUwMC0zYWI5ZDgxZTA2MjgiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZS1zeXN0ZW06YWRtaW4tdXNlciJ9.cGFjyvVMD_7TLPfW4Doe_YBjAHvRHkwJ_cohDiLLyuuUCo1cZJlgMfYiiR4VZ-d7PWWA70geD6Zg0Es3jh_TyK_KIClMC0vcXU6lmXBDEqxRXgu08Jw3VdIFnEuNGlRWC6etsDC4lfaL_npyoHjt7xqsMpyEn408dx3yfATZJdaVkO5BsYUMGL6rpIQXDdGHl23bf4eCYPWH1G8XW3OpaVwCF9FwRcLSqzAjKLk7wdtO-OFR4mzYAVPyYNR99RfpmCh9eHnLZjJl6QJ2at5_VuEO8fuSRcx3ZBz-NslaToI2Ru5QP4kwobzZrGMgbkj4oX4kQKsM0wStPBzdcqe7Ng
把获取到的Token复制到登录界面的Token输入框中: