首先下載部署的必要文件:
wget https://raw.githubusercontent.com/kubernetes/dashboard/master/aio/deploy/recommended.yaml -O kube-dash.yaml --no-check-certificate
默認(rèn)Dashboard的Service類(lèi)型是ClusterIP谭梗,我們集群外面不方便訪問(wèn)惶看,我們最好是將Service類(lèi)型修改為NodePoart或LoadBalancer(前提是你的集群支持LoadBalancer)有滑,以LoadBalancer為例娄琉。
修改文件kube-dash.yaml文件往果,將kubernetes-dashboard Service部分修改成如下:
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
spec:
type: LoadBalancer
ports:
- port: 443
targetPort: 8443
selector:
k8s-app: kubernetes-dashboard
創(chuàng)建kube-dash-admin-user.yaml文件:
vim kube-dash-admin-user.yaml
寫(xiě)入如下內(nèi)容:
apiVersion: v1
kind: ServiceAccount
metadata:
name: admin-user
namespace: kubernetes-dashboard
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: admin-user
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: admin-user
namespace: kubernetes-dashboard
執(zhí)行命令:
kubectl apply -f kube-dash.yaml # 創(chuàng)建dashboard服務(wù)
kubectl apply -f kube-dash-admin-user.yaml # 創(chuàng)建kubernetes集群的管理員角色和賬號(hào)
執(zhí)行完之后役听,我們查看Dashboard的Service:
kubectl get svc -n kubernetes-dashboard
輸出以下內(nèi)容颓鲜,可以看到表窘,kubernetes-dashboard的svc的EXTERNAL-IP為192.168.115.141,這就是LoadBalancer為我們自動(dòng)分配的一個(gè)IP甜滨。
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
dashboard-metrics-scraper ClusterIP 10.0.0.210 <none> 8000/TCP 52s
kubernetes-dashboard LoadBalancer 10.0.0.51 192.168.115.141 443:31385/TCP 52s
這時(shí)我們以https://192.168.115.141
訪問(wèn)部署的dashboard乐严,第一次訪問(wèn)可能需要點(diǎn)擊 Advanced
=> Proceed to 192.168.115.141 (unsafe)
進(jìn)入。
注意:必須以https方式訪問(wèn)衣摩,因?yàn)閐ashboard是默認(rèn)開(kāi)啟更為安全的https通信昂验。
需要使用Token登錄,使用如下命令獲取token:
kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}')
輸出內(nèi)容艾扮,獲取到token既琴。
Name: admin-user-token-l56hp
Namespace: kubernetes-dashboard
Labels: <none>
Annotations: kubernetes.io/service-account.name: admin-user
kubernetes.io/service-account.uid: 95db28c5-4951-4aae-bf59-b0c26c8b35c7
Type: kubernetes.io/service-account-token
Data
====
ca.crt: 1375 bytes
namespace: 20 bytes
token: eyJhbGciOiJSUzI1NiIsImtpZCI6IjYtQW51Z2xPMi1WTmpEZEtIX3BBYXd1YWpGLVU2Y0J0S1dmZE9lR3hoYU0ifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi11c2VyLXRva2VuLWw1NmhwIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImFkbWluLXVzZXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiI5NWRiMjhjNS00OTUxLTRhYWUtYmY1OS1iMGMyNmM4YjM1YzciLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZXJuZXRlcy1kYXNoYm9hcmQ6YWRtaW4tdXNlciJ9.Dqnk21CcBWU7SHgKRUu8uebL1djF1BJChNT5qTvk1uGLTF3AN9RMmacXlzO2xC5fP3zasmBFjcmS_JY76k7CS6DHdHKgxgB8vlEfIbh-i4YTA7cKK3_Ko5hAy7e6GhoPsfcYnV5QVec2mlvfMoozJT62UT62YkNrfUZXwFz02V4EfNgCgWVPKgiKzciMVOMNJ6-FKiiXyfhl4zprb8hSPzpc0F2Jd62Ykoltuir74UoByOazAnr7bA9ZTXSf1k8fjUaOUsBh37ap_eHg3Yh2gIcYMBxsp1tV0VVNKJDnVCN-lRBhfUciK93kvxU3I8xjWRv6JUHifCvHUiiWXjGZ8A
拿到token,拷貝到dashboard進(jìn)行登錄泡嘴。登入后甫恩,可以看到K8S的資源信息。
Kubernetes-Dashboard部署完成酌予。