在之前的博客记录中,我已经搭建了kubernetes集群,并发布了一个测试应用。本篇文章将讲述创建kubernetes-dashboard.在kubernetes官方文档,有记录详情请参考如下链接:https://kubernetes.io/docs/tasks/access-application-cluster/web-ui-dashboard/#deploying-the-dashboard-ui

Deploying the Dashboard UI

The Dashboard UI is not deployed by default. To deploy it, run the following command:

kubectl create -f https://raw.githubusercontent.com/kubernetes/dashboard/master/aio/deploy/recommended/kubernetes-dashboard.yaml
由于网络限制,我们在创建的时候可能会失败,参考各位前辈的博客,我发现了这个比较实用的文件,具体内容如下
[root@ES01 k8s]# cat kubenets-dashboard.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.

# ------------------- 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 Role & Role Binding ------------------- #

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
   name: kubernetes-dashboard-minimal
   namespace: kube-system
rules:
   # Allow Dashboard to create 'kubernetes-dashboard-key-holder' secret.
- apiGroups: [""]
   resources: ["secrets"]
   verbs: ["create"]
   # Allow Dashboard to create 'kubernetes-dashboard-settings' config map.
- apiGroups: [""]
   resources: ["configmaps"]
   verbs: ["create"]
   # Allow Dashboard to get, update and delete Dashboard exclusive secrets.
- apiGroups: [""]
   resources: ["secrets"]
   resourceNames: ["kubernetes-dashboard-key-holder", "kubernetes-dashboard-certs"]
   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 from heapster.
- apiGroups: [""]
   resources: ["services"]
   resourceNames: ["heapster"]
   verbs: ["proxy"]
- apiGroups: [""]
   resources: ["services/proxy"]
   resourceNames: ["heapster", "http:heapster:", "https:heapster:"]
   verbs: ["get"]

---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
   name: kubernetes-dashboard-minimal
   namespace: kube-system
roleRef:
   apiGroup: rbac.authorization.k8s.io
   kind: Role
   name: kubernetes-dashboard-minimal
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: siriuszg/kubernetes-dashboard-amd64:v1.10.0 (此处为引用镜像的位置,默认修改此处即可)
         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://192.168.156.33:8080
         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:
   type: NodePort  (官方文档貌似是不存在此字段的,需要自行添加)
   ports:
     - port: 443
       targetPort: 8443
       nodePort: 30001  (指定node节点的端口,这个可以随意设置,只要服务不冲突就好)
   selector:
     k8s-app: kubernetes-dashboard

在创建了kubernetes-dashboard文件后,直接在kubernetes的控制节点,执行如下命令即可

kubectl  apply -f kubenets-dashboard.yaml

创建完这些,这都还不够,我们还需要创建认证用户文件,具体认证用户的配置,请参考如下

[root@ES01 k8s]# cat admin-token.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
   labels:
     k8s-app: kubernetes-dashboard
   name: admin
   namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
   name: admin
roleRef:
   apiGroup: rbac.authorization.k8s.io
   kind: ClusterRole
   name: cluster-admin
subjects:
- kind: ServiceAccount
   name: admin
   namespace: kube-system

创建了配置文件之后,应用配置文件即可

kubectl  create -f admin-token.yaml

至此,kubernetes-dashboard的准备工作就结束了。在命令行验证一下部署情况,在master控制节点执行如下操作

[root@ES01 k8s]#  kubectl get secrets -n kube-system
NAME                                                                                TYPE                                                                                DATA   AGE
admin-token-ffmrz                                                         kubernetes.io/service-account-token                        3          16d
default-token-r59lt                                                         kubernetes.io/service-account-token                        3          20d
kubernetes-dashboard-certs                                       Opaque                                                                             0          16d
kubernetes-dashboard-key-holder                            Opaque                                                                              2          19d
kubernetes-dashboard-token-dbqw5                       kubernetes.io/service-account-token                          3          16d

[root@ES01 k8s]#  kubectl get pods -n kube-system -o wide
NAME                                                                                                                     READY   STATUS    RESTARTS   AGE   IP                       NODE                 NOMINATED NODE   READINESS GATES
kubernetes-dashboard-856d9bf5bc-mpt6p                                                      1/1       Running           0          16d   10.254.91.2     192.168.156.36                        

从输出结果来看,kubernetes-dashboard运行在192.168.156.36节点,我们只需要访问节点的的port端口30001即可。具体地址为https://192.168.156.36:30001

创建kubernetes-dashboard_第1张图片

在该界面,我们需要使用令牌验证登录,那么如何获取令牌呢,so  easy ,操作如下

[root@ES01 k8s]#  kubectl describe secret/$(kubectl get secret -nkube-system |grep admin|awk '{print $1}') -nkube-system
Name:         admin-token-ffmrz
Namespace:    kube-system
Labels:      
Annotations:  kubernetes.io/service-account.name: admin
               kubernetes.io/service-account.uid: b541a186-3fdc-11e9-96ca-0050569a27a4

Type:  kubernetes.io/service-account-token

Data
====
ca.crt:     1359 bytes
namespace:  11 bytes
token:      eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi10b2tlbi1mZm1yeiIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJhZG1pbiIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6ImI1NDFhMTg2LTNmZGMtMTFlOS05NmNhLTAwNTA1NjlhMjdhNCIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDprdWJlLXN5c3RlbTphZG1pbiJ9.XNrqifzzvzaAI3YfNuHvZDxB1tmiulYbNGBq4P3OTPnHE9zZr5hcwAjA-oSbpHcJLJTfB4Lkb_lux_MLPi6ttF-lELXZmMOFtKy_ffkzd04Y5G9CcFS_Y5r3CtiWDf4rzwCXUkdu43WJxmIK2AFRS5f_Xi0Ow2D4hEa97fRMfVeAmWjhLY1HJPgbhhZpGVH_-6WEf0cJsaibs69-AlJS9o4ahoNv0K5GznrwQf0bIf9twjYv4W8k7F6wLQrsJq05kYYph4rL3_Xc_Gr8ZnM5wxneyWzukETLuvbE8Oa1-itDb-1zV3UJWJuO8cJHrGCntw4Pf9Z21Bml3OGpSxWC5Q

将上面输出的token值复制到输入令牌并点击登录即可。dashboard界面展示如下

创建kubernetes-dashboard_第2张图片

通过Web UI,我们可以执行各种操作,Web UI设计还是比较直观形象的。