How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (2023)

This tutorial shows you how to deploy a simple container application to your Kubernetes cluster using the Jenkins CI/CD pipeline.Jenkinsis one of the most popular automation platforms for continuous integration/continuous delivery and deployment (CI/CD). It is written in Java and server based. Jenkins has helped DevOps engineers and developers automate most development workflows.

In an organization, Jenkins automates the process of building, deploying, testing, and deploying software while implementing continuous integration and continuous delivery. It ensures that the software is always up to date and that the end user enjoys new versions. They use the Jenkins multi-stage CI/CD pipeline to deploy the simple containerized application.

How Jenkins works

Jenkins implements development workflows using CI/CD pipelines. A Jenkins pipeline has multiple stages and steps to implement the CI/CD workflow. In Jenkins you use aJenkins fileto define and declare all stages of your Jenkins CI/CD pipeline. Jenkinsfile is popularly known as "pipeline as code" because it writes its CI/CD pipeline as executable code. Jenkins runs the Jenkinsfile and deploys the pipeline. The pipeline automates the stages defined in the Jenkins file. You can continue reading this Sweet Code PostJenkins Pipeline.

In this tutorial, our "Jenkinsfile" has several phases. The stages are for:

  1. Create a Docker image
  2. Pushing the Docker image to Docker Hub.
  3. Pull the Docker image from the Docker Hub repository and create a containerized application.
  4. Deploy the containerized application to the Kubernetes cluster.

Jenkins also uses plugins that allow developers to integrate various third-party software into Jenkins. Plugins help implement continuous integration/delivery and deployment. In this tutorial, you will install thestevedoreYKubernetesPlugins on the Jenkins platform.

If you use Jenkins, you must connect it to your source code management (SCM) Git repository. The repository hosts your Jenkinsfile and other application files. Jenkins uses the files in the GitHub repository to deploy the applications to the Kubernetes cluster.

requirements

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

  1. Docker-Desktop: you will usestevedoreto build and run Jenkins as a Docker container. This sweet code post will show you how to do it.Install and use Docker
  2. GitHubAccount: Uses GitHub as its source code management (SCM) Git repository. It will push your Jenkinsfile, application, and deployment files to your GitHub repository.
  3. Kubernetes Cluster: Multiple cloud-based Kubernetes clusters can host your deployed containerized application, such as:
  4. Kubectl
  5. Docker HubAccount

In this tutorial you will use theminicube. It is the most popular local Kubernetes cluster. We'll use Minikube because it's free and easy to set up on your computer. If you want to know more about Minikube, read thisArticlein sweet code

It is the command line tool interface for Kubernetes. It allows DevOps professionals to run commands and deploy Kubernetes objects to the Kubernetes cluster. Check out this Sweet Code post on how to get startedKubectl.

The Jenkins CI/CD pipeline builds the Docker image and pushes it to your Docker Hub repository. It also pulls the Docker image from the Docker Hub registry and creates a containerized application.

How to deploy to Kubernetes cluster using CI/CD Jenkins pipeline

Step 1: Run Jenkins as a Docker container

To install Jenkins, run it as a docker container with the following docker run command:

docker run --name myjenkins-container -p 8080:8080 -p 50000:50000 -v /var/jenkins_home jenkins

When you run the above command:

  • It will run the "Jenkins".official docker image.
  • Starts the Jenkins container. It will then expose it on port 8080 and the nodes on port 50000. It accesses the Jenkins container on port 8080.

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

How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (1)

From the image above, the Jenkins container is locked. You need the admin password to unlock Jenkins.

Recover the administrator password

Run the following command to get the initial admin password to unlock Jenkins:

Docker Protocols myjenkins-container

After running the command, the following initial admin password will appear in your terminal:

How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (2)

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

How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (3)

(Video) Deploy to Kubernetes Cluster | CI/CD Kubernetes using Jenkins Pipeline | Jenkins Pipeline Tutorial

Next, select "Install suggested plugins" to allow the installation of the required plugins. These are the basic plugins that Jenkins needs to run. The plugins to install are shown in the following image:

How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (4)

Create the first admin user

After installing all the basic plugins shown above, you will be redirected to another page where you can create your first admin user:

How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (5)

Fill in all the fields and click the "Save and continue" button. Then you will be redirected to another page as shown below:

How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (6)

Then click "Save and Finish" and your Jenkins platform is up and ready to go. You will be redirected to another page as shown below:

How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (7)

Finally, click on "Start using Jenkins". The "Welcome to Jenkins" panel opens:

How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (8)

You interact with the Jenkins dashboard when you manage Jenkins, create CI/CD pipelines, and perform configuration. You have completed the first step.

Step 2 – Install the Docker pipeline plugin

To install the Docker Pipeline plugin, click "Manage Jenkins":

How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (9)

Next, click on "Manage Plugins":

How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (10)

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

How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (11)

The Docker pipeline allows you to inject Docker commands into your Jenkins CI/CD pipeline scripts. Without this plugin, Jenkins does not recognize or understand Docker commands.

Step 3: Install the Kubernetes plugin

To install the Kubernetes plugin, search for Kubernetes Pipeline. Then click the "Download now and install after reboot" button:

How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (12)

(Video) CICD Pipeline To Deploy To Kubernetes Cluster Using Jenkins | Jenkins Kubernetes Integration

The Docker pipeline allows you to integrate Kubernetes with Jenkins. The Kubernetes plugin allows you to deploy your containerized application to your Kubernetes cluster using CI/CD Jenkins Pipeline.

Step 4: Add credentials to the Jenkins Credentials Manager

Add the GitHub and Docker Hub credentials to the Jenkins credentials manager. Jenkins uses Git-Hub credentials to authenticate with GitHub and Docker-Hub credentials to authenticate with Docker-Hub.

Add Docker Hub credentials

To add the Docker Hub credentials:

  1. Go back to the Jenkins dashboard and click "Manage Jenkins".How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (13)
  2. Click Manage credentials.How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (14)
  3. Click Add credentials.How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (15)
  4. Add Docker Hub Username and PasswordHow to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (16)

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

Add the Git Hub credentials

Add your GitHub username and password as shown below:

How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (17)

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

Step 5: Launch Minikube

To start Minikube, open your terminal as administrator and run the following command:

start minikube

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

How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (18)

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

Step 6 – Create a new GitHub repository

You must sign in to your personal GitHub account. Then you need to add a new GitHub repository as shown in the image below:

How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (19)

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

Step 7: Create a simple React.js application

Create a new folder on your computer called jenkins-deploy. In the jenkins-deploy folder, run the following command to create a simple React.js application:

npx create-react-app jenkins-kubernetes-deployment

The command creates a simple React.js application. Also, a new folder called jenkins-kubernetes-deployment is generated in the root jenkins-deploy folder.

Step 8: Create a Dockerfile

The Dockerfile contains commands that the Jenkins CI/CD pipeline uses to build the Docker image for the simple React.js application. Create a Dockerfile in the jenkins-kubernetes-deployment folder and paste the following Docker snippet:

# It will use node: 19-alpine3.16 as the parent image to create the docker image FROM node: 19-alpine3.16 # It will create a working directory for docker. The Docker image will be built in this working directory. install all dependencies of the React.js application RUN npm i<!-- Copy the remaining folders and files of the React.js application from the local "jenkins-kubernetes-deployment" folder to the Docker working directory "react-app" -->COPY. .#Expose the React.js application container on port 3000EXPOSE 3000#The command to start the React.js application containerCMD ["npm", "start"]
Step 9: Create a YAML file for the Kubernetes deployment

A Kubernetes deployment YAML file creates the pods for the React.js application container in the Kubernetes cluster. Pods host the application container, and each pod has the necessary Kubernetes resources. In the jenkins-kubernetes-deployment folder, create a deployment.yaml file and paste the following YAML snippet:

apiVersion: apps/v1kind: Deploymentmetadata: name: deployment #The name of the Kubernetes deployment to create on the Kubernetes cluster label: app:respond-appspec: replicas: 2 #The number of pods to deploy on the Kubernetes cluster to create React.js app container selector: match tags: app: react app template: metadata: tags: app: react app specification: container: -name: react app #The name of the image of the react.js application container: bravinwasike /react -app:latest #The Docker image to build the React.js application container ports: - containerPort: 3000 #The port for the React.js application container

The above deployment.yaml file creates two pods in the Kubernertes application. It will pull the docker image bravinwasike/react-app:latest from the docker hub repository and create a containerized app.

Step 10: Create a YAML file for the Kubernetes service deployment

A Kubernetes service implementation YAML file creates a Kubernetes service in the Kubernetes cluster. The Kubernetes service deploys the pods for the React.js application container outside of the Kubernetes cluster. It uses the Kubernetes service to access the React.js application container from outside the Kubernetes cluster.

(Video) CI CD Pipeline Using Jenkins | Deploy Docker Image to Kubernetes using Jenkins | JavaTechie

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

apiVersion: v1kind: Servicemetadata: name: service #The name of the Kubernetes service to create in the Kubernetes clusterspec: selector: app: respond-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 Jenkins file

The Jenkinsfile will have several stages to define our Jenkins CI/CD pipeline. In the jenkins-kubernetes-deployment folder, create a Jenkins file and paste the following Jenkins pipeline snippet:

Pipeline { Umgebung { Dockerimagename="bravinwasike/react-app" DockerImage="" } Any Agent Stage { Stage('Payment Source') { Steps { Git 'https://github.com/Bravinsimiyu/jenkins-kubernetes- deployment. git' } } stage('Build Image') { steps{ script { dockerImage = docker.build dockerimagename } } } stage('Push Image') { environment { registrationCredential = 'dockerhub-credentials' } steps{ script { docker.withRegistry ( 'https://registry.hub.docker.com', registrationCredential ) { dockerImage.push("latest") } } } } stage('Bereitstellen des React.js-Containers in Kubernetes') { steps { script { kubernetesDeploy (settings: "Bereitstellung.yaml", "Dienst.yaml") } } } }}

Jenkinsfile creates a Jenkins pipeline with four stages:

  1. Checkout-Quelle
  2. build image
  3. slide image
  4. Deploy the React.js container to Kubernetes
Checkout-Quellphase

This stage of the Jenkins pipeline uses https://github.com/Bravinsimiyu/jenkins-kubernetes-deployment.git as its GitHub repository. Extract and scan all the files in this GitHub repository.

Create Image Stage

This stage of the Jenkins pipeline uses the created Dockerfile to create a Docker image named bravinwasike/react-app.

Push-Image-Phase

This stage of the Jenkins pipeline pushes the bravinwasike/react-app Docker image to Docker Hub using the dockerhub credentials.

Deploy React.js containers to the Kubernetes scenario

It will pull the docker image bravinwasike/react-app:latest from the docker hub repository and create a containerized app. The React.js container is then deployed to Kubernetes.

Step 12: Push the files to your GitHub repository

To push all the application files to your GitHub repository, run the following Git commands shown below:

How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (20)

After running the Git commands, move all the React.js, Dockerfile, Jenkinsfile, deployment.yaml, and service.yaml application files to your GitHub repository:

How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (21)

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

Step 13: Create a pipeline with multiple branches
  1. Open the Jenkins Dashboard and click New Item.
  2. Enter an element name

To create a pipeline with multiple branches, complete the following steps:

  1. Open the Jenkins Dashboard and click New Item.
    How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (22)
  2. Enter an element name.
    How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (23)
  3. Scroll down and select "Multibranch Pipeline" and then click "OK".
    How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (24)
Step 14: Set up the multi-branch pipeline

To configure the multi-branch pipeline:

  1. Click Branch Sources.How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (25)
  2. Select "GitHub" from the dropdown menuHow to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (26)
  3. Add the GitHub credentials ID and the GitHub repository URLHow to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (27)
Scan the GitHub repository

Click Scan repository now to scan the added GitHub repository:

How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (28)

Scan all the files in this GitHub repository and find the "Jenkisfile". Jenkins uses this file to build the multi-branch pipeline:

How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (29)

Step 15. Create the multi-branch pipeline

To build the multi-branch pipeline, click Build Now:

How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (30)

(Video) CICD Pipeline to deploy Kubernetes Applications using Terraform, EKS, and Jenkins

Jenkins uses the Jenkinsfile to create the multi-branch pipeline stages as shown below:

How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (31)

Multiple Branch Pipe Outlet

To get multi-branch pipeline output, click Console Output:

How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (32)

It implements all the stages defined in the Jenkins file and produces the following results:

How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (33)

How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (34)

The Jenkins CI/CD pipeline issues 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: Access the deployed containerized application

It uses the Kubernetes service to access the React.js application container from outside the Kubernetes cluster. Run this command to get the Kubernetes service:

Get the kubectl service

The command generates the following Kubernetes service in your terminal:

How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (35)

Then run the following command to get the URL:

Minikube Service React App Service

The command generates the following URL:

How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (36)

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

How to Deploy an Application to Kubernetes Cluster Using Jenkins CI/CD Pipeline (37)

You can now access the React.js application you deployed through the CI/CD pipeline.

In this tutorial, you learned how to deploy an application to a Kubernetes cluster using the 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 the Jenkins credential manager. It then created the React.js application files, the Dockerfile, the Jenkinsfile, the deployment.yaml file, and the service.yaml file.

In the following steps, you push the files to the GitHub repository and set up a multi-branch pipeline. After building the Jenkins CI/CD pipeline with the Jenkis file, 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 to speed up development workflows. It can be adopted in any organization.

Videos

1. #6: [Method-1] Deploy to Kubernetes from Jenkins Pipeline | Jenkins CI/CD Pipeline with Kubernetes
(DevOps Hint)
2. End to end automation of application deployment in kubernetes using jenkins
(Linux Cloud DevOps & More by Abushad)
3. Set up complete CI/CD Jenkins pipeline for kubernetes - Part 11
(Rahul Wagh)
4. Complete Real-time Deployment on Kubernetes cluster using jenkins CI/CD Project| Introduction -01 |
(Mr. DevOps )
5. Kubernetes CI/CD | Jenkins Pipeline Tutorial | Automate Kubernetes Deployments
(Java Home Cloud)
6. [ Kube 49.1 ] Deploy to Kubernetes cluster using Jenkins CI/CD pipeline | Building with Kaniko tool
(Just me and Opensource)

References

Top Articles
Latest Posts
Article information

Author: Van Hayes

Last Updated: 25/03/2023

Views: 6517

Rating: 4.6 / 5 (66 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Van Hayes

Birthday: 1994-06-07

Address: 2004 Kling Rapid, New Destiny, MT 64658-2367

Phone: +512425013758

Job: National Farming Director

Hobby: Reading, Polo, Genealogy, amateur radio, Scouting, Stand-up comedy, Cryptography

Introduction: My name is Van Hayes, I am a thankful, friendly, smiling, calm, powerful, fine, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.