Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor

Harbor Privat…

How to Build and Deploy Application on


Kubernetes with CI/CD Pipeline Using Jenkins,
Docker, Harbor Private Repository, and
ArgoCD
Tanmay Bhandge · Follow
7 min read · May 19, 2024

Listen Share More

CI/CD pipelines are necessary to effectively produce high-quality software in today’s


software development environment. This article explains how to set up a continuous
integration/delivery pipeline (CI/CD) using Jenkins, Docker images, the Harbour
private repository, Git, and ArgoCD. This will generate images seamlessly, push
them to the harbor, and then have them automatically deploy to the Kubernetes
pods.

Getting Started
This is the general workflow

1. Docker File Creation: Intially, a Docker file needs to be created. In this file, a
DevOps engineer specifies the environment and dependencies needed for the
application.

2. Commit Code to the Repository: A repository, like Git, stores the source code and
the Docker file. I’m going to store the repository on GitHub.

3. Jenkins Pulls the Code: We can launch the Jenkins job to pull the most recent
code from the source code repository in order to begin the build process.

4. Building and Pushing the Docker Image: Jenkins uses the Docker file to build a
Docker image. Jenkins pushes the completed image to the Harbour private

https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-d… 1/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…

repository. You can look at here to learn how to build the Harbour private
repository.

5. Updating the Deployment Repository: Jenkins makes changes to the deployment


file repository, which contains the Kubernetes deployment configurations needed
for the application.

6. Syncing with ArgoCD: To ensure everything is current, ArgoCD, a Kubernetes


continuous delivery tool, retrieves the most recent updates from the deployment
source.

7. Deploying to Kubernetes: Lastly, ArgoCD ensures everything functions


configured by syncing these modifications with the Kubernetes cluster and
deploying the application into the designated pods.

The Workflow

CI/CD Pipeline Using GitHub, Jenkins, Harbor, Argo CD, and Kubernetes

Implementation
Tools needed
Kubernetes Cluster

Docker

Jenkins
Open in app
Harbor
Search
ArgoCD

GitHub

https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-d… 2/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…

1. Create the Docker File


We will be using the DockerFile mentioned below

It is using the below index.html file.

https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-d… 3/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…

You can find all the files in the GitHub repository below:
https://github.com/tanmaybhandge/Harbor_CICD_Pipeline.git

2. Trigger Jenkins Jobs


We have two jobs configured on Jenkins.

The first job (Upsteam job) involves cloning a GitHub repository to obtain the latest
source code. From this code, a Docker image will be created. Then it will build the
image, logs into a Harbor private registry, pushes the image, and tags it with the
build number of the upstream job.

After the image is successfully pushed, a downstream job will be start by the
upstream job. The downstream job will receive the build number as a parameter,

https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-d… 4/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…

which will have the build number. Then downstream job will modify the image
version same as build number and will push the changes on the Kubernetes
deployment file.

What is the Upstram and Downstream job means ?

When you have one Jenkins job that starts another, the job that initiates the process
is called the upstream job, while the job that gets started as a result is known as the
downstream job. In the below example Build_Docker_Image_Push_Harbor is the
upstream job and push_image_tag_git is the downstream job.

Job: Build_Docker_Image_Push_Harbor

Create the Pipeline job Build_Docker_Image_Push_Harbor and paste the following


script.

This Jenkins pipeline script automates the process of building a Docker image from
a GitHub repository, pushing the image to a Harbor repository, and triggering
another Jenkins job push_image_tag_git to modify the deployment file on GitHub
and commit the changes.

pipeline {
agent any

stages {
stage('Build') {
steps {
// Get some code from a GitHub repository
git branch: 'main', url: 'https://github.com/tanmaybhandge/Harb
sh 'docker build -t library/harbor_cicd_v2 .'

}
stage('Push to Harbor') {
environment {
DOCKER_CREDENTIALS = credentials('Harbor')
}
steps {
script {
// Login to Harbor using credentials
sh "docker login -u ${DOCKER_CREDENTIALS_USR} -p ${DOCKER_C

// Tag the image


sh 'docker tag library/harbor_cicd_v2 10.1.1.1/library/harb

https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-d… 5/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…

// Push the image to Harbor


sh 'docker push 10.1.1.1/library/harbor_cicd:v${BUILD_NUMBE
}
}
}
stage('Trigger GitHub Push') {
steps {
build job: 'push_image_tag_git', wait: true, parameters: [strin
}
}

}
}

There is no checkbox selected on the job Build_Docker_Image_Push_Harbor you just


need to paste the above script into the Pipeline script.

Pipeline script

Stages
Here is the explanation of each stage

Build

Git Checkout: Clones the repository from the specified URL and checks out the
‘main’ branch.

https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-d… 6/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…

Docker Build: Builds a Docker image from the Dockerfile in the repository,
tagging it as library/harbor_cicd_v2 .

Push to Harbor

Environment Variables: Fetches Docker credentials stored in Jenkins using the


ID ‘Harbor’. I have already created the Harbor credentials in Jenkins (default
credentials of Harbor: Username admin , Password Harbor12345 )

Docker Login: Logs into the Harbor registry using the retrieved credentials.

Docker Tag: Tags the built Docker image with a new tag that includes the Jenkins
build number. We will use the build number as the tag of the current pipeline.

Docker Push: Pushes the tagged Docker image to the specified Harbor
repository.

Trigger GitHub Push

Trigger Another Job: Initiates the push_image_tag_git job.

Pass Parameters: Sends the current build number to the triggered job.
Job: Push_Image_Tag_Git
This job will modify the Kubernetes deployment YAML file hosted on GitHub to
include the new image tag pushed by the upstream job. To achieve this, you’ll need
the following:

Parameterized Trigger Plugin

GitHub Plugin

GitHub credentials with push access permission

Here is the configuration of the job

Create the Freestryle Project with the name push_image_tag_git

A. Ensure the option This project is parameterised is selected, and then proceed to
configure the string parameter accordingly.

https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-d… 7/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…

B. In the Source Code Managment section, select Git, paste the repository URL, and
choose the appropriate credentials which has the permission/access to your GitHub.
For the Branches to Build section, specify your branch. I’m using the default main

branch.

C. Add Execute Shell on the Build Steps and paste the following.

https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-d… 8/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…

Build Steps

sed -i "s#image: 10.1.1.1/library/harbor_cicd:v.*#image: 10.1.1.1/library/harbo


cat Deployment/deployment.yaml
git config --global user.email "tanmay8928@gmail.com"
git config --global user.name "Tanmay"
git add .
git commit -m "Deployment file modified by Jenkins job with the image Harbor im

This script locates the image tag in the Deployment.yaml file, replaces it with the
specified version from the Jenkins build (${Build_Number_Image}), configures the
email and name for Git commits, adds all modified files to the Git staging area, and
commits the changes with a descriptive message.

D. In the Post-Build Actions section, select ‘Push Only If Build Succeeds’ Then,
specify the branch name and the target remote name under the ‘Branches’ field. I
am specifying branch name as main and the target remote name as origin

https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-d… 9/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…

3. Trigger the Build Docker job.


Manually trigger the Build_Docker_Image_Push_Harbor it will perform the below
actions and it will trigger the Push_image_Tag_Git job. Both of the jobs will perform
the below

Build_Docker_Image_Push_Harbor

Build Stage: Clones a GitHub repository and builds a Docker image.

Push to Harbor Stage: Logs into a Harbor registry, tags the image with the build
number, and pushes it.

Trigger GitHub Push Stage: Triggers downstream job Push_image_Tag_Git and

passes the build number as a parameter.

Push_image_Tag_Git

Execute Shell: It will locate the image tag in the Deployment.yaml file, replace it
with the specified version from the previous Jenkins build, configures the email
and name for Git commits, adds all modified files to the Git staging area, and
commits the changes with a descriptive message.

Here is the output of the Status

https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-… 10/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…

To ensure synchronization between the Docker image build and the Kubernetes
Deployment, the version specified in the Deployment.yaml file will be updated to
reflect the build number generated during the Build_Docker_Image_Push_Harbor

4. Re-create Pods with a newer Version of Image


I’ve set up ArgoCD application to keep a close eye on deployment.yaml file, making
sure everything stays in sync and up to date on the Kubernetes Cluster. So whenever
there’s a tweak in our deployment.yaml, ArgoCD jumps right in, ensuring
Kubernetes Deployment environment stays in sync without us having to lift a finger.

Here are the configuration details of the ArgoCD Application:

CLUSTER: https://kubernetes.default.svc

NAMESPACE: webapp

https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-… 11/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…

REPO URL: https://github.com/tanmaybhandge/Harbor_CICD_Pipeline.git

PATH: Deployment

You may need to create the webapp namespace in your Kubrnetes Cluster.

5. Access the Pods


As everything looks healthy on the ArgoCD Deployment. We may test the deployed
application.

I have exposed the deployment via a NodePort service for testing purposes. To
access the application, use the service port along with the IP address of the worker

https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-… 12/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…

node. This setup allows easy access to the webpage for testing and validating the
application’s functionality.

apiVersion: v1
kind: Service
metadata:
name: webapp-service
spec:
selector:
app: webapp
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer

If you’ve made it this far, I hope you’ve found this enjoyable to read! If you have any
questions or just want to connect, feel free to reach out to me on LinkedIn. Cheers!

Kubernetes Ci Cd Pipeline Argo Cd DevOps Jenkins Pipeline

Follow

https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-… 13/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…

Written by Tanmay Bhandge


147 Followers

More from Tanmay Bhandge

Tanmay Bhandge

MongoDB: From Basics to Deployment on Kubernetes


I’ve been on quite the journey exploring MongoDB, often called the “NoSQL database for the
modern age,” and let me tell you, it’s living up…

12 min read · Sep 24, 2023

77

https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-… 14/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…

Tanmay Bhandge

How do you use the local images in Kubernetes?


We frequently encounter situations when navigating the Kubernetes images that need us to
work with local container images that are kept…

2 min read · Apr 10, 2024

Tanmay Bhandge

CI/CD with GitHub, GitHub Actions, Argo CD and Kubernetes Cluster


https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-… 15/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…

Introduction

9 min read · Jun 4, 2023

81 2

Tanmay Bhandge

Scaling Vertically: A Practical Approach to Kubernetes Vertical Pod


Autoscaling
Let’s stroll into the world of Kubernetes Auto Scaling, but before we dive deep, let’s grasp the
essence of scaling using a real-life…

10 min read · Nov 12, 2023

18

See all from Tanmay Bhandge

Recommended from Medium


https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-… 16/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…

Spacelift in Spacelift

26 Top Kubernetes Tools for 2024


Kubernetes is the most popular container orchestration tool, but it gets even better when
combined with other tools. The Kubernetes…

12 min read · May 29, 2024

68 2

Rajesh Thakur in DevOps.dev

3-Tier Ultimate DevOps CICD Pipeline Project | DevOps Project


https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-… 17/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…

Step 1:- Create a EC2 instance for local deployment.

6 min read · Jun 3, 2024

16 1

Lists

General Coding Knowledge


20 stories · 1285 saves

Productivity
241 stories · 452 saves

Natural Language Processing


1499 stories · 1027 saves

Nidhi Ashtikar

123 Essential Linux Commands Every DevOps Engineer Should Master


Linux is an open-source operating system kernel developed by Linus Torvalds in 1991. It’s the
foundation of various Linux distributions…

10 min read · May 17, 2024

19 2

https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-… 18/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…

Tom Jose in kotaicode

Cost-Efficient Kubernetes Setup in AWS using EKS with Karpenter and


Fargate
Introduction

8 min read · May 27, 2024

61

https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-… 19/20
6/11/24, 10:22 PM How to Build and Deploy Application on Kubernetes with CI/CD Pipeline Using Jenkins, Docker, Harbor Privat…

Faisal Kuzhan

DAY_25/90 => INTERVIEW QUESTIONS ON AWS


Below are some basic AWS interview questions along with the answers. ✍
17 min read · Feb 12, 2024

108 5

Antoine Fongang in AWS Tip

Project 48: Implementing Azure IaC DevOps for Terraform Project: CI/CD
with Azure DevOps
Scenario:

· 16 min read · 5 days ago

63

See more recommendations

https://medium.com/@tanmaybhandge/how-to-build-and-deploy-application-on-kubernetes-with-ci-cd-pipeline-using-jenkins-… 20/20

You might also like