# Deployment of Swiggy application using github actions

Today we are going to deploy the Swiggy application using github actions as a ci-cd tool in place of jenkins or circle-ci below is the completion steps for this project

## **Completion Steps →**

Step 1 → Setup Terraform and configure aws on your local machine

Step 2 → Building a simple Infrastructure from code using terraform

Step 3 → IAM Role for EC2

Step 4 →Setup github actions with ec2

Step 5 →setup sonarqube and dockerhub for github actions

Step 6→ Elastic kubernetes service or Eks cluster setup

Step 7 → build and push docker image

Step 8 →Monitering via Prmotheus and grafana

Step 9 → Destroy all the things

# **before do anything →**

1. open your terminal and make a separate folder for swiggy→mkdir swiggy
    
2. cd swiggy
    
3. clone the github repo
    
4. git clone [https://github.com/Aakibgithuber/deployment-using-github-actions.git](https://github.com/Aakibgithuber/deployment-using-github-actions.git)
    

# **Step 1 → Setup Terraform and configure aws on your local machine**

## **1\. Setup Terraform**

To install terraform copy and paste the below commands

```plaintext
sudo su
snap install terraform --classic
```

```plaintext
which terraform
```

![](https://miro.medium.com/v2/resize:fit:728/0*ZVrCIg1GxmgCbQGa.png align="left")

## **2\. Configure aws**

1. **create an IAM user**
    
2. go to your aws account and type IAM
    

![](https://miro.medium.com/v2/resize:fit:875/0*CNM3Jum2VOhesGLL.png align="left")

click on iam

**3\. click on user →create user**

![](https://miro.medium.com/v2/resize:fit:320/0*fklJuWcMRHvKgRoX.png align="left")

**4\. Give a name to your user and tick on provide user access to management console and then click on I want an IAM user option**

![](https://miro.medium.com/v2/resize:fit:875/1*Oe6K7xrUfChDe760DGRv7Q.png align="left")

5\. choose a password for your user **→click next**

6\. Attach the policies directly to your iam user → click next

**note →I will provide the administrator accesss for now but we careful while attaching the policies on your workapce**

![](https://miro.medium.com/v2/resize:fit:875/0*Go9EwYHu6LPUc2vW.png align="left")

review and create user

7\. click on **create user**

![](https://miro.medium.com/v2/resize:fit:2400/1*E80ByaZdeSKnjLjtizNw4g.png align="left")

![](https://miro.medium.com/v2/resize:fit:2400/1*Js8kcLPtVGbVRqruWNqdKA.png align="left")

8\. download your password file if it is autogenerated otherwise it is your’s choice

![](https://miro.medium.com/v2/resize:fit:875/1*d87q36jpEqPVw34HbBjQGw.png align="left")

9\. Now **click on your IAM user →security credentials**

![](https://miro.medium.com/v2/resize:fit:875/0*lnnuzKB5rrF4byjO.png align="left")

10\. scroll down to access keys and create an access keys

![](https://miro.medium.com/v2/resize:fit:875/0*jOJBfGvul0cOPT8b.png align="left")

11.**choose aws cli from the options listed**

![](https://miro.medium.com/v2/resize:fit:875/1*pBhl0jd65NuxAr1uWxpJdg.png align="left")

**12.** click next and download you csv file for username and password

![](https://miro.medium.com/v2/resize:fit:875/1*jG2K1gso7JKuK6x1PQ6z3g.png align="left")

**13.** go to your terminal and type →aws configure

**14.** Now it is ask to your access key and secret key for this open your csv file and paste the access and secret key and remain everything default

![](https://miro.medium.com/v2/resize:fit:875/0*WyuK1ZLDmeoCitIq.png align="left")

**15.** Now you are ready to configure aws from your terminal

# **Step 2 → Building a simple Infrastructure from code using terraform**

1. go to folder → cd Instance-terraform
    
2. there are three files present main.tf, script.sh , provider.tf
    
3. open the file →vim Main.tf
    

![](https://miro.medium.com/v2/resize:fit:875/1*w1D7Cdybf9dqVUdmITZ46g.png align="left")

**4\. change this section → ami = # your ami id , key\_name= #your key pair if any**

# **Now run terraform commands →**

1. **main.tf includes userdata which links script.sh file on which execution install docker,trivy,and start the sonarqube container on port 9000**
    

```plaintext
terraform init
terraform validate
terraform plan
terraform apply --auto-approve
```

![](https://miro.medium.com/v2/resize:fit:875/0*OKflZStrcamz-IPC.png align="left")

![](https://miro.medium.com/v2/resize:fit:875/0*QTz9iJEJAeD4ExOZ.png align="left")

terraform init,validate and plan output

![](https://miro.medium.com/v2/resize:fit:875/0*R_At6SbS2OuM7KBk.png align="left")

terraform apply

**Go to your aws console and checkout the ec2 instances**

![](https://miro.medium.com/v2/resize:fit:875/1*OBpgJmJjsND7gI3-pYXXnw.png align="left")

**Here we see swiggy base server instance is created by terraform with the given configuration**

# **Step 3 → IAM Role for EC2**

**Why we need IAM role for EC2 →** It is used by your ec2 instance to create EKS cluster and manage s3 bucket by applying this IAM role it gives the authenticity to your ec2 to do changes in aws account

## **1\. creating IAM role**

1. on the search bar type IAM
    

![](https://miro.medium.com/v2/resize:fit:875/0*mEy3EzRP2YCXsXZe.png align="left")

2\. click on Roles on the left side

![](https://miro.medium.com/v2/resize:fit:310/0*RgaCs8q0L6gwDy-p.png align="left")

3\. click on create role and choose EC2 from the dropdown

![](https://miro.medium.com/v2/resize:fit:875/0*tjGQeJQ5KZyUbJrt.png align="left")

4\. click on next

5\. choose **administrator access** on permission sections

![](https://miro.medium.com/v2/resize:fit:875/0*RgapUi7jYOxGTVEa.png align="left")

6\. click on next and give a name to your role

![](https://miro.medium.com/v2/resize:fit:875/1*aww_IvNxGtJMCe-J2tg94g.png align="left")

7\. click on **create role option** and your IAM role is created

## **2\. Attach IAM role with your EC2**

1. go to EC2 section
    
2. click on actions → security → modify IAM role option
    

![](https://miro.medium.com/v2/resize:fit:875/1*n-DXuiwFYab-wZkD4yVkuQ.png align="left")

3\. choose the role from dropdown and click on update IAM role

![](https://miro.medium.com/v2/resize:fit:875/1*jAeXLG6IK3P_ds7y3xsMyw.png align="left")

# **why we need IAM Role →**

Imagine you have a robot (EC2 instance) that does tasks for you in a big factory (AWS environment). Now, this robot needs to access different rooms (AWS services like S3, DynamoDB, etc.) to perform its tasks.

Here’s where IAM (Identity and Access Management) comes in:

1. **Robot Needs a Key to Enter Rooms:** The IAM Role is like giving your robot a special key. This key allows the robot to enter specific rooms (access certain AWS services). Without this key, the robot can’t get in.
    
2. **Different Keys for Different Robots:** Each robot (EC2 instance) can have its own key (IAM Role) with specific permissions. So, one robot may have a key to enter the storage room (access S3), while another robot has a key to enter the database room (access DynamoDB).
    
3. **No Need for Hardcoding Passwords:** Using IAM Roles means you don’t have to hardcode passwords (access credentials) into the robot. It’s like not writing down passwords on the robot itself. The robot just uses its key when needed.
    
4. **Easily Change Permissions:** If you want to change what a robot can do, you just change the permissions on its key (IAM Role). No need to reprogram the robot or give it a new password; just update the permissions on its key.
    
5. **Secure and Controlled Access:** IAM Roles help keep things secure. You can control exactly what each robot is allowed to do. This way, if one robot is compromised or needs a different role, you can easily adjust its permissions without affecting others.
    

**Now everything is done what we have to do is just throw some commands and build our infrastructure using terraform to run swiggy app**

# **Step 4 →Setup github actions with ec2**

1. go to your project repo of github →click on setting →Actions →Runners
    

![](https://miro.medium.com/v2/resize:fit:1219/1*pkB7T4cexa9X4GR30bOQVw.png align="left")

![](https://miro.medium.com/v2/resize:fit:450/1*8RbupQyptKXDLPm9BbZScw.png align="left")

![](https://miro.medium.com/v2/resize:fit:1250/1*VAkb0I1cInq31oTUPV5Veg.png align="left")

Click on new self hosted runner

![](https://miro.medium.com/v2/resize:fit:875/1*fBhmFITsiB3Dohuhelx7Ng.png align="left")

select the runner image as i am using linux so chose linux

![](https://miro.medium.com/v2/resize:fit:875/1*ZvuGzlx1UniU-YqZ9dg_KA.png align="left")

2\. These commands gives by the github that are used to run on your linux server

3\. go to your ec2 and connnect to it and run the following commands one by one

```plaintext
# Create a folder
mkdir actions-runner && cd actions-runner# Download the latest runner package

curl -o actions-runner-linux-x64-2.313.0.tar.gz -L https://github.com/actions/runner/releases/download/v2.313.0/actions-runner-linux-x64-2.313.0.tar.gz

# Optional: Validate the hash
echo "56910d6628b41f99d9a1c5fe9df54981ad5d8c9e42fc14899dcc177e222e71c4  actions-runner-linux-x64-2.313.0.tar.gz" | shasum -a 256 -c

# Extract the installer
tar xzf ./actions-runner-linux-x64-2.313.0.tar.gz
```

![](https://miro.medium.com/v2/resize:fit:1250/1*Unhu2fI-me_W50Rhm7tXYQ.png align="left")

```plaintext
Configure
# Create the runner and start the configuration experience
./config.sh --url https://github.com/Aakibgithuber/Swiggy-app-deployment-using-github-actions- --token AYEA4XIU5ILR2XRXF6F2DFLF2GYZW# Last step, run it!
./run.sh
```

![](https://miro.medium.com/v2/resize:fit:875/1*9yTvkKKr6qf6-tqY0JxznQ.png align="left")

# **Step 5 →setup sonarqube and dockerhub for github actions**

## **1\. Sonarqube setup →**

copy the public ip of your machine

1. **go to your browser and type →&lt;publicip&gt;:9000**
    

![](https://miro.medium.com/v2/resize:fit:875/0*k8Ve8CTAqauddJR8.png align="left")

sonarqube window open

**2.** iniatially username and password is admin

![](https://miro.medium.com/v2/resize:fit:714/0*EpXYXBV83cR1zBSN.png align="left")

**3.** update your password

![](https://miro.medium.com/v2/resize:fit:875/0*2FESjzdaor3T_6d9.png align="left")

**4\.** welcome window of Sonarqube

## **setup projects in sonarqube for github actions**

1. go to your sonarqube server
    
2. click on projects
    
3. in the name field type swiggy
    
4. click on set up
    

![](https://miro.medium.com/v2/resize:fit:434/1*f_VKZ4kcoyRUoigFGPP8kA.png align="left")

![](https://miro.medium.com/v2/resize:fit:875/1*3mnz7EUnzvPC0vW0QEp-cA.png align="left")

5\. click on github actions

![](https://miro.medium.com/v2/resize:fit:1250/1*1JDLGqgBHNQ6YZjpp33eqg.png align="left")

6\. generate a token

![](https://miro.medium.com/v2/1*1fDdcxm98e_s332C5EEcCw.png align="left")

click on generate

![](https://miro.medium.com/v2/resize:fit:784/1*dmfXu-6HGIXW2yMkR_nwhA.png align="left")

**7\. copy token and go to your github repo →setting →secretand variable →actions →and create new repository secrets**

![](https://miro.medium.com/v2/resize:fit:445/1*f-9e_wpO8Y1Bus219a9a_Q.png align="left")

![](https://miro.medium.com/v2/resize:fit:1150/1*BTM7KjhWzlFrKhCVYDolOg.png align="left")

8\. paste the name and token generated above

![](https://miro.medium.com/v2/resize:fit:1098/1*o7GNvWcly3KCQim2CSKMbw.png align="left")

![](https://miro.medium.com/v2/resize:fit:726/1*bvmnz5oSDjuIaTifzUk_1A.png align="left")

9\. create one more new secret repo and add the above info there

![](https://miro.medium.com/v2/resize:fit:875/1*DVUC8eVwC-SdrmtK4WjGFQ.png align="left")

10\. Now go back to your sonarqube and click on continue

![](https://miro.medium.com/v2/resize:fit:1250/1*oT9ueJeux9193YvzFCSf2w.png align="left")

11\. Now go to your github and create a new file as below instuctions

![](https://miro.medium.com/v2/resize:fit:303/1*baquftH71ZXuw0qWVbk_Xg.png align="left")

![](https://miro.medium.com/v2/resize:fit:808/1*h9fMdwCr_Q_qLS9HtL1AmA.png align="left")

![](https://miro.medium.com/v2/resize:fit:1000/1*4y66eFM0SkAAVp2qxlhOpw.png align="left")

12\. Name the file as given above and enter the data as shown

![](https://miro.medium.com/v2/resize:fit:735/1*VOrEuj2qEWdZHj110spcfw.png align="left")

13\. create one more file according to above instructions

![](https://miro.medium.com/v2/resize:fit:551/1*5x6raDpYAvoHVdVyMZCdcA.png align="left")

14\. enter the above data in your file

![](https://miro.medium.com/v2/resize:fit:1250/1*HrOaBk7DxEz7O_zocuxWMg.png align="left")

15\. commit changes and go to your ec2 and start a runner by command

```plaintext
./run.sh
```

![](https://miro.medium.com/v2/resize:fit:611/1*-0hmY-UujIhkDenVyC89Cg.png align="left")

16\. Now go to Actions and see your build.yaml is automatically triggerd

![](https://miro.medium.com/v2/resize:fit:875/1*6L5eL4RxueyxOd5_GgTu7Q.png align="left")

![](https://miro.medium.com/v2/resize:fit:2263/1*-I1_XEBH6YlmRX51o7YZCw.png align="left")

![](https://miro.medium.com/v2/resize:fit:2400/1*-6DreV00zQMoOqb0SzWHYA.png align="left")

17\. checkout the sonarqube

![](https://miro.medium.com/v2/resize:fit:875/1*2BRfskQhhuX9ysclLU0XEQ.png align="left")

## **2\. setup dockerhub for github actions**

1. go to your dockerhub account and create a token under myaccount →setting →Security
    

![](https://miro.medium.com/v2/resize:fit:594/1*GC5XNzblC0xbnKNprcAQdw.png align="left")

![](https://miro.medium.com/v2/resize:fit:443/1*aGRevItmqTElSiJ2Hgv2rQ.png align="left")

![](https://miro.medium.com/v2/resize:fit:875/1*f6S-c21hv2fYTOMidOyghw.png align="left")

create a new access token

![](https://miro.medium.com/v2/resize:fit:875/1*dXzAX62o6vh4T3BCoJSong.png align="left")

2\. Now again go to your github account → setting →Secrets and variables →actions →create a new repo secret

![](https://miro.medium.com/v2/resize:fit:875/1*8wBY2gqDzYAR18tdF8U1nA.png align="left")

paste your token here

3\. create a one more secret of your username

![](https://miro.medium.com/v2/resize:fit:875/1*66u9ZFl0_Y-3EylIHRnOgA.png align="left")

![](https://miro.medium.com/v2/resize:fit:875/1*qvywVZ20oE9-fgmOeNdA3w.png align="left")

Now you are connected with your dockerhub via github

# **Step 6→ Elastic kubernetes service or Eks cluster setup**

1. Go to your ec2 and run the following command
    

```plaintext
aws configure
```

2\. Provide the access key and secret key setup above

![](https://miro.medium.com/v2/resize:fit:875/1*jG2K1gso7JKuK6x1PQ6z3g.png align="left")

![](https://miro.medium.com/v2/resize:fit:875/1*41DvJspqnrqiSitM1gEOXQ.png align="left")

3\. fetch the git hub repo of EKS terraform folder

```plaintext
git clone https://github.com/Aakibgithuber/deployment-using-github-actions.git
```

4\. go to EKS terraform folder by

```plaintext
cd deployment-using-github-actions/Eks-terraform
ls
```

![](https://miro.medium.com/v2/resize:fit:875/1*yuk4FLRawG_YlpjRQjGJ-w.png align="left")

5\. Now Setup your EKS cluster by the below terraform commands

```plaintext
terraform init
terraform validate
terraform plan
terraform apply
```

6\. It takes 5 to 10 min to create a cluster

![](https://miro.medium.com/v2/resize:fit:875/1*eZR3unPfgCqc2JLIS4SENg.png align="left")

![](https://miro.medium.com/v2/resize:fit:875/1*gsRgh9ssKhPVBLYojCAQKw.png align="left")

Your cluster is ready

# **Step 7 → build and push docker image**

1. Now go to your github workflows folder and edit the build.yaml file
    

add the following code

```plaintext
name: Build

on:
  push:
    branches:
      - master

jobs:
  build:
    name: Build
    runs-on: [self-hosted]
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0  # Shallow clones should be disabled for a better relevancy of analysis
      - uses: sonarsource/sonarqube-scan-action@master
        env:
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
          SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
      - name: NPM Install
        run: npm install
        
      - name: Docker build and push
        run: |
          # Run commands to build and push Docker images
          docker build -t swiggy-clone .
          docker tag swiggy-clone aakibkhan1212/swiggy-clone:latest
          docker login -u ${{ secrets.Dockerhub_username }} -p ${{ secrets.Dockerhub_token }}
          docker push aakibkhan1212/swiggy-clone:latest
        env:
          DOCKER_CLI_ACI: 1
```

**Importent note → make sure to change the dockerhub repo name and tag of your image**

![](https://miro.medium.com/v2/resize:fit:875/1*mklnLWcM9vO3i8z7gzevtQ.png align="left")

2\. Preview and commit the changes

![](https://miro.medium.com/v2/resize:fit:875/1*UNksqOFmKteMUjaDd75vtw.png align="left")

3\. Go to actions →build to see the changes

![](https://miro.medium.com/v2/resize:fit:875/1*vZz2Ed3hgS21fTvjw6pA5Q.png align="left")

Docker image is created and pushed to your dockerhub

# **Step 7 →Deployment on kubernetes**

1. Go to your repo and make changes on build.yml file and add the following code
    

```plaintext
deploy:
   needs: build
   runs-on: [self-hosted]
   steps:
      - name: docker pull image
        run: docker pull aakibkhan1212/swiggy-clone:latest
      - name: Image scan
        run: trivy image aakibkhan1212/swiggy-clone:latest > trivyimagedeploy.txt
      - name: Deploy to container
        run: docker run -d --name swiggy-clone -p 3000:3000 aakibkhan1212/swiggy-clone:latest
```

![](https://miro.medium.com/v2/resize:fit:875/1*q0OiiXfILYJvar-LJnLFdQ.png align="left")

2\. Above code runs the application locally

![](https://miro.medium.com/v2/resize:fit:875/1*62bLEe4HbyIMjlgF3k8f6A.png align="left")

3\. To run it on kubernetes cluster add the following code in your build.yml

```plaintext
- name: Update kubeconfig
        run: aws eks --region ap-south-1 update-kubeconfig --name EKS_CLOUD
      - name: Deploy to kubernetes
        run: kubectl apply -f deployment-service.yml2. commit the changes and this will deploy the image locally on the base server
```

![](https://miro.medium.com/v2/resize:fit:875/1*9qb3UYWgDDgZqClE_I8kxA.png align="left")

Deployment is also completed check the runner on ec2

![](https://miro.medium.com/v2/resize:fit:771/1*djQPf-RWzq6QtDmaqyDAtw.png align="left")

6\. run the following command

```plaintext
kubectl get all 
```

![](https://miro.medium.com/v2/resize:fit:875/1*yC63cCX6nUGhRxipncD1Mg.png align="left")

7\. copy the load balancer ingress and paste it on browser

![](https://miro.medium.com/v2/resize:fit:875/1*5h-t4_XX_uPW5asMwizeEg.png align="left")

**Your Swiggy app is Now deployed on the Kubernetes cluster**

8\. Complete code for build.yaml file

```plaintext
name: Build

on:
  push:
    branches:
      - master

jobs:
  build:
    name: Build
    runs-on: [self-hosted]
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0  # Shallow clones should be disabled for a better relevancy of analysis
      - uses: sonarsource/sonarqube-scan-action@master
        env:
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
          SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
      - name: NPM Install
        run: npm install
        
      - name: Docker build and push
        run: |
          # Run commands to build and push Docker images
          docker build -t swiggy-clone .
          docker tag swiggy-clone aakibkhan1212/swiggy-clone:latest
          docker login -u ${{ secrets.Dockerhub_username }} -p ${{ secrets.Dockerhub_token }}
          docker push aakibkhan1212/swiggy-clone:latest
        env:
          DOCKER_CLI_ACI: 1
  deploy:
   needs: build
   runs-on: [self-hosted]
   steps:
      - name: docker pull image
        run: docker pull aakibkhan1212/swiggy-clone:latest
      - name: Image scan
        run: trivy image aakibkhan1212/swiggy-clone:latest > trivyimagedeploy.txt
      - name: Deploy to container
        run: docker run -d --name swiggy-clone1 -p 3000:3000 aakibkhan1212/swiggy-clone:latest
      - name: Update kubeconfig
        run: aws eks --region ap-south-1 update-kubeconfig --name EKS_CLOUD
      - name: Deploy to kubernetes
        run: kubectl apply -f deployment-service.yml
```

**Make changes in the above code according to your github repo and dockerhub account**

# **Step7 →Monitering via Prmotheus and grafana**

* **Prometheus** is like a detective that constantly watches your software and gathers data about how it’s performing. It’s good at collecting metrics, like how fast your software is running or how many users are visiting your website.
    
* **Grafana**, on the other hand, is like a dashboard designer. It takes all the data collected by Prometheus and turns it into easy-to-read charts and graphs. This helps you see how well your software is doing at a glance and spot any problems quickly.
    

In other words, Prometheus collects the information, and Grafana makes it look pretty and understandable so you can make decisions about your software. They’re often used together to monitor and manage applications and infrastructure.

# **1\. Setup another server or EC2 for moniterning**

1. go to ec2 console and launch an instance having a base image ofu buntu and with t2.medium specs because **Minimum Requirements to Install Prometheus :**
    

* 2 CPU cores.
    
* 4 GB of memory.
    
* 20 GB of free disk space.
    

# **2\. Installing Prometheus:**

1. **First, create a dedicated Linux user for Prometheus and download Prometheus:**
    

```plaintext
sudo useradd — system — no-create-home — shell /bin/false prometheus
wget https://github.com/prometheus/prometheus/releases/download/v2.47.1/prometheus-2.47.1.linux-amd64.tar.gz
```

**2\. Extract Prometheus files, move them, and create directories:**

```plaintext
tar -xvf prometheus-2.47.1.linux-amd64.tar.gz
cd prometheus-2.47.1.linux-amd64/
sudo mkdir -p /data /etc/prometheus
sudo mv prometheus promtool /usr/local/bin/
sudo mv consoles/ console_libraries/ /etc/prometheus/
sudo mv prometheus.yml /etc/prometheus/prometheus.yml
```

**3\. Set ownership for directories:**

```plaintext
useradd prometheus
sudo chown -R prometheus:prometheus /etc/prometheus/ /data/
```

**4\. Create a systemd unit configuration file for Prometheus:**

```plaintext
sudo nano /etc/systemd/system/prometheus.service
```

Add the following code to the `prometheus.service` file:

```plaintext
[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target
```

```plaintext
StartLimitIntervalSec=500
StartLimitBurst=5[Service]
User=prometheus
Group=prometheus
Type=simple
Restart=on-failure
RestartSec=5s
ExecStart=/usr/local/bin/prometheus \
  --config.file=/etc/prometheus/prometheus.yml \
  --storage.tsdb.path=/data \
  --web.console.templates=/etc/prometheus/consoles \
  --web.console.libraries=/etc/prometheus/console_libraries \
  --web.listen-address=0.0.0.0:9090 \
  --web.enable-lifecycle[Install]
WantedBy=multi-user.target
```

**b.** press →**ctrl+o** #for save and then **ctrl+x** #for exit from the file

Here’s a explanation of the key parts in this above file:

* `User` and `Group` specify the Linux user and group under which Prometheus will run.
    
* `ExecStart` is where you specify the Prometheus binary path, the location of the configuration file (`prometheus.yml`), the storage directory, and other settings.
    
* `web.listen-address` configures Prometheus to listen on all network interfaces on port 9090.
    
* `web.enable-lifecycle` allows for management of Prometheus through API calls.
    

**5\. Enable and start Prometheus:**

```plaintext
sudo systemctl enable prometheus
sudo systemctl start prometheus
```

```plaintext
sudo systemctl status prometheus
```

![](https://miro.medium.com/v2/resize:fit:875/0*p3j-nugz-05_otV8.png align="left")

**Now go to your security group of your ec2 to enable port 9090 in which prometheus will run**

go to → [http://public\_ip:9090](http://public_ip:9090/) to see the webpage of prometheus

![](https://miro.medium.com/v2/resize:fit:875/0*Q45QrzJ7VxZDU16U.png align="left")

# **3\. Installing Node Exporter:**

Node exporter is like a “reporter” tool for Prometheus, which helps collect and provide information about a computer (node) so Prometheus can monitor it. It gathers data about things like CPU usage, memory, disk space, and network activity on that computer.

A Node Port Exporter is a specific kind of Node Exporter that is used to collect information about network ports on a computer. It tells Prometheus which network ports are open and what kind of data is going in and out of those ports. This information is useful for monitoring network-related activities and can help you ensure that your applications and services are running smoothly and securely.

Run the following commands for installation

1. **Create a system user for Node Exporter and download Node Exporter:**
    

```plaintext
sudo useradd — system — no-create-home — shell /bin/false node_exporter
```

```plaintext
wget https://github.com/prometheus/node_exporter/releases/download/v1.6.1/node_exporter-1.6.1.linux-amd64.tar.gz
```

**2\. Extract Node Exporter files, move the binary, and clean up:**

```plaintext
tar -xvf node_exporter-1.6.1.linux-amd64.tar.gz
sudo mv node_exporter-1.6.1.linux-amd64/node_exporter /usr/local/bin/
rm -rf node_exporter*
```

**3\. Create a systemd unit configuration file for Node Exporter:**

```plaintext
sudo nano /etc/systemd/system/node_exporter.service
```

add the following code to the `node_exporter.service` file:

provide more detailed information about what might be going wrong. For example:

```plaintext
[Unit]
Description=Node Exporter
After=network.target
```

```plaintext
[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/local/bin/node_exporter[Install]
WantedBy=default.target
```

**4\. Enable and start Node Exporter:**

```plaintext
sudo useradd -m -s /bin/bash node_exporter
```

```plaintext
sudo chown node_exporter:node_exporter /usr/local/bin/node_exporter
```

```plaintext
sudo systemctl daemon-reload
sudo systemctl start node_exporter
sudo systemctl enable node_exporter
sudo systemctl status node_exporter
```

**node exporter service is now running**

![](https://miro.medium.com/v2/resize:fit:875/0*_RezB4OV2R3FLYzT.png align="left")

You can access Node Exporter metrics in Prometheus.

![](https://miro.medium.com/v2/resize:fit:875/0*VUrPoFvoxBDnqIVT.png align="left")

publicip:9100

# **5\. Configure Prometheus Plugin Integration:**

1. go to your EC2 and run →
    

```plaintext
cd /etc/prometheus
```

2\. you have to edit the prometheus.yml file to moniter anything

```plaintext
scrape_configs:
  - job_name: 'node_exporter'
    static_configs:
      - targets: ['localhost:9100']
  - job_name: 'jenkins'
    metrics_path: '/prometheus'
    static_configs:
      - targets: ['<your-jenkins-ip>:<your-jenkins-port>']
```

**add the above code with proper indentation like this →**

![](https://miro.medium.com/v2/resize:fit:875/0*lp5GuQ95QCM7tAsF.png align="left")

press esc+:wq to save and exit

**a. Check the validity of the configuration file →**

```plaintext
promtool check config /etc/prometheus/prometheus.yml
```

***o/p →success***

**b.** Reload the Prometheus configuration without restarting →

**curl -X POST** [**http://localhost:9090/-/reload**](http://localhost:9090/-/reload)

**go to your prometheus tab again and click on status and select targets you will there is three targets present as we enter in yaml file for moniterning**

![](https://miro.medium.com/v2/resize:fit:875/0*qEXiQXPSeRvPjhXW.png align="left")

prometheus targets dashboard

# **5\. Setup Grafana**

**Install Dependencies:**

```plaintext
sudo apt-get update
sudo apt-get install -y apt-transport-https software-properties-common
```

**Add the GPG Key for Grafana:**

```plaintext
wget -q -O —  https://packages.grafana.com/gpg.key | sudo apt-key add -
```

**Add the repository for Grafana stable releases:**

```plaintext
echo “deb https://packages.grafana.com/oss/deb stable main” | sudo tee — a /etc/apt/sources.list.d/grafana.list
```

**Update the package list , install and start Grafana:**

```plaintext
sudo apt-get update
sudo apt-get -y install grafana
```

```plaintext
sudo systemctl enable grafana-server
```

```plaintext
sudo systemctl start grafana-server
```

```plaintext
sudo systemctl status grafana-server
```

![](https://miro.medium.com/v2/resize:fit:875/0*F4Ga1Ff4-8GAKq8O.png align="left")

**Now go to your ec2 security group and open port no. 3000 in which grafana runs**

![](https://miro.medium.com/v2/resize:fit:875/0*gikNOXz5PbMSrTGS.png align="left")

Go and browse [http://public\_ip:3000](http://public_ip:3000/) to access your grafana web interface

![](https://miro.medium.com/v2/resize:fit:875/0*skvxP-lfzwMSuhY4.png align="left")

**username = admin, password =admin**

# **6\. Add Prometheus Data Source:**

To visualize metrics, you need to add a data source.

**Follow these steps:**

* Click on the gear icon (⚙️) in the left sidebar to open the “Configuration” menu.
    
* Select “Data Sources.”
    
* Click on the “Add data source” button.
    

![](https://miro.medium.com/v2/resize:fit:875/0*w44kOkSggDQFNZt_.png align="left")

* Choose “Prometheus” as the data source type.
    
* In the “HTTP” section:
    
* Set the “URL” to `http://localhost:9090` (assuming Prometheus is running on the same server).
    
* Click the “Save & Test” button to ensure the data source is working.
    

# **7\. Import a Dashboard**

Importing a dashboard in Grafana is like using a ready-made template to quickly create a set of charts and graphs for monitoring your data, without having to build them from scratch.

* Click on the “+” (plus) icon in the left sidebar to open the “Create” menu.
    

![](https://miro.medium.com/v2/resize:fit:371/0*wEqXCvihMnA7_xEw.png align="left")

* Select “Dashboard.”
    
* Click on the “Import” dashboard option.
    
* Enter the dashboard code you want to import (e.g., code 1860).
    
* Click the “Load” button.
    

![](https://miro.medium.com/v2/resize:fit:875/0*RMxya0jS_nWw_ZhN.png align="left")

* Select the data source you added (Prometheus) from the dropdown.
    
* Click on the “Import” button.
    

![](https://miro.medium.com/v2/resize:fit:875/0*gMpMGG6VLcvFc1U3.png align="left")

![](https://miro.medium.com/v2/resize:fit:875/0*jmVncgP8mNJBHar9.png align="left")

# **Step 8 → Destroy all the things**

1. go to your ec2 and run
    

```plaintext
terraform destroy
```

![](https://miro.medium.com/v2/resize:fit:875/0*bXRM78fsJq7g8npY.png align="left")

2\. It will destroy your kubernetes cluster and node group too

3\. go to your terminal and again run terraform destroy

![](https://miro.medium.com/v2/resize:fit:875/1*9AbalVr19qRHybFuXFitQQ.png align="left")

4\. all the things are destroyed and your project is also done

where we use github actions as ci-cd tool
