577 字
3 分钟
Deploying Prometheus + Grafana with Docker
Resource Inventory#
| Host | IP |
|---|---|
| Prometheus + Grafana | 10.0.0.1 |
| Software | Version |
|---|---|
| docker | 20.10.12 |
| docker-compose | 1.23.1 |
| Prometheus | 2.18.1 |
| Grafana | 7.2.1 |
| Pushgateway | 1.4.2 |
| Alertmanager | 0.11.0 |
| node-exporter | 1.3.1 |
I. Installing Docker#
1. Use a domestic yum repo#
# 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.repo2. Remove old versions of docker#
## 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#
# yum -y install docker-ce-20.10.12-3.el7 docker-ce-cli-20.10.12-3.el74. Configure registry mirror#
# mkdir /etc/docker# vi /etc/docker/daemon.json
{ "registry-mirrors": ["https://xxxxxxxxx.mirror.aliyuncs.com"]}5. Start docker#
# systemctl start docker# systemctl enable docker# systemctl status dockerII. Installing Docker-compose#
1. Install Docker-compose#
## 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-compose2. Check docker-compose version#
# docker-compose versionIII. Install prometheus + grafana + pushgateway#
1. Get docker-compose.yaml#
## 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-stack2. Edit the docker-compose.yml file#
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#
# 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#
# vim /data/prometheus/prometheus.yml
# my global configglobal: 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 configurationalerting: 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 deployedb | alertmanager.yml#
# vim /data/alertmanager/alertmanager.yml
global: smtp_smarthost: 'localhost:25' 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#
# docker-compose -f docker-compose.yml pull# docker-compose -f docker-compose.yml up -dLog in to Grafana with the default credentials: username
admin, passwordadmin.
a | Check service status#
# docker-compse ps
# docker ps -a6. Use curl to generate test data#
- You can use $RANDOM to generate random numbers
# while true; do echo "mymetric $RANDOM" | curl --data-binary @- http://localhost:9091/metrics/job/my-push-job sleep 1done“mymetric” is the metric being monitored, and pushing this metric to Pushgateway is a simple and intuitive way to test.
- Open Pushgateway in your browser: “http://10.0.0.1:9091/”
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