一、dashboard简介

Dashboard 是基于网页的 Kubernetes 用户界面。 可以使用 Dashboard 将容器应用部署到 Kubernetes 集群中,也可以对容器应用排错,还能管理集群资源。 你可以使用 Dashboard 获取运行在集群中的应用的概览信息,也可以创建或者修改 Kubernetes 资源 (如 Deployment,Job,DaemonSet 等等)。 例如,你可以对 Deployment 实现弹性伸缩、发起滚动升级、重启 Pod 或者使用向导创建新的应用。

Dashboard 同时展示了 Kubernetes 集群中的资源状态信息和所有报错信息。

二、部署 Dashboard UI

2.1 创建相关资源

1)在线版

默认情况下不会部署 Dashboard。可以通过以下命令部署:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.2.0/aio/deploy/recommended.yaml# kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.2.0/aio/deploy/recommended.yaml

2)离线版

先下载并导入镜像

# docker load -i dashboard_v2.2.0.tar  #导入镜像
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
kubernetesui/dashboard v2.2.0 5c4ee6ca42ce 7 months ago 225MB
# wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.2.0/aio/deploy/recommended.yaml #下载该yaml文件,再上传至服务器
# kubectl apply https://raw.githubusercontent.com/kubernetes/dashboard/v2.2.0/aio/deploy/recommended.yaml


2.2 修改Service访问类型

dashboard 服务的 namespace 是 kubernetes-dashboard,但是该服务的类型是ClusterIP,不便于我们通过浏览器访问,因此需要改成NodePort型的。

1)删除Service

# kubectl get svc --all-namespaces
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
default kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 10h
kube-system kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 10h
kubernetes-dashboard dashboard-metrics-scraper ClusterIP 10.97.34.243 <none> 8000/TCP 2m13s
kubernetes-dashboard kubernetes-dashboard ClusterIP 10.108.174.247 <none> 443/TCP 2m13s
# kubectl delete service kubernetes-dashboard --namespace=kubernetes-dashboard
service "kubernetes-dashboard" deleted

2)重新创建service

# cat dashboard-svc.yaml
apiVersion: v1
kind: Service
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
spec:
type: NodePort
ports:
- port: 443
targetPort: 8443
selector:
k8s-app: kubernetes-dashboard
# kubectl apply -f dashboard-svc.yaml
service/kubernetes-dashboard created
# kubectl get svc -n kubernetes-dashboard
NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes-dashboard dashboard-metrics-scraper ClusterIP 10.97.34.243 <none> 8000/TCP 12m
kubernetes-dashboard kubernetes-dashboard NodePort 10.111.194.202 <none> 443:30075/TCP 20s

2.3 创建kubernetes-dashboard管理员角色

想要访问dashboard服务,就要有访问权限

# cat dashboard-svc-account.yaml 
apiVersion: v1
kind: ServiceAccount
metadata:
name: dashboard-admin
namespace: kube-system
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: dashboard-admin
subjects:
- kind: ServiceAccount
name: dashboard-admin
namespace: kube-system
roleRef:
kind: ClusterRole
name: cluster-admin
apiGroup: rbac.authorization.k8s.io
# kubectl apply -f dashboard-svc-account.yaml


三、访问并登录Dashboard

3.1 获取service访问地址

通过浏览器访问:https://NodeIP:Port

# kubectl get service -n kubernetes-dashboard
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
dashboard-metrics-scraper ClusterIP 10.108.13.160 <none> 8000/TCP 49d
kubernetes-dashboard NodePort 10.97.60.149 <none> 443:31255/TCP 49d

然后即可通过NodeIP+service Port访问了。

https://192.168.2.201:31255

NodeIP为部署dashboard所在节点的IP地址

3.2 获取登录Token

# kubectl get secret -n kube-system |grep admin|awk '{print $1}'
dashboard-admin-token-fcf4p
# kubectl describe secret dashboard-admin-token-fcf4p -n kube-system

Dashboard-v2.2.0部署_云原生

3.3 使用Token登录

将获取到的Token复制到token输入处进行粘贴

Dashboard-v2.2.0部署_kubernetes_02


四、卸载Dashboard

4.1 查看kubernetes-dashboard下所有的资源对象

# kubectl get all -n kubernetes-dashboard

4.2 删除命名空间下所有资源对象

# kubectl delete service dashboard-metrics-scraper -n kubernetes-dashboard
# kubectl delete all -n kubernetes-dashboard