监控Pod指标数据需要使⽤cadvisor,cadvisor由⾕歌开源,在kubernetes v1.11及之前的版本内置在kubelet中
并监听在4194端⼝(https://github.com/kubernetes/kubernetes/pull/65707),从v1.12开始kubelet中的cadvisor
被移除,因此需要单独通过daemonset等⽅式部署。
cadvisor(容器顾问)不仅可以搜集⼀台机器上所有运⾏的容器信息,还提供基础查询界⾯和http接⼝,⽅便其
他组件如Prometheus进⾏数据抓取,cAdvisor可以对节点机器上的容器进⾏实时监控和性能数据采集,包括容器
的CPU使⽤情况、内存使⽤情况、⽹络吞吐量及⽂件系统使⽤情况。
https://github.com/google/cadvisor
1.cadvisor部署
1.1容器方式运行
使用国外服务器下载镜像上传到阿里云
docker pull gcr.io/cadvisor/cadvisor:v0.47.0
registry.cn-beijing.aliyuncs.com/luohuiwen20/cadvisor:v0.47.0
https://github.com/google/cadvisor
sudo docker run \
--volume=/:/rootfs:ro \
--volume=/var/run:/var/run:ro \
--volume=/sys:/sys:ro \
--volume=/var/lib/docker/:/var/lib/docker:ro \
--volume=/dev/disk/:/dev/disk:ro \
--publish=8080:8080 \
--detach=true \
--name=cadvisor \
--privileged \
--device=/dev/kmsg \
registry.cn-beijing.aliyuncs.com/luohuiwen20/cadvisor:v0.47.0
1.2 demonset 方式
kubectl apply -f case1-daemonset-deploy-cadvisor.yaml
2.node-exporter 部署
daemonset 运行 node-exporter
kubectl apply -f case2-daemonset-deploy-node-exporter.yaml
启动失败,换镜像也不行,发现在1.27上已经部二进制署过node-exporter,端口占用,停止node-exporter后重新部署
访问node节点8080端口测试
http://192.168.1.27:8080/containers/
3. 配置promethues
1.二进制部署
wget https://github.com/prometheus/prometheus/releases/download/v2.37.8/prometheus-2.37.8.linux-amd64.tar.gz
tar -xf prometheus-2.37.8.linux-amd64.tar.gz
root@k8s-master02:/apps# ln -s prometheus-2.37.8.linux-amd64/ prometheus
配置Prometheus service ,默认监听9090端口
root@k8s-master02:/apps/prometheus# cat /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io/docs/introduction/overview/
After=network.target
[Service]
Restart=on-failure
WorkingDirectory=/apps/prometheus/
#--web.enable-lifecycle表示启用配置热加载功能
ExecStart=/apps/prometheus/prometheus --config.file=/apps/prometheus/prometheus.yml --web.enable-lifecycle --storage.tsdb.retention=720h
[Install]
WantedBy=multi-user.target
prometheus添加被监控的node节点
- job_name: "prometheus-cadvisor"
static_configs:
- targets: ["192.168.1.24:8080","192.168.1.25:8080","192.168.1.26:8080","192.168.1.27:8080","192.168.1.28:8080","192.168.1.29:8080"]
重新加载配置
curl -X POST http://192.168.1.25:9090/-/reload
3.2deloyment方式部署
kubectl apply -f case3-1-prometheus-cfg.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: prometheus-server
namespace: monitoring
labels:
app: prometheus
spec:
replicas: 1
selector:
matchLabels:
app: prometheus
component: server
#matchExpressions:
#- {key: app, operator: In, values: [prometheus]}
#- {key: component, operator: In, values: [server]}
template:
metadata:
labels:
app: prometheus
component: server
annotations:
prometheus.io/scrape: 'false'
spec:
nodeName: 192.168.1.28
serviceAccountName: monitor
containers:
- name: prometheus
image: prom/prometheus:v2.31.2
imagePullPolicy: IfNotPresent
command:
- prometheus
- --config.file=/etc/prometheus/prometheus.yml
- --storage.tsdb.path=/prometheus
- --storage.tsdb.retention=720h
ports:
- containerPort: 9090
protocol: TCP
volumeMounts:
- mountPath: /etc/prometheus/prometheus.yml
name: prometheus-config
subPath: prometheus.yml
- mountPath: /prometheus/
name: prometheus-storage-volume
- mountPath: /etc/localtime
name: time
volumes:
- name: prometheus-config
configMap:
name: prometheus-config
items:
- key: prometheus.yml
path: prometheus.yml
mode: 0644
- name: prometheus-storage-volume
hostPath:
path: /data/prometheusdata
type: Directory
- name: time
hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai
部署svc
kubectl apply -f case3-3-prometheus-svc.yaml
访问http://192.168.1.28:30090/
4.部署granfana
4.1部署grafana
机器192.168.1.21
https://grafana.com/grafana/download?pg=get&plcmt=selfmanaged-box1-cta1
sudo apt-get install -y adduser libfontconfig1
wget https://dl.grafana.com/enterprise/release/grafana-enterprise_9.5.2_amd64.deb
sudo dpkg -i grafana-enterprise_9.5.2_amd64.deb
systemctl start grafana-server.service
systemctl enable grafana-server.servic
http://192.168.1.21:3000/
4.2 配置grafana
访问http://192.168.1.21:3000/granfana添加数据源
地址填写promethues:http://192.168.1.28:30090/
4.3导入模板
14282
5.prmetheus服务发现
prometheus 默认是采用 pull 方式拉取监控数据的,也就是定时去目标主机上 抓 取metrics 数 据 , 每 一 个 被抓取 的目标 需 要 暴露 一个 HTTP 接口 ,prometheus 通过这个暴露的接口就可以获取到相应的指标数据,这种方式需要由目标服务决定采集的目标有哪些,通过配置在 scrape_configs 中的各种 job来实现,无法动态感知新服务,如果后面增加了节点或者组件信息,就得手动修promrtheus 配置,并重启 promethues,很不方便,所以出现了动态服务发现,动态服务发现能够自动发现集群中的新端点,并加入到配置中,通过服务发现,Prometheus 能查询到需要监控的 Target 列表,然后轮询这些 Target 获取监控数据。prometheus 获取数据源 target 的方式有多种,目前支持的服务发现有很多种,常用的主要分为以下几种:https://prometheus.io/docs/prometheus/latest/configuration/configuration/#configuration-file
kubernetes_sd_configs: #基于 Kubernetes API 实现的服务发现,让 prometheus 动态发现 kubernetes 中被监控的目标
static_configs: #静态服务发现,基于 prometheus 配置文件指定的监控目标
dns_sd_configs: #DNS 服务发现监控目标
consul_sd_configs: #Consul 服务发现,基于 consul 服务动态发现监控目标file_sd_configs: #基于指定的文件实现服务发现,基于指定的文件发现监控目标
promethues 的静态静态服务发现 static_configs:每当有一个新的目标实例需要监控,都需要手动修改配置文件配置目标 target。
promethues 的 consul 服务发现 consul_sd_configs:Prometheus 一直监视 consul 服务,当发现在 consul中注册的服务有变化,prometheus 就会自动监控到所有注册到 consul 中的目标资源。promethues 的 k8s 服务发现
kubernetes_sd_configs:Prometheus 与 Kubernetes 的 API 进行交互,动态的发现 Kubernetes 中部署的所有可监控的目标资源。
5.1部署croedns
croedns 模板14981
原来只有一个endpoints,通过 kubectl scale deployment -n kube-system coredns --replicas=2
会自动发现