Prometheus基础-监控linux、系统服务、docker、mysql-元一软件 📅 2026/7/15 5:20:57 服务启动[rootprometheus prometheus]# systemctl daemon-reload[rootprometheus prometheus]# systemctl restart node_exporter[rootprometheus prometheus]# systemctl restart prometheus[rootprometheus prometheus]# systemctl restart grafana[rootprometheus prometheus]# systemctl restart updatetime工具检测[rootprometheus prometheus]# ./promtool check config ./prometheus.yml热加载[rootprometheus prometheus]# ps -ef|grep prometheus[rootprometheus prometheus]# kill -HUP 1267时间同步$ yuminstallntpdate-y$ ntpdate time.windows.comprometheushttp://192.168.153.22:9090/ -------------------------------prometheus----------------------------------------# vi /usr/lib/systemd/system/prometheus.service[Unit]Descriptionprometheus[Service]ExecStart/opt/monitor/prometheus/prometheus--config.file/opt/monitor/prometheus/prometheus.ymlExecReload/bin/kill-HUP$MAINPIDKillModeprocessRestarton-failure[Install]WantedBymulti-user.target ---------------------------------------------------------------------[rootprometheus prometheus]# systemctl daemon-reload[rootprometheus prometheus]# systemctl start prometheusgrafanahttp://192.168.153.21:3000/ ------------------------------------grafana----------------------------------[rootprometheus ~]# vi /usr/lib/systemd/system/grafana.service[Unit]Descriptiongrafana[Service]ExecStart/opt/monitor/grafana/bin/grafana-server-homepath/opt/monitor/grafanaExecReload/bin/kill-HUP$MAINPIDKillModeprocessRestarton-failure[Install]WantedBymulti-user.target --------------------------------------------------------------------------------------[rootprometheus prometheus]# systemctl daemon-reload[rootprometheus prometheus]# systemctl start grafananode_exporter[rootk8snode1 prometheus]# vi /usr/lib/systemd/system/node_exporter.service[Unit]Descriptionnode_exporter[Service]ExecStart/usr/local/node_exporter/node_exporterExecReload/bin/kill-HUP$MAINPIDKillModeprocessRestarton-failure[Install]WantedBymulti-user.target ----------------------------------------------------------------------------[rootprometheus ~]# systemctl daemon-reload[rootprometheus ~]# systemctl start node_exporter配置被监控端scrape_configs:# The job name is added as a label jobjob_name to any timeseries scraped from this config.- job_name:prometheusstatic_configs: - targets:[localhost:9090]- job_name:Linux Serverstatic_configs: - targets:[192.168.153.20:9100]1635429871239.png监控Linux服务器使用Grafana展示node_exporter数据指标仪表盘ID92761635430281665.png1635430316866.pngnode_exporter启用HTTP认证安装httpd-tool[rootprometheus ~]# yum install httpd-tools –y[rootprometheus ~]# htpasswd -nBC 12 | tr -d :\nNew password: Re-type new password:$2y$12$w78Y3R7SLaGlPY4OkmRCcu7esY.xzENZeDTj.87QhWsBRADyaRDLi[rootprometheus node_exporter]# lsLICENSE node_exporter NOTICE[rootprometheus node_exporter]# vi config.ymlbasic_auth_users: lql:$2y$12$w78Y3R7SLaGlPY4OkmRCcu7esY.xzENZeDTj.87QhWsBRADyaRDLi系统服务-node_exporter[rootprometheus node_exporter]# vi /usr/lib/systemd/system/node_exporter.service[Unit]Descriptionnode_exporter[Service]ExecStart/usr/local/node_exporter/node_exporter--web.config/usr/local/node_exporter/config.ymlExecReload/bin/kill-HUP$MAINPIDKillModeprocessRestarton-failure[Install]WantedBymulti-user.target ----------------------------------------------------------------------------[rootprometheus ~]# systemctl daemon-reload[rootprometheus ~]# systemctl start node_exporterprimetheus配置文件- job_name:Linux Serverbasic_auth: username: lql password:12345678static_configs: - targets:[192.168.153.20:9100]监控系统服务运行状态服务配置[rootprometheus node_exporter]# vi /usr/lib/systemd/system/node_exporter.service[Unit]Descriptionnode_exporter[Service]ExecStart/usr/local/node_exporter/node_exporter--web.config/usr/local/node_exporter/config.yml--collector.systemd--collector.systemd.unit-whitelist(docker|sshd|nginx).serviceExecReload/bin/kill-HUP$MAINPIDKillModeprocessRestarton-failure[Install]WantedBymulti-user.target ----------------------------------------------------------------------------------[rootprometheus prometheus]# systemctl daemon-reload[rootprometheus prometheus]# systemctl restart node_exporterhttp://192.168.153.17:9100/metrics服务检测1635475436994.png查看服务状态1635475662804.png1635475816420.png#docker服务关闭value则为0 systemctl stop dokcer 监控dockerDocker部署cAdvisordockerrun-d\--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\--publish8080:8080\--detachtrue\--namecadvisor\google/cadvisor:latest http://192.168.153.17:8080/metricsprometheus配置- job_name:dockerstatic_configs: - targets:[192.168.153.17:8080]1635476978343.png导入granfana1635477572914.png增加相关变量1635489169318.pnglabel_values(up,instance).*:8080#修改指标【instance$Node】每个指标都要修改#比如sum(rate(container_cpu_user_seconds_total{image!,instance$Node}[5m]) * 100)监控mysql启动mysql[rootansible ~]# docker start mysql启动mysqld_exporter服务[rootansible mysql_exporter]# lsLICENSE mysqld_exporter NOTICE#配置文件写入mysql的用户名和密码[rootansible mysql_exporter]# vi .my.cnf[client]userrootpassword123#启动服务[rootansible mysql_exporter]# ./mysqld_exporter --config.my-cnf.my.cnfhttp://192.168.153.17:9104/prometheus配置http://192.168.153.17:9104/ - job_name:mysqlstatic_configs: - targets:[192.168.153.17:9104]导入granfana1635491714113.png