基于Docker从零搭建Prometheus+Grafana

环境版本:Ubuntu Kylin 20.04

1 安装docker

使用国内 daocloud 一键安装命令:

curl -sSL https://get.daocloud.io/docker | sh

2 拉取相关镜像

docker pull prom/node-exporter
docker pull prom/prometheus
docker pull grafana/grafana

3 node exporter

输出被监控组件信息的HTTP接口被叫做exporter 。目前互联网公司常用的组件大部分都有exporter可以直接使用,比如Varnish、Haproxy、Nginx、MySQL、Linux 系统信息 (包括磁盘、内存、CPU、网络等等),具体支持的源看:https://github.com/prometheus。

3.1 start node exporter
drguo@ubuntu:~/prometheus$ sudo docker run -d -p 9100:9100 -v "/proc:/host/proc:ro" -v "/sys:/host/sys:ro" -v "/:/rootfs:ro"   --net="host"   prom/node-exporter
3.2 查看端口9100
drguo@ubuntu:~/prometheus$ netstat -anpt
3.3 访问url
http://localhost:9100/metrics

在这里插入图片描述

4 prometheus

4.1 编辑prometheus.yml

本文只使用了一台服务器,“- targets: [‘192.168.222.129:9100’]”中的IP为本机IP。

vim /home/drguo/prometheus/prometheus.yml
global:
  scrape_interval: 15s # 默认抓取间隔, 15秒向目标抓取一次数据。
  evaluation_interval: 15s
# 这里表示抓取对象的配置
scrape_configs:
  #这个配置是表示在这个配置内的时间序例,每一条都会自动添加上这个{job_name:"prometheus"}的标签  - job_name: 'prometheus'
  - job_name: prometheus
    static_configs:
      - targets: ['localhost:9090']
        labels:
          instance: prometheus

  - job_name: linux
    static_configs:
      - targets: ['192.168.222.129:9100']
        labels:
          instance: localhost
4.2 start prometheus
drguo@ubuntu:~/prometheus$ sudo docker run -d -p 9090:9090 -v /home/drguo/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
4.3 查看端口9090
drguo@ubuntu:~/prometheus$ netstat -anpt
4.4 访问url
http://192.168.222.129:9090/graph

在这里插入图片描述

5 grafana

5.1 start grafana
drguo@ubuntu:~/grafana-storage$ sudo docker run -d -p 3000:3000 -v /home/drguo/grafana-storage:/var/lib/grafana grafana/grafana
5.2 查看端口3000
drguo@ubuntu:~/prometheus$ netstat -anpt
5.3 访问url
http://192.168.222.129:3000

在这里插入图片描述

5.4 简单使用grafana

将prometheus地址(http://192.168.222.129:9090)添加后save & test
在这里插入图片描述import 模板
在这里插入图片描述点击左边模板名
在这里插入图片描述

5.5 设置邮件报警

grafana.ini

[smtp]
# 启用 smtp
enabled = true
# 邮件服务器地址和端口
host = smtp.qq.com:587
# 账号
user = xx@qq.com
# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""
# 授权码
password = xx
# cert_file =
# key_file =
# 忽略 SSL 认证
skip_verify = true
from_address = xx@qq.com
from_name = Grafana
ehlo_identity = qq.com

使用本地文件启动 grafana-server

sudo docker run -d -p 3000:3000 -v /home/drguo/grafana.ini:/etc/grafana/grafana.ini -v /home/drguo/grafana-storage:/var/lib/grafana grafana/grafana grafana-server /etc/grafana/grafana.ini

登录grafana>Alerting>New channel

备注

## Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Memberships) 等等。
-a (all)显示所有选项,默认不显示LISTEN相关
-t (tcp)仅显示tcp相关选项
-u (udp)仅显示udp相关选项
-n 拒绝显示别名,能显示数字的全部转化成数字。
-l 仅列出有在 Listen (监听) 的服務状态

-p 显示建立相关链接的程序名
-r 显示路由信息,路由表
-e 显示扩展信息,例如uid等
-s 按各个协议进行统计
-c 每隔一个固定时间,执行该netstat命令。
提示:LISTEN和LISTENING的状态只有用-a或者-l才能看到

## Conflict The container name "/grafana" is already in use by container
drguo@ubuntu:~$ sudo docker run -d -p 3000:3000 --name=grafana -v /home/drguo/grafana-storage:/var/lib/grafana grafana/grafana
docker: Error response from daemon: Conflict. The container name "/grafana" is already in use by container "cbc10f31a3fd85a8e9db990af80f8e23b2ab796fc87268572c87c940f5efa062". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.

drguo@ubuntu:~$ sudo docker container ls -a
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS                        PORTS                    NAMES
c5e0afb1a24f        prom/prometheus      "/bin/prometheus --c…"   4 minutes ago       Up 4 minutes                  0.0.0.0:9090->9090/tcp   great_khayyam
3af60ea3f5b5        prom/prometheus      "/bin/prometheus --c…"   18 minutes ago      Exited (137) 10 minutes ago                            great_herschel
aa87fa1cc5f6        prom/node-exporter   "/bin/node_exporter"     22 minutes ago      Up 22 minutes                                          angry_mcnulty
cbc10f31a3fd        grafana/grafana      "/run.sh"                8 days ago          Exited (137) 8 days ago                                grafana
77ea904e5540        prom/prometheus      "/bin/prometheus --c…"   8 days ago          Exited (137) 8 days ago                                agitated_swartz
12fa7ed83cd0        prom/prometheus      "/bin/prometheus --c…"   8 days ago          Exited (137) 8 days ago                                beautiful_vaughan
0322192a2ae1        prom/prometheus      "/bin/prometheus --c…"   8 days ago          Exited (137) 8 days ago                                elated_wing
e571c84045b1        prom/prometheus      "/bin/prometheus --c…"   8 days ago          Exited (2) 8 days ago                                  practical_chaum
bda166396f23        prom/prometheus      "/bin/prometheus --c…"   8 days ago          Exited (2) 8 days ago                                  thirsty_faraday
9d37efdae13f        prom/prometheus      "/bin/prometheus --c…"   8 days ago          Exited (2) 8 days ago                                  xenodochial_murdock
765e2b9f7b97        prom/node-exporter   "/bin/node_exporter"     8 days ago          Exited (137) 8 days ago                                modest_jang

drguo@ubuntu:~$ sudo docker container rm grafana
grafana
drguo@ubuntu:~$ sudo docker container ls -a
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS                        PORTS                    NAMES
c5e0afb1a24f        prom/prometheus      "/bin/prometheus --c…"   10 minutes ago      Up 10 minutes                 0.0.0.0:9090->9090/tcp   great_khayyam
3af60ea3f5b5        prom/prometheus      "/bin/prometheus --c…"   24 minutes ago      Exited (137) 16 minutes ago                            great_herschel
aa87fa1cc5f6        prom/node-exporter   "/bin/node_exporter"     28 minutes ago      Up 28 minutes                                          angry_mcnulty
77ea904e5540        prom/prometheus      "/bin/prometheus --c…"   8 days ago          Exited (137) 8 days ago                                agitated_swartz
12fa7ed83cd0        prom/prometheus      "/bin/prometheus --c…"   8 days ago          Exited (137) 8 days ago                                beautiful_vaughan
0322192a2ae1        prom/prometheus      "/bin/prometheus --c…"   8 days ago          Exited (137) 8 days ago                                elated_wing
e571c84045b1        prom/prometheus      "/bin/prometheus --c…"   8 days ago          Exited (2) 8 days ago                                  practical_chaum
bda166396f23        prom/prometheus      "/bin/prometheus --c…"   8 days ago          Exited (2) 8 days ago                                  thirsty_faraday
9d37efdae13f        prom/prometheus      "/bin/prometheus --c…"   8 days ago          Exited (2) 8 days ago                                  xenodochial_murdock
765e2b9f7b97        prom/node-exporter   "/bin/node_exporter"     8 days ago          Exited (137) 8 days ago                                modest_jang


## stop
drguo@ubuntu:~$ sudo docker ps
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS              PORTS                    NAMES
870d6a3aa8f5        grafana/grafana      "/run.sh"                23 hours ago        Up 23 hours         0.0.0.0:3000->3000/tcp   loving_satoshi
c5e0afb1a24f        prom/prometheus      "/bin/prometheus --c…"   23 hours ago        Up 23 hours         0.0.0.0:9090->9090/tcp   great_khayyam
aa87fa1cc5f6        prom/node-exporter   "/bin/node_exporter"     24 hours ago        Up 24 hours                                  angry_mcnulty
drguo@ubuntu:~$ sudo docker stop grafana/grafana
Error response from daemon: No such container: grafana/grafana
drguo@ubuntu:~$ sudo docker stop grafana
Error response from daemon: No such container: grafana
drguo@ubuntu:~$ sudo docker stop 870d6a3aa8f5
870d6a3aa8f5
drguo@ubuntu:~$ sudo docker stop c5e0afb1a24f
c5e0afb1a24f
drguo@ubuntu:~$ sudo docker stop aa87fa1cc5f6
aa87fa1cc5f6
drguo@ubuntu:~$ sudo docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES


## kill
drguo@ubuntu:~$ sudo docker ps
CONTAINER ID        IMAGE                COMMAND                  CREATED             STATUS              PORTS                    NAMES
3af60ea3f5b5        prom/prometheus      "/bin/prometheus --c…"   5 minutes ago       Up 5 minutes        0.0.0.0:9090->9090/tcp   great_herschel
aa87fa1cc5f6        prom/node-exporter   "/bin/node_exporter"     9 minutes ago       Up 9 minutes                                 angry_mcnulty

drguo@ubuntu:~$ sudo docker kill -s KILL 3af60ea3f5b5
3af60ea3f5b5

参考链接:

https://www.cnblogs.com/xiao987334176/p/9930517.html
https://www.cnblogs.com/chenqionghe/p/10494868.html
https://github.com/prometheus
https://www.runoob.com/docker/docker-command-manual.html

光于前裕于后 CSDN认证博客专家 数据分析 神经网络 图像处理
本人有多年大数据与机器学习开发经验,并乐于总结与分享,如有侵权或写的不对的地方可以私信我,有问题也可以问我哟,free~
©️2020 CSDN 皮肤主题: 成长之路 设计师:Amelia_0503 返回首页