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:
- Create a Docker image
- Pushing 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.
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:
- 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
- GitHubAccount: Uses GitHub as its source code management (SCM) Git repository. It will push your Jenkinsfile, application, and deployment files to your GitHub repository.
- Kubernetes Cluster: Multiple cloud-based Kubernetes clusters can host your deployed containerized application, such as:
- Kubectl
- 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:
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:
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:
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:
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:
Fill in all the fields and click the "Save and continue" button. Then you will be redirected to another page as shown below:
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:
Finally, click on "Start using Jenkins". The "Welcome to Jenkins" panel opens:
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":
Next, click on "Manage Plugins":
Next, search for Docker Pipeline. Then click the "Download now and install after reboot" button:
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:
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:
- Go back to the Jenkins dashboard and click "Manage Jenkins".
- Click Manage credentials.
- Click Add credentials.
- Add Docker Hub Username and Password
After adding all the information, click the "Create" button.
Add the Git Hub credentials
Add your GitHub username and password as shown below:
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:
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:
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.
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:
- Checkout-Quelle
- build image
- slide image
- 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:
After running the Git commands, move all the React.js, Dockerfile, Jenkinsfile, deployment.yaml, and service.yaml application files to your GitHub repository:
All our files are ready. Let's create a multi-branch pipeline on the Jenkins platform.
Step 13: Create a pipeline with multiple branches
- Open the Jenkins Dashboard and click New Item.
- Enter an element name
To create a pipeline with multiple branches, complete the following steps:
- Open the Jenkins Dashboard and click New Item.
- Enter an element name.
- Scroll down and select "Multibranch Pipeline" and then click "OK".
Step 14: Set up the multi-branch pipeline
To configure the multi-branch pipeline:
- Click Branch Sources.
- Select "GitHub" from the dropdown menu
- Add the GitHub credentials ID and the GitHub repository URL
Scan the GitHub repository
Click Scan repository now to scan the added GitHub repository:
Scan all the files in this GitHub repository and find the "Jenkisfile". Jenkins uses this file to build the multi-branch pipeline:
Step 15. Create the multi-branch pipeline
To build the multi-branch pipeline, click Build Now:
Jenkins uses the Jenkinsfile to create the multi-branch pipeline stages as shown below:
Multiple Branch Pipe Outlet
To get multi-branch pipeline output, click Console Output:
It implements all the stages defined in the Jenkins file and produces the following results:
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:
Then run the following command to get the URL:
Minikube Service React App Service
The command generates the following URL:
Copy and paste the URL into your browser to access the deployed containerized app (React.js app):
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.