0. 概述

在 Kubernetes 中,以前我比较习惯于使用命令行,但是,有时觉得敲命令挺累,反而喜欢浏览器这样的直观展示,所以就想装个 Dashboard,本文记录一下创建 Dashboard 的简单步骤。

1. 部署 Dashboard

部署 Dashboard 其实已经非常简单了,只需要 Apply 一个 YAML 配置即可:

[[email protected]]# kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0/aio/deploy/recommended.yaml

但是,APPLY 完之后,你没法直接访问,官方给的访问方式是 kubectl proxy,但是这种方式的 URL 太长,不好记忆,所以我换一种方式,通过 NodePort 的形式来访问。

2. 添加 Service

虽然 Dashboard APPLY 完也有 Service,但是是 ClusterIP 类型的,所以一般情况下外部环境是访问不了的,所以我额外创建了一个 NodePort 类型的:

[[email protected]]# cat service.yaml
apiVersion: v1
kind: Service
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard-nodeport
  namespace: kubernetes-dashboard
spec:
  ports:
  - port: 443
    nodePort: 30443
    protocol: TCP
    targetPort: 8443
  selector:
    k8s-app: kubernetes-dashboard
  type: NodePort

这样,我就可以通过 http://<cluster-ip>:30443 访问到 Dashboard 了,但是问题又接踵而来,打开页面就要求要 Token/Kubeconfig:

图 1:Kubernetes Dashboard 添加 Token

3. 生成 Token

当然,可以直接将 Cluster 中的 Kubeconfig 复制出来,直接使用,但是我这里就遵循官方的指导使用 Token,分为几步:

3.1 创建 Service Account

[[email protected]]# cat service-account.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  name: admin-user
  namespace: kubernetes-dashboard

3.2 绑定角色

[[email protected]]# cat clusterrole-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

3.3 获取 token

[[email protected]]# kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}')

Name:         admin-user-token-tjgg8
Namespace:    kubernetes-dashboard
Labels:       <none>
Annotations:  kubernetes.io/service-account.name: admin-user
              kubernetes.io/service-account.uid: ff4e303a-6074-4d2a-a540-d0eee7e55128

Type:  kubernetes.io/service-account-token

Data
====
token:      eyJhbGciOiJSUzI1NiIsImtpZCI6IlR0b2o1ZVhPXzhOXzI3T2RLVVVJREhaTHhweEVNOVJsWDc5MFZLUVBTaXMifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi11c2VyLXRva2VuLXRqZ2c4Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImFkbWluLXVzZXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiJmZjRlMzAzYS02MDc0LTRkMmEtYTU0MC1kMGVlZTdlNTUxMjgiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZXJuZXRlcy1kYXNoYm9hcmQ6YWRtaW4tdXNlciJ9.4exHS60g2YfkGF3cONMnOfWXupxWjeruy5jKkW2xV8KyQiaey-NcKlIRzuocb570XrR_UPF7okZiZ2ycKq8J58NmWL0ZlSygKzT_NfkO7fKeK14MZWYZkO_VsFe4YjVeEuQNkoq6xkM8OTSsFBS-4RCWCMHDysKn_0vAjy8pqfUOqahdia4Cbj654Ag09_JA1GPzNK11tR5i5ZMOSjW6shRJ7AmBDFMa-EWxK-f_1B-899H4iEBBKNaekmPPfZcI6lnsGpEVvap04jZUwhsH8f3Z6_XpJn-2Tiyy7ObPP7RzIXXkOOQFhU0ND9B2bnGaFbQp7YmkCC_QrRa4Fj0bQQ
ca.crt:     1025 bytes
namespace:  20 bytes

然后这里的一长串 Token 复制进 Dashboard UI 就可以了,Enjoy 的 Dashboard:

图 2:Dashboard UI

4. Ref