其他容器监控接入
更新时间:2024-08-20
1. pod容器需满足以下条件:
- 暴露TCP port {{PORT}}
- label中包含KV对 {{KEY}}:{{VALUE}}
2. 创建service
apiVersion: v1
kind: Service
metadata:
annotations:
prometheus.io/port: "{{PORT}}"
prometheus.io/scrape: "true"
name: {{NAME}}
namespace: {{NS}}
spec:
ports:
- port: {{PORT}}
protocol: TCP
targetPort: {{PORT}}
selector:
{{KEY}}: {{VALUE}}
3. 配置例子
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: operator
namespace: bec-cds
spec:
replicas: 1
selector:
matchLabels:
bec.baidubce.com/cds-component: operator
template:
metadata:
labels:
bec.baidubce.com/cds-component: operator
spec:
containers:
- args:
- -http-addr=:8001 # 该 pod 在 8001 端口暴漏 http /metrics 接口
- -log-file=/cds/operator/log/operator.log
- -log-stdout-also
- -leader-elect
command:
- /cds/operator/bin/operator
image: registry.baidubce.com/bec_sandbox/cds-operator:dev-mnt1
name: operator
---
apiVersion: v1
kind: Service
metadata:
annotations:
prometheus.io/scrape: "true" # 有该 annotation 的 svc 所选择的 pod,会被 vmagent 拉取 http://<podIP>:<port>/metrics 这个地址的内容;
# 当 service 选中多个 pod 时,每个 pod 都会被拉取,多个 pod 建的指标通过 instance label 区分;
prometheus.io/port: 8001 # 若有多个,默认全部拉取,该 annotation 指定仅从一个 port 拉取指标;
# .spec.ports 只有一个 port 时,该 annotation 可以省略;
name: operator
namespace: bec-cds
spec:
ports:
- port: 8001
protocol: TCP
targetPort: 8001
selector:
bec.baidubce.com/cds-component: operator
type: ClusterIP
4. check检查
- 在容器内部curl metrics,检验服务本身是否正常
- 在vmagent内部通过service的ip:port curl (如果没有curl,apk add curl)metrics,检验service是否正常
- kubectl -n monitor port-forward vmagent-0 8429:8429,然后打开chrome,访问 http://localhost:8429 ,检查vmagent是否成功抓取指标;如图例,上面一行是成功,下面一行红色的是失败
- 前往grafana explore,检查指标是否正常上传
- 前往grafana dashboard,检查图表是否正常渲染