Dashboard为Kubernetes官方的一个webui,集合了所有命令行可以操作的资源,可以根据浏览器的语言自动进行语言的识别。通过webui,我们可以更直观的看到Kubernetes集群的运行情况。不过在安装Dashboard的过程中有一些坑,因此在这里整理成文档以便在今后方便查阅。

安装

Dashboard的安装相对还是简单的,参考官方的github就可以。

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml

不过在这中间有一个坑,那就是Dashboard的webui默认是自动生成证书的,由于时间和名称的问题,会导致Chrome和IE等浏览器无法打开界面,这里需要我们自己制作证书处理。

开启NodePort

kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kube-system
spec:
  ports:
    - port: 443
      targetPort: 8443
      nodePort: 30003
  selector:
    k8s-app: kubernetes-dashboard
  type: NodePort

使用kubectl apply的命令将该service应用在Kubernetes集群中,可以看到该service已经生效。

[root@k8s ~]# kubectl get svc -n kube-system kubernetes-dashboard
NAME                   TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)         AGE
kubernetes-dashboard   NodePort   10.106.67.12   <none>        443:30003/TCP   40m

通过浏览器访问Dashboard,我们发现无法访问界面,会有类似x.x.x.x通常会使用加密技术来保护您的信息,Google Chrome此次尝试连接到x.x.x.x时,此网站发回了异常的错误凭据。...的字样,更换IE、360浏览器均无法访问,在机器上使用curl -k https://x.x.x.x:30003/命令测试,是可以通的。

出现以上现象是因为Dashboard默认为webui生成的证书时间是无效的,时间是过期的,因此需要解决下该问题,以通过浏览器来访问。

解决证书过期问题

[root@k8s ~]# openssl genrsa -out dashboard.key 2048
Generating RSA private key, 2048 bit long modulus
.............................................................+++
................+++
e is 65537 (0x10001)
[root@k8s ~]# openssl req -new -out dashboard.csr -key dashboard.key -subj '/CN=<机器IP>'
[root@k8s ~]# ls
dashboard.csr  dashboard.key  dashboard.yaml
[root@k8s ~]# openssl x509 -req -in dashboard.csr -signkey dashboard.key -out dashboard.crt
Signature ok
subject=/CN=<机器IP>
Getting Private key

查看官方的yaml我们可以看到里面已有secret,并且挂载到POD里,我们直接删除掉官方的yaml里的secret,通过上面的证书自己生成secret。

[root@k8s ~]# kubectl create secret generic kubernetes-dashboard-certs --from-file=dashboard.key --from-file=dashboard.crt -n kube-system
secret/kubernetes-dashboard-certs created

生成secret后apply即可访问webui。

免登录

Dashboard默认是需要认证的,而我们在开发的过程中,可能希望跳过登录,这个时候我们需要在Deployment中开启--enable-skip-login配置,以跳过认证。

赋予集群管理员权限

我们在开发过程中,希望Dashboard直接拥有集群管理员的权限,因此我们需要修改Dashboard的RBAC,删除官方的yaml中带的RBAC相关资源,使用如下的RBAC:

# ------------------- Dashboard ClusterRole ----------------------- #
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
  - kind: ServiceAccount
    name: kubernetes-dashboard
    namespace: kube-system

总结

在安装Dashboard的过程中,需要执行以下几步来完成安装:

  1. 复制最下方的yaml到机器的dashboard.yaml

  2. 执行以下命令完成安装。

[root@k8s ~]# openssl genrsa -out dashboard.key 2048
[root@k8s ~]# openssl req -new -out dashboard.csr -key dashboard.key -subj '/CN=<机器IP>'
[root@k8s ~]# kubectl create secret generic kubernetes-dashboard-certs --from-file=dashboard.key --from-file=dashboard.crt -n kube-system
secret/kubernetes-dashboard-certs created
[root@k8s ~]# kubectl apply -f dashboard.yaml

执行完上述命令后,可以通过浏览器https://x.x.x.x:30003/访问webui。

# 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.

# ------------------- Dashboard Secret ------------------- #

# apiVersion: v1
# kind: Secret
# metadata:
#  labels:
#    k8s-app: kubernetes-dashboard
#  name: kubernetes-dashboard-certs
#  namespace: kube-system
# type: Opaque

---
# ------------------- Dashboard Service Account ------------------- #

apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kube-system

---
# ------------------- Dashboard ClusterRole ----------------------- #
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: kubernetes-dashboard
  namespace: kubernetes-dashboard
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
  - kind: ServiceAccount
    name: kubernetes-dashboard
    namespace: kube-system
---
# ------------------- Dashboard Deployment ------------------- #

kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kube-system
spec:
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      k8s-app: kubernetes-dashboard
  template:
    metadata:
      labels:
        k8s-app: kubernetes-dashboard
    spec:
      containers:
      - name: kubernetes-dashboard
        image: k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1
        ports:
        - containerPort: 8443
          protocol: TCP
        args:
          - --auto-generate-certificates
          # 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
          # Skip auth
          - --enable-skip-login
        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
      volumes:
      - name: kubernetes-dashboard-certs
        secret:
          secretName: kubernetes-dashboard-certs
      - name: tmp-volume
        emptyDir: {}
      serviceAccountName: kubernetes-dashboard
      # Comment the following tolerations if Dashboard must not be deployed on master
      tolerations:
      - key: node-role.kubernetes.io/master
        effect: NoSchedule

---
# ------------------- Dashboard Service ------------------- #

kind: Service
apiVersion: v1
metadata:
  labels:
    k8s-app: kubernetes-dashboard
  name: kubernetes-dashboard
  namespace: kube-system
spec:
  ports:
    - port: 443
      targetPort: 8443
      nodePort: 30003
  selector:
    k8s-app: kubernetes-dashboard
  type: NodePort

参考

https://www.jianshu.com/p/c6d560d12d50