577 字
3 分钟

Deploying Prometheus + Grafana with Docker

Resource Inventory##

HostIP
Prometheus + Grafana10.0.0.1
SoftwareVersion
docker20.10.12
docker-compose1.23.1
Prometheus2.18.1
Grafana7.2.1
Pushgateway1.4.2
Alertmanager0.11.0
node-exporter1.3.1

I. Installing Docker##

1. Use a domestic yum repo##

Terminal window
# yum install -y yum-utils device-mapper-persistent-data lvm2
# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

2. Remove old versions of docker##

Terminal window
## If docker is already installed on the host and it's not the desired version, remove it first.
# yum remove -y docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine \
container*

3. Install Docker 20.10##

Terminal window
# yum -y install docker-ce-20.10.12-3.el7 docker-ce-cli-20.10.12-3.el7

4. Configure registry mirror##

Terminal window
# mkdir /etc/docker
# vi /etc/docker/daemon.json
{
"registry-mirrors": ["https://xxxxxxxxx.mirror.aliyuncs.com"]
}

5. Start docker##

Terminal window
# systemctl start docker
# systemctl enable docker
# systemctl status docker

II. Installing Docker-compose##

1. Install Docker-compose##

Terminal window
## github.com may time out; you can use the following URL, download it locally, then upload it to the server
# curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# curl -k "https://dl.cactifans.com/zabbix_docker/docker-compose" -o /usr/bin/docker-compose
# chmod a+x /usr/bin/docker-compose

2. Check docker-compose version##

Terminal window
# docker-compose version

III. Install prometheus + grafana + pushgateway##

1. Get docker-compose.yaml##

Terminal window
## If you use the open source project below, you can skip this step and just use the docker-compose.yaml file provided there
# cd /opt/
# git clone https://github.com/evnsio/prom-stack.git
# cd prom-stack

2. Edit the docker-compose.yml file##

Terminal window
version: "3"
services:
pushgateway:
#image: prom/pushgateway:v0.4.0
image: prom/pushgateway:v1.4.2
container_name: pushgateway
command: --persistence.file=/pushgateway/pushgateway.data
ports:
- 9091:9091
volumes:
- /data/pushgateway:/pushgateway
prometheus:
#image: prom/prometheus:v2.0.0
image: prom/prometheus:v2.18.1
container_name: prometheus
command: --config.file=/etc/prometheus/prometheus.yml --storage.tsdb.path=/prometheus --storage.tsdb.retention=30d
user: 0:0
ports:
- 9090:9090
volumes:
- /data/prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
- /data/prometheus/alerts/:/etc/prometheus/rules.d/
- /data/prometheus/data/:/prometheus
grafana:
#image: grafana/grafana:4.6.2
image: grafana/grafana:7.2.1
container_name: grafana
ports:
- 3000:3000
volumes:
- /data/grafana:/var/lib/grafana
environment:
- GF_SECURITY_ADMIN_PASSWORD=admin
alertmanager:
image: prom/alertmanager:v0.11.0
container_name: alertmanager
command: --config.file=/etc/alertmanager/alertmanager.yml
ports:
- 9093:9093
volumes:
- /data/alertmanager/alertmanager.yml:/etc/alertmanager/alertmanager.yml
###############node-exporter###############
node-exporter:
image: "prom/node-exporter:v1.3.1"
#hostname: node-exporter
container_name: node-exporter
ports:
- '9100:9100'
volumes:
- /etc/localtime:/etc/localtime:ro
- /proc:/host/proc:ro
- /sys:/host/sys:ro
- /:/rootfs:ro
- /etc/hostname:/etc/hostname:ro
restart: always
network_mode: host
command:
- '--path.procfs=/host/proc'
- '--path.sysfs=/host/sys'
- '--path.rootfs=/rootfs'

3. Create mount directories for services##

Terminal window
# mkdir -pv /data/{pushgateway,prometheus,alertmanager,grafana}
# mkdir /data/prometheus/{alerts,data}
## Set file permissions for Grafana persistent storage
# chown 472:472 -R /data/grafana/

4. Edit configuration files##

a | prometheus.yml##

Terminal window
# vim /data/prometheus/prometheus.yml
# my global config
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.
# scrape_timeout is set to the global default (10s).
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'edocyun-prom-stack'
# Alertmanager configuration
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: 'prom-stack'
static_configs:
- targets:
- prometheus:9090
- pushgateway:9091
- alertmanager:9093
- grafana:3000
- job_name: 'node-exporter'
static_configs:
- targets:
- 10.0.0.1:9100 ## The host needs to have the node-exporter service deployed

b | alertmanager.yml##

Terminal window
# vim /data/alertmanager/alertmanager.yml
global:
smtp_smarthost: 'localhost:25'
smtp_from: '[email protected]'
smtp_auth_username: 'alertmanager'
smtp_auth_password: 'password'
route:
group_by: ['cluster']
receiver: team-a
receivers:
- name: 'team-a'
email_configs:

5. Start the services##

Terminal window
# docker-compose -f docker-compose.yml pull
# docker-compose -f docker-compose.yml up -d

Log in to Grafana with the default credentials: username admin, password admin.

a | Check service status##

Terminal window
# docker-compse ps
# docker ps -a

6. Use curl to generate test data##

  • You can use $RANDOM to generate random numbers
Terminal window
# while true; do
echo "mymetric $RANDOM" | curl --data-binary @- http://localhost:9091/metrics/job/my-push-job
sleep 1
done

“mymetric” is the metric being monitored, and pushing this metric to Pushgateway is a simple and intuitive way to test.

You should see that the test data has already been sent into Pushgateway.

Deploying Prometheus + Grafana with Docker
https://catcat.blog/en/docker-install-prometheus-grafana.html
作者
猫猫博客
发布于
2023-04-21
许可协议
CC BY-NC-SA 4.0