Revolutionize Your Jenkins Workflow with Prometheus and Grafana Monitoring
Prometheus is a time series database used for collecting metrics from an application and we visualize it in form of charts and graphs on grafana.
Check out my blog on Docker in case you are having trouble with it.
/ What do you mean by monitoring and why is it needed?
A typical system is usually architected as a microservice. What happens if suddenly you wake up someday and see that your authentication service is down? Naturally, you rush to fix it. Now, in a microservice environment the issue could be in the same service or the service authService depends on. It could take hours to find the bug and fix it just by looking at the code and individual logs of your servers. Very Difficult, right? To avoid these situations and fix our systems at super speed we incorporate amonitoring system in our application.
Metrics, Logs and Traces
Metrics are numerical data of a system . Logs are collection of logs at a single place from all the services, Traces provide visibility into how requests behave as they move throughout the entire system
Jenkins Monitoring
Let’s start monitoring jenkins, a ci/cd tool. Since running jenkins requires a server, monitoring it is one of the tasks as a Devops Engineer.
Setting up Prometheus Grafana and Jenkins
Lets Start with writing our docker-compose.yaml file
in our root directory or you can clone the git repository for the configurations
version: "3"
services:
grafana:
image: grafana/grafana
container_name: grafana
user: "0:0"
ports:
- 3000:3000
volumes:
- ./data/grafana:/var/lib/grafana
prom:
image: prom/prometheus
container_name: prometheus
ports:
- 9090:9090
volumes:
- ./config/prometheus.yml:/etc/prometheus/prometheus.yml
node-exporter:
image: prom/node-exporter:latest
container_name: node-exporter
ports:
- 9100:9100
jenkins:
image: jenkins/jenkins:latest
container_name: jenkins
ports:
- 8080:8080
- 50000:50000
volumes:
- ./jenkins:/var/jenkins_home
Create a config/prometheus.yml file next
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: "codelab-monitor"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ["localhost:9090"]
- job_name: "node_exporter"
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ["host.docker.internal:9100"]
- job_name: "jenkins"
metrics_path: /prometheus
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ["host.docker.internal:8080"]
Now run docker-compose up -d
and wait till all 4 containers are running
Visit Prometheus on localhost:9090. Navigate to Status > Targets
Next, visit jenkins on localhost:8080, you would be prompted with a password screen, open you IDE, navigate to root directory, a jenkins folder should have been created , open jenkins/secrets/initialAdminpassword
and copy paste password in you browser.
Click on install suggested plugins, continue to the Jenkins Dashboard.
Next navigate to manage Jenkins/plugins/available Plugins. Search Prometheus and install the plugin Prometheus metrics
Restart Jenkins if required and after installing the plugin restart or refresh prometheus and Jenkins will be up and running inside prometheus
Grafana
- Visit localhost:3000/ -> both username and password should be admin
- Click on Add your first data source
- Select Prometheus
- Go to terminal and and type docker inspect prometheus | grep IPAddress
- Copy the IPAdress (eg: 172.9.0.4)
- Go back to browser and and type the promethes server URL (eg: http://172.18.0.4:9090 )
- Scroll down and select save and test
8. Go back to home and click on create new dashboard > Add Visualization> Add data source(prometheus)
9. Select metrics and create your illustrations
Conclusion
In this article, we talked monitoring your jenkins application using prometheus and grafana
Share this article with your peers. Reach out to me on GitHub or LinkedIn for any queries.
Drop me a line in the comments if I’ve made any mistakes or could be helpful in anyway.
Thank You.