k8s仪表盘部署

仪表盘文件下载链接
https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml
https://drive.zznnwn.cloudns.biz/onedrive/k8s部署配置文件及cni/使用container作为容器运行时/recommended_v270.yaml

部署仪表盘

1
2
kubectl apply -f "https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml"
kubectl apply -f "https://drive.zznnwn.cloudns.biz/onedrive/k8s部署配置文件及cni/使用container作为容器运行时/recommended_v270.yaml"

命名空间中的名称创建服务帐户(admin-user.yml)

1
2
3
4
5
apiVersion: v1
kind: ServiceAccount
metadata:
name: admin-user
namespace: kubernetes-dashboard

部署admin-user.yml

1
2
kubectl apply -f admin-user.yml 
kubectl apply -f "https://drive.zznnwn.cloudns.biz/onedrive/k8s部署配置文件及cni/使用container作为容器运行时/admin-user.yml"

创建 ClusterRoleBinding(ClusterRoleBinding.yml)

在大多数情况下,在使用 或任何其他常用工具预配集群后,集群中已存在。我们可以使用它并只为我们的 . 如果它不存在,则需要先创建此角色并手动授予所需的权限。kops kubeadm ClusterRole cluster-admin ClusterRoleBinding ServiceAccount

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

部署ClusterRoleBinding.yml

1
2
kubectl apply -f ClusterRoleBinding.yml 
kubectl apply -f "https://drive.zznnwn.cloudns.biz/onedrive/k8s部署配置文件及cni/使用container作为容器运行时/ClusterRoleBinding.yml"

获取 ServiceAccount 的持有者令牌

现在我们需要找到可用于登录的令牌。执行以下命令:

1
kubectl -n kubernetes-dashboard create token admin-user

它应该打印如下内容:

1
eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJhZG1pbi11c2VyLXRva2VuLXY1N253Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6ImFkbWluLXVzZXIiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC51aWQiOiIwMzAzMjQzYy00MDQwLTRhNTgtOGE0Ny04NDllZTliYTc5YzEiLCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6a3ViZXJuZXRlcy1kYXNoYm9hcmQ6YWRtaW4tdXNlciJ9.Z2JrQlitASVwWbc-s6deLRFVk5DWD3P_vjUFXsqVSY10pbjFLG4njoZwh8p3tLxnX_VBsr7_6bwxhWSYChp9hwxznemD5x5HLtjb16kI9Z7yFWLtohzkTwuFbqmQaMoget_nYcQBUC5fDmBHRfFvNKePh_vSSb2h_aYXa8GV5AcfPQpY7r461itme1EXHQJqv-SN-zUnguDguCTjD80pFZ_CmnSE1z9QdMHPB8hoB4V68gtswR1VLa6mSYdgPwCHauuOobojALSaMc3RH7MmFUumAgguhqAkX3Omqd3rJbYOMRuMjhANqd08piDC3aIabINX6gP5-Tuuw2svnV6NYQ

查看 Kubernetes 文档,了解有关 ServiceAccount 的 API 令牌的更多信息。

获取 ServiceAccount 的长期持有者令牌(建议使用此种)

我们还可以创建一个带有 secret 的令牌,该令牌绑定了服务帐户,令牌将保存在 Secret 中文件(secret_token.yml)

1
2
3
4
5
6
7
8
apiVersion: v1
kind: Secret
metadata:
name: admin-user
namespace: kubernetes-dashboard
annotations:
kubernetes.io/service-account.name: "admin-user"
type: kubernetes.io/service-account-token

部署

1
2
kubectl apply -f secret_token.yml
kubectl apply -f "https://drive.zznnwn.cloudns.biz/onedrive/k8s部署配置文件及cni/使用container作为容器运行时/secret_token.yml"

创建 Secret 后,我们可以执行以下命令来获取保存在 Secret 中的 Token:

1
kubectl get secret admin-user -n kubernetes-dashboard -o jsonpath={".data.token"} | base64 -d

查看 Kubernetes 文档,了解有关 ServiceAccount 的长期 API 令牌的更多信息。

https://github.com/zznn-cloud/zznn-cloud-blog-images/raw/main/Qexo/24/3/image_451018f8aaf9cdda00b42a1da75d3b46.png

最后将dashboard端口暴露在外用于访问

1
2
kubectl  patch svc kubernetes-dashboard -n kubernetes-dashboard \
-p '{"spec":{"type":"NodePort","ports":[{"port":443,"targetPort":8443,"nodePort":30443}]}}'

https://github.com/zznn-cloud/zznn-cloud-blog-images/raw/main/Qexo/24/3/image_57bf21fb0b0a561b21cf24556ecc01a5.png

扩展一键部署

1
curl -sSL https://drive.zznnwn.cloudns.biz/onedrive/脚本/k8s一键部署脚本/k8s-dashboard.sh | bash

登陆仪表盘

访问:https://localhost:8443/#/login

清理和后续步骤

删除 admin 和 .ServiceAccount ClusterRoleBinding

1
2
kubectl -n kubernetes-dashboard delete serviceaccount admin-user
kubectl -n kubernetes-dashboard delete clusterrolebinding admin-user

要了解有关如何在 Kubernetes 中授予/拒绝权限的更多信息,请阅读官方身份验证授权文档。

本文参考

https://kubernetes.io/zh-cn/docs/tasks/access-application-cluster/web-ui-dashboard/
https://github.com/kubernetes/dashboard/blob/master/docs/user/access-control/creating-sample-user.md