Dashboard 是基於網頁的 Kubernetes 使用者介面。 你可以使用 Dashboard 將容器應用部署到 Kubernetes 叢集中,也可以對容器應用排錯,還能管理叢集資源。 你可以使用 Dashboard 獲取執行在叢集中的應用的概覽資訊,也可以建立或者修改 Kubernetes 資源 (如 Deployment,Job,DaemonSet 等等)。 例如,你可以對 Deployment 實現彈性伸縮、發起捲動升級、重啟 Pod 或者使用嚮導建立新的應用。
預設安裝完成的dashboard的存取方式是ClusterIP,進而想存取dashboard需更改為nodeport或者loadbalancer或者設定為ingress的方式才能存取dashbaord。本文將以ingress-nginx釋出dashboard在外部存取。
我們可以看到預設是clusterip只能在叢集內部存取,是無法在叢集外部存取的。
# kubectl get pods,svc -n kubernetes-dashboard
NAME READY STATUS RESTARTS AGE
pod/dashboard-metrics-scraper-7c857855d9-chmm9 1/1 Running 0 74m
pod/kubernetes-dashboard-6b79449649-xgdph 1/1 Running 0 74m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/dashboard-metrics-scraper ClusterIP 10.96.129.181 <none> 8000/TCP 74m
service/kubernetes-dashboard ClusterIP 10.96.13.147 <none> 443/TCP 74m
# kubectl get pods -n ingress-nginx
NAME READY STATUS RESTARTS AGE
ingress-nginx-controller-bnmpt 1/1 Running 25 (3d2h ago) 18d
ingress-nginx-controller-cfblk 1/1 Running 26 (3d1h ago) 18d
自簽發證書。當然生產環境中理應當使用機構簽發的證書。
# openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout kube-dashboard.key -out kube-dashboard.crt -subj "/CN=dashboard.kube.com/O=k8s.dashboard.local"
Generating a 2048 bit RSA private key
......+++
.......+++
writing new private key to 'kube-dashboard.key'
-----
建立tls型別的Secret為ingress提供設定。
# kubectl create secret tls dashboard-tls --key kube-dashboard.key --cert kube-dashboard.crt -n kubernetes-dashboard
secret/dashboard-tls created
檢視secrets,可以看見型別為tls型別
# kubectl get secret -n kubernetes-dashboard
NAME TYPE DATA AGE
dashboard-tls kubernetes.io/tls 2 15h
default-token-7d7z8 kubernetes.io/service-account-token 3 17h
kubernetes-dashboard-certs Opaque 0 17h
kubernetes-dashboard-csrf Opaque 1 17h
kubernetes-dashboard-key-holder Opaque 2 17h
kubernetes-dashboard-token-c2z56 kubernetes.io/service-account-token 3 17h
Nginx Ingress Controller預設使用HTTP協定轉發請求到後端業務容器。當您的業務容器為HTTPS協定時,可以通過使用註解nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"來使得Nginx Ingress Controller使用HTTP協定轉發請求到後端業務容器。
# cat ingress-dashboard.yaml
apiVersion: networking.k8s.io/v1 型別為v1
kind: Ingress
metadata:
name: dashboard-ingress
namespace: kubernetes-dashboard
annotations:
nginx.ingress.kubernetes.io/backend-protocol: "HTTPS" #注意這裡:必須指定後端服務為HTTPS服務。
spec:
ingressClassName: "nginx" 控制器的型別為nginx
tls:
- hosts:
- k8s.dashboard.local 主機名
secretName: dashboard-tls 這裡參照建立的secrets
rules:
- host: k8s.dashboard.local
http:
paths:
- path: /
pathType: Prefix 起始與根都進行代理。
backend:
service:
name: kubernetes-dashboard service名稱
port: 後端埠
number: 443
載入組態檔
# kubectl apply -f ingress-dashboard.yaml
ingress.networking.k8s.io/dashboard-ingress created
# kubectl describe ingress -n kubernetes-dashboard
Name: dashboard-ingress
Namespace: kubernetes-dashboard
Address: xxxxx
Default backend: default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
TLS:
dashboard-tls terminates k8s.dashboard.local
Rules:
Host Path Backends
---- ---- --------
k8s.dashboard.local 可以看到後端的服務已被代理到。
/ kubernetes-dashboard:443 (192.168.3.56:8443)
Annotations: nginx.ingress.kubernetes.io/backend-protocol: HTTPS
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Sync 6s (x2 over 54s) nginx-ingress-controller Scheduled for sync
echo "xxxxx k8s.dashboard.local" >> /etc/hosts