prometheus+grafana(Docker版)
<h2>Prometheus监控:容器、linux主机、MySQL、nginx</h2>
<p>环境
<code>最小版安装:192.168.10.143</code></p>
<pre><code class="language-bash">yum install -y wget vim git gcc gcc-c++ make libevent unzip perl automake autoconf libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel</code></pre>
<p>Docker安装地址:<a href="https://get.daocloud.io/">https://get.daocloud.io/</a>
一键安装:curl -sSL <a href="https://get.daocloud.io/docker">https://get.daocloud.io/docker</a> | sh</p>
<h2>安装docker</h2>
<pre><code class="language-bash">curl -sSL https://get.daocloud.io/docker | sh
rpm -qa | grep docker
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io
systemctl restart docker
docker version</code></pre>
<h2>编写yml脚本</h2>
<pre><code class="language-bash">#普罗米修斯使用yaml文件进行配置。
[root@master ~]# vim prometheus/prometheus.yml
global:
#每15s抓取一次自己的指标
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
- job_name: "prometheus"
static_configs:
#默认端口9090
- targets: ["localhost:9090"]</code></pre>
<h2>运行一个名为prometheus容器</h2>
<pre><code class="language-bash">[root@master ~]# docker run -d \
> -p 9090:9090 \
> --restart always \
> --name prometheus \
> -v $PWD/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml \
> -e TZ=Asia/Shanghai \
> -v /etc/localtime:/etc/localtime \
> prom/prometheus</code></pre>
<h2>查看端口</h2>
<pre><code class="language-bash">[root@master ~]# netstat -luntp | grep 9090
tcp 0 0 0.0.0.0:9090 0.0.0.0:* LISTEN 2612/docker-proxy
tcp6 0 0 :::9090 :::* LISTEN 2620/docker-proxy</code></pre>
<p><a href="http://192.168.10.143:9090/targets">http://192.168.10.143:9090/targets</a></p>
<p><a href="http://192.168.10.143:9090/metrics">http://192.168.10.143:9090/metrics</a> 有内容说明数据源没问题</p>
<h2>创建grafana容器</h2>
<pre><code class="language-bash">[root@master ~]# mkdir $PWD/grafana && chmod 777 $PWD/grafana
[root@master ~]# docker run -d \
> -p 3000:3000 \
> --restart always \
> --name=grafana \
> -v $PWD/grafana:/var/lib/grafana \
> -e TZ=Asia/Shanghai \
> -v /etc/localtime:/etc/localtime \
> grafana/grafana</code></pre>
<h2>查看端口</h2>
<pre><code class="language-bash">[root@master ~]# netstat -luntp | grep 3000
tcp 0 0 0.0.0.0:3000 0.0.0.0:* LISTEN 2868/docker-proxy
tcp6 0 0 :::3000 :::* LISTEN 2876/docker-proxy</code></pre>
<p><a href="http://192.168.10.143:3000">http://192.168.10.143:3000</a><br />
默认admin admin
admin123456</p>
<h2>添加prometheus源</h2>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=677d11116aabb7aad53c9dbd056f7d95&file=file.png" alt="" /></p>
<h2>创建cadvisor容器</h2>
<pre><code class="language-bash">[root@master ~]# docker run \
> --volume=/:/rootfs:ro \
> --volume=/var/run:/var/run:rw \
> --volume=/sys:/sys:ro \
> --volume=/var/lib/docker/:/var/lib/docker:ro \
> --volume=/dev/disk/:/dev/disk:ro \
> --publish=8081:8080 \
> --detach=true \
> --name=cadvisor \
> google/cadvisor:latest</code></pre>
<p>访问web主页:<a href="http://192.168.10.143:8081">http://192.168.10.143:8081</a> </p>
<h2>与Prometheus集成,将节点导出器添加到scrape_configs列表中</h2>
<pre><code class="language-bash">vim prometheus/prometheus.yml
- job_name: "container"
static_configs:
- targets: ['192.168.10.143:8081'] #本地cadvisor访问地址
[root@master ~]# docker restart prometheus</code></pre>
<p>数据源测试
<a href="http://192.168.10.143:8081/metrics">http://192.168.10.143:8081/metrics</a></p>
<p>impoort导入模板编号:395 13631 14841</p>
<h2>prometheus监控主机、内存、硬盘等</h2>
<pre><code class="language-bash">#docker方式部署优点:快速
[root@master ~]# docker run -d \
> --name node_exporter \
> --restart=always \
> --net="host" \
> --pid="host" \
> -v "/proc:/host/proc:ro" \
> -v "/sys:/host/sys:ro" \
> -v "/:/rootfs:ro" \
> -e TZ=Asia/Shanghai \
> -v /etc/localtime:/etc/localtime \
> prom/node-exporter \
> --path.procfs=/host/proc \
> --path.rootfs=/rootfs \
> --path.sysfs=/host/sys \
> --collector.filesystem.ignored-mount-points='^/(sys|proc|dev|host|etc)($$|/)'</code></pre>
<h2>查看端口</h2>
<pre><code class="language-bash">[root@master ~]# netstat -luntp | grep 9100
tcp6 0 0 :::9100 :::* LISTEN 22321/node_exporter</code></pre>
<h2>与Prometheus集成</h2>
<pre><code class="language-bash">[root@master ~]# vim prometheus/prometheus.yml
- job_name: "node"
static_configs:
- targets: ["192.168.10.143:9100"]
[root@master ~]# docker restart prometheus
prometheus</code></pre>
<h2>在node上产生一点负载</h2>
<pre><code class="language-bash">yum install epel-release stress-ng -y
stress-ng -c -0 -l 85</code></pre>
<p>impoort导入模板编号:9276 8919 1860
测试访问数据:<a href="http://192.168.10.143:9100/metrics">http://192.168.10.143:9100/metrics</a></p>
<h2>prometheus监控mysql</h2>
<pre><code class="language-bash">#拉取镜像
docker pull mysql
docker pull prom/mysqld-exporter</code></pre>
<h3>创建一个桥接网络,给mysql使用</h3>
<pre><code class="language-bash">docker network create my-mysql-network
685caab3bdc41539a209ffea4d21fc1ac9f36c1ce3510cef804381511fa089b4
docker network ls</code></pre>
<h3>运行一个mysql容器</h3>
<pre><code class="language-bash">[root@master ~]# docker run -d \
-u root \
--name Mysql \
-p 3306:3306 \
--restart always \
--network my-mysql-network \
--network-alias mysql \
-v $PWD/mysql/logs:/logs \
-v $PWD/mysql/data:/var/lib/mysql \
-v $PWD/mysql/conf:/etc/mysql/conf.d \
-e TZ=Asia/Shanghai \
-v /etc/localtime:/etc/localtime \
-e MYSQL_ROOT_PASSWORD=123456 \
mysql:latest</code></pre>
<h3>创建mysql-exporter容器</h3>
<pre><code class="language-bash">[root@master ~]# docker run -d \
> -p 9104:9104 \
> --restart always \
> --name mysql_exporter \
> --network my-mysql-network \
> --network-alias mysql_exporter \
> -e TZ=Asia/Shanghai \
> -v /etc/localtime:/etc/localtime \
> -e DATA_SOURCE_NAME="root:123456@(192.168.10.143:3306)/" \
> prom/mysqld-exporter
其中:-e DATA_SOURCE_NAME="user:password@(hostname:3306)"
user: root
password: 123456
模板编号:7362
测试访问数据:http://192.168.10.143:9104/metrics
netstat -luntp | grep 9104</code></pre>
<h3>与Prometheus集成</h3>
<pre><code class="language-bash">vim prometheus/prometheus.yml
- job_name: "mysql"
static_configs:
- targets: ["192.168.10.143:9104"]</code></pre>
<h2>源码安装nginx</h2>
<pre><code class="language-bash">[root@master ~]# wget http://nginx.org/download/nginx-1.21.4.tar.gz
[root@master ~]# cd && tar xf nginx-1.21.4.tar.gz && cd nginx-1.21.4
[root@master nginx-1.21.4]# ./configure && make -j2 && make install
[root@master nginx-1.21.4]# /usr/local/nginx/sbin/nginx -v
[root@master nginx-1.21.4]# ps -ef | grep nginx && netstat -luntp | grep nginx</code></pre>
<h3>配置nginx-module-vts模块</h3>
<pre><code class="language-bash">[root@master ~]# unzip nginx-module-vts-master.zip
[root@master ~]# cd nginx-1.21.4
[root@master nginx-1.21.4]# ./configure --add-module=/root/nginx-module-vts-master
[root@master nginx-1.21.4]# make -j2
[root@master nginx-1.21.4]# /root/nginx-1.21.4/objs/nginx -v</code></pre>
<h3>修改配置</h3>
<pre><code class="language-bash">vim /usr/local/nginx/conf/nginx.conf
...
http下新增配置
vhost_traffic_status_zone;
vhost_traffic_status_filter_by_host on;
80端口下,新增status接口监控
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
...</code></pre>
<h3>重新加载配置</h3>
<pre><code class="language-bash">/root/nginx-1.21.4/objs/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful</code></pre>
<h3>备份原文件</h3>
<pre><code class="language-bash">cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak</code></pre>
<h3>替换nginx二进制文件</h3>
<pre><code class="language-bash">cp -f ~/nginx-1.21.4/objs/nginx /usr/local/nginx/sbin/nginx
cp: overwrite ‘/usr/local/nginx/sbin/nginx’? y</code></pre>
<h3>正确性检查</h3>
<pre><code class="language-bash">/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
reload nginx
yum install -y psmisc
killall nginx && /usr/local/nginx/sbin/nginx</code></pre>
<h3>查看编译结果</h3>
<pre><code class="language-bash">[root@master ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.21.4
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
configure arguments: --add-module=/root/nginx-module-vts-master</code></pre>
<p>测试:192.168.10.143/status</p>
<h3>安装nginx采集组件(nginx服务所在主机)</h3>
<pre><code class="language-bash">[root@master ~]# docker run -d \
> -p 9913:9913 \
> --restart always \
> --name=nginx-vts-exporter \
> -e TZ=Asia/Shanghai \
> -v /etc/localtime:/etc/localtime \
> -e NGINX_STATUS="http://192.168.10.143/status/format/json" \
> sophos/nginx-vts-exporter
[root@master ~]# netstat -luntp | grep 9913</code></pre>
<h3>与prometheus集成</h3>
<pre><code class="language-bash">vim prometheus/prometheus.yml
- job_name: "nginx"
static_configs:
- targets: ["192.168.10.143:9913"]</code></pre>
<p>docker restart prometheus
数据测试:192.168.10.143:9913/metrics
import模板编号: 2949</p>