Docker Monitoring with cAdvisor, Prometheus and Grafana
Docker is the open-source tool, which works like a virtual machine. However, it doesn’t create whole operating system, it has just minimum set of operating system for running application. Docker allows you to get more apps running on the server and also makes it very easy to package and ship programs.
While we have more running application, it is very important to monitor these containers performances, resource usage, etc. A container monitoring system collects metrics to ensure application running on containers are performing properly. Metrics are tracked and analyzed in real time to determine if an application is meeting expected goals.
In this article, we will cover docker monitoring by using cAdvisor, Prometheus and Grafana. These three tools are open source and easy to setup. Moreover, they can be integrated smoothly. We will setup grafana and cAdvisor using Docker container. Prometheus will run on the system.
Step-1 Install cAdvisor
cAdvisor (Container Advisor) collects, aggregates, processes and exports information about running containers. For installing cAdvisor with docker.
sudo docker run
--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 \
--name=cadvisor \
gcr.io/google-containers/cadvisor:latest \
cAdvisor is now running on?https://localhost:8080
Step-2 Install Prometheus
Prometheus is an open-source system monitoring and alerting toolkit. Prometheus is very easy to setup.?Firstly, we need to download the latest release of prometheus from?https://prometheus.io/download/?.
Extract the downloaded file and set it as working directory.
tar xvfz prometheus-*.tar.g
cd prometheus-*z
Secondly, we need to configure?prometheus.ymlbefore starting prometheus.
# global confi
global:
scrape_interval: 15s
evaluation_interval: 15s
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
- job_name: 'cadvisor'
scrape_interval: 10s
metrics_path: '/metrics'
static_configs:
- targets: ['localhost:8080']
labels:
group: 'cadvisor'g
Now, we are ready to start prometheus
./prometheus --config.file=prometheus.yml
Prometheus will start in 30 seconds. We can access prometheus main page from?https://localhost:9090/metrics
For checking configuration on prometheus, we should follow the path:?status?→?Targets
领英推荐
Configuration is done. We are ready to visualize the metrics.
Step-3 Install Grafana
Grafana is an open source metric analytics & visualization suite. It is used for visualizing time series data for infrastructure and application analytics. Grafana installation is very laborless, and we will run it using the official Docker container.
$ docker run -d -p 3000:3000 grafana/grafana
Grafana is running on port 3000 so, we will use?https://localhost:3000?for going main page of grafana.
Firstly,?we need to add data source. Click on?create your first data source?button. Prometheus is default?time series database?of grafana thus, we don’t need any data source plugin.
Write your prometheus URL.
Then, we can create our dashboard after adding data source. There are different kinds of dashboards with different data sources at grafana dashboard home page?https://grafana.com/grafana/dashboards. At this page, we will search?docker monitoring?for?prometheus?data source. This page has so many docker monitoring dashboards, but in this tutorial, we will use?docker monitoring by philicious?https://grafana.com/grafana/dashboards/193.
We will download this dashboard as json format.
At the main page of grafana; if we click on?Home?at left-top of the page,?import dashboard?option will become visible. Upload the downloaded json file from this page.
Name of dashboard will come automatically, but we have to select prometheus option. Then, importing is ready.
This dashboard shows us running containers, total memory usage, total cpu usage etc. As we can see there are 3 running containers which are cAdvisor, jenkins and grafana. If we want to evaluate our docker container usages, cAdvisor is using the highest CPU but jenkins has the highest memory usage.
I hope this article help you to monitor your docker containers.