Dashboard

Kubernetes Dashboard 的部署非常簡單,只需要運行

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta8/aio/deploy/recommended.yaml

稍等一會,dashborad 就會創建好

$ kubectl -n kubernetes-dashboard get pod
NAME                                         READY   STATUS    RESTARTS   AGE
dashboard-metrics-scraper-76585494d8-xhhzx   1/1     Running   0          20m
kubernetes-dashboard-5996555fd8-snzh9        1/1     Running   0          20m
$ kubectl -n kubernetes-dashboard get service
NAME                        TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
dashboard-metrics-scraper   ClusterIP   10.0.58.210    <none>        8000/TCP   20m
kubernetes-dashboard        ClusterIP   10.0.182.172   <none>        443/TCP    20m

然後運行 kubectl proxy 之後就可以通過下面的鏈接來訪問了:

http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/

登錄認證

通過導入 API Server 的證書登錄

在 v1.7 之前的版本中,Dashboard 並不提供登陸的功能,並且以 http 的方式來運行,所以你可以直接通過 kubectl port-forard 或者 kubectl proxy 來訪問它。

當然也可以直接通過 API Server 的代理地址來訪問,即 kubectl cluster-info 輸出中 kubernetes-dashboard 的地址。由於 kubernetes API Server 是以 https 的方式運行,所以在訪問時需要把證書導入系統中:

# generate p12 cert
kubectl config view --flatten -o jsonpath='{.users[?(.name == "username")].user.client-key-data}' | base64 -d > client.key
kubectl config view --flatten -o jsonpath='{.users[?(.name == "username")].user.client-certificate-data}' | base64 -d > client.crt
openssl pkcs12 -export -in client.crt -inkey client.key -out client.p12

將 kube.p12 導入系統就可以用瀏覽器來直接訪問 https://<apiserver-url>/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/#/overview 了。

使用 kubeconfig 配置文件登錄

從 v1.7.0 版本開始,Dashboard 支持以 kubeconfig 配置文件的方式登錄。打開 Dashboard 頁面會自動跳轉到登錄的界面,選擇 Kubeconfig 方式,並選擇本地的 kubeconfig 配置文件即可。

使用受限 Token 登錄

從 v1.7.0 版本開始,Dashboard 支持以 Token 的方式登錄。注意從 Kubernetes 中取得的 Token 需要以 Base64 解碼後纔可以用來登錄。

下面是一個在開啓 RBAC 時創建一個只可以訪問 demo namespace 的 service account token 示例:

# 創建 demo namespace
kubectl create namespace demo

# 創建並限制只可以訪問 demo namespace
cat <<EOF | kubectl apply -f -
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  namespace: demo
  name: default-role
rules:
  - apiGroups:
    - '*'
    resources:
    - '*'
    verbs:
    - '*'
EOF
kubectl create rolebinding default-rolebinding --serviceaccount=demo:default --namespace=demo --role=default-role

# 獲取 token
secret=$(kubectl -n demo get sa default -o jsonpath='{.secrets[0].name}')
kubectl -n demo get secret $secret -o jsonpath='{.data.token}' | base64 -d

注意,由於使用該 token 僅可以訪問 demo namespace,故而需要登錄後將訪問 URL 中的 default 改成 demo。

使用 admin token 登錄

跟上一步類似,也可以創建一個 admin 用戶的 token 來登錄 dashboard:

kubectl create serviceaccount admin
kubectl create clusterrolebinding dash-admin --clusterrole=cluster-admin --serviceaccount=default:admin
secret=$(kubectl get sa admin -o jsonpath='{.secrets[0].name}')
kubectl get secret $secret -o go-template='{{ .data.token | base64decode }}'

其他用戶界面

除了 Kubernetes 社區提供的 Dashboard,還可以使用下列用戶界面來管理 Kubernetes 集群

  • Cabin:Android/iOS App,用於在移動端管理 Kubernetes

  • Kubernetic:Kubernetes 桌面客戶端

  • Kubernator:低級(low-level) Web 界面,用於直接管理 Kubernetes 的資源對象(即 YAML 配置)

kubernator

Last updated