How to Deploy a Containerized Application to Kubernetes Cluster using Jenkins CI/CD Pipeline
Tags: devops, aws, kubernetes, docker
Published: 2023-06-02 11:58:09

This tutorial will teach you how to deploy a simple containerized application to the Kubernetes cluster using Jenkins CI/CD Pipeline. [Jenkins](https://www.jenkins.io/) is one of the most popular continuous integration/continuous delivery and deployment (CI/CD) automation platforms. Jenkins is written in Java and is server based. Jenkins has helped DevOps engineers and developers automate most of the development workflows. 

In an organization, Jenkins automates the building, staging, testing, and deploying of software while implementing continuous integration and continuous delivery. It ensures the software is always up to date and the end user enjoys the new releases. We will use Jenkins CI/CD Pipeline with multiple stages to deploy our simple containerized application. 
that will
### How Jenkins Works
Jenkins implements the development workflows using CI/CD pipelines. A Jenkins pipeline has multiple stages and steps for implementing the CI/CD workflow. In Jenkins, you use a [Jenkinsfile](https://www.jenkins.io/doc/book/pipeline/jenkinsfile/) to define and declare all the stages for your Jenkins CI/CD Pipeline. `Jenkinsfile` is popularly known as "pipeline as code" since you write your CI/CD pipeline as executable code. Jenkins will execute the `Jenkinsfile` and implement the pipeline. The pipeline will automate the stages defined in the Jenkinsfile.

In this tutorial, our `Jenkinsfile` will have multiple stages. The stages will be for:
- Building a Docker image
- Pushing the Docker image to Docker Hub.
- Pulling the Docker image from the Docker Hub repository and creating a containerized application.
- Deploy the containerized application to the Kubernetes cluster. 

Jenkins also uses plugins that enable developers to integrate different third-party software with Jenkins. Plugins help in implementing continuous integration/delivery and deployment. In this tutorial, we will install the [Docker](https://www.docker.com/) and [Kubernetes](https://kubernetes.io/) plugins in the Jenkins platform.

When you are using Jenkins, you have to connect it to your Source Code Management (SCM) Git repository. The repository will host your `Jenkinsfile` and other application files. Jenkins will use the files in the GitHub repository to deploy the applications to the Kubernetes cluster.

Before you start working on this project, you need to have the following:

1. [Docker Desktop](https://www.docker.com/products/docker-desktop/): 
We will use [Docker](https://www.docker.com/) to build and run Jenkins as a Docker Container.

2. [GitHub](https://github.com/) account:
You will use GitHub as your Source Code Management (SCM) Git repository. You will push your Jenkinsfile, application, and deployment files to your GitHub repository. 

3. [Kubernetes Cluster](https://www.vmware.com/topics/glossary/content/kubernetes-cluster.html)
Various cloud-based Kubernetes clusters can host your deployed containerized application such as:
1. [Amazon EKS cloud cluster](https://aws.amazon.com/eks/)
2. [DigitalOcean Kubernetes](https://docs.digitalocean.com/products/kubernetes/)
3. [IBM Cloud Kubernetes service]()
4. [Microsoft Azure Kubernetes Service](https://azure.microsoft.com/en-us/products/kubernetes-service/)
5. [OpenShift Kubernetes](https://cloud.redhat.com/products/container-platform)
5. [Linode Kubernetes Engine](https://www.linode.com/docs/guides/kubernetes/)
6. [Google Kubernetes Engine](https://cloud.google.com/kubernetes-engine)
7. [Civo Kubernetes](https://www.civo.com/)

In this tutorial, we will use the [Minikube]((https://minikube.sigs.k8s.io/docs/start/)). It is the most popular local Kubernetes cluster. We will use Minikube since it's free to use and easy to set up on your computer. If you want to understand more about Minikube, read this [article](https://sweetcode.io/how-to-deploy-a-flask-application-to-a-kubernetes-cluster/).

3. [Kubectl](https://kubernetes.io/docs/tasks/tools/)
It is the command line tool interface for Kubernetes. It allows DevOps practitioners to run commands and deploy Kubernetes objects to the Kubernetes cluster. 

4. [Docker Hub](https://hub.docker.com/) account
The Jenkins CI/CD Pipeline will build the Docker image and push it to your Docker Hub repository. It will also pull the Docker image from the Docker Hub registry and create a containerized application.

### How to Deploy to Kubernetes Cluster using CI/CD Jenkins Pipeline
To deploy to Kubernetes Cluster using CI/CD Jenkins Pipeline, you will implement the following steps:

1. Run Jenkins as a Docker Container
2. Install Docker Pipeline Plugin
3. Install Kubernetes Plugin
4. Add Credentials to Jenkins Credentials Manager
5. Start Minikube
6. Create a new GitHub Repository
7. Create a Simple React.js application
8. Create a Dockerfile
9. Create a Kubernetes Deployment YAML file
10. Create a Kubernetes Service Deployment YAML file
11. Create a Jenkinsfile
12. Push the files to your GitHub repository
13. Create a Multi-branch Pipeline
14. Configure the Muliti-branch Pipeline
15. Build the Muliti-branch Pipeline
16. Accessing the deployed containerized application

Let's start working on these steps.

### Step 1: Run Jenkins as a Docker Container
To install Jenkins, you will run it as a Docker container using the following Docker run command:

```docker
docker run --name myjenkins-container -p 8080:8080 -p 50000:50000 -v /var/jenkins_home jenkins
```
When you execute the command above:
- It will run the `jenkins` [official Docker image](https://hub.docker.com/_/jenkins).
- It will start the Jenkins container. It will then expose it on port 8080 and the nodes on port 50000. You will access the Jenkins container on port 8080.  

Open your web browser and type localhost:8080 to access the Jenkins application:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9c246cvgt7gpigrfg53t.png)

From the image above, the Jenkins container is locked. We will require the administrator password to unlock Jenkins.

#### Getting the administrator password
To get the initial administrator password for unlocking Jenkins, run the following command:

```docker
docker logs myjenkins-container
```
After executing the command, the following initial administrator password will be displayed on your terminal:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2gjgpcmoxbdynxp0440l.png)

Next, copy the initial administrator password and paste it into the password field. After unlocking Jenkins using the initial administrator password, you will be redirected to another page as shown below:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r9ea8wy8b1uyctcsw5tz.png)

Next, select `Install suggested plugins` to allow the installation of the necessary plugins. These are the basic plugins that Jenkins requires to run. The plugins to be installed are shown in the image below:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xu7ozb6ca1jmxir27to9.png)

#### Create First Admin User
After installing all the basic plugins shown above, you will be redirected to another page for you to create your first admin user:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n58makds7k76m8blbn6w.png)

You will fill in all the fields and the click `Save and Continue` button. You will then be redirected to another page as shown below:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wt41znscso003bml6e3l.png)

Next, click the `Save and Finish` and your Jenkins platform will be running and ready to use. You will be redirected to another page as shown below:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z92e0eeh9ktu53na0dyz.png)

Finally, click the `Start using Jenkins`. It will open the "Welcome to Jenkins" Dashboard:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/glg8j64rmmnfab30ayub.png)

You will interact with the Jenkins Dashboard when managing Jenkins, creating CI/CD pipelines, and making configurations. We have completed the first step.

### Step 2: Install Docker Pipeline Plugin
To install Docker Pipeline Plugin, click `Manage Jenkins` as shown below:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ya0nea1a8p1p1wlp5xa1.png)

Next, click `Manage Plugins` as shown below:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gqekdehnk46eypic5oxm.png)

Next, search for Docker Pipeline. Then click the `Download now and install after restart button:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n13jjooglg5tuoe3588o.png)

The Docker pipeline will enable you to add Docker commands in your Jenkins CI/CD pipeline scripts. Without this plugin, Jenkins will not recognize and understand Docker commands.

### Step 3: Install Kubernetes Plugin
To install the Kubernetes Plugin, you will search for Kubernetes Pipeline. Then click the `Download now and install after restart button: 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hop57w98nzkeiee8bxt5.png)

The Docker pipeline will enable you to integrate Kubernetes with Jenkins. With the Kubernetes plugin, you can deploy the containerized application to the Kubernetes cluster using CI/CD Jenkins Pipeline.

### Step 4: Add Credentials to Jenkins Credentials Manager
You will add the GitHub and Docker Hub credentials to the Jenkins Credentials manager. Jenkins will use Git Hub credentials to authenticate to GitHub and Docker Hub credentials to authenticate to Docker Hub.

#### Adding the Docker Hub Credentials
To add the Docker Hub credentials:

Step 1: Go back to the Jenkins Dashboard and click `Manage Jenkins`


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pccdzwmld5sft75r3tgr.png)

Step 2: Click "Manage Credentials"


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/csfj8rfgsgeqbd2ippaa.png)

Step 3: Click `Add Credentials`


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9t3widla40q0mgbg0wav.png)

Step 4: Add Docker Hub username and password


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nm4yvmgyu5wzy0b1ke5u.png)

After adding all the information, click the `Create` button.

#### Adding the Git Hub Credentials
You will add your GitHub username and password as shown below:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sfqtioqyogayad3whpk0.png)

After adding all the information, click the `Create` button. You will have created the two credentials that Jenkins will use for authentication (logging into Git Hub and Docker Hub.)  

### Step 5: Start Minikube
To start Minikube, open your terminal as admin and run the following command:

```minikube
minikube start
```
It will take a while to start Minikube. After a few minutes, Minikube will start running on your local machine and it is ready for use as shown below:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3mezfiuu2vfrfu2j4iwj.png)

Both our Jenkins and Kubernetes environments are ready. The next step is to create new a GitHub repository.

### Step 6: Create a new GitHub Repository
You will be required to login into your personal GitHub account. You will then a new GitHub repository as shown in the image below:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7b7gbvdawktvs793c00b.png)

You will push your Jenkinsfile, application files, and deployment files to the new GitHub repository. Let's start working on the application.

### Step 7: Create a Simple React.js application
In your computer, create a new folder named `jenkins-deploy`. In the `jenkins-deploy` folder, run the following command to create a simple React.js application:

```npx
npx create-react-app jenkins-kubernetes-deployment
```
The command will create a simple React.js application. It will also generate a new folder named `jenkins-kubernetes-deployment` inside the  `jenkins-deploy` root folder.

### Step 8: Create a Dockerfile
The Dockerfile will contain commands that the Jenkins CI/CD pipeline will use to build the Docker image for the simple React.js application. In the `jenkins-kubernetes-deployment` folder, create a Dockerfile and paste the following Docker snippet:

```docker
#It will use node:19-alpine3.16 as the parent image for building the Docker image
FROM node:19-alpine3.16

#It will create a working directory for Docker. The Docker image will be created in this working directory.
WORKDIR /react-app

#Copy the React.js application dependencies from the package.json to the react-app working directory.
COPY package.json .

COPY package-lock.json .

#install all the React.js application dependencies
RUN npm i

<!-- Copy the remaining React.js application folders and files from the `jenkins-kubernetes-deployment` local folder to the Docker react-app working directory -->
COPY . .

#Expose the React.js application container on port 3000
EXPOSE 3000

#The command to start the React.js application container
CMD ["npm", "start"]
```

### Step 9: Create a Kubernetes Deployment YAML file
A Kubernetes Deployment YAML file creates the pods for the React.js application container in the Kubernetes cluster. The pods will host the application container, and each pod will have the necessary Kubernetes resources. In the `jenkins-kubernetes-deployment` folder, create a `deployment.yaml` file and paste the following YAML snippet:

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment #The name of the Kubernetes Deployment to be created in the Kubernetes cluster
  labels:
    app: react-app
spec:
  replicas: 2 #The number of pods to be created in the Kubernetes cluster for the React.js application container
  selector:
    matchLabels:
      app: react-app
  template:
    metadata:
      labels:
        app: react-app 
    spec:
      containers:
      - name: react-app #The name of the react.js application container
        image: bravinwasike/react-app:latest #The Docker image for building the React.js application container
        ports:
        - containerPort: 3000 #The port for the React.js application container
```
The `deployment.yaml` file above will create two pods in the Kubernertes application. It will pull `bravinwasike/react-app:latest` Docker image from the Docker Hub repository and create a containerized application.

### Step 10: Create a Kubernetes Service Deployment YAML file
A Kubernetes Service Deployment YAML file will create a Kubernetes Service in the Kubernetes cluster. The Kubernetes Service will expose the pods for the React.js application container outside the Kubernetes cluster. You will use the Kubernetes Service to access the React.js application container from outside the Kubernetes cluster. 

In the `jenkins-kubernetes-deployment` folder, create a `service.yaml` file and paste the following YAML snippet:

```yaml
apiVersion: v1
kind: Service
metadata:
  name: service #The name of the Kubernetes Service to be created in the Kubernetes cluster
spec:
  selector:
    app: react-app 
  type: LoadBalancer #Type of the Kubernetes Service
  ports:
  - protocol: TCP
    port: 3000 #Service port
    targetPort: 3000 #The port for the React.js application container
```
### Step 11: Create a Jenkinsfile
The `Jenkinsfile` will have multiple stages for defining our CI/CD Jenkins Pipeline. In the `jenkins-kubernetes-deployment` folder, create a `Jenkinsfile` and paste the following Jenkins pipeline snippet:

```jenkins
pipeline {

  environment {
    dockerimagename = "bravinwasike/react-app"
    dockerImage = ""
  }

  agent any

  stages {

    stage('Checkout Source') {
      steps {
        git 'https://github.com/Bravinsimiyu/jenkins-kubernetes-deployment.git'
      }
    }

    stage('Build image') {
      steps{
        script {
          dockerImage = docker.build dockerimagename
        }
      }
    }

    stage('Pushing Image') {
      environment {
               registryCredential = 'dockerhub-credentials'
           }
      steps{
        script {
          docker.withRegistry( 'https://registry.hub.docker.com', registryCredential ) {
            dockerImage.push("latest")
          }
        }
      }
    }

    stage('Deploying React.js container to Kubernetes') {
      steps {
        script {
          kubernetesDeploy(configs: "deployment.yaml", "service.yaml")
        }
      }
    }

  }

}
```

The Jenkinsfile will create a Jenkins Pipeline with four stages:
- Checkout Source
- Build image
- Pushing Image
- Deploying React.js container to Kubernetes

#### Checkout Source Stage
This Jenkins Pipeline stage will use `https://github.com/Bravinsimiyu/jenkins-kubernetes-deployment.git` as the GitHub repository. It will pull and scan all the files in this GitHub repository.

#### Build Image Stage
This Jenkins Pipeline stage will use the created Dockerfile to build a Docker image named `bravinwasike/react-app`.

#### Pushing Image Stage
This Jenkins Pipeline stage will push the `bravinwasike/react-app` Docker image to Docker Hub using the `dockerhub-credentials`

#### Deploying React.js container to Kubernetes Stage
It will pull `bravinwasike/react-app:latest` Docker image from the Docker Hub repository and create a containerized application. It will then deploy the React.js container to Kubernetes.

### Step 12: Push the Files to your GitHub Repository
The push all the application files to your GitHub repository, run the following Git commands shown below:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8mtb5ulw3l511l84uttl.png)

After running the Git commands, you will push all the React.js application files, the `Dockerfile`, the `Jenkinsfile`, the `deployment.yaml` file, and the `service.yaml` file to your GitHub Repository:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/di85ryxg6fotjkf5d2ad.png)

All our files are ready, Let's create a multi-branch pipeline in the Jenkins platform.

### Step 13: Create a Multi-branch Pipeline
To Create a Multi-branch Pipeline, follow the steps below:

Step 1: Open the Jenkins Dashboard and Click `New Item`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3xhclrtlblqb4r5v3jy4.png)

Step 2: Enter an item name


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rphwem5y4z7kjyvf7rfj.png)

Step 3: Scroll down and Select the `Multibranch Pipeline` then click `OK`


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l34ic2jyypp5d8v70cih.png)

### Step 14: Configure the Multi-branch Pipeline
To configure the Multi-branch Pipeline:

Step 1: Click `Branch Sources`:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cxgcofkbi6v8qdj5vi8z.png)

Step 2: Select `GitHub` from the dropdown menu


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4idi1973gfimo3o4smj2.png)

Step 3: Add the GitHub credentials ID and the GitHub repository URL


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3e1kcll79wo2v9rvncqc.png)

#### Scan the GitHub Repository
You will click `Scan Repository Now` to scan the GitHub Repository you have added:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bcjc0vp9kcu9zuflpycp.png)

It will scan all the files in this GitHub repository and find the `Jenkisfile`. Jenkins will use this file to build the Multi-branch pipeline:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uuq66udlxgi85nowpdnb.png)

### Step 15. Build the Muliti-branch Pipeline
To build the Multi-branch Pipeline, click "Build Now":


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h1w316l2ocsd6a98ljvj.png)

Jenkins will use the Jenkinsfile to build the Multi-branch pipeline stages as shown below:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7lek2alwx1b4qf9fkif6.png)

#### Muliti-branch Pipeline Output
To get the Muliti-branch Pipeline output, click "Console Ouput":

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i8c30g9ldgb94o42ica4.png)

It will implement all the stages defined in the Jenkinsfile and produce the following outputs:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s99x441d9bvlw8aas3pu.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zdz81pxqczckedprugwl.png)

The Jenkins CI/CD pipeline outputs a `SUCCESS` message. The Jenkins CI/CD pipeline was able to: 
- Build the Docker image.
- Push the Docker image to Docker Hub.
- Pull the Docker image from the Docker Hub repository and create a containerized application.
- Deploy the containerized application to the Kubernetes cluster. 

### Step 16: Accessing the Deployed Containerized Application
You will use the Kubernetes Service to access the React.js application container from outside the Kubernetes cluster. To get the Kubernetes Service, run this command:

```kubectl
kubectl get service
```
The command will output the following Kubernetes Service in your terminal:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a20xkaawyt12r0v0lcis.png)

You will then run the following command to get the URL:

```minikube
minikube service react-app-service
```
The command will output the following URL:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b5qgtur22zsg42zj1onu.png)

Copy the URL and paste it into your browser to access the deployed containerized application (React.js application):


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ufx1vy9585hdhwpjomfw.png)

You can now access the React.js application that you have deployed using the CI/CD pipeline.

### Conclusion
In this tutorial, you have learned how to deploy to the Kubernetes cluster using Jenkins CI/CD Pipeline. This tutorial covers how Jenkins works and how to run Jenkins as a Docker Container. After running the Jenkins container you added credentials to Jenkins Credentials Manager. You then created the React.js application files, the `Dockerfile`, the `Jenkinsfile`, the `deployment.yaml` file, and the `service.yaml` file.

In the next steps, you pushed the files to the GitHub repository and configured a multi-branch Pipeline. After building the Jenkins CI/CD pipeline using the Jenkisfile, you accessed the deployed containerized application. You used the Kubernetes Service to access the React.js application. The Jenkins CI/CD pipeline deployed the containerized application to the Kubernetes cluster. Jenkins is the best way of accelerating the development workflows. It can be adopted in any organization.

If you like this tutorial, let's connect on [Twitter](https://twitter.com/wasike_bravin) and [LinkedIn](https://www.linkedin.com/in/bravin-wasike-976755198/) for more insightful DevOps articles, tips and discussions.