k8s (十一)—资源监控(metrics-server部署、Dashboard部署)
1.metrics-server简介当发api请求时各个不同的server提供了不一样的api,调用的时候要指定不一样的访问类型,比较麻烦,为了方便提供统一的接口(kube-aggregator),统一接口会分析访问api具体的类型,帮我们负载到具体的api上2.metrics-server部署资源下载[root@server2 ~]# mkdir metrics建立目录[root@server2
·
1.metrics-server简介
当发api请求时各个不同的server提供了不一样的api,调用的时候要指定不一样的访问类型,比较麻烦,为了方便提供统一的接口(kube-aggregator),统一接口会分析访问api具体的类型,帮我们负载到具体的api上
2.metrics-server部署
[root@server2 ~]# mkdir metrics 建立目录
[root@server2 ~]# cd metrics/
[root@server2 metrics]# wget https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml 下载yaml配置文件
[root@server2 metrics]# ls
components.yaml 下载成功
[root@server2 metrics]# vim components.yaml 查看配置文件所需镜像
[root@server1 metrics]# docker search metrics-server 搜索所需镜像
[root@server1 ~]# docker pull willdockerhub/metrics-server:v0.6.1 拉取镜像
[root@server1 ~]# docker tag willdockerhub/metrics-server:v0.6.1 reg.westos.org/library/metrics-server:v0.6.1 更改标签
[root@server1 ~]# docker push reg.westos.org/library/metrics-server:v0.6.1 上传到镜像仓库
[root@server2 metrics]# vim components.yaml
[root@server2 metrics]# kubectl apply -f components.yaml 创建
serviceaccount/metrics-server created
clusterrole.rbac.authorization.k8s.io/system:aggregated-metrics-reader created
clusterrole.rbac.authorization.k8s.io/system:metrics-server created
rolebinding.rbac.authorization.k8s.io/metrics-server-auth-reader created
clusterrolebinding.rbac.authorization.k8s.io/metrics-server:system:auth-delegator created
clusterrolebinding.rbac.authorization.k8s.io/system:metrics-server created
service/metrics-server created
deployment.apps/metrics-server created
apiservice.apiregistration.k8s.io/v1beta1.metrics.k8s.io created
[root@server2 metrics]# kubectl get pod -n kube-system 查看pod
[root@server2 metrics]# kubectl -n kube-system logs metrics-server-698b864569-cf6np 查看日志
[root@server2 metrics]# vim /var/lib/kubelet/config.yaml 编辑k8s配置文件
[root@server2 metrics]# systemctl restart kubelet 重启服务
在server3、server4进行同样操作
[root@server2 metrics]# kubectl get csr 查看谁在集群里需要证书签名,获取证书
NAME AGE SIGNERNAME REQUESTOR REQUESTEDDURATION CONDITION
csr-fc6cc 97s kubernetes.io/kubelet-serving system:node:server3 <none> Pending
csr-p5l97 4m10s kubernetes.io/kubelet-serving system:node:server2 <none> Pending
csr-pzf4v 62s kubernetes.io/kubelet-serving system:node:server4 <none> Pending
[root@server2 metrics]# kubectl certificate approve csr-fc6cc 通过签名申请
certificatesigningrequest.certificates.k8s.io/csr-fc6cc approved
[root@server2 metrics]# kubectl certificate approve csr-p5l97 通过签名申请
certificatesigningrequest.certificates.k8s.io/csr-p5l97 approved
[root@server2 metrics]# kubectl certificate approve csr-pzf4v 通过签名申请
certificatesigningrequest.certificates.k8s.io/csr-pzf4v approved
[root@server2 metrics]# kubectl -n kube-system get pod 查看pod
访问流程:
[root@server2 metrics]# kubectl -n kube-system top pod 查看pod的cpu和内存资源使用情况
NAME CPU(cores) MEMORY(bytes)
coredns-7b56f6bc55-85s7v 1m 18Mi
coredns-7b56f6bc55-b6n5x 1m 26Mi
etcd-server2 16m 194Mi
kube-apiserver-server2 43m 306Mi
kube-controller-manager-server2 19m 89Mi
kube-flannel-ds-4hjs4 2m 18Mi
kube-flannel-ds-svd75 2m 18Mi
kube-flannel-ds-v6gs9 2m 22Mi
kube-proxy-m2sx7 1m 24Mi
kube-proxy-mszbj 1m 17Mi
kube-proxy-zqkrw 1m 15Mi
kube-scheduler-server2 3m 34Mi
metrics-server-698b864569-cf6np 3m 19Mi
[root@server2 metrics]# kubectl -n kube-system top node 查看node节点的cpu和内存资源使用情况
NAME CPU(cores) CPU% MEMORY(bytes) MEMORY%
server2 132m 6% 1230Mi 64%
server3 28m 1% 485Mi 25%
server4 27m 1% 559Mi 29%
3. Dashboard部署
[root@server2 ~]# mkdir dashboard
[root@server2 ~]# cd dashboard/
[root@server2 dashboard]# wget https://raw.githubusercontent.com/kubernetes/dashboard/v2.5.1/aio/deploy/recommended.yaml 下载yaml文件
[root@server2 dashboard]# ls
recommended.yaml
[root@server1 ~]# docker pull kubernetesui/dashboard:v2.5.1 k8s官方镜像直接拉取
[root@server1 ~]# docker pull kubernetesui/metrics-scraper:v1.0.7 k8s官方镜像直接拉取
在仓库中新建项目
[root@server1 ~]# docker tag kubernetesui/dashboard:v2.5.1 reg.westos.org/kubernetesui/dashboard:v2.5.1 修改标签
[root@server1 ~]# docker tag kubernetesui/metrics-scraper:v1.0.7 reg.westos.org/kubernetesui/metrics-scraper:v1.0.7 修改标签
[root@server1 ~]# docker push reg.westos.org/kubernetesui/dashboard:v2.5.1 上传镜像
[root@server1 ~]# docker push reg.westos.org/kubernetesui/metrics-scraper:v1.0.7 上传镜像
[root@server2 dashboard]# kubectl apply -f recommended.yaml 创建
[root@server2 dashboard]# kubectl get ns
[root@server2 dashboard]# kubectl -n kubernetes-dashboard get all
如何外部访问:
通过ingress结合LoadBalancer去访问
[root@server2 ~]# kubectl -n kubernetes-dashboard get svc 查看svc服务
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
dashboard-metrics-scraper ClusterIP 10.104.235.178 <none> 8000/TCP 11h
kubernetes-dashboard ClusterIP 10.98.140.202 <none> 443/TCP 11h 端口为443
通过ingress结合LoadBalancer去访问
[root@server2 ~]# kubectl -n ingress-nginx get svc 查看ingress服务,之前已经搭建好了,已经存在
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ingress-nginx-controller LoadBalancer 10.103.15.227 172.25.50.101 80:32347/TCP,443:32365/TCP 63m 分配的ip是172.25.50.101
ingress-nginx-controller-admission ClusterIP 10.98.183.150 <none> 443/TCP 63m
[root@foundation50 ~]# vim /etc/hosts 宿主机上添加解析
172.25.50.101 dashboard.westos.org
[root@server2 dashboard]# vim ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress 创建ingress
metadata:
name: ingress-dashboard
namespace: kubernetes-dashboard 指定namespace
spec:
# tls:
# - hosts:
# - dashboard.westos.org
# secretName: tls-secret
ingressClassName: nginx
rules:
- host: dashboard.westos.org
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: kubernetes-dashboard svc名字
port:
number: 443 端口443
[root@server2 dashboard]# kubectl apply -f ingress.yaml 创建ingress
ingress.networking.k8s.io/ingress-demo configured
[root@server2 dashboard]# kubectl -n kubernetes-dashboard get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
ingress-dashboard nginx dashboard.westos.org 172.25.50.3 80, 443 50s
浏览器访问 https://dashboard.westos.org/ 发现访问不了,用ingress访问不了,此实验失败
用第二种方法:
[root@server2 dashboard]# kubectl -n kubernetes-dashboard edit svc kubernetes-dashboard 编辑kubernetes-dashboard svc
[root@server2 dashboard]# kubectl -n kubernetes-dashboard get svc 查看svc,
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
dashboard-metrics-scraper ClusterIP 10.104.235.178 <none> 8000/TCP 14h
kubernetes-dashboard NodePort 10.98.140.202 <none> 443:32224/TCP 14h 访问模式变成NodePort,访问端口为32224
[root@server2 dashboard]# kubectl -n kubernetes-dashboard get pod -o wide 查看调度情况,发现调度到server3上
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
dashboard-metrics-scraper-799d786dbf-266m6 1/1 Running 0 14h 10.244.3.12 server4 <none> <none>
kubernetes-dashboard-fb8648fd9-zcwb6 1/1 Running 0 14h 10.244.1.43 server3 <none> <none>
访问https://172.25.50.3:32224
登陆dashboard需要认证,需要获取dashboard pod的token
如何查找token
[root@server2 dashboard]# kubectl -n kubernetes-dashboard get secrets
这个secrets就挂载了token信息
[root@server2 dashboard]# kubectl -n kubernetes-dashboard describe secrets kubernetes-dashboard-token-j67hr 查看详细信息
登陆后,有很多报错
授权:
[root@server2 dashboard]# kubectl -n kubernetes-dashboard get role 查看namespace(kubernetes-dashboard)有那些角色
NAME CREATED AT
kubernetes-dashboard 2022-04-19T16:10:40Z
[root@server2 dashboard]# kubectl -n kubernetes-dashboard get rolebindings.rbac.authorization.k8s.io 查看绑定了那些角色
NAME ROLE AGE
kubernetes-dashboard Role/kubernetes-dashboard 62m
[root@server2 dashboard]# kubectl -n kubernetes-dashboard describe role kubernetes-dashboard 查看角色权限有哪些
--------- ----------------- -------------- -----
secrets [] [kubernetes-dashboard-certs] [get update delete]
secrets [] [kubernetes-dashboard-csrf] [get update delete]
secrets [] [kubernetes-dashboard-key-holder] [get update delete]
configmaps [] [kubernetes-dashboard-settings] [get update]
services/proxy [] [dashboard-metrics-scraper] [get]
services/proxy [] [heapster] [get]
services/proxy [] [http:dashboard-metrics-scraper] [get]
services/proxy [] [http:heapster:] [get]
services/proxy [] [https:heapster:] [get]
services [] [dashboard-metrics-scraper] [proxy]
services [] [heapster] [proxy]
[root@server2 dashboard]# kubectl -n kubernetes-dashboard get clusterrolebindings.rbac.authorization.k8s.io |grep dashboard
kubernetes-dashboard ClusterRole/kubernetes-dashboard 4h32m
[root@server2 dashboard]# vim rbac.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: kubernetes-dashboard-admin
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: kubernetes-dashboard 此处不用在重新创建一个sa,因为已经有 kubernetes-dashboard这个sa
namespace: kubernetes-dashboard
[root@server2 dashboard]# kubectl -n kubernetes-dashboard get sa
[root@server2 dashboard]# kubectl apply -f rbac.yaml 创建
clusterrolebinding.rbac.authorization.k8s.io/kubernetes-dashboard-admin created
更多推荐
已为社区贡献9条内容
所有评论(0)