在以前的博客記錄中,我已經搭建了kubernetes集羣,併發布了一個測試應用。本篇文章將講述建立kubernetes-dashboard.在kubernetes官方文檔,有記錄詳情請參考以下連接:https://kubernetes.io/docs/tasks/access-application-cluster/web-ui-dashboard/#deploying-the-dashboard-uinode
The Dashboard UI is not deployed by default. To deploy it, run the following command:git
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.github
# ------------------- Dashboard Secret ------------------- #web
apiVersion: v1
kind: Secret
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard-certs
namespace: kube-system
type: Opaqueexpress
---
# ------------------- Dashboard Service Account ------------------- #apache
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kube-systemapi
---
# ------------------- 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-systemapp
---
# ------------------- 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 <none> <none>
從輸出結果來看,kubernetes-dashboard運行在192.168.156.36節點,咱們只須要訪問節點的的port端口30001便可。具體地址爲https://192.168.156.36:30001
在該界面,咱們須要使用令牌驗證登陸,那麼如何獲取令牌呢,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: <none>
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界面展現以下
經過Web UI,咱們能夠執行各類操做,Web UI設計仍是比較直觀形象的。