本節內容:
- 配置dashboard
- 執行所有定義的文件
- 檢查執行結果
- 訪問dashboard
這是接着上一篇《二進制方式部署Kubernetes 1.6.0集群(開啟TLS)》寫的。
Kubernetes Dashboard is a general purpose, web-based UI for Kubernetes clusters. It allows users to manage applications running in the cluster and troubleshoot them, as well as manage the cluster itself.
一、配置dashboard
官方文件目錄:https://github.com/kubernetes/kubernetes/tree/master/cluster/addons/dashboard
我使用的文件:
從 https://github.com/rootsongjc/follow-me-install-kubernetes-cluster/tree/master/manifests/dashboard 下載3個文件下來,並上傳到/opt/kube-dashboard/目錄下。
[root@node1 opt]# mkdir kube-dashboard [root@node1 opt]# cd kube-dashboard/ [root@node1 kube-dashboard]# ls dashboard-controller.yaml dashboard-rbac.yaml dashboard-service.yaml
修改dashboard-controller.yaml文件,將里面的image改為:
index.tenxcloud.com/jimmy/kubernetes-dashboard-amd64:v1.6.0
由於 kube-apiserver 啟用了 RBAC 授權,而官方源碼目錄的 dashboard-controller.yaml 沒有定義授權的 ServiceAccount,所以后續訪問 kube-apiserver 的 API 時會被拒絕,web中提示:
Forbidden (403) User "system:serviceaccount:kube-system:default" cannot list jobs.batch in the namespace "default". (get jobs.batch)
因此,增加了一個dashboard-rbac.yaml文件,定義一個名為 dashboard 的 ServiceAccount,然后將它和 Cluster Role view 綁定。
二、執行所有定義的文件
# pwd /opt/kube-dashboard # ls dashboard-controller.yaml dashboard-rbac.yaml dashboard-service.yaml # kubectl create -f . deployment "kubernetes-dashboard" created serviceaccount "dashboard" created clusterrolebinding "dashboard" created service "kubernetes-dashboard" created
三、檢查執行結果
1. 查看分配的 NodePort
# kubectl get services kubernetes-dashboard -n kube-system NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes-dashboard 10.254.207.77 <nodes> 80:32281/TCP 41s
- NodePort 32281映射到 dashboard pod 80端口。
2. 檢查 controller
# kubectl get deployment kubernetes-dashboard -n kube-system NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE kubernetes-dashboard 1 1 1 1 13m # kubectl get pods -n kube-system | grep dashboard kubernetes-dashboard-2888692679-tv54g 1/1 Running 0 13m
四、訪問dashboard
有以下三種方式:
- kubernetes-dashboard 服務暴露了 NodePort,可以使用 http://NodeIP:nodePort 地址訪問 dashboard;
- 通過 kube-apiserver 訪問 dashboard(https 6443端口和http 8080端口方式);
- 通過 kubectl proxy 訪問 dashboard
1. 使用 http://NodeIP:nodePort 地址訪問 dashboard
# kubectl get services kubernetes-dashboard -n kube-system NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes-dashboard 10.254.207.77 <nodes> 80:32281/TCP 41s
然后檢查出這個pod是運行在集群中哪個服務器上的,我這里是檢查是運行在node1節點上的,所以瀏覽器輸入http://172.16.7.151:32281/

2. 通過 kubectl proxy 訪問 dashboard
(1)啟動代理
[root@node1 kube-dashboard]# kubectl proxy --address='172.16.7.151' --port=8086 --accept-hosts='^*$' Starting to serve on 172.16.7.151:8086
- 需要指定 --accept-hosts 選項,否則瀏覽器訪問 dashboard 頁面時提示 “Unauthorized”
(2)訪問
瀏覽器訪問 URL:http://172.16.7.151:8086/ui 自動跳轉到:http://172.16.7.151:8086/api/v1/proxy/namespaces/kube-system/services/kubernetes-dashboard/#/workload?namespace=default

3. 通過 kube-apiserver 訪問dashboard
(1)獲取集群服務地址列表
[root@node1 ~]# kubectl cluster-info Kubernetes master is running at https://172.16.7.151:6443 KubeDNS is running at https://172.16.7.151:6443/api/v1/proxy/namespaces/kube-system/services/kube-dns kubernetes-dashboard is running at https://172.16.7.151:6443/api/v1/proxy/namespaces/kube-system/services/kubernetes-dashboard To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
(2)導入證書
將生成的admin.pem證書轉換格式。
[root@node1 ~]# cd /etc/kubernetes/ssl/ [root@node1 ~]# openssl pkcs12 -export -in admin.pem -out admin.p12 -inkey admin-key.pem
將生成的admin.p12證書導入的你的電腦,導出的時候記住你設置的密碼,導入的時候還要用到。
如果你不想使用https的話,可以直接訪問insecure port 8080端口:http://172.16.7.151:8080/api/v1/proxy/namespaces/kube-system/services/kubernetes-dashboard

由於缺少 Heapster 插件,當前 dashboard 不能展示 Pod、Nodes 的 CPU、內存等 metric 圖形。
