Docker部署监控-Prometheus+Grafana

prometheus部署

Docker镜像

docker pull prom/prometheus:v2.25.1

镜像导出命令

docker save prom/prometheus:v2.25.1 -o docker-prometheus-v2.25.1.tar

镜像导入命令

docker load -i docker-prometheus-v2.25.1.tar

创建数据目录

mkdir -pv /data/prometheus/{
     conf,data}

## 配置文件

global:
  scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.

alerting:
  alertmanagers:
  - static_configs:
    - targets:
      # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090']

  - job_name: 'node_exporter'
    static_configs:
    - targets: ['3.1.101.33:9100','3.1.101.34:9100','3.1.101.35:9100']

docker-compose编排

version: "3"
services:
  prometheus:
    container_name: prometheus
    image: prom/prometheus:v2.25.1
    user: root
    ports:
      - 9090:9090
    restart: always
    volumes:
      - /data/prometheus/conf/prometheus.yml:/etc/prometheus/prometheus.yml
      - /data/prometheus/data:/prometheus
      - /etc/localtime:/etc/localtime

启动参数

/usr/local/prometheus/prometheus
–config.file=/usr/local/prometheus/prometheus.yml
–storage.tsdb.path=/data/prometheus
–storage.tsdb.retention=30d
–web.console.libraries=/usr/local/prometheus/console_libraries
–web.console.templates=/usr/local/prometheus/consoles
–web.listen-address=0.0.0.0:9090
–web.read-timeout=5m
–web.max-connections=10
–query.max-concurrency=20
–query.timeout=2m
–web.enable-lifecycle

grafana部署

Docker镜像

docker pull grafana/grafana:7.4.1

镜像导出命令

docker save grafana/grafana:7.4.1 -o docker-grafana-7.4.1.tar

镜像导入命令

docker load -i docker-grafana-7.4.1.tar

创建数据目录

mkdir -pv /data/grafana/{
     conf,data,logs,plugins}

docker-compose编排

version: "3"
services:
  grafana:
    container_name: grafana
    image: grafana/grafana:7.4.1
    user: root
    ports:
      - 3000:3000
    restart: always
    environment:
      GF_PATHS_DATA: /data/grafana/data
      GF_PATHS_LOGS: /data/grafana/logs
      GF_PATHS_PLUGINS: /data/grafana/plugins
    volumes:
      - /data/grafana:/data/grafana
      - /etc/localtime:/etc/localtime

常用模版:

linux主机: 8919

docker容器: 12781,10566

node-exporter部署

二进制部署

https://prometheus.io/

解压部署

tar -zxf node_exporter-1.1.2.linux-amd64.tar.gz
mv node_exporter-1.1.2.linux-amd64 /usr/local/node_exporter

配置service启动文件

cat > /usr/lib/systemd/system/node-exporter.service << 'EOF'
[Unit]
Description=This is prometheus node exporter
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/node_exporter/node_exporter
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

启动node-exporter

systemctl daemon-reload
systemctl restart node-exporter
systemctl enable node-exporter

Docker部署

docker-compose编排

version: "3"
services:
  node-exporter:
    container_name: node-exporter
    image: prom/node-exporter:v1.1.2
    restart: always
    network_mode: host
    command:
      - '--web.listen-address=:9100'
      - '--path.rootfs=/rootfs'
      - '--path.procfs=/host/proc'
      - '--path.sysfs=/host/sys'
      - '--collector.filesystem.ignored-mount-points=^/(sys|proc|dev|host|etc)($$|/)'
#      - '--collector.textfile.directory=/node_exporter/prom'
    volumes:
      - /proc:/host/proc
      - /sys:/host/sys
      - /:/rootfs

redis-exporter

grafana模版ID: 2751,763

二进制部署

下载地址

https://github.com/oliver006/redis_exporter/releases

解压部署

tar -zxf redis_exporter-v1.20.0.linux-amd64.tar.gz
mv redis_exporter-v1.20.0.linux-amd64 /usr/local/redis_exporter

配置service启动文件

cat > /usr/lib/systemd/system/redis-exporter.service << 'EOF'
[Unit]
Description=This is prometheus redis exporter
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/redis_exporter/redis_exporter -redis.addr 3.1.101.35:6379 -redis-only-metrics -web.listen-address 0.0.0.0:9121 -redis.password g1tredis2o2l
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

启动redis-exporter

systemctl daemon-reload
systemctl restart redis-exporter
systemctl enable redis-exporter

Docker部署

docker-compose编排

version: "3"
services:
  redis-exporter:
    container_name: redis-exporter
    image: bitnami/redis-exporter:1.20.0 
    restart: always
    ports:
      - "9121:9121"
    command: "-redis.addr 3.1.101.33:6379 -redis-only-metrics -web.listen-address 0.0.0.0:9121 -redis.password g1tredis2o2l"

elasticsearch_exporter部署

二进制部署

下载地址: https://github.com/justwatchcom/elasticsearch_exporter/releases

解压部署

tar -zxf elasticsearch_exporter-1.1.0.linux-amd64.tar.gz
mv elasticsearch_exporter-1.1.0.linux-amd64 /usr/local/elasticsearch_exporter

配置service启动文件

cat > /usr/lib/systemd/system/es-exporter.service << 'EOF'
[Unit]
Description=This is prometheus elasticsearch exporter
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/elasticsearch_exporter/elasticsearch_exporter \
         --es.all --es.indices --es.cluster_settings \
         --es.indices_settings --es.shards --es.snapshots \
         --es.timeout=10s --web.listen-address=0.0.0.0:9115 \
         --web.telemetry-path=/metrics \
         --es.uri http://3.1.101.33:9200
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

启动es-exporter

systemctl daemon-reload
systemctl restart es-exporter
systemctl enable es-exporter

grafana模版ID: 2322

Docker部署

docker-compose编排

version: "3"
services:
  es-exporter:
    container_name: es-exporter
    image: justwatch/elasticsearch_exporter:1.1.0 
    restart: always
    ports:
      - "9115:9115"
    command: "--es.all --es.indices --es.cluster_settings --es.indices_settings --es.shards --es.snapshots --es.timeout=10s --web.listen-address=0.0.0.0:9115 --web.telemetry-path=/metrics --es.uri http://3.1.101.33:9200"

kafka_exporter部署

二进制部署

下载地址: https://github.com/danielqsj/kafka_exporter/releases/

解压部署

tar -zxf kafka_exporter-1.2.0.linux-amd64.tar.gz
mv kafka_exporter-1.2.0.linux-amd64 /usr/local/kafka_exporter

配置service启动文件

cat > /usr/lib/systemd/system/kafka-exporter.service << 'EOF'
[Unit]
Description=This is prometheus kafka exporter
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/kafka_exporter/kafka_exporter --kafka.server=3.1.101.35:9092
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

启动kafka-exporter

systemctl daemon-reload
systemctl restart kafka-exporter
systemctl enable kafka-exporter

Docker部署

docker-compose编排

version: "3"
services:
  kafka-exporter:
    container_name: kafka-exporter
    image: danielqsj/kafka-exporter:v1.3.0
    restart: always
    ports:
      - "9308:9308"
    command: "--kafka.server=3.1.101.33:9092"

cAdvisor部署

常用模版ID: 13584,13946,13531

Docker镜像

docker pull google/cadvisor:v0.33.0

镜像导出命令

docker save google/cadvisor:v0.33.0 -o docker-cadvisor-v0.33.0.tar

镜像导入命令

docker load -i docker-cadvisor-v0.33.0.tar

docker-compose编排

version: "3"
services:
  cadvisor:
    container_name: cadvisor
    image: google/cadvisor:v0.33.0
    user: root
    privileged: true
    ports:
      - 9180:8080
    restart: always
    volumes:
      - /:/rootfs
      - /var/run:/var/run
      - /sys:/sys
      - /var/lib/docker/:/var/lib/docker
      - /dev/disk/:/dev/disk
docker run -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 \
--publish=8080:8080 \
--detach=true \
--privileged=true \
--name=cadvisor \
google/cadvisor:latest

四个-v挂载操作不能省,如果不挂载,将无法连接到docker deamon

你可能感兴趣的:(Docker,监控相关,prometheus,grafana,exporter)