<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nigel Bomett</title>
    <description>The latest articles on DEV Community by Nigel Bomett (@nigel_bomett).</description>
    <link>https://dev.to/nigel_bomett</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1797434%2Fddd3cf78-8567-42e8-b0d2-6a61b50a1dc8.png</url>
      <title>DEV Community: Nigel Bomett</title>
      <link>https://dev.to/nigel_bomett</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nigel_bomett"/>
    <language>en</language>
    <item>
      <title>DevOps Monitoring and Automation Tool using Jenkins, Prometheus, Grafana and Docker</title>
      <dc:creator>Nigel Bomett</dc:creator>
      <pubDate>Thu, 18 Jul 2024 20:27:11 +0000</pubDate>
      <link>https://dev.to/nigel_bomett/devops-monitoring-and-automation-tool-using-jenkins-prometheus-grafana-and-docker-m8n</link>
      <guid>https://dev.to/nigel_bomett/devops-monitoring-and-automation-tool-using-jenkins-prometheus-grafana-and-docker-m8n</guid>
      <description>&lt;p&gt;In this guide, we will walk through the process of setting up a DevOps monitoring and automation tool and understanding how different tools can be used together for this purpose: Jenkins for continuous integration and continuous deployment(CI/CD), Prometheus for metrics collection, Grafana for visualization, and Docker for containerization.&lt;/p&gt;

&lt;p&gt;...   &lt;/p&gt;

&lt;h2&gt;
  
  
  Install Docker
&lt;/h2&gt;

&lt;p&gt;If you haven't already installed Docker, follow the installation instructions for your operating system from the &lt;a href="https://www.docker.com/" rel="noopener noreferrer"&gt;Docker website&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Set up Jenkins
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Run Jenkins in a Docker Container:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; ```
 docker run -u 0 --privileged --name jenkins -it -d -p 8080:8080 -p 
 50000:50000 
 \-v/var/run/docker.sock:/var/run/docker.sock 
 \-v $(which docker):/usr/bin/docker 
 \-v /home/jenkins_home:/var/jenkins_home 
 \jenkins/jenkins:latest
```
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Access and Setup Jenkins:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Open your browser and go to &lt;code&gt;http://localhost:8080/&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You will be requested to provide a password and there is a guide &lt;br&gt;
on where to find it however, in case you can not locate the file &lt;br&gt;
you can run the command: &lt;code&gt;docker logs &amp;lt;docker container id&amp;gt;&lt;/code&gt; where &lt;br&gt;
the 'docker container id' is the id of the docker container running &lt;br&gt;
Jenkins, the password will be displayed in the logs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;For this tutorial, you can select to install suggested plugins. &lt;br&gt;
We will install any other required plugins later on.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;From the dashboard add a new item, I will be using, Pipeline. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Install Plugins:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Go to "Manage Jenkins" &amp;gt; "Manage Plugins".&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the "Available" tab, search for "Docker Plugin", "Docker Pipeline &lt;br&gt;
Plugin", "Prometheus metrics Plugin" and install them. Restart Jenkins &lt;br&gt;
if prompted.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Set Up Prometheus and Grafana
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Create a Docker Network:&lt;br&gt;
&lt;code&gt;docker network create monitoring&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Run Prometheus:&lt;br&gt;
Create a &lt;code&gt;prometheus.yml&lt;/code&gt; configuration file:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'prometheus'
    static_configs:
      - targets: ['localhost:9090']

  - job_name: 'jenkins'
    static_configs:
      - targets: ['jenkins:8080']
      metrics_path: '/prometheus'

  - job_name: 'node'
    static_configs:
      - targets: ['node-exporter:9100']

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Run Prometheus with this configuration:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -d --name=prometheus 
\--network=monitoring 
\-p 9090:9090 
\-v $(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml \prom/prometheus

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Run Node Exporter:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -d --name=node-exporter 
\--network=monitoring 
\--restart=unless-stopped 
\prom/node-exporter

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Run Grafana:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;docker run -d --name=grafana 
\--network=monitoring 
\-p 3000:3000 
\grafana/grafana

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Configure Grafana
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Access Grafana&lt;br&gt;
Open your browser and go to &lt;code&gt;http:localhost:3000&lt;/code&gt;.Log in with the default username and password(&lt;code&gt;admin&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add Prometheus as a Data Source:&lt;br&gt;
Go to "Data Sources" on the left sidebar, "Add data source" and select "Prometheus".&lt;br&gt;
Enter &lt;code&gt;http://prometheus:9090&lt;/code&gt; as the Prometheus server URL.&lt;br&gt;
Click "Save &amp;amp; Test"&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create a Dashboard:&lt;br&gt;
Select "Dashboards" from the left sidebar to create a new dashboard. You can import dashboard using ID or URL. For example ID: '11159'&lt;br&gt;
You can edit one of the panels or 'add visualization', then add a query for the data you want to see. &lt;br&gt;
For this example, we will be looking at how long it takes Jenkins to process a job. Add the query:&lt;br&gt;
&lt;code&gt;default_jenkins_builds_last_build_duration_milliseconds&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Automate application deployment with Jenkins
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Set Up a Jenkins Pipeline:
From the application you had set up in Jenkins, go to "configure" and scroll down to "Pipeline Script"
The script below will guide you, you may customize to fit the application you are deploying:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pipeline {
    agent any

    environment {
        DB_HOST = 'your-db-host'
        DB_USER = 'your-db-user'
        DB_PASS = 'your-db-password'
        DB_NAME = 'your-db-name'
    }

    stages {
        stage('Checkout') {
            steps {
                script {
                    checkout([$class: 'GitSCM', branches: [[name: '*/main']],
                      doGenerateSubmoduleConfigurations: false,
                      extensions: [[$class: 'CleanCheckout']],
                      submoduleCfg: [],
                      userRemoteConfigs: [[url: 'https://github.com/yourusername/your-nodejs-typescript-api-repo.git',
                      credentialsId: 'git-credentials-id']]
                    ])
                }
            }
        }

        stage('Install Dependencies') {
            steps {
                sh 'npm install'
            }
        }

        stage('Build') {
            steps {
                sh 'npm run build'
            }
        }

        stage('Test') {
            steps {
                sh 'npm test'
            }
        }

        stage('Build Docker Image') {
            steps {
                script {
                    sh 'docker build -t your-dockerhub-username/your-nodejs-typescript-api:latest .'
                }
            }
        }

        stage('Push Docker Image') {
            steps {
                script {
                    withCredentials([usernamePassword(credentialsId: 'dockerhub-credentials', usernameVariable: 'DOCKER_USERNAME', passwordVariable: 'DOCKER_PASSWORD')]) {
                        sh 'echo $DOCKER_PASSWORD | docker login -u $DOCKER_USERNAME --password-stdin'
                    }
                    sh 'docker push your-dockerhub-username/your-nodejs-typescript-api:latest'
                }
            }
        }

        stage('Deploy') {
            steps {
                script {
                    sh '''
                    docker run -d -p 3000:3000 \
                        -e NODE_ENV=$NODE_ENV \
                        -e DB_HOST=$DB_HOST \
                        -e DB_USER=$DB_USER \
                        -e DB_PASS=$DB_PASS \
                        -e DB_NAME=$DB_NAME \
                        your-dockerhub-username/your-nodejs-typescript-api:latest
                    '''
                }
            }
        }

        stage('Monitor') {
            steps {
                script {
                    // Additional monitoring setup can be added here
                }
            }
        }
    }

    post {
        success {
            echo 'Build succeeded!'
        }
        failure {
            echo 'Build failed!'
        }
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Credentials
&lt;/h3&gt;

&lt;p&gt;If you will be pulling code from Github you will need to add credentials for git hub and also Docker.&lt;br&gt;
Go to "Manage Jenkins" &amp;gt; "Credentials" select global, then add your credentials for both Github and Docker. Take note of the id as this is what is used as reference in the Pipeline Script.&lt;/p&gt;

&lt;h3&gt;
  
  
  Build Application
&lt;/h3&gt;

&lt;p&gt;Build your application and view the metrics on the Grafana dashboard 😃.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk2v7tuil4qyrtrg66w7g.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk2v7tuil4qyrtrg66w7g.jpg" alt="Image description" width="800" height="301"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>devops</category>
      <category>jenkins</category>
      <category>docker</category>
      <category>grafana</category>
    </item>
  </channel>
</rss>
