为Kubernetes安装Dashboard
在《使用VirtualBox安装CentOS 7和Kubernetes》章节中,我们已经成功地使用VirtualBox虚拟机安装了CentOS 7.9、Docker和Kubernetes,现在我们继续安装Dashboard服务,以便于我们可以更直观地了解Kubernetes。
查找Dashboard版本
我们可在GitHub中Dashboard的Release页面查找Dashboard的可用版本:
https://github.com/kubernetes/dashboard/releases?after=v2.0.0
Kubernetes-Dashboard在GitHub的上的发布页面
此时,我们使用v2.4.0版本即可。在下方的“Installation”部分我们可以看到该版本的安装命令:
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.4.0/aio/deploy/recommended.yaml
尝试执行该命令,会发生如下错误:
The connection to the server raw.githubusercontent.com was refused - did you specify the right host or port?
这是因为域名“raw.githubusercontent.com”无法被解析。因此我们只能手动下载配置文件。
下载recommended.yaml配置文件
首先,我们访问Dashboard项目主页,找到“v2.4.0”版本的Tag。
找到Kubernetes-Dashboard项目的v2.4.0版本的Tag
然后,我们切换到该Tag,并找到“/aio/deploy”目录下的“recommended.yaml”配置文件。
在Kubernetes-Dashboard项目的v2.4.0版本中找到recommended.yaml配置文件
最后,我们复制该配置文件的内容,并写入到本地文件中:
vi recommended.yaml
文件的内容为:
# Copyright 2017 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
apiVersion: v1
kind: Namespace
metadata:
name: kubernetes-dashboard
---
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
---
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
spec:
ports:
- port: 443
targetPort: 8443
selector:
k8s-app: kubernetes-dashboard
---
apiVersion: v1
kind: Secret
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard-certs
namespace: kubernetes-dashboard
type: Opaque
---
apiVersion: v1
kind: Secret
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard-csrf
namespace: kubernetes-dashboard
type: Opaque
data:
csrf: ""
---
apiVersion: v1
kind: Secret
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard-key-holder
namespace: kubernetes-dashboard
type: Opaque
---
kind: ConfigMap
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard-settings
namespace: kubernetes-dashboard
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
rules:
# Allow Dashboard to get, update and delete Dashboard exclusive secrets.
- apiGroups: [""]
resources: ["secrets"]
resourceNames: ["kubernetes-dashboard-key-holder", "kubernetes-dashboard-certs", "kubernetes-dashboard-csrf"]
verbs: ["get", "update", "delete"]
# Allow Dashboard to get and update 'kubernetes-dashboard-settings' config map.
- apiGroups: [""]
resources: ["configmaps"]
resourceNames: ["kubernetes-dashboard-settings"]
verbs: ["get", "update"]
# Allow Dashboard to get metrics.
- apiGroups: [""]
resources: ["services"]
resourceNames: ["heapster", "dashboard-metrics-scraper"]
verbs: ["proxy"]
- apiGroups: [""]
resources: ["services/proxy"]
resourceNames: ["heapster", "http:heapster:", "https:heapster:", "dashboard-metrics-scraper", "http:dashboard-metrics-scraper"]
verbs: ["get"]
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
rules:
# Allow Metrics Scraper to get metrics from the Metrics server
- apiGroups: ["metrics.k8s.io"]
resources: ["pods", "nodes"]
verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: kubernetes-dashboard
subjects:
- kind: ServiceAccount
name: kubernetes-dashboard
namespace: kubernetes-dashboard
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: kubernetes-dashboard
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: kubernetes-dashboard
subjects:
- kind: ServiceAccount
name: kubernetes-dashboard
namespace: kubernetes-dashboard
---
kind: Deployment
apiVersion: apps/v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
spec:
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
k8s-app: kubernetes-dashboard
template:
metadata:
labels:
k8s-app: kubernetes-dashboard
spec:
containers:
- name: kubernetes-dashboard
image: kubernetesui/dashboard:v2.4.0
imagePullPolicy: Always
ports:
- containerPort: 8443
protocol: TCP
args:
- --auto-generate-certificates
- --namespace=kubernetes-dashboard
# Uncomment the following line to manually specify Kubernetes API server Host
# If not specified, Dashboard will attempt to auto discover the API server and connect
# to it. Uncomment only if the default does not work.
# - --apiserver-host=http://my-address:port
volumeMounts:
- name: kubernetes-dashboard-certs
mountPath: /certs
# Create on-disk volume to store exec logs
- mountPath: /tmp
name: tmp-volume
livenessProbe:
httpGet:
scheme: HTTPS
path: /
port: 8443
initialDelaySeconds: 30
timeoutSeconds: 30
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
runAsUser: 1001
runAsGroup: 2001
volumes:
- name: kubernetes-dashboard-certs
secret:
secretName: kubernetes-dashboard-certs
- name: tmp-volume
emptyDir: {}
serviceAccountName: kubernetes-dashboard
nodeSelector:
"kubernetes.io/os": linux
# Comment the following tolerations if Dashboard must not be deployed on master
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
---
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: dashboard-metrics-scraper
name: dashboard-metrics-scraper
namespace: kubernetes-dashboard
spec:
ports:
- port: 8000
targetPort: 8000
selector:
k8s-app: dashboard-metrics-scraper
---
kind: Deployment
apiVersion: apps/v1
metadata:
labels:
k8s-app: dashboard-metrics-scraper
name: dashboard-metrics-scraper
namespace: kubernetes-dashboard
spec:
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
k8s-app: dashboard-metrics-scraper
template:
metadata:
labels:
k8s-app: dashboard-metrics-scraper
spec:
securityContext:
seccompProfile:
type: RuntimeDefault
containers:
- name: dashboard-metrics-scraper
image: kubernetesui/metrics-scraper:v1.0.7
ports:
- containerPort: 8000
protocol: TCP
livenessProbe:
httpGet:
scheme: HTTP
path: /
port: 8000
initialDelaySeconds: 30
timeoutSeconds: 30
volumeMounts:
- mountPath: /tmp
name: tmp-volume
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
runAsUser: 1001
runAsGroup: 2001
serviceAccountName: kubernetes-dashboard
nodeSelector:
"kubernetes.io/os": linux
# Comment the following tolerations if Dashboard must not be deployed on master
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
volumes:
- name: tmp-volume
emptyDir: {}
拉取所需的Docker镜像
我们首先通过以下命令,列出Dashboard所需要的镜像:
cat recommended.yaml | grep image:
其输出结果为:
image: kubernetesui/dashboard:v2.4.0
image: kubernetesui/metrics-scraper:v1.0.7
可知我们需要两个镜像,我们首先将这两个镜像拉取到本地,以加速后续的安装过程。
在拉取镜像之前,我们先配置Docker官方在国内的加速。我们打开“/etc/docker/daemon.json”配置文件:
vi /etc/docker/daemon.json
然后增加镜像站配置:
{
"exec-opts": [
"native.cgroupdriver=systemd"
],
"registry-mirrors": [
"https://registry.docker-cn.com"
]
}
现在我们分别拉取这两个镜像。与镜像站建立连接可能需要一些时间,我们需要耐心等待。
首先拉取“
kubernetesui/dashboard:v2.4.0”镜像:
docker image pull kubernetesui/dashboard:v2.4.0
拉取成功后的输出为:
v2.4.0: Pulling from kubernetesui/dashboard
5a24d13191c9: Pull complete
476e0d029a85: Pull complete
Digest: sha256:526850ae4ea9aba360e72b6df69fd3126b129d446efe83ac5250282b85f95b7f
Status: Downloaded newer image for kubernetesui/dashboard:v2.4.0
docker.io/kubernetesui/dashboard:v2.4.0
然后拉取“
kubernetesui/metrics-scraper:v1.0.7”镜像:
docker image pull kubernetesui/metrics-scraper:v1.0.7
拉取成功后的输出为:
v1.0.7: Pulling from kubernetesui/metrics-scraper
18dd5eddb60d: Pull complete
1930c20668a8: Pull complete
Digest: sha256:36d5b3f60e1a144cc5ada820910535074bdf5cf73fb70d1ff1681537eef4e172
Status: Downloaded newer image for kubernetesui/metrics-scraper:v1.0.7
docker.io/kubernetesui/metrics-scraper:v1.0.7
修改recommended.yaml配置
现在,我们需要修改“recommended.yaml”的配置,是为了两个目的:
- 修改服务配置,使用NodePort网络将主机端口映射给POD,使我们后续在宿主机可以直接访问Dashboard页面。
- 修改镜像的拉取策略,使之当本地有镜像时使用本地镜像,而不是每次尝试重新拉取。
首先,我们为Dashboard服务配置NodePort。我们修改Service配置(在配置文件中“kind: Service”的片段中),通过NodePort将主机端口映射到POD,使其可被外网访问。这么做主要是因为我们的POD部署在虚拟机中,以此来使我们可以在宿主机中通过浏览器访问Dashboard页面。
我们需要在“metadata.spec”下增加“type”属性,并将其的值设置为“NodePort”,同时在“ports”定义的端口中增加“nodePort”属性,指定使用的主机端口,主机端口需要在30000~32767之间,这里我们使用30443端口,如下所示:
在recommended.yaml配置文件中增加NodePort配置
然后,我们修改镜像的拉取策略,使其优先使用本地镜像。
镜像拉取策略的配置为“imagePullPolicy”,其有三个候选值,分别为:
- Always,表示总是拉取远端镜像。
- IfNotPresent,当本地没有镜像时才拉取远端镜像。
- Never,总是使用本地镜像。
我们需要在配置中找到“imagePullPolicy”,并将值改为“IfNotPresent”:
修改recommended.yaml文件的imagePullPolicy配置
创建Dashboard
执行以下命令,创建Dashboard:
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
然后我们来查看一下Dashboard相关POD的运行情况。
kubectl get pods -n kubernetes-dashboard
其中“-n”用以指定所查询的POD所属的命名空间,等同于“--namespace”。
其输出结果为:
NAME READY STATUS RESTARTS AGE
dashboard-metrics-scraper-799d786dbf-9b6cw 1/1 Running 0 19s
kubernetes-dashboard-674846bfb9-p5sz9 1/1 Running 0 19s
可见两个POD都处于运行态,现在我们可以尝试在宿主机通过虚拟机的IP和之前配置的NodePort端口来访问Dashboard的页面。注意需要通过https访问:
尝试访问Kubernetes-Dashboard页面
访问Dashboard需要登录。那么接下来,我们来创建一个用户,来访问这个页面。
创建admin用户
首先,我们创建“admin-user.yaml”文件:
vi admin-user.yaml
将以下内容输入到文件中:
apiVersion: v1
kind: ServiceAccount
metadata:
name: admin-user
namespace: kube-system
然后我们执行以下命令来创建用户:
kubectl apply -f admin-user.yaml
输出结果如下:
serviceaccount/admin-user created
表示我们已成功创建了用户,然后我们为用户绑定角色。
首先创建“
admin-user-role-binding.yaml”:
vi admin-user-role-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: kube-system
然后执行以下命令为用户绑定角色:
kubectl apply -f admin-user-role-binding.yaml
输出结果如下:
clusterrolebinding.rbac.authorization.k8s.io/admin-user created
现在,我们执行以下命令来查看用户的Token:
kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}')
其输出结果为:
Name: admin-user-token-7nl67
Namespace: kube-system
Labels:
Annotations: kubernetes.io/service-account.name: admin-user
kubernetes.io/service-account.uid: 84e2459e-03d9-4ee0-ace9-5cc51ea48392
Type: kubernetes.io/service-account-token
Data
====
ca.crt: 1099 bytes
namespace: 11 bytes
token: eyJhbGciOiJSUzI1NiIsImtpZCI6ImZoSTlyQmFOV0dYeFB5S1RSUmh1cDJTejVYY2pNSFBjbG1UemRYSjZRdDQifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi11c2VyLXRva2VuLTdubDY3Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImFkbWluLXVzZXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiI4NGUyNDU5ZS0wM2Q5LTRlZTAtYWNlOS01Y2M1MWVhNDgzOTIiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZS1zeXN0ZW06YWRtaW4tdXNlciJ9.sAPC2v71g3JfvlCeSRJMKK-67MP1f6PyGI7sX0nPs_jgk-somRYQNydV6Qdb07ZlW3Czy1nvbxT2nM9E1OhXXW7YqHW2P5R8SrDy6dR2N0IzZCatcY6ZLXwaFTo8aCVQ67U90mLBvsVlHOJQ1lX8J9ElMuG64YKzORn4mStBbgCfKRINoFQctLCGIqfko9GAHZiKC3DYfbeE1VBtNHvrhkfJP2VjCz44_4s3sSlFoOMVRdEE2OOq2fZ-h2KlQo3t4PtqtDBUnIBX9Yf7E2QUooatkXlhrtQ7z4lEwcLmyxyWVF1O6d1DEudDjZ6Zkgd4nSYj62MXcb4pnDb9DJIAqQ
可以看到,输出结果的最下方即为Token。现在我们将Token复制到Dashboard登录页面的输入框中:
将Token复制到Kubernetes-Dashboard登录页面
点击登录,即可登录到Dashboard。
Kubernetes-Dashboard主页