
我们为什么要做监控?
就比如马路上边的各种摄像头,它能监控车流量,监控交通故障等。出现故障可以第一时间确定发生地点。当然,在我们这里领域里,监控也是起着同样的作用。它也能帮助我们监控业务流量,业务健康状态,服务器状态等等,一套成熟的监控体系将会给我们运维、开发人员带来极大便利性。
本文将介绍如何搭建一套目前企业较为流行的持久化监控系统,将使用Influxdb作为prometheus持久化存储。
主要架构:Kubernetes+Prometheus+Influxdb+Grafana+Prometheus-alert
架构图如下:

环境介绍
•操作系统:Centos7.6•Kubernetes: v1.20.5•Helm: v3.5.4•Prometheus: v2.19.0•Influxdb: v1.8.0•Grafana: v6.7.1
Prometheus和Grafana是部署到k8s上面。Influxdb是使用单独的机器部署。
一、安装Influxdb
Influxdb部署在服务器上:192.168.241.143
1、下载安装包
wget https://dl.influxdata.com/influxdb/releases/influxdb-1.8.0.x86_64.rpm
2、安装
yum -y localinstall influxdb-1.8.0.x86_64.rpm
3、启动Influxdb
systemctl start influxdb
systemctl enable influxdb
安装后, 在/usr/bin下面有如下文件
# influx (按两下tab键)
influx influxd influx_inspect influx_stress influx_tsm
-------
influxd Influxdb服务器
influx Influxdb命令行客户端
influx_inspect 查看工具
influx_stress 压力测试工具
influx_tsm 数据库转换工具(将数据库从b1或bz1格式转换为tsm1格式)
在 /var/lib/influxdb/下面会有如下文件夹
# ls /var/lib/influxdb/
data meta
4、创建http接口用于普罗米修斯
192.168.241.143是我的Influxdb地址
curl -XPOST http://192.168.241.143:8086/query --data-urlencode "q=CREATE DATABASE prometheus"
二、准备Prometheus的chart
Prometheus使用的是Helm方式部署到k8s里面。
下载地址:
1、先将chart克隆到本地。
git clone https://github.com/zyiqian/charts.git
ls charts
prometheus prometheus-k8s-values.yaml README.md
2、创建一个prometheus的命名空间
kubectl create namespace monitoring
三、准备remote_storage_adapter
对于业务比较大的环境Local storage是绝对满足不了的,那么就要用remote storage了。
Prometheus的remote storage需要借助adapter实现,adapter会提供write url和read url给Prometheus,这样Prometheus获取到数据后就会先写到本地然后再调用write url写到远端。
1、还需要下载一个可执行文件remote_storage_adapter。
GitHub地址:https://github.com/prometheus/prometheus/blob/main/documentation/examples/remote_storage/remote_storage_adapter/README.md
需要安装一个go环境build。我已经build好了,下载下面的地址可以直接用。
wget https://github.com/zyiqian/charts/blob/main/prometheus/remote_storage_adapter
chmod +x remote_storage_adapter
2、现在我们启动一个remote_storage_adapter来对接Influxdb和prometheus。
./remote_storage_adapter --influxdb-url=http://192.168.241.143:8086/ --influxdb.database="prometheus" --influxdb.retention-policy=autogen
如果想获取相应的帮助可以使用:./remote_storage_adapter -h来获取相应帮助(修改绑定的端口,Influxdb的设置等..)
将remote_storage_adapter注册为系统服务
cat>/lib/systemd/system/remote_storage_adapter.service<<EOF
[Service]
Restart=on-failure
WorkingDirectory=/root/
ExecStart=/root/remote_storage_adapter --influxdb-url=http://192.168.241.143:8086/ --influxdb.database="prometheus" --influxdb.retention-policy=autogen
[Install]
WantedBy=multi-user.target
EOF
chown 644 /lib/systemd/system/remote_storage_adapter.service
systemctl daemon-reload
systemctl enable remote_storage_adapter
systemctl start remote_storage_adapter
systemctl status remote_storage_adapter
3、修改prometheus chart里面的values文件
找到prometheus-k8s-values.yaml里面remoteWrite和remoteRead。
将地址替换成自己Influxdb的地址.
修改service的type为NodePort的类型
cd prometheus
vim prometheus-k8s-values.yaml
........
remoteWrite:
- url: "http://192.168.241.143:8086/api/v1/prom/write?db=prometheus"
##