Prometheus+cAdvisor container monitoring
Introduction
Prometheus
官网Prometheus - Monitoring system & time series database
The second project of the CNCF Cloud Native Foundation
cAdvisor
cAdvisor (Container Advisor) is a container monitoring tool open sourced by Google, which can be used to monitor the usage and performance of container resources. It runs as a daemon and is used to collect, aggregate, process and export information about running containers. Specifically, for each container, this component records its resource isolation parameters, historical resource usage, histograms of complete historical resource usage, and network statistics.
cAdvisor itself supports Docker containers, and also provides support for other types of containers as much as possible, striving to be compatible and adaptable to all types of containers.
The officially recommended software for collecting data for Prometheus can obtain the resource usage of the host and the resource usage of the container.
Build ideas
The redis container is used as the monitored container, Cadvisor collects it, and Prometheus is used as the data source.
Build Prometheus+cAdvisor monitoring container
1. Pull the Prometheus image
[root@docker harbor]# docker pull prom/prometheus
2. Start the prometheus container
[root@docker harbor]# docker run -d -p9090:9090 --name myprom-1 prom/prometheus
3.Web page access
4. Copy the Prometheus configuration file from the container, place it under /myprom/, and stop running prometheus to prevent subsequent port occupation when the Prometheus container is enabled.
[root@docker harbor]# mkdir /myprom
[root@docker harbor]# cd /myprom/
[root@docker myprom]# ls
[root@docker myprom]# docker cp myprom-1:/etc/prometheus/prometheus.yml .
Successfully copied 2.56kB to /myprom/.
[root@docker myprom]# ls
prometheus.yml
[root@docker myprom]# docker stop myprom-1
5. Modify the prometheus.yml configuration file and add the monitored container cAdvisor as the target container
[root@docker myprom]# vim prometheus.yml
global:
alerting:
alertmanagers:
- static_configs:
- targets:
rule_files:
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
- job_name: cadvisor
scrape_interval: 5s
static_configs:
- targets:
- cadvisor:8080
6. Use docker compose to start redis, Prometheus, and cadvisor
[root@docker myprom]# vim docker-compose.yml
version: '3.2'
services:
prometheus:
image: prom/prometheus:latest
container_name: prometheus
ports:
- 9090:9090
command:
- --config.file=/etc/prometheus/prometheus.yml
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml:ro
depends_on:
- cadvisor
cadvisor:
image: gcr.io/cadvisor/cadvisor:latest
container_name: cadvisor
ports:
- 8080:8080
volumes:
- /:/rootfs:ro
- /var/run:/var/run:rw
- /sys:/sys:ro
- /var/lib/docker/:/var/lib/docker:ro
depends_on:
- redis
redis:
image: redis:latest
container_name: redis
ports:
- 6379:6379
Since the cadvisor image cannot be downloaded in China, you need to prepare it yourself.
/var/lib/docker/:/var/lib/docker:ro All docker data is mounted to cadvisor, so cadvisor can monitor the container
7. Upload the prepared cadvisor image to /myprom in Linux and import the image
[root@docker myprom]# ls
cadvisor.tar docker-compose.yml prometheus.yml
[root@docker myprom]# docker load -i cadvisor.tar
8. Start docker compose
[root@docker myprom]# docker compose up -d
[root@docker myprom]# docker ps
Prometheus, cadvisor, and redis have all been started successfully
If an error is reported, it is likely that the previously started Prometheus occupies port 9090. Just docker stop to stop the previous container.
9. Web page access cadvisor, prometheus
Build successfully!