1. 程式人生 > 實用技巧 >Kubernetes附加元件Dashboard部署實戰篇

Kubernetes附加元件Dashboard部署實戰篇

一.Kubernetes的附加元件Dashboard概述

  在Kubernetes有一個著名的附加元件叫面板(Dashboard),該元件實現了Kubernetes的WebUI,通過該元件我們可以實現對Kubernetes叢集的管理。

  Kubernetes支援多使用者管理,換句話說,就是你可以使用不同許可權的使用者通過Dashboard進行登入從而獲得不同的角色許可權對Kubernetes叢集進行管理,但是Dashborad元件本身並不做任何的認證功能,Dashboard僅僅將認證請求代理至Kubernetes。

  由於Dashboard元件是以Pod形式執行在Kubernetes叢集之上,因此他通過ServiceAccount的使用者賬號進行認證。

  由於Dashboard元件一般都是在叢集外部需要進行訪問,因此我們應該將Dashboard元件的網路設定為NodePort型別,或者使用Ingress進行代理,生產環境中推薦使用後者。

二.基於Kubernetes自建ca證書進行證書認證

1>.建立私鑰

[[email protected] /etc/kubernetes/pki]# openssl genrsa -out dashboard.key 2048

2>.建立證書請求

[[email protected] /etc/kubernetes/pki]# openssl req -new -key dashboard.key -out dashboard.csr -subj "/O=jason/CN=dashboard"

3>.使用kubernetes自建的ca證書進行認證

[[email protected] /etc/kubernetes/pki]# openssl x509 -req -in dashboard.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out dashboard.crt -days 3650

三.基於自建的證書和私鑰建立secret用於Dashborad

1>.建立用於Dashboard元件的Secret資源

[[email protected] /etc/kubernetes/pki]# kubectl create secret generic kubernetes-dashboard-certs -n kube-system --from-file=dashboard.crt --from-file=dashboard.key

2>.檢視Dashboard的secret檔案

[[email protected] ~]# kubectl get secret -n kube-system kubernetes-dashboard-certs -o yaml

四.部署Dashboard

1>.參考官方文件

  博主推薦閱讀:
    https://kubernetes.io/docs/tasks/access-application-cluster/web-ui-dashboard/#deploying-the-dashboard-ui

2>.檢視官方的資源清單

3>.如下圖所示,將Google官網的映象指向國內的阿里映象倉庫(需要登入阿里官網檢視)

4>.將修好後的yaml檔案上傳到伺服器(window如果使用另存為的網頁檔案的字尾名是".txt",需要我們上傳到伺服器後進行更名)並應用

[[email protected] ~]# cat recommended.yaml [[email protected] ~]# kubectl apply -f recommended.yaml [[email protected] ~]# kubectl get all -n kubernetes-dashboard

5>.修改service的型別

[[email protected] ~]# cat recommended.yaml        #修改名為kubernetes-dashboard的service資源將其TYPE型別改為NodePort型別,也可以使用命令列的方式修改,如下所示。
[[email protected] ~]# kubectl get all  -n kubernetes-dashboard
NAME                                             READY   STATUS    RESTARTS   AGE
pod/dashboard-metrics-scraper-76978898b7-hh7pp   1/1     Running   0          5m23s
pod/kubernetes-dashboard-5677747d9d-k8h4n        1/1     Running   0          5m23s

NAME                                TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
service/dashboard-metrics-scraper   ClusterIP   10.105.15.32    <none>        8000/TCP   5m23s
service/kubernetes-dashboard        ClusterIP   10.103.133.58   <none>        443/TCP    5m23s

NAME                                        READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/dashboard-metrics-scraper   1/1     1            1           5m23s
deployment.apps/kubernetes-dashboard        1/1     1            1           5m23s

NAME                                                   DESIRED   CURRENT   READY   AGE
replicaset.apps/dashboard-metrics-scraper-76978898b7   1         1         1       5m23s
replicaset.apps/kubernetes-dashboard-5677747d9d        1         1         1       5m23s
[[email protected] ~]# 
[[email protected] ~]# kubectl edit service/kubernetes-dashboard -n kubernetes-dashboard
service/kubernetes-dashboard edited
[[email protected] ~]# 
[[email protected] ~]# kubectl get all  -n kubernetes-dashboard
NAME                                             READY   STATUS    RESTARTS   AGE
pod/dashboard-metrics-scraper-76978898b7-hh7pp   1/1     Running   0          8m24s
pod/kubernetes-dashboard-5677747d9d-k8h4n        1/1     Running   0          8m24s

NAME                                TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)         AGE
service/dashboard-metrics-scraper   ClusterIP   10.105.15.32    <none>        8000/TCP        8m24s
service/kubernetes-dashboard        NodePort    10.103.133.58   <none>        443:32242/TCP   8m24s

NAME                                        READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/dashboard-metrics-scraper   1/1     1            1           8m24s
deployment.apps/kubernetes-dashboard        1/1     1            1           8m24s

NAME                                                   DESIRED   CURRENT   READY   AGE
replicaset.apps/dashboard-metrics-scraper-76978898b7   1         1         1       8m24s
replicaset.apps/kubernetes-dashboard-5677747d9d        1         1         1       8m24s
[[email protected] ~]# 

6>.使用Google瀏覽器訪問Dashboard的WebUI失敗,但是使用360的瀏覽器訪問正常

  如下圖所示,使用Google瀏覽器訪問Dashboard的WebUI時失敗:
    https://master200.yinzhengjie.org.cn:31797/#/login

  如下圖所示,使用瀏覽器瀏覽器訪問Dashboard的WebUI時成功:
    https://master200.yinzhengjie.org.cn:31797/#/login

7>.於是從網際網路找到較老的yaml版本並做簡要修改(還是中文版本的喲~,接下來的試驗我就是用這個版本試驗了,畢竟母語看起來舒服點~)

[[email protected] ~]# cat kubernetes-dashboard.yaml
[[email protected] ~]# ll
total 16
-rw-r--r-- 1 root root 4689 Feb 19 08:29 kubernetes-dashboard.yaml
-rw-r--r-- 1 root root 7772 Feb 19 08:11 recommended.yaml
[[email protected] ~]# 
[[email protected] ~]# 
[[email protected] ~]# kubectl apply -f kubernetes-dashboard.yaml 
secret/kubernetes-dashboard-certs created
serviceaccount/kubernetes-dashboard created
role.rbac.authorization.k8s.io/kubernetes-dashboard-minimal created
rolebinding.rbac.authorization.k8s.io/kubernetes-dashboard-minimal created
deployment.apps/kubernetes-dashboard created
service/kubernetes-dashboard created
[[email protected] ~]# 
[[email protected] ~]# kubectl get service -n kube-system | grep dashboard
kubernetes-dashboard   NodePort    10.110.69.244   <none>        443:31358/TCP            14s
[[email protected] ~]# 
[[email protected] ~]# 

五.登入dashboard

1>.建立服務賬戶(serviceaccount)資源

[[email protected] ~]# kubectl create serviceaccount ui-admin -n kube-system
serviceaccount/ui-admin created
[[email protected] ~]# 
[[email protected] ~]# kubectl get sa  -n kube-system ui-admin
NAME       SECRETS   AGE
ui-admin   1         63s
[[email protected] ~]# 
[[email protected] ~]# 

2>.建立叢集角色繫結

[[email protected] ~]# kubectl get sa  -n kube-system ui-admin
NAME       SECRETS   AGE
ui-admin   1         135m
[[email protected] ~]# 
[[email protected] ~]# kubectl get clusterrole  -n kube-system cluster-admin
NAME            AGE
cluster-admin   14d
[[email protected] ~]# 
[[email protected] ~]# kubectl create clusterrolebinding cluster-ui-admin --clusterrole=cluster-admin --serviceaccount=kube-system:ui-admin
clusterrolebinding.rbac.authorization.k8s.io/cluster-ui-admin created
[[email protected] ~]# 
[[email protected] ~]# kubectl get clusterrolebinding cluster-ui-admin
NAME               AGE
cluster-ui-admin   26s
[[email protected] ~]# 

3>.獲取ui-admin的token

[[email protected] ~]# kubectl get sa ui-admin -n kube-system -o yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  creationTimestamp: "2020-02-19T00:47:06Z"
  name: ui-admin
  namespace: kube-system
  resourceVersion: "175736"
  selfLink: /api/v1/namespaces/kube-system/serviceaccounts/ui-admin
  uid: 1b64fa2d-ade7-4a8e-91c8-f73e224b058c
secrets:
- name: ui-admin-token-rgh7t
[[email protected] ~]# 
[[email protected] ~]# kubectl describe secret ui-admin-token-rgh7t -n kube-system
Name:         ui-admin-token-rgh7t
Namespace:    kube-system
Labels:       <none>
Annotations:  kubernetes.io/service-account.name: ui-admin
              kubernetes.io/service-account.uid: 1b64fa2d-ade7-4a8e-91c8-f73e224b058c

Type:  kubernetes.io/service-account-token

Data
====
ca.crt:     1025 bytes
namespace:  11 bytes
token:      eyJhbGciOiJSUzI1NiIsImtpZCI6ImViN0hUNExFU2dOZ2NhNmZSS0dGMFNIM3JnR0ZQTXN5azlRV2hkS3BPS00ifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV
0Lm5hbWUiOiJ1aS1hZG1pbi10b2tlbi1yZ2g3dCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJ1aS1hZG1pbiIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6IjFiNjRmYTJkLWFkZTctNGE4ZS05MWM4LWY3M2UyMjRiMDU4YyIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDprdWJlLXN5c3RlbTp1aS1hZG1pbiJ9.Ap5rByabSmJezjHgh78tpxsqxRvE-uTkOUn_UaW-G9LDX91Bb5WjAv5yQ4tbnC9Y4VjKUI86AO9AI3RMFUNI80A62LuhX8JdxgEVmAEttMsrFc3H2LHCRfjiPYfkobRftk82OX6Ks4cnxRN9bOFTLIOqVIump4Vj9y_5MrbelkAcRdMSIhQMkHVhwuiAzR_r81FLunf6udSsBf_Zwl4feu5QSB2IkCkNtVrEYDbsGe1a1X8EUA89IEDy5bgjrZxlDgdiWvk-3mKz_5Q8r4ZZuXE75c6xKM6gIbAmVKJat3jwTN8pYJIkJqohYtOQyndcEBihyvI2JwytU8I-yncaJA[[email protected] ~]# 
[[email protected] ~]# 

4>.基於ui-admin繫結的secrets的token進行認證,如下圖所示

5>.建立kubeconfig的配置檔案並在dashboard的WebUI中選擇以kubeconfig的方式登入

[[email protected] ~]# kubectl get sa ui-admin -n kube-system -o yaml
apiVersion: v1
kind: ServiceAccount
metadata:
  creationTimestamp: "2020-02-19T00:47:06Z"
  name: ui-admin
  namespace: kube-system
  resourceVersion: "175736"
  selfLink: /api/v1/namespaces/kube-system/serviceaccounts/ui-admin
  uid: 1b64fa2d-ade7-4a8e-91c8-f73e224b058c
secrets:
- name: ui-admin-token-rgh7t
[[email protected] ~]# 
[[email protected] ~]# kubectl describe secret  -n kube-system ui-admin-token-rgh7t
Name:         ui-admin-token-rgh7t
Namespace:    kube-system
Labels:       <none>
Annotations:  kubernetes.io/service-account.name: ui-admin
              kubernetes.io/service-account.uid: 1b64fa2d-ade7-4a8e-91c8-f73e224b058c

Type:  kubernetes.io/service-account-token

Data
====
namespace:  11 bytes
token:      eyJhbGciOiJSUzI1NiIsImtpZCI6ImViN0hUNExFU2dOZ2NhNmZSS0dGMFNIM3JnR0ZQTXN5azlRV2hkS3BPS00ifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV
0Lm5hbWUiOiJ1aS1hZG1pbi10b2tlbi1yZ2g3dCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJ1aS1hZG1pbiIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6IjFiNjRmYTJkLWFkZTctNGE4ZS05MWM4LWY3M2UyMjRiMDU4YyIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDprdWJlLXN5c3RlbTp1aS1hZG1pbiJ9.Ap5rByabSmJezjHgh78tpxsqxRvE-uTkOUn_UaW-G9LDX91Bb5WjAv5yQ4tbnC9Y4VjKUI86AO9AI3RMFUNI80A62LuhX8JdxgEVmAEttMsrFc3H2LHCRfjiPYfkobRftk82OX6Ks4cnxRN9bOFTLIOqVIump4Vj9y_5MrbelkAcRdMSIhQMkHVhwuiAzR_r81FLunf6udSsBf_Zwl4feu5QSB2IkCkNtVrEYDbsGe1a1X8EUA89IEDy5bgjrZxlDgdiWvk-3mKz_5Q8r4ZZuXE75c6xKM6gIbAmVKJat3jwTN8pYJIkJqohYtOQyndcEBihyvI2JwytU8I-yncaJAca.crt:     1025 bytes
[[email protected] ~]# 
[[email protected] ~]# kubectl config set-cluster yinzhengjie-k8s --server="https://172.200.1.200:6443" --certificate-authority=/etc/kubernetes/pki/ca.crt --embed-certs=true --kubeconfig=/tmp/ui-admin.kubeconfig
Cluster "yinzhengjie-k8s" set.
[[email protected] ~]# 
[[email protected] ~]# kubectl config set-credentials ui-admin --token=eyJhbGciOiJSUzI1NiIsImtpZCI6ImViN0hUNExFU2dOZ2NhNmZSS0dGMFNIM3JnR0ZQTXN5azlRV2hkS3BPS00ifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJ1aS1hZG1pbi10b2tlbi1yZ2g3dCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJ1aS1hZG1pbiIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6IjFiNjRmYTJkLWFkZTctNGE4ZS05MWM4LWY3M2UyMjRiMDU4YyIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDprdWJlLXN5c3RlbTp1aS1hZG1pbiJ9.Ap5rByabSmJezjHgh78tpxsqxRvE-uTkOUn_UaW-G9LDX91Bb5WjAv5yQ4tbnC9Y4VjKUI86AO9AI3RMFUNI80A62LuhX8JdxgEVmAEttMsrFc3H2LHCRfjiPYfkobRftk82OX6Ks4cnxRN9bOFTLIOqVIump4Vj9y_5MrbelkAcRdMSIhQMkHVhwuiAzR_r81FLunf6udSsBf_Zwl4feu5QSB2IkCkNtVrEYDbsGe1a1X8EUA89IEDy5bgjrZxlDgdiWvk-3mKz_5Q8r4ZZuXE75c6xKM6gIbAmVKJat3jwTN8pYJIkJqohYtOQyndcEBihyvI2JwytU8I-yncaJA --kubeconfig=/tmp/ui-admin.kubeconfig
User "ui-admin" set.
[[email protected] ~]# 
[[email protected] ~]# kubectl config set-context ui-admin@yinzhengjie-k8s --cluster=yinzhengjie-k8s --user=ui-admin --kubeconfig=/tmp/ui-admin.kubeconfig
Context "ui-admin@yinzhengjie-k8s" created.
[[email protected] ~]# 
[[email protected] ~]# kubectl config use-context ui-admin@yinzhengjie-k8s --kubeconfig=/tmp/ui-admin.kubeconfig
Switched to context "ui-admin@yinzhengjie-k8s".
[[email protected] ~]# 
[[email protected] ~]# kubectl config view --kubeconfig=/tmp/ui-admin.kubeconfig
apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://172.200.1.200:6443
  name: yinzhengjie-k8s
contexts:
- context:
    cluster: yinzhengjie-k8s
    user: ui-admin
  name: ui-admin@yinzhengjie-k8s
current-context: ui-admin@yinzhengjie-k8s
kind: Config
preferences: {}
users:
- name: ui-admin
  user:
    token: eyJhbGciOiJSUzI1NiIsImtpZCI6ImViN0hUNExFU2dOZ2NhNmZSS0dGMFNIM3JnR0ZQTXN5azlRV2hkS3BPS00ifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0
Lm5hbWUiOiJ1aS1hZG1pbi10b2tlbi1yZ2g3dCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJ1aS1hZG1pbiIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6IjFiNjRmYTJkLWFkZTctNGE4ZS05MWM4LWY3M2UyMjRiMDU4YyIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDprdWJlLXN5c3RlbTp1aS1hZG1pbiJ9.Ap5rByabSmJezjHgh78tpxsqxRvE-uTkOUn_UaW-G9LDX91Bb5WjAv5yQ4tbnC9Y4VjKUI86AO9AI3RMFUNI80A62LuhX8JdxgEVmAEttMsrFc3H2LHCRfjiPYfkobRftk82OX6Ks4cnxRN9bOFTLIOqVIump4Vj9y_5MrbelkAcRdMSIhQMkHVhwuiAzR_r81FLunf6udSsBf_Zwl4feu5QSB2IkCkNtVrEYDbsGe1a1X8EUA89IEDy5bgjrZxlDgdiWvk-3mKz_5Q8r4ZZuXE75c6xKM6gIbAmVKJat3jwTN8pYJIkJqohYtOQyndcEBihyvI2JwytU8I-yncaJA[[email protected] ~]# 
[[email protected] ~]#