k8s之监控集群资源利用率
一、查看资源集群状态
1、集群整体状态
kubectl cluster-info
2、更多集群信息
kubectl cluster-info dump
3、查看资源信息
kubectl describe <资源> <名称>
4、实时查看资源信息
kubectl get pod<Pod名称> --watch
一、Metrics Server介绍
Metrics Server是一个集群范围的资源使用情况的数据聚合器。作为一个应用部署在集群中。Metric server从每个节点上KubeletAPI收集指标,通过Kubernetes聚合器注册在Master APIServer中。
1、工作原理
2、Metrics Server部署
①项目地址
https://github.com/kubernetes-sigs/metrics-server
②下载Yaml文件
wget https://github.com/kubernetes-sigs/metrics-server/releases/download/v0.3.7/components.yaml
③修改Yaml文件
containers: - name: metrics-server image: lizhenliang/metrics-server-amd64:v0.3.1 #修改成国内镜像,这里使用lizhenliang已有的镜像
imagePullPolicy: IfNotPresent args: - --cert-dir=/tmp - --secure-port=4443 - --kubelet-insecure-tls #跳过tls检查 - --kubelet-preferred-address-types=InternalIP #以node ip连接kubelet
④部署Metrics
kubectl apply -f components.yaml #yaml文件应用,可以在任何路径下面应用
验证是否部署成功
[root@k8s-master k8s]# kubectl get pods -n kube-system NAME READY STATUS RESTARTS AGE calico-kube-controllers-676c4cbdf-f9m8w 1/1 Running 0 3d23h calico-node-5wfdf 1/1 Running 0 3d23h calico-node-xzrwc 1/1 Running 0 3d23h coredns-7ff77c879f-c77qj 1/1 Running 0 3d23h coredns-7ff77c879f-tjws5 1/1 Running 0 3d23h etcd-k8s-master 1/1 Running 0 3d23h kube-apiserver-k8s-master 1/1 Running 0 3d23h kube-controller-manager-k8s-master 1/1 Running 0 3d23h kube-proxy-jmqnj 1/1 Running 0 3d23h kube-proxy-vnzhk 1/1 Running 0 3d23h kube-scheduler-k8s-master 1/1 Running 0 3d23h metrics-server-8fcfb55ff-j8vmz 1/1 Running 1 2m #状态已经Running
⑤监控集群资源利用率
查看Metrics是否注册到APIsevice中
[root@k8s-master k8s]# kubectl get apiservice NAME SERVICE AVAILABLE AGE v1. Local True 4d19h v1.admissionregistration.k8s.io Local True 4d19h v1.apiextensions.k8s.io Local True 4d19h v1.apps Local True 4d19h v1.authentication.k8s.io Local True 4d19h v1.authorization.k8s.io Local True 4d19h v1.autoscaling Local True 4d19h v1.batch Local True 4d19h v1.coordination.k8s.io Local True 4d19h v1.crd.projectcalico.org Local True 9m2s v1.networking.k8s.io Local True 4d19h v1.rbac.authorization.k8s.io Local True 4d19h v1.scheduling.k8s.io Local True 4d19h v1.storage.k8s.io Local True 4d19h v1beta1.admissionregistration.k8s.io Local True 4d19h v1beta1.apiextensions.k8s.io Local True 4d19h v1beta1.authentication.k8s.io Local True 4d19h v1beta1.authorization.k8s.io Local True 4d19h v1beta1.batch Local True 4d19h v1beta1.certificates.k8s.io Local True 4d19h v1beta1.coordination.k8s.io Local True 4d19h v1beta1.discovery.k8s.io Local True 4d19h v1beta1.events.k8s.io Local True 4d19h v1beta1.extensions Local True 4d19h v1beta1.metrics.k8s.io kube-system/metrics-server True 53s v1beta1.networking.k8s.io Local True 4d19h v1beta1.node.k8s.io Local True 4d19h v1beta1.policy Local True 4d19h v1beta1.rbac.authorization.k8s.io Local True 4d19h v1beta1.scheduling.k8s.io Local True 4d19h v1beta1.storage.k8s.io Local True 4d19h v2beta1.autoscaling Local True 4d19h v2beta2.autoscaling Local True 4d19h
查看节点资源使用率
[root@k8s-master k8s]# kubectl top node NAME CPU(cores) CPU% MEMORY(bytes) MEMORY% k8s-master 211m 21% 977Mi 33% k8s-node1 87m 8% 398Mi 13%
查看Pod资源使用率,未排序
[root@k8s-master k8s]# kubectl top pod -A NAMESPACE NAME CPU(cores) MEMORY(bytes) kube-system calico-kube-controllers-676c4cbdf-f9m8w 1m 13Mi kube-system calico-node-5wfdf 25m 76Mi kube-system calico-node-xzrwc 23m 85Mi kube-system coredns-7ff77c879f-c77qj 3m 11Mi kube-system coredns-7ff77c879f-tjws5 3m 11Mi kube-system etcd-k8s-master 17m 52Mi kube-system kube-apiserver-k8s-master 36m 439Mi kube-system kube-controller-manager-k8s-master 12m 41Mi kube-system kube-proxy-7v26z 1m 17Mi kube-system kube-proxy-nxlrh 1m 16Mi kube-system kube-scheduler-k8s-master 4m 16Mi kube-system metrics-server-6767f6d894-jsbff 1m 14Mi kubernetes-dashboard dashboard-metrics-scraper-6b4884c9d5-rdgz8 1m 7Mi kubernetes-dashboard kubernetes-dashboard-7b544877d5-5thm9 1m 14Mi
查看Pod资源使用率,并按照内存大小进行排序(cpu列说明:1000m=1c,即1000m代表1个cpu逻辑核,当cpu值显示大于100m时,建议留意下)
知识改变命运!!