Install Prometheus using Docker
Overview
After completing installation of Docker on Windows in the previous tutorial, will see how to download a specific image or set of images using docker pull command.
Docker Tutorial :
- Install Docker On Windows
- Docker Hello World Example
- Install Prometheus using Docker
- Install Grafana using Docker
- Install RabbitMQ using Docker
Q: What is Prometheus?
Ans:
Prometheus is a free event monitoring and alerting software application. It logs real-time metrics in a time series database built with an HTTP pull model, allowing for flexible queries and real-time alerting.
What is Docker?
Docker is a set of platform as a service tools that deliver software in containers using OS-level virtualization. Containers are self-contained, containing their own software, libraries, and configuration files, and communicating with one another via well-defined channels. Docker makes it simple for developers to package, ship, and execute any application as a lightweight, portable, self-contained container that can operate almost anywhere.
Pull an image from Docker Hub
Use docker pull to download a specific image or set of images (i.e., a repository). If no tag is specified, Docker Engine defaults to the :latest tag. This command retrieves the latest image:
Using the docker pull command, you can get the Prometheus docker image -
$ docker pull prom/prometheus
The image ID is a SHA256 digest covering the image's configuration and layers, and Docker uses a content-addressable image store.
Multiple layers can be included in a Docker image. The image in the example above has below layers as shown.
Once the image has been downloaded, use the docker image ls
command to see a list of
all the images available locally.
C:\Users>docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
prom/prometheus latest 3313ec19d029 13 days ago 189MB
docker/getting-started latest 083d7564d904 3 weeks ago 28MB
hello-world latest d1165f221234 4 months ago 13.3kB
Finally, we'll use Docker to run Prometheus. To start a Prometheus server in the background, type the following command:
$ docker run -d --name=prometheus -p 9090:9090 -v <PATH_TO_prometheus.yml_FILE>:/etc/prometheus/prometheus.yml prom/prometheus --config.file=/etc/prometheus/prometheus.yml
Ensure to replace PATH_TO_prometheus.yml_FILE
with the Path where you have kept
prometheus.yml file.
You can refer Prometheus documentation configuration from here.
NOTE: Download the prometheus.yml from the Spring Boot Prometheus Example under resources folder.
In this example, I have kept the file (prometheus.yml) at D drive. So I have replaced PATH_TO_prometheus.yml_FILE
with D:\prometheus.yml
.
However after executing the command, I encountered the error docker: Error response from
daemon: Conflict.
The container name "/prometheus" is already in use by container
C:\Users>docker run -d --name=prometheus -p 9090:9090 -v D:\prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus --config.file=/etc/prometheus/prometheus.yml
docker: Error response from daemon: Conflict.
The container name "/prometheus" is already in use by container "865ad37d121bfdb8819b5459637ff407d0bb40eb4a2fd93a739030e008ef919e".
You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.
To resolve above error use below command to Force to remove one or more containers.
docker rm -f prometheus
Now, use below docker command to run Prometheus.
docker run -d --name=prometheus -p 9090:9090 -v D:\prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus --config.file=/etc/prometheus/prometheus.yml
Visualizing Spring Boot Metrics via the Prometheus Dashboard
That's all! Now you may browse the Prometheus dashboard via http://localhost:9090.
We can verify if our connections are working and correctly configured by going to Status > Targets.
If you encounter the error Get
"http://localhost:8080/actuator/prometheus":
dial tcp 127.0.0.1:8080: connect: connection refused
in the above
status page, replace localhost with the right system IP address in the prometheus.yml file from
Spring Boot Prometheus Example.
Use ipconfig
command to get the IP address of the system.
C:\Users>ipconfig
IPv4 Address. . . . . . . . : 192.168.1.9
global:
scrape_interval: 15s # Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['127.0.0.1:9090']
- job_name: 'spring-actuator'
metrics_path: '/actuator/prometheus'
scrape_interval: 5s
static_configs:
- targets: ['192.168.1.9:8080'] # refer system ip address rather that localhost
I have used Spring Boot Prometheus Example to visualize it
in
Prometheus Dashboard.
Here are a few Prometheus graphs for our Spring Boot application's metrics.
System's CPU usage
-system_cpu_usage
Api Latency
When we start the Spring Boot Prometheus Example, and visit to http://localhost:8080/actuator/prometheus, we can see the statistics of the application.
Refer
http_server_requests_second_max{uri="/testApi"}
expression in Prometheus
graph to
see the test api latency.