# DevSecOps: Deploying the 2048 Game on Docker and Kubernetes with Jenkins CI/CD


We will be using Jenkins as a CICD tool and deploying our application on a Docker container and Kubernetes Cluster. I Hope this detailed blog is useful to you all.

**GitHub Repo** :[https://github.com/SushantOps/2048-React-CICD.git](https://github.com/SushantOps/2048-React-CICD.git)

### **Steps:-**

**Step 1** — Launch an Ubuntu(22.04) T2 Large Instance

**Step 2** — Install Jenkins, Docker and Trivy. Create a SonarQube Container using Docker.

**Step 3** — Install Plugins like JDK, SonarQube Scanner, Nodejs, and OWASP Dependency Check.

**Step 4** — Create a Pipeline Project in Jenkins using a Declarative Pipeline

**Step 5** — Install OWASP Dependency Check Plugins

**Step 6** — Docker Image Build and Push

**Step 7** — Deploy the image using Docker

**Step 8** — Kubernetes master and slave setup on Ubuntu (20.04)

**Step 9** — Access the Game on Browser.

**Step 10** — Terminate the AWS EC2 Instances.

***Now, let’s get started and dig deeper into each of these steps:-***

## **Step 1:Launch an Ubuntu(22.04) T2 Large Instance**

Launch an AWS T2 Large Instance. Use the image as Ubuntu. You can create a new key pair or use an existing one. Enable HTTP and HTTPS settings in the Security Group and open all ports

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

## **Step 2 — Install Jenkins, Docker and Trivy**

### **2A — To Install Jenkins**

Connect to your console, and enter these commands to Install Jenkins

```go
vi jenkins.sh
```

```bash
#!/bin/bash
sudo apt update -y
#sudo apt upgrade -y
wget -O - https://packages.adoptium.net/artifactory/api/gpg/key/public | tee /etc/apt/keyrings/adoptium.asc
echo "deb [signed-by=/etc/apt/keyrings/adoptium.asc] https://packages.adoptium.net/artifactory/deb $(awk -F= '/^VERSION_CODENAME/{print$2}' /etc/os-release) main" | tee /etc/apt/sources.list.d/adoptium.list
sudo apt update -y
sudo apt install temurin-17-jdk -y
/usr/bin/java --version
curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
                  /usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
                  https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
                              /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update -y
sudo apt-get install jenkins -y
sudo systemctl start jenkins
sudo systemctl status jenkins
```

```bash
sudo chmod 777 jenkins.sh
./jenkins.sh    # this will installl jenkins
```

Once Jenkins is installed, you will need to go to your AWS EC2 Security Group and open Inbound Port 8080, since Jenkins works on Port 8080.

Now, grab your Public IP Address

```go
<EC2 Public IP Address:8080>
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
```

and Jenkins Getting Started Screen.

### **2B — Install Docker**

```go
sudo apt-get update
sudo apt-get install docker.io -y
sudo usermod -aG docker $USER   #my case is ubuntu
newgrp docker
sudo chmod 777 /var/run/docker.sock
```

After the docker installation, we create a SonarQube container (Remember to add 9000 ports in the security group).

```go
docker run -d --name sonar -p 9000:9000 sonarqube:lts-community
```

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

Now our SonarQube is up and running

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

Enter username and password, click on login and change password

```go
username admin
password admin
```

Update New password, This is Sonar Dashboard.

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

### **2C — Install Trivy**

```go
vi trivy.sh
```

```go
sudo apt-get install wget apt-transport-https gnupg lsb-release -y
wget -qO - https://aquasecurity.github.io/trivy-repo/deb/public.key | gpg --dearmor | sudo tee /usr/share/keyrings/trivy.gpg > /dev/null
echo "deb [signed-by=/usr/share/keyrings/trivy.gpg] https://aquasecurity.github.io/trivy-repo/deb $(lsb_release -sc) main" | sudo tee -a /etc/apt/sources.list.d/trivy.list
sudo apt-get update
sudo apt-get install trivy -y
```

Next, we will log in to Jenkins and start to configure our Pipeline in Jenkins

## **Step 3 — Install Plugins like JDK, SonarQube Scanner, NodeJS, OWASP Dependency Check**

### **3A — Install Plugin**

Go to Manage Jenkins →Plugins → Available Plugins →

Install below plugins

1 → Eclipse Temurin Installer (Install without restart)

2 → SonarQube Scanner (Install without restart)

3 → NodeJS Plugin (Install Without restart)

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

### **3B — Configure Java and Nodejs in Global Tool Configuration**

Go to Manage Jenkins → Tools → Install JDK(17) and NodeJS(16)→ Click on Apply and Save

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

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

### **3C — Create a Job**

create a job as 2048 Name, select pipeline and click on ok.

## **Step 4 — Configure Sonar Server in Manage Jenkins**

Grab the Public IP Address of your EC2 Instance, SonarQube works on Port 9000, so &lt;Public IP&gt;:9000. Go to your SonarQube Server. Click on Administration → Security → Users → Click on Tokens and Update Token → Give it a name → and click on Generate Token , Create a token with a name and generate.

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

copy Token

Go to Jenkins Dashboard → Manage Jenkins → Credentials → Add Secret Text. It should look like this

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

You will see this page once you click on create

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

Now, go to Dashboard → Manage Jenkins → System and Add like the below image.

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

Click on Apply and Save

**The Configure System option** is used in Jenkins to configure different server

**Global Tool Configuration** is used to configure different tools that we install using Plugins

We will install a sonar scanner in the tools.

![](https://miro.medium.com/v2/resize:fit:736/1*rwg8eLCaK1blfopR-EY2-A.png align="left")

In the SonarQube Dashboard add a quality gate also

Administration → Configuration →Webhooks

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

Click on Create

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

Let’s go to our Pipeline and add the script in our Pipeline Script.

```go
pipeline {
    agent any
    tools{
        jdk 'jdk17'
        nodejs 'node16'
    }
    environment {
        SCANNER_HOME=tool 'sonar-scanner'
    }

    stages {
        stage('clean workspace'){
            steps{
                cleanWs()
            }
        }
        stage('Checkout from Git'){
            steps{
                git 'https://github.com/SushantOps/2048-React-CICD.git'
            }
        }
        stage("Sonarqube Analysis "){
            steps{
                withSonarQubeEnv('sonar-server') {
                    sh ''' $SCANNER_HOME/bin/sonar-scanner -Dsonar.projectName=Game \
                    -Dsonar.projectKey=Game '''
                }
            }
        }
        stage("quality gate"){
           steps {
                script {
                    waitForQualityGate abortPipeline: false, credentialsId: 'Sonar_cred' 
                }
            } 
        }
        stage('Install Dependencies') {
            steps {
                sh "npm install"
            }
        }
    }
}
```

Click on Build now, you will see the stage view like this

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

To see the report, you can go to SonarQube Server and go to Projects.

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

You can see the report has been generated and the status shows as passed. You can see that there are 868 lines. To see a detailed report, you can go to issues.

## **Step 5 — Install OWASP Dependency Check Plugins**

Go to Dashboard → Manage Jenkins → Plugins → OWASP Dependency-Check. Click on it and install it without restart.

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

First, we configured the Plugin and next, we had to configure the Tool

Go to Dashboard → Manage Jenkins → Tools →

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

Click on Apply and Save here.

Now go configure → Pipeline and add this stage to your pipeline and build.

```go
stage('OWASP FS SCAN') {
            steps {
                dependencyCheck additionalArguments: '--scan ./ --disableYarnAudit --disableNodeAudit', odcInstallation: 'DP-Check'
                dependencyCheckPublisher pattern: '**/dependency-check-report.xml'
            }
        }
        stage('TRIVY FS SCAN') {
            steps {
                sh "trivy fs . > trivyfs.txt"
            }
        }
```

The stage view would look like this,

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

You will see that in status, a graph will also be generated and Vulnerabilities.

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

## **Step 6 — Docker Image Build and Push**

We need to install the Docker tool in our system, Go to Dashboard → Manage Plugins → Available plugins → Search for Docker and install these plugins

`Docker`

`Docker Commons`

`Docker Pipeline`

`Docker API`

`docker-build-step`

and click on install without restart

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

Now, go to Dashboard → Manage Jenkins → Tools →

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

Add Docker Hub Username and Password under Global Credentials

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

Add this stage to Pipeline Script

```go
stage("Docker Build & Push"){
            steps{
                script{
                   withDockerRegistry(credentialsId: 'docker', toolName: 'docker'){   
                       sh "docker build -t 2048 ."
                       sh "docker tag 2048 sushantkapare1717/2048:latest "
                       sh "docker push sushantkapare1717/2048:latest "
                    }
                }
            }
        }
        stage("TRIVY"){
            steps{
                sh "trivy image sushantkapare1717/2048:latest > trivy.txt" 
            }
        }
```

You will see the output below, with a dependency trend.

![](https://miro.medium.com/v2/resize:fit:736/0*KsR-Lxb0HGn9m6Qu.png align="left")

When you log in to Docker hub, you will see a new image is created

Now Run the container to see if the game coming up or not by adding below stage

```go
stage('Deploy to container'){
            steps{
                sh 'docker run -d --name 2048 -p 3000:3000 sushantkapare1717/2048:latest'
            }
        }
```

stage view

![](https://miro.medium.com/v2/resize:fit:736/0*RE7DPtM_B-v28mXv.png align="left")

```bash
<Jenkins-public-ip:3000>
```

You will get this output

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

## **Step 8 — Kubernetes Setup**

Connect your machines to Putty or Mobaxtreme

**Take-Two Ubuntu 20.04 instances one for k8s master and the other one for worker.**

Install Kubectl on Jenkins machine also.

### **Kubectl is to be installed on Jenkins also**

```bash
sudo apt update
sudo apt install curl -y
curl -LO https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
kubectl version --client
```

### **— — — — — Master Node — — — — — —**

```bash
sudo hostnamectl set-hostname K8s-Master
```

### **— — — — — Worker Node — — — — — —**

```bash
sudo hostnamectl set-hostname K8s-Worker
```

### **— — — — — — Both Master & Node — — — — —**

```bash
sudo apt-get update 
sudo apt-get install -y docker.io
sudo usermod –aG docker Ubuntu
newgrp docker
sudo chmod 777 /var/run/docker.sock
sudo curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -

sudo tee /etc/apt/sources.list.d/kubernetes.list <<EOF
deb https://apt.kubernetes.io/ kubernetes-xenial main     # 3lines same command
EOF

sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo snap install kube-apiserver
```

### **— — — — — — — — Master — — — — — — — -**

```bash
sudo kubeadm init --pod-network-cidr=10.244.0.0/16
# in case your in root exit from it and run below commands
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
```

### **— — — — — Worker Node — — — — — —**

```go
sudo kubeadm join <master-node-ip>:<master-node-port> --token <token> --discovery-token-ca-cert-hash <hash>
```

Copy the config file to Jenkins master or the local file manager and save it

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

copy it and save it in any folder save it as secret-file.txt

Note: create a secret-file.txt in your file explorer save the config in it and use this at the Kubernetes credential section.

Install Kubernetes Plugin, Once it’s installed successfully

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

go to manage Jenkins → manage credentials → Click on Jenkins global → add credentials

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

final step to deploy on the Kubernetes cluster

```go
stage('Deploy to kubernets'){
            steps{
                script{
                    withKubeConfig(caCertificate: '', clusterName: '', contextName: '', credentialsId: 'k8s', namespace: '', restrictKubeConfigAccess: false, serverUrl: '') {
                       sh 'kubectl apply -f deployment.yaml'
                  }
                }
            }
        }
```

stage view

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

In the Kubernetes cluster give this command

```bash
kubectl get all 
kubectl get svc 
```

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

## **Step 9: Access from a Web browser with browser**

```go
<public-ip-of-slave:service port>
```

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

## **Step 10: Terminate instances.**

## **Complete Pipeline**

```go
pipeline{
    agent any
    tools{
        jdk 'jdk17'
        nodejs 'node16'
    }
    environment {
        SCANNER_HOME=tool 'sonar-scanner'
    }
    stages {
        stage('clean workspace'){
            steps{
                cleanWs()
            }
        }
        stage('Checkout from Git'){
            steps{
                git branch: 'master', url: 'https://github.com/Aj7Ay/2048-React-CICD.git'
            }
        }
        stage("Sonarqube Analysis "){
            steps{
                withSonarQubeEnv('sonar-server') {
                    sh ''' $SCANNER_HOME/bin/sonar-scanner -Dsonar.projectName=Game \
                    -Dsonar.projectKey=Game '''
                }
            }
        }
        stage("quality gate"){
           steps {
                script {
                    waitForQualityGate abortPipeline: false, credentialsId: 'Sonar-token' 
                }
            } 
        }
        stage('Install Dependencies') {
            steps {
                sh "npm install"
            }
        }
        stage('OWASP FS SCAN') {
            steps {
                dependencyCheck additionalArguments: '--scan ./ --disableYarnAudit --disableNodeAudit', odcInstallation: 'DP-Check'
                dependencyCheckPublisher pattern: '**/dependency-check-report.xml'
            }
        }
        stage('TRIVY FS SCAN') {
            steps {
                sh "trivy fs . > trivyfs.txt"
            }
        }
        stage("Docker Build & Push"){
            steps{
                script{
                   withDockerRegistry(credentialsId: 'docker', toolName: 'docker'){   
                       sh "docker build -t 2048 ."
                       sh "docker tag 2048 sushantkapare1717/2048:latest "
                       sh "docker push sushantkapare1717/2048:latest "
                    }
                }
            }
        }
        stage("TRIVY"){
            steps{
                sh "trivy image sushantkapare1717/2048:latest > trivy.txt" 
            }
        }
        stage('Deploy to container'){
            steps{
                sh 'docker run -d --name 2048 -p 3000:3000 sushantkapare1717/2048:latest'
            }
        }
        stage('Deploy to kubernets'){
            steps{
                script{
                    withKubeConfig(caCertificate: '', clusterName: '', contextName: '', credentialsId: 'k8s', namespace: '', restrictKubeConfigAccess: false, serverUrl: '') {
                       sh 'kubectl apply -f deployment.yaml'
                  }
                }
            }
        }
    }
}
```

**If this post was helpful, please do follow and click the clap 👏 button below to show your support.**
