0%

(二)kubernetes(k8s)dashboard

top

简介

Dashboard 是基于网页的 Kubernetes 用户界面。可以很直观的管理k8s,降低新手学习难度。
github地址为:https://github.com/kubernetes/dashboard

安装

下载yaml文件

1
wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.3/aio/deploy/recommended.yaml

修改配置

找到Service部分

1
2
3
4
5
6
7
8
9
10
11
12
13
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
spec:
ports:
- port: 443
targetPort: 8443
selector:
k8s-app: kubernetes-dashboard

改成如下内容,添加NodePort,nodePort为30002

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kubernetes-dashboard
spec:
type: NodePort
ports:
- port: 443
targetPort: 8443
nodePort: 30002
selector:
k8s-app: kubernetes-dashboard

创建dashboard

1
kubectl apply -f recommended.yaml

查看dashboard状态

1
kubectl get svc,pod,deployment -o wide -n kubernetes-dashboard

结果如下:
kubernetes-dashboard

创建account

新建配置文件serviceAccount.yaml,内容如下

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

通过kubectl apply创建

1
kubectl apply -f serviceAccount.yaml

查询是否创建成功

1
kubectl -n kubernetes-dashboard  get secret | grep "admin-user"

admin-user

设置权限

新建配置文件clusterRoleBinding.yaml,内容如下

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

通过kubectl apply创建

1
kubectl apply -f clusterRoleBinding.yaml

查询secret

1
kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}')

复制token,并记录下来
secret

访问dashboard

IP地址为master节点IP,端口为30002,完整url为https://192.168.233.129:30002
登陆方式选择token,复制上面记录的token即可
dslogin

效果如下:
ds

小结

我们可以通过命令行管理k8s,也可以通过ui管理,对于新手来说可以用ui,熟练之后建议用命令行。

bottom