DevOps is an important piece of the puzzle. And doing CI/CD on Kubernetes is one of the important tasks when working with Kubernetes. In this tutorial you will learn how to deploy applications on Kubernetes using Jenkins. Below is the flow of what we will be accomplishing at the end of this tutorial.
If you want to learn about Kubernetes. I have a whole tutorial series which covers the important topics of Kubernetes.
- Kubernetes Tutorial Series: Kubernetes Architecture and Installation
- Kubernetes Tutorial Series: Kubernetes Objects
- Kubernetes Tutorial Series: Resource Allocation for Containers
- Kubernetes Tutorial Series: Autoscaling in Kubernetes
- Kubernetes Tutorial Series: RBAC in Kubernetes
- Kubernetes Tutorial Series: Ingress
- A Kubernetes Cluster. If you don’t have one, I have covered how to set one up here.
- A Ubuntu machine to install Docker and Jenkins.
- A Ubuntu machine to connect with the Kubernetes Cluster. (Note: For trying things out, this can be same machine on which Jenkins and Docker is installed).
- Basic knowledge of CI/CD and Jenkins is a plus but is not required.
Installing Jenkins and Docker
To install the Jenkins you can either provision a new Ubuntu machine or you can install it on the same machine from which you are accessing your Kubernetes Cluster. Anyways, below are the commands to install Jenkins on an Ubuntu machine:
sudo apt update sudo apt install openjdk-8-jdk wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add - sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list' sudo apt update sudo apt install jenkins
Once Jenkins is installed let’s start and enable it so that it is automatically started when the server restarts
systemctl start jenkins systemctl enable jenkins systemctl status jenkins
Jenkins runs on port 8080. Let’s open that port by running the below command:
sudo ufw allow 8080
Now go to any browser of your choice and
Now, paste the password that you will get by running the below command:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Select ‘Install suggested plugin’.
You should see below screenshot and it can take couple of minutes.
After this you will be prompted to set Admin user. Fill in the required details and click on ‘Save and Finish’. You have successfully installed Jenkins.
Run the below commands to install docker in your environment. We need docker so that Jenkins can use it to build and push the images to your docker registry.
sudo apt-get update sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ gnupg-agent \ software-properties-common curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - sudo apt-key fingerprint 0EBFCD88 sudo add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ $(lsb_release -cs) \ stable" sudo apt-get update sudo apt-get install docker-ce docker-ce-cli containerd.io
Run the below command to check status of Docker. it should be in active and running state.
sudo systemctl status docker
While we are here, let’s add jenkins to docker group so that it will have appropriate permissions to interact with docker server.
sudo usermod -aG docker jenkins
Generating Personal Access Token
A personal access token is required to authenticate to GitHub. You can create a personal access token and use it in place of a password when performing Git operations over HTTPS with Git on the command line or the API. Go to Settings and Click on Developer Settings on the left hand side.
Click on Personal Access Token and then Generate New Token.
Give an token description and chose admin:repo_hook as the scope and then click on Generate Token.
You will get a token like below. Copy it somewhere safe because you will not be able to see it again.
Forking the Github Repository
Go to this repository: https://github.com/SamPriyadarshi/cicd-pipeline-train-schedule-autodeploy and fork this repository to your own github account. This repository contains the sample application and the required files for setting up the pipeline. This repository contains a very simple train-schedule app made using Node.js. Thanks to the guys at Linux Academy for providing this application for training purposes.
Let’s Briefly look at the application manifest file. If you will go to this URL you will find the yaml file. It consists of:
- A service of type LoadBalancer exposed on port 8080.
- A Deployment with two replicas specifying a container which is exposed on port 8080 and requesting 200m of vCPU. Learn more about Requests and Limit.
Let’s look at the Jenkinsfile which contains the details of how the CI/CD pipeline should work. Go to this URL to take a look at the Jenkinsfile. There are total 4 stages that we have defined in the Jenkinsfile:
- Build Stage: In this stage we just doing a bunch of simple testing.
- Building Docker images: In this stage we are building docker image of our application.
- Pushing Docker images: In this stage we are pushing the docker images that we built in the previous stage to our Docker Hub Registry. You will need your docker hub credentials to do this, which we will configure in the next section.
- Deploy to Kubernetes Cluster: In this stage we are deploying our application in the Kubernetes Cluster. You will need the kubeconfig file to deploy the application. We will configure this in the next section.
Note: Be sure to change the
<YOUR-DOCKER-USERNAME>/train-schedule. Otherwise your build will fail.
Configuring Jenkins Pipeline
Let’s now move on to configuring Jenkins so that we can setup a CI/CD Pipeline.
Let’s first install the Kubernetes Plugin by Clicking on Manage Jenkins on the right hand side and then Manage Plugins.
Go to Available tab on the top. In the search box type kubernetes and you should see a plugin for Kubernetes Continuous Deploy. Click on the checkbox and hit Install without restart. Wait for it to get installed.
After installing the plugin let’s now add some credentials to authenticate to Docker Hub, Github and Kubernetes API Server.
Docker Hub: Click on Credentials on the left hand side and then click on Jenkins and then Global Credentials (unrestricted) and finally Add Credentails. Provide your Docker Hub Login credentials as shown in the image below.
Github username and Password: Again Click on Add Credentails and provide your Github credentials as shown in the below screenshot:
Kubeconfig: Again click on Add Credentails to add your kubeconfig details. Go to your server which you use to connect to the Kubernetes Cluster and run the below command to fetch kubeconfig contents which are responsible for authenticating to the Kubernetes API Server. Copy the contents and paste it in the content section as shown in the screenshot below.
Let’s now give Jenkins access to our Github so that it can manage the hooks. We will use the Personal Access Token that we generated earlier.
Click on Manage Jenkins and then click on Configure System.
Scroll down to Github section and then click on Add Github Server and then choose Github Server from the dropdown.
Click on Add and then choose Jenkins from the dropdown.
In the Kind choose Secret text from the dropdown and paste your Personal Access Token that you generated above in the Secret section. In the ID give your github username. Refer the screenshot below.
In the credentials choose the credential that you just configured. And make sure that the manage hooks section is ticked.
Now scroll down to Global Pipeline Libraries and proceed with the below configuration as shown in the screenshot. Choose the name as jenkins-shared-library. Retrieval method and Modern SCM. Source Code Management as Github. Choose the Github API Key as the Credential and provide your username in Owner and select the repository which contains the code.
Click on Save. And you are done.
Let’s now move on to the final piece of the puzzle. Creating a new job.
Click on New Item.
Enter an item name and choose Multibranch Pipeline and click on OK.
Click on Add source and choose Github and after that a dropdown will automatically appear listing all your repository. Choose the one that you forked above.
After that click on Save and you should see an item in your Jenkins Dashboard.
Click on that job and choose the master branch. You should see a build in progress like below.
And in some time it will succeed. And if you click on Console Output, you should see all the logs.
And if you scroll down to the end you will the status of Success.
List all the running pods and services in your cluster by running the below commands:
kubectl get pods kubectl get svc
You should see 2 pods running and Service of type LoadBalancer.
If you hit the LoadBalancer on port 8080 For Ex:-
<LoadBalancerIPorDNS>:8080 . You should see your application up and running.
If you want to test it again, just change something in the app.js and an automatic build will take place in Jenkins and your application will be up and running with this new change.
Congratulations! you have learned how to configure CI/CD pipeline using Kubernetes and Jenkins.
Please let me know your thoughts and queries in the comments section below.