Abstract: This paper mainly introduces the monitoring scheme of Grafana + Promethrus + CAdvisor based on Docker Swarm. If the monitoring scheme is based on Docker, only need to change the configuration of Docker-stack.yml.
Grafana
configuration
Create the grafana folder and create docker-stack.yml under that folder
version: "3"
services:
cadvisor:
image: grafana/grafana:latest
ports:
- 3000:3000
networks:
- huzhihui
deploy:
replicas: 1
restart_policy:
condition: on-failure
placement:
constraints:
- node.role == manager
networks:
huzhihui:
external: true
Copy the code
Start grafana run the docker stack deploy -c docker-stack.yml grafana command to deploy grafana
prometheus
configuration
Create a Prometheus folder, and then create a conf folder under that folder to save the configuration and create docker-stack.yml
prometheus.yml
Configuration files (inconf
The file below)
scrape_configs: # Make Prometheus scrape itself for metrics. - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] - job_name: 'docker' static_configs: - targets: ['192.168.137.130:8080','192.168.137.131:8080'] -job_name: 'springboot' metrics_path: '/actuator/prometheus' static_configs: - targets: ['192.168.137.2:8080'] # Create a job for Docker Swarm containers. -job_name: 'dockerswarm' dockerswarm_sd_configs: - host: unix:///var/run/docker.sock role: tasks relabel_configs: # Only keep containers that should be running. - source_labels: [__meta_dockerswarm_task_desired_state] regex: running action: keep # Only keep containers that have a `prometheus-job` label. - source_labels: [__meta_dockerswarm_service_label_prometheus_job] regex: .+ action: keep # Use the prometheus-job Swarm label as Prometheus job label. - source_labels: [__meta_dockerswarm_service_label_prometheus_job] target_label: jobCopy the code
- create
docker-stack.yml
file
version: "3"
services:
prometheus:
image: prom/prometheus
ports:
- "9090:9090"
volumes:
- ./conf/:/etc/prometheus/
networks:
- huzhihui
deploy:
replicas: 1
restart_policy:
condition: on-failure
placement:
constraints:
- node.role == manager
networks:
huzhihui:
external: true
Copy the code
Start Prometheus Run the docker stack deploy -c docker-stack.yml Prometheus command
cadvisor
configuration
Create the CAdvisor folder and then create docker-stack.yml
version: "3"
services:
cadvisor:
image: google/cadvisor
volumes:
- /var/run/docker.sock:/var/run/docker.sock,ro
- /:/rootfs,ro
- /var/run:/var/run
- /sys:/sys,ro
- /var/lib/docker:/var/lib/docker,ro
command:
- '-docker_only'
ports:
- 8080:8080
networks:
- huzhihui
deploy:
mode: global
restart_policy:
condition: on-failure
networks:
huzhihui:
external: true
Copy the code
Start the CAdvisor. Run the docker stack deploy -c docker-stack.yml cAdvisor command
Integrated description
Open grafana to configure the data source and log in first. The default user name and password are admin
New data source
prometheus
Data source configuration for
The following uses the Springboot project as an example. For other monitoring, the following configuration is added to the configuration file
Springboot project configuration
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency>Copy the code
Application. Yml Added the configuration
management:
endpoints:
web:
exposure:
include: "*"
Copy the code
Start the service, add springboot project monitoring chart, recommended to use this grafana.com/grafana/das…
It needs to be modified a little bit to be compatible with this chart
Modify to the following
After saving, you can display the monitoring screen