1.下载dashboard的资源清单
[root@master231 dashboard]# kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.5.1/aio/deploy/recommended.yaml # 线上同学。
2.导入镜像
wget http://192.168.11.253/Kubernetes/day09-/images/oldboyedu-dashboard_v2.5.1.tar.gz
wget http://192.168.11.253/Kubernetes/day09-/images/oldboyedu-metrics-scraper_v1.0.7.tar.gz
docker load -i oldboyedu-dashboard_v2.5.1.tar.gz
docker load -i oldboyedu-metrics-scraper_v1.0.7.tar.gz
docker push harbor.oldboyedu.com/add-ons/metrics-scraper:v1.0.7
docker push harbor.oldboyedu.com/add-ons/dashboard:v2.5.1
3.修改资源清单,让svc的类型为NodePort。
[root@master231 dashboard]# wget http://192.168.11.253/Kubernetes/day09-/images/deploy-dashboard.yaml # 线下同学
4.浏览器访问dashboard页面,第一次访问时,需要输入以下代码
thisisunsafe
5.权限不足解决方案-自定义登录用户
5.1 编写K8S的yaml资源清单文件
cat > oldboyedu-dashboard-rbac.yaml <<'EOF'
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
k8s-app: kubernetes-dashboard
# 创建一个名为"oldboyedu"的账户
name: oldboyedu
namespace: kubernetes-dashboard
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard-oldboyedu
namespace: kubernetes-dashboard
roleRef:
apiGroup: rbac.authorization.k8s.io
# 既然绑定的是集群角色,那么类型也应该为"ClusterRole",而不是"Role"哟~
kind: ClusterRole
# 关于集群角色可以使用"kubectl get clusterrole | grep admin"进行过滤哟~
name: cluster-admin
subjects:
- kind: ServiceAccount
# 此处要注意哈,绑定的要和我们上面的服务账户一致哟~
name: oldboyedu
namespace: kubernetes-dashboard
EOF
5.2创建资源清单
kubectl apply -f oldboyedu-dashboard-rbac.yaml
5.3 查看sa资源的Tokens名称
[root@master231 dashboard]# kubectl describe serviceaccounts -n kubernetes-dashboard oldboyedu | grep Tokens
Tokens: oldboyedu-token-crgl6
[root@master231 dashboard]#
5.4 根据上一步的token名称的查看token值
[root@master231 dashboard]# kubectl -n kubernetes-dashboard describe secrets oldboyedu-token-crgl6 | awk '/^token/{print $2}'
5.5 登录dashboard的WebUI,使用上一步的Token值登录即可(注意,复制时不要有换行哟)
如上图所示。
温馨提示:
如下图所示,由于咱们创建的ServiceAccount绑定的角色为"cluster-admin"这个角色,因此oldboyedu用户的token是可以访问集群的所有资源的哟~
- 使用kubeconfig登录
(1)编写生成kubeconf的配置文件的脚本
cat > oldboyedu-generate-context-conf.sh <<'EOF'
#!/bin/bash
# auther: Jason Yin
# 获取secret的名称
SECRET_NAME=`kubectl get secrets -n kubernetes-dashboard | grep oldboyedu | awk {'print $1'}`
# 指定API SERVER的地址
API_SERVER=master231:6443
# 指定kubeconfig配置文件的路径名称
KUBECONFIG_NAME=./oldboyedu-k8s-dashboard-admin.conf
# 获取oldboyedu用户的tocken
OLDBOYEDU_TOCKEN=`kubectl get secrets -n kubernetes-dashboard $SECRET_NAME -o jsonpath={.data.token} | base64 -d`
# 在kubeconfig配置文件中设置群集项
kubectl config set-cluster oldboyedu-k8s-dashboard-cluster --server=$API_SERVER --kubeconfig=$KUBECONFIG_NAME
# 在kubeconfig中设置用户项
kubectl config set-credentials oldboyedu-k8s-dashboard-user --token=$OLDBOYEDU_TOCKEN --kubeconfig=$KUBECONFIG_NAME
# 配置上下文,即绑定用户和集群的上下文关系,可以将多个集群和用户进行绑定哟~
kubectl config set-context oldboyedu-admin --cluster=oldboyedu-k8s-dashboard-cluster --user=oldboyedu-k8s-dashboard-user --kubeconfig=$KUBECONFIG_NAME
# 配置当前使用的上下文
kubectl config use-context oldboyedu-admin --kubeconfig=$KUBECONFIG_NAME
EOF
(2)运行上述脚本并下载上一步生成的配置文件到桌面,如上图所示,选择并选择该文件进行登录
sz oldboyedu-k8s-dashboard-admin.conf
(3)进入到dashboard的WebUI
如下图所示,我们可以访问任意的Pod,当然也可以直接进入到有终端的容器哟