一、Kubernetes Dashboard 简介

Kubernetes Dashboard是k8s的管理工具,先引用官方的文档说明:

Dashboard is a web-based Kubernetes user interface. You can use Dashboard to deploy containerized applications to a Kubernetes cluster, troubleshoot your containerized application, and manage the cluster resources. You can use Dashboard to get an overview of applications running on your cluster, as well as for creating or modifying individual Kubernetes resources (such as Deployments, Jobs, DaemonSets, etc). For example, you can scale a Deployment, initiate a rolling update, restart a pod or deploy new applications using a deploy wizard.

大意就是:

Dashboard 是基于 Web 的 Kubernetes 用户界面。您可以使用 Dashboard 将容器化应用程序部署到 Kubernetes 群集、对容器化应用程序进行故障排除以及管理群集资源。您可以使用 Dashboard 获取群集上运行的应用程序的概述,以及创建或修改单个 Kubernetes 资源(如部署、作业、守护进程集等)。例如,您可以缩放部署、启动滚动更新、重新启动窗格或使用部署向导部署新应用程序。

仪表板还提供有关群集中 Kubernetes 资源的状态以及可能发生的任何错误的信息。

下面说说如何在Docker Desktop的Kubernetes中安装。

二、获取部署文件

先到下面的链接查看最新版的Kubernetes Dashboard:https://github.com/kubernetes/dashboard/releases/

然后找到部署的yaml文件,本人安装时是使用:https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.4/aio/deploy/recommended.yaml

当然,我知道大家的网络都不是太好,于是将其下载到本地,方便大家复制。以下内容修改了Service部分,修改后的文件如下:

  1. # Copyright 2017 The Kubernetes Authors.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. apiVersion: v1
  15. kind: Namespace
  16. metadata:
  17. name: kubernetes-dashboard
  18. ---
  19. apiVersion: v1
  20. kind: ServiceAccount
  21. metadata:
  22. labels:
  23. k8s-app: kubernetes-dashboard
  24. name: kubernetes-dashboard
  25. namespace: kubernetes-dashboard
  26. ---
  27. kind: Service
  28. apiVersion: v1
  29. metadata:
  30. labels:
  31. k8s-app: kubernetes-dashboard
  32. name: kubernetes-dashboard
  33. namespace: kubernetes-dashboard
  34. spec:
  35. ports:
  36. - port: 443
  37. targetPort: 8443
  38. nodePort: 30443
  39. name: kubernetes-dashboard
  40. type: NodePort
  41. selector:
  42. k8s-app: kubernetes-dashboard
  43. ---
  44. apiVersion: v1
  45. kind: Secret
  46. metadata:
  47. labels:
  48. k8s-app: kubernetes-dashboard
  49. name: kubernetes-dashboard-certs
  50. namespace: kubernetes-dashboard
  51. type: Opaque
  52. ---
  53. apiVersion: v1
  54. kind: Secret
  55. metadata:
  56. labels:
  57. k8s-app: kubernetes-dashboard
  58. name: kubernetes-dashboard-csrf
  59. namespace: kubernetes-dashboard
  60. type: Opaque
  61. data:
  62. csrf: ""
  63. ---
  64. apiVersion: v1
  65. kind: Secret
  66. metadata:
  67. labels:
  68. k8s-app: kubernetes-dashboard
  69. name: kubernetes-dashboard-key-holder
  70. namespace: kubernetes-dashboard
  71. type: Opaque
  72. ---
  73. kind: ConfigMap
  74. apiVersion: v1
  75. metadata:
  76. labels:
  77. k8s-app: kubernetes-dashboard
  78. name: kubernetes-dashboard-settings
  79. namespace: kubernetes-dashboard
  80. ---
  81. kind: Role
  82. apiVersion: rbac.authorization.k8s.io/v1
  83. metadata:
  84. labels:
  85. k8s-app: kubernetes-dashboard
  86. name: kubernetes-dashboard
  87. namespace: kubernetes-dashboard
  88. rules:
  89. # Allow Dashboard to get, update and delete Dashboard exclusive secrets.
  90. - apiGroups: [""]
  91. resources: ["secrets"]
  92. resourceNames: ["kubernetes-dashboard-key-holder", "kubernetes-dashboard-certs", "kubernetes-dashboard-csrf"]
  93. verbs: ["get", "update", "delete"]
  94. # Allow Dashboard to get and update 'kubernetes-dashboard-settings' config map.
  95. - apiGroups: [""]
  96. resources: ["configmaps"]
  97. resourceNames: ["kubernetes-dashboard-settings"]
  98. verbs: ["get", "update"]
  99. # Allow Dashboard to get metrics.
  100. - apiGroups: [""]
  101. resources: ["services"]
  102. resourceNames: ["heapster", "dashboard-metrics-scraper"]
  103. verbs: ["proxy"]
  104. - apiGroups: [""]
  105. resources: ["services/proxy"]
  106. resourceNames: ["heapster", "http:heapster:", "https:heapster:", "dashboard-metrics-scraper", "http:dashboard-metrics-scraper"]
  107. verbs: ["get"]
  108. ---
  109. kind: ClusterRole
  110. apiVersion: rbac.authorization.k8s.io/v1
  111. metadata:
  112. labels:
  113. k8s-app: kubernetes-dashboard
  114. name: kubernetes-dashboard
  115. rules:
  116. # Allow Metrics Scraper to get metrics from the Metrics server
  117. - apiGroups: ["metrics.k8s.io"]
  118. resources: ["pods", "nodes"]
  119. verbs: ["get", "list", "watch"]
  120. ---
  121. apiVersion: rbac.authorization.k8s.io/v1
  122. kind: RoleBinding
  123. metadata:
  124. labels:
  125. k8s-app: kubernetes-dashboard
  126. name: kubernetes-dashboard
  127. namespace: kubernetes-dashboard
  128. roleRef:
  129. apiGroup: rbac.authorization.k8s.io
  130. kind: Role
  131. name: kubernetes-dashboard
  132. subjects:
  133. - kind: ServiceAccount
  134. name: kubernetes-dashboard
  135. namespace: kubernetes-dashboard
  136. ---
  137. apiVersion: rbac.authorization.k8s.io/v1
  138. kind: ClusterRoleBinding
  139. metadata:
  140. name: kubernetes-dashboard
  141. roleRef:
  142. apiGroup: rbac.authorization.k8s.io
  143. kind: ClusterRole
  144. name: kubernetes-dashboard
  145. subjects:
  146. - kind: ServiceAccount
  147. name: kubernetes-dashboard
  148. namespace: kubernetes-dashboard
  149. ---
  150. kind: Deployment
  151. apiVersion: apps/v1
  152. metadata:
  153. labels:
  154. k8s-app: kubernetes-dashboard
  155. name: kubernetes-dashboard
  156. namespace: kubernetes-dashboard
  157. spec:
  158. replicas: 1
  159. revisionHistoryLimit: 10
  160. selector:
  161. matchLabels:
  162. k8s-app: kubernetes-dashboard
  163. template:
  164. metadata:
  165. labels:
  166. k8s-app: kubernetes-dashboard
  167. spec:
  168. containers:
  169. - name: kubernetes-dashboard
  170. image: kubernetesui/dashboard:v2.0.4
  171. imagePullPolicy: Always
  172. ports:
  173. - containerPort: 8443
  174. protocol: TCP
  175. args:
  176. - --auto-generate-certificates
  177. - --namespace=kubernetes-dashboard
  178. # Uncomment the following line to manually specify Kubernetes API server Host
  179. # If not specified, Dashboard will attempt to auto discover the API server and connect
  180. # to it. Uncomment only if the default does not work.
  181. # - --apiserver-host=http://my-address:port
  182. volumeMounts:
  183. - name: kubernetes-dashboard-certs
  184. mountPath: /certs
  185. # Create on-disk volume to store exec logs
  186. - mountPath: /tmp
  187. name: tmp-volume
  188. livenessProbe:
  189. httpGet:
  190. scheme: HTTPS
  191. path: /
  192. port: 8443
  193. initialDelaySeconds: 30
  194. timeoutSeconds: 30
  195. securityContext:
  196. allowPrivilegeEscalation: false
  197. readOnlyRootFilesystem: true
  198. runAsUser: 1001
  199. runAsGroup: 2001
  200. volumes:
  201. - name: kubernetes-dashboard-certs
  202. secret:
  203. secretName: kubernetes-dashboard-certs
  204. - name: tmp-volume
  205. emptyDir: {}
  206. serviceAccountName: kubernetes-dashboard
  207. nodeSelector:
  208. "kubernetes.io/os": linux
  209. # Comment the following tolerations if Dashboard must not be deployed on master
  210. tolerations:
  211. - key: node-role.kubernetes.io/master
  212. effect: NoSchedule
  213. ---
  214. kind: Service
  215. apiVersion: v1
  216. metadata:
  217. labels:
  218. k8s-app: dashboard-metrics-scraper
  219. name: dashboard-metrics-scraper
  220. namespace: kubernetes-dashboard
  221. spec:
  222. ports:
  223. - port: 8000
  224. targetPort: 8000
  225. selector:
  226. k8s-app: dashboard-metrics-scraper
  227. ---
  228. kind: Deployment
  229. apiVersion: apps/v1
  230. metadata:
  231. labels:
  232. k8s-app: dashboard-metrics-scraper
  233. name: dashboard-metrics-scraper
  234. namespace: kubernetes-dashboard
  235. spec:
  236. replicas: 1
  237. revisionHistoryLimit: 10
  238. selector:
  239. matchLabels:
  240. k8s-app: dashboard-metrics-scraper
  241. template:
  242. metadata:
  243. labels:
  244. k8s-app: dashboard-metrics-scraper
  245. annotations:
  246. seccomp.security.alpha.kubernetes.io/pod: 'runtime/default'
  247. spec:
  248. containers:
  249. - name: dashboard-metrics-scraper
  250. image: kubernetesui/metrics-scraper:v1.0.4
  251. ports:
  252. - containerPort: 8000
  253. protocol: TCP
  254. livenessProbe:
  255. httpGet:
  256. scheme: HTTP
  257. path: /
  258. port: 8000
  259. initialDelaySeconds: 30
  260. timeoutSeconds: 30
  261. volumeMounts:
  262. - mountPath: /tmp
  263. name: tmp-volume
  264. securityContext:
  265. allowPrivilegeEscalation: false
  266. readOnlyRootFilesystem: true
  267. runAsUser: 1001
  268. runAsGroup: 2001
  269. serviceAccountName: kubernetes-dashboard
  270. nodeSelector:
  271. "kubernetes.io/os": linux
  272. # Comment the following tolerations if Dashboard must not be deployed on master
  273. tolerations:
  274. - key: node-role.kubernetes.io/master
  275. effect: NoSchedule
  276. volumes:
  277. - name: tmp-volume
  278. emptyDir: {}

三、部署Kubernetes Dashboard

修改完成后,使用此文件进行部署:

  1. $ kubectl apply -f kubernetes-dashboard.yaml
  2. namespace/kubernetes-dashboard created
  3. serviceaccount/dashboard created
  4. clusterrolebinding.rbac.authorization.k8s.io/dashboard created
  5. service/kubernetes-dashboard created
  6. secret/kubernetes-dashboard-certs created
  7. secret/kubernetes-dashboard-csrf created
  8. secret/kubernetes-dashboard-key-holder created
  9. configmap/kubernetes-dashboard-settings created
  10. role.rbac.authorization.k8s.io/kubernetes-dashboard created
  11. clusterrole.rbac.authorization.k8s.io/kubernetes-dashboard created
  12. rolebinding.rbac.authorization.k8s.io/kubernetes-dashboard created
  13. clusterrolebinding.rbac.authorization.k8s.io/kubernetes-dashboard created
  14. deployment.apps/kubernetes-dashboard created
  15. service/dashboard-metrics-scraper created
  16. deployment.apps/dashboard-metrics-scraper created

当然,如果客官网络方便的话,可以直接使用:

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

不过相关的Service还是得重写后部署,暴露端口以提供宿主机访问:

  1. kind: Service
  2. apiVersion: v1
  3. metadata:
  4. labels:
  5. k8s-app: kubernetes-dashboard
  6. name: kubernetes-dashboard
  7. namespace: kubernetes-dashboard
  8. spec:
  9. ports:
  10. - port: 443
  11. targetPort: 8443
  12. nodePort: 30443
  13. name: kubernetes-dashboard
  14. type: NodePort
  15. selector:
  16. k8s-app: kubernetes-dashboard

部署完毕后,查看其token:

  1. $ kubectl -n kubernetes-dashboard describe secret kubernetes-dashboard
  2. Name: kubernetes-dashboard-certs
  3. Namespace: kubernetes-dashboard
  4. Labels: k8s-app=kubernetes-dashboard
  5. Annotations:
  6. Type: Opaque
  7. Data
  8. ====
  9. Name: kubernetes-dashboard-csrf
  10. Namespace: kubernetes-dashboard
  11. Labels: k8s-app=kubernetes-dashboard
  12. Annotations:
  13. Type: Opaque
  14. Data
  15. ====
  16. csrf: 256 bytes
  17. Name: kubernetes-dashboard-key-holder
  18. Namespace: kubernetes-dashboard
  19. Labels: <none>
  20. Annotations: <none>
  21. Type: Opaque
  22. Data
  23. ====
  24. priv: 1679 bytes
  25. pub: 459 bytes
  26. Name: kubernetes-dashboard-token-qbqfb
  27. Namespace: kubernetes-dashboard
  28. Labels: <none>
  29. Annotations: kubernetes.io/service-account.name: kubernetes-dashboard
  30. kubernetes.io/service-account.uid: 7d7d2c68-ea5a-4ec4-9571-6651bb57553b
  31. Type: kubernetes.io/service-account-token
  32. Data
  33. ====
  34. ca.crt: 1025 bytes
  35. namespace: 20 bytes
  36. token: eyJhbGciOiJSUzI1NiIsImtpZCI6IkZMOVFkTXlLZnpQSmZxOThCT0NRdUVfa182bl9qZnA5MDVHelRILXpVSTAifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJrdWJlcm5ldGVzLWRhc2hib2FyZC10b2tlbi1xYnFmYiIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6IjdkN2QyYzY4LWVhNWEtNGVjNC05NTcxLTY2NTFiYjU3NTUzYiIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDprdWJlcm5ldGVzLWRhc2hib2FyZDprdWJlcm5ldGVzLWRhc2hib2FyZCJ9.O-FSx2r7mLFcOxYrQu8Y5yzK9FIdGtWX3tMVgMib-O2xx3XVOKx91pCHsZZH-4k8UF4yg6YVt9iT5F9MWoHoZS0gjIqRaY0q2BVgioNuPoaT5Dbnm2Y9J9OpqZ3vWqGyX_skNlFBxzpoobz3fKjRB2MrSFWZ8WCPifapv-vLivW46TtQrzeRMkLsOCjd1sPZ9VxLg8PnB0SZsYHppZkWpnTme267aBEg2_iI62sykH8-NM_5rzapQx1MSf3fd7mA7l_lZbmgPUZXk5fejaG7ji5ltzhQSZYXUUm9L9PkKT5ksXvfEY_BjbVvahRTJDVBZPxFpZt9M-tOcsN6Fh-3SQ

看到最底下就是token了。

四、使用token登录

打开浏览器,输入:https://localhost:30443/ ,首次打开会提示SSL证书错误:
📃 部署Kubernetes Dashboard - 图1
点击继续,输入获取到的token:
📃 部署Kubernetes Dashboard - 图2
就进入到了管理界面:
📃 部署Kubernetes Dashboard - 图3

五、使用kubeconfig登录

打开 ~/.kube/config 文件,将token粘到user节点下:
image.png
使用此文件登录即可:
image.png

参考资料