Dashboard是Kubernetes集群可视化Web面板,用户可以通过Kubernetes dashboard来部署监控应用等。
本文当前所部署dashboard版本v2.0.3,请注意版本不同可能会导致部分操作不同。
当前基于CentOS7所配置的集群,如果系统不同可能会导致操作失败。
如果之前参考过网上一些复制粘贴的文章安装失败过,请先删除dashboard
kubectl delete -f recommended.yaml kubectl delete -f kubernetes-dashboard.yaml
|-----|--------------------------------------------------------------------------------| | 1 2 | kubectl delete -f recommended.yaml kubectl delete -f kubernetes-dashboard.yaml |
本文参考:https://github.com/kubernetes/dashboard
k8s集群创建参考:https://blog.whsir.com/post-5353.html
安装配置开始
1、安装
master执行dashboard安装命令
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.3/aio/deploy/recommended.yaml
|---|------------------------------------------------------------------------------------------------------------| | 1 | kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.3/aio/deploy/recommended.yaml |
2、查看pod
kubectl get po -n kubernetes-dashboard -o wide
|---|------------------------------------------------| | 1 | kubectl get po -n kubernetes-dashboard -o wide |
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
dashboard-metrics-scraper-6b4884c9d5-lzgxf 1/1 Running 0 68s 10.244.1.11 node01 <none> <none>
kubernetes-dashboard-7f99b75bf4-f9gtj 1/1 Running 0 68s 10.244.2.11 node02 <none> <none>
必须是running状态才可以,如果失败请检查日志,即
kubectl describe pod dashboard-metrics-scraper-6b4884c9d5-lzgxf -n kubernetes-dashboard kubectl describe pod kubernetes-dashboard-7f99b75bf4-f9gtj -n kubernetes-dashboard
|-----|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 | kubectl describe pod dashboard-metrics-scraper-6b4884c9d5-lzgxf -n kubernetes-dashboard kubectl describe pod kubernetes-dashboard-7f99b75bf4-f9gtj -n kubernetes-dashboard |
3、启动本地代理服务器(可选)
注意:以下两种访问方式仅限于本机localhost即127.0.0.1访问!
kubectl proxy
|---|---------------| | 1 | kubectl proxy |
Starting to serve on 127.0.0.1:8001
默认执行在前台,会开启8001访问端口
http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/
为了更简单,不输入这么长的URL,我们可以不使用kubectl proxy命令,而是使用以下命令
kubectl port-forward -n kubernetes-dashboard service/kubernetes-dashboard 8080:443
|---|------------------------------------------------------------------------------------| | 1 | kubectl port-forward -n kubernetes-dashboard service/kubernetes-dashboard 8080:443 |
然后访问https://localhost:8080
4、更改NodePort访问
为了更方便的访问dashboard,编辑kubernetes-dashboard服务,将type: ClusterIP更改为type: NodePort
kubectl -n kubernetes-dashboard edit service kubernetes-dashboard
|---|-------------------------------------------------------------------| | 1 | kubectl -n kubernetes-dashboard edit service kubernetes-dashboard |
Please edit the object below. Lines beginning with a '#' will be ignored, # and an empty file will abort the edit. If an error occurs while saving this file will be # reopened with the relevant failures. # apiVersion: v1 kind: Service metadata: annotations: kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"k8s-app":"kubernetes-dashboard"},"name":"kubernetes-dashboard","namespace":"kubernetes-dashboard"},"spec":{"ports":[{"port":443,"targetPort":8443}],"selector":{"k8s-app":"kubernetes-dashboard"}}} creationTimestamp: "2020-07-01T01:45:57Z" labels: k8s-app: kubernetes-dashboard managedFields: - apiVersion: v1 fieldsType: FieldsV1 fieldsV1: f:metadata: f:annotations: .: {} f:kubectl.kubernetes.io/last-applied-configuration: {} f:labels: .: {} f:k8s-app: {} f:spec: f:externalTrafficPolicy: {} f:ports: .: {} k:{"port":443,"protocol":"TCP"}: .: {} f:port: {} f:protocol: {} f:targetPort: {} f:selector: .: {} f:k8s-app: {} f:sessionAffinity: {} f:type: {} manager: kubectl operation: Update time: "2020-07-01T02:05:03Z" name: kubernetes-dashboard namespace: kubernetes-dashboard resourceVersion: "106077" selfLink: /api/v1/namespaces/kubernetes-dashboard/services/kubernetes-dashboard uid: 6e33f082-a294-4729-b6ab-bc87d690d209 spec: clusterIP: 10.104.160.49 externalTrafficPolicy: Cluster ports: - nodePort: 30641 port: 443 protocol: TCP targetPort: 8443 selector: k8s-app: kubernetes-dashboard sessionAffinity: None type: NodePort status: loadBalancer: {}
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | # Please edit the object below. Lines beginning with a '#' will be ignored, # and an empty file will abort the edit. If an error occurs while saving this file will be # reopened with the relevant failures. # apiVersion: v1 kind: Service metadata: annotations: kubectl.kubernetes.io/last-applied-configuration: | {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"k8s-app":"kubernetes-dashboard"},"name":"kubernetes-dashboard","namespace":"kubernetes-dashboard"},"spec":{"ports":[{"port":443,"targetPort":8443}],"selector":{"k8s-app":"kubernetes-dashboard"}}} creationTimestamp: "2020-07-01T01:45:57Z" labels: k8s-app: kubernetes-dashboard managedFields: - apiVersion: v1 fieldsType: FieldsV1 fieldsV1: f:metadata: f:annotations: .: {} f:kubectl.kubernetes.io/last-applied-configuration: {} f:labels: .: {} f:k8s-app: {} f:spec: f:externalTrafficPolicy: {} f:ports: .: {} k:{"port":443,"protocol":"TCP"}: .: {} f:port: {} f:protocol: {} f:targetPort: {} f:selector: .: {} f:k8s-app: {} f:sessionAffinity: {} f:type: {} manager: kubectl operation: Update time: "2020-07-01T02:05:03Z" name: kubernetes-dashboard namespace: kubernetes-dashboard resourceVersion: "106077" selfLink: /api/v1/namespaces/kubernetes-dashboard/services/kubernetes-dashboard uid: 6e33f082-a294-4729-b6ab-bc87d690d209 spec: clusterIP: 10.104.160.49 externalTrafficPolicy: Cluster ports: - nodePort: 30641 port: 443 protocol: TCP targetPort: 8443 selector: k8s-app: kubernetes-dashboard sessionAffinity: None type: NodePort status: loadBalancer: {} |
5、查看dashboard访问端口
kubectl -n kubernetes-dashboard get service kubernetes-dashboard
|---|------------------------------------------------------------------| | 1 | kubectl -n kubernetes-dashboard get service kubernetes-dashboard |
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes-dashboard NodePort 10.104.160.49 <none> 443:30641/TCP 19m
此时可以看到当前dashboard访问端口为30641,即:https://192.168.50.140:30641/
6、创建用户获取Token
根据官方文档进行操作:https://github.com/kubernetes/dashboard/blob/master/docs/user/access-control/creating-sample-user.md
如果之前创建过,请先执行以下两条命令,以免存在导致create失败
kubectl delete -f admin-user.yaml kubectl delete -f admin-user-role-binding.yaml
|-----|----------------------------------------------------------------------------------| | 1 2 | kubectl delete -f admin-user.yaml kubectl delete -f admin-user-role-binding.yaml |
①创建admin-user.yaml,文件内容如下
apiVersion: v1 kind: ServiceAccount metadata: name: admin-user namespace: kubernetes-dashboard
|-----------|------------------------------------------------------------------------------------------------| | 1 2 3 4 5 | apiVersion: v1 kind: ServiceAccount metadata: name: admin-user namespace: kubernetes-dashboard |
执行
kubectl create -f admin-user.yaml
|---|-----------------------------------| | 1 | kubectl create -f admin-user.yaml |
②创建admin-user-role-binding.yaml,文件内容如下
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
|----------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 12 | 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 |
注意:Kubernetes版本之间apiVersion可能不同,Kubernetes v1.8版本的apiVersion是rbac.authorization.k8s.io/v1beta1
执行
kubectl create -f admin-user-role-binding.yaml
|---|------------------------------------------------| | 1 | kubectl create -f admin-user-role-binding.yaml |
③获取Token
kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}')
|---|--------------------------------------------------------------------------------------------------------------------------------------| | 1 | kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}') |
Name: admin-user-token-t7k5l Namespace: kubernetes-dashboard Labels: <none> Annotations: kubernetes.io/service-account.name: admin-user kubernetes.io/service-account.uid: ef2273d7-ac60-4564-afb5-70aca4c99025 Type: kubernetes.io/service-account-token Data ==== ca.crt: 1025 bytes namespace: 20 bytes token: eyJhbGciOiJSUzI1NiIsImtpZCI6IkN1aVk3Z3NxU3NFY2NvZnlxTG5EUEpyUE9POUhDUTlEYzAzNFhNNkUtOHMifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi11c2VyLXRva2VuLXQ3azVsIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImFkbWluLXVzZXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiJlZjIyNzNkNy1hYzYwLTQ1NjQtYWZiNS03MGFjYTRjOTkwMjUiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZXJuZXRlcy1kYXNoYm9hcmQ6YWRtaW4tdXNlciJ9.bjvy5KonoVPf_vjJRi4QOOu84WJBAXDCs3FypLBeesY8v7EG8I3ONXokmdB3sEY3avEdI_54HbAM1YTOKAsCexG6WkS6h0D8TsaoBN5f7KVtpGPQ1iJwAqtewKWKP6r_E1hrmAaogpqHy5yykBF-2DOp48iGLUpg5YIs6hZM27lpLWOMRLSKIAnYbuL4bRksfQFTEEm09kcEKNrSUEEtKmuItdnbmTNdgiHGeB_iYmNeDlyaTADyr5aRTOOjoixyh0EBKTukCBDcLlE9iNbVYlDZCi5C_2VBOj6bVJWmIePBH-mVv5wzbOkTOJtLEJ7PBu2KX2XxCaRIJ_K7l8pgaA
|-------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 1 2 3 4 5 6 7 8 9 10 11 12 13 | Name: admin-user-token-t7k5l Namespace: kubernetes-dashboard Labels: <none> Annotations: kubernetes.io/service-account.name: admin-user kubernetes.io/service-account.uid: ef2273d7-ac60-4564-afb5-70aca4c99025 Type: kubernetes.io/service-account-token Data ==== ca.crt: 1025 bytes namespace: 20 bytes token: eyJhbGciOiJSUzI1NiIsImtpZCI6IkN1aVk3Z3NxU3NFY2NvZnlxTG5EUEpyUE9POUhDUTlEYzAzNFhNNkUtOHMifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi11c2VyLXRva2VuLXQ3azVsIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImFkbWluLXVzZXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiJlZjIyNzNkNy1hYzYwLTQ1NjQtYWZiNS03MGFjYTRjOTkwMjUiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZXJuZXRlcy1kYXNoYm9hcmQ6YWRtaW4tdXNlciJ9.bjvy5KonoVPf_vjJRi4QOOu84WJBAXDCs3FypLBeesY8v7EG8I3ONXokmdB3sEY3avEdI_54HbAM1YTOKAsCexG6WkS6h0D8TsaoBN5f7KVtpGPQ1iJwAqtewKWKP6r_E1hrmAaogpqHy5yykBF-2DOp48iGLUpg5YIs6hZM27lpLWOMRLSKIAnYbuL4bRksfQFTEEm09kcEKNrSUEEtKmuItdnbmTNdgiHGeB_iYmNeDlyaTADyr5aRTOOjoixyh0EBKTukCBDcLlE9iNbVYlDZCi5C_2VBOj6bVJWmIePBH-mVv5wzbOkTOJtLEJ7PBu2KX2XxCaRIJ_K7l8pgaA |
复制其Token登录dashboard