k8s 探索7:使用 Dashboard 管理资源

k8s 探索7:使用 Dashboard 管理资源

🎈「k8s 探索」系列:

1 简介

Kubernetes Dashboard 是用于 Kubernetes 集群的通用、基于 Web 的 UI。它允许用户管理集群中运行的应用程序并对其进行故障排除,以及管理集群本身。

Dashboard UI

2 安装 Dashboard

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

修改 recommended.yaml 中的 kubernetes-dashboard Service,将 nodePort: 30000 暴露出来:

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: 30000
...

ClusterRole 中的 kubernetes-dashboard 修改为 cluster-admin,这样就可以得到整个集群的权限。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
...
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: kubernetes-dashboard
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: kubernetes-dashboard # 修改为 cluster-admin
subjects:
- kind: ServiceAccount
name: kubernetes-dashboard
namespace: kubernetes-dashboard
...

创建应用程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
kubectl apply -f recommended.yaml

# 查看所有 kubernetes-dashboard 相关服务的状态
watch -n 1 kubectl get all -n kubernetes-dashboard
# NAME READY STATUS RESTARTS AGE
# pod/dashboard-metrics-scraper-7487b56f4c-glh4j 1/1 Running 0 2d
# pod/kubernetes-dashboard-67484c44f6-lc98l 1/1 Running 5 (45h ago) 2d

# NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
# service/dashboard-metrics-scraper ClusterIP 10.100.23.33 <none> 8000/TCP 2d
# service/kubernetes-dashboard NodePort 10.111.155.210 <none> 443:30000/TCP 2d

# NAME READY UP-TO-DATE AVAILABLE AGE
# deployment.apps/dashboard-metrics-scraper 1/1 1 1 2d
# deployment.apps/kubernetes-dashboard 1/1 1 1 2d

# NAME DESIRED CURRENT READY AGE
# replicaset.apps/dashboard-metrics-scraper-7487b56f4c 1 1 1 2d
# replicaset.apps/kubernetes-dashboard-67484c44f6 1 1 1 2d

当所有服务的都 READY 的时候,就可以正常访问了。

3 访问 Dashboard

1
2
3
# 提取 dashboard 的 token
kubectl -n kubernetes-dashboard get secret $(kubectl -n kubernetes-dashboard get sa/kubernetes-dashboard -o jsonpath="{.secrets[0].name}") -o go-template="{{.data.token | base64decode}}"
# eyJhbGciOiJSUzI1NiIsImtpZCI6InFvbll...

访问 https://127.0.0.1:30000,输入 token 即可登录。

4 参考

🎈「k8s 探索」系列:

作者

Ailln

发布于

2021-10-05

更新于

2024-03-02

许可协议

评论