监控k8s:

prometheus---->apiserver(192.168.0.13:6443 )---->kubelet(cadvisor)

prometheus采集是通过访问k8s的统一接口aipserver这个地址获取数据的,apiserver是去请求kubelet的,而kubelet集成了cadvisor,cadvisor用于收集正在运行的容器资源使用和性能信息。

apiserver一般是不能被访问的,使用https协议,需要授权,那么prometheus怎么获取数据呢?就需 要在k8s上授权RBAC,授权后就会产生一个token,prometheus就是拿着这个token去访问apiserver的。

监控K8s集群Pod步骤:

k8s节点配置

1、K8s RBAC授权

kubectl apply -f rbac.yaml

2、获取Token并保存到文件

kubectl get sa prometheus -n kube-system -o yaml
kubectl describe secret prometheus-token-xxx -n kube-system
kubectl describe secret prometheus-token-xxx -n kube-system >token.k8s
将获取的token.k8s拿到prometheus服务器上,让prometheus去拿着token访问k8s的apiserver接口
scp token.k8s root@192.168.0.11:/opt/monitor/prometheus/
token文件保留最后一串字符(token的值)其余不要

prometheus配置

3、创建Job和kubeconfig_sd_configs

- job_name: kubernetes-nodes-cadvisor 
metrics_path: /metrics
scheme: https #apiserver接口是https协议的
kubernetes_sd_configs: #启用kubernetes服务发现机制
- role: node #服务发现类型角色 node:发现集群中的节点,默认地址为kubelet的HTTP端口
api_server: https://192.168.0.13:6443
bearer_token_file: /opt/monitor/prometheus/token.k8s #这个是访问k8s获取node节点相关的资源
tls_config:
insecure_skip_verify: true #跳过https的证书效验
bearer_token_file: /opt/monitor/prometheus/token.k8s #这个是获取采集相关的资源
tls_config:
insecure_skip_verify: true

修改配置文件中对应自己的aipserver接口指标的地址 192.168.0.13:6443

[root@localhost ~]# kubectl get ep
NAME ENDPOINTS AGE
kubernetes 192.168.0.13:6443 155m

4、Grafana导入仪表盘

使用kubeadm部署的master节点默认是污点节点,去除污点

让它可以运行pod,否则该节点不会分配到pod

kubectl describe node |grep Taint
kubectl taint node k8s-master node-role.kubernetes.io/master-
kubectl describe node |grep Taint

监控K8s资源对象状态步骤:

k8s节点配置

1.部署kube-state-metrics

kubectl apply -f kube-state-metrics.yaml
访问:curl http://10.244.0.4:8081/metrics #访问到即采集到资源对象的指标

prometheus配置

2、创建Job和kubeconfig_sd_configs

- job_name: kubernetes-nodes-cadvisor 
metrics_path: /metrics
scheme: https #apiserver接口是https协议的
kubernetes_sd_configs: #启用kubernetes服务发现机制
- role: node #服务发现类型角色 node:发现集群中的节点,默认地址为kubelet的HTTP端口
api_server: https://192.168.0.13:6443
bearer_token_file: /opt/monitor/prometheus/token.k8s #这个是访问k8s获取node节点相关的资源
tls_config:
insecure_skip_verify: true #跳过https的证书效验
bearer_token_file: /opt/monitor/prometheus/token.k8s #这个是获取采集相关的资源
tls_config:
insecure_skip_verify: true

修改配置文件中对应自己的aipserver接口指标的地址 192.168.0.13:6443

[root@localhost ~]# kubectl get ep
NAME ENDPOINTS AGE
kubernetes 192.168.0.13:6443 155m

配置完成后,prometheus如果想采集到k8s资源对象的指标,必须能够访问k8s暴露的指标,

也就是必须能够访问到k8s内部的pod IP,这样才能采集到。

添加一条能够访问到pod的路由

ip route add 10.244.0.0/16 via 192.168.0.13 dev ens33
访问:curl http://10.244.0.4:8081/metrics #访问到即采集到资源对象的指标

3、Grafana导入仪表盘

k8s常用命令

kubectl get pods -n kube-system
kubectl get pod -n kube-system -o wide
kubectl describe pod <pod名> -n kube-system
kubectl create deployment web --image=nginx