Jenkins Kubernetes- Integration

In this article we are going to deploy mongo and spring controller containers in kubernetes machine using Jenkins based on AWS Cloud provider .

Pre-requisites

Image for post
Image for post
Image for post
Image for post

Jenkins EC2 Instance

Connect to your Jenkins EC2 instance using gitbash terminal or putty using your *.pem key as a root user.

Change the host name of the Ec2 instance using the following command

Since Jenkins is a Java application, the first step is to install Java. Update the package index and install the Java 8 OpenJDK package with the following commands:

sudo apt update

sudo apt install openjdk-8-jdk -y

Import the GPG keys of the Jenkins repository using the following wget command:

The command above should output OK which means that the key has been successfully imported and packages from this repository will be considered trusted.

Next, add the Jenkins repository to the system with:

Jenkins service will automatically start after the installation process is complete. You can verify it by printing the service status:

systemctl status jenkins

Image for post
Image for post

To set up your new Jenkins installation, open your browser, type your domain or IP address 8080 make sure you opened port 8080 in AWS security groups

http://your_ip_or_domain:8080

and screen similar to the following will be displayed:

Image for post
Image for post

During the installation, the Jenkins installer creates an initial 32-character long alphanumeric password. Use the following command to print the password on your terminal:

Image for post
Image for post

Copy the password from your terminal, paste it into the Administrator password field and click Continue and install Suggested Plugins.

1. Install Docker

2. Add Jenkins User to docker group

3. Restart Jenkins

Logged into your Ubuntu 18.04 server as a sudo non-root user, first update your default packages.

You can confirm that you have installed Git correctly by running this command and receiving output similar to the following:

git --version

Now that you have Git installed and to prevent warnings, you should configure it with your information.

git config --global user.name "<yourusername>"

git config --global user.email "your-email@domain.com"

Verify Username and Email Configurations

git config --list

docker login — username=yourdockerhubusername

password: yourdockerhubpassword

docker pull dockerhandson/spring-boot-mongo

docker tag dockerhandson/spring-boot-mongo <yourtagname>

docker tag <yourtagname> yourdockerhubusername/<yourrepositoryname>

docker push <yourdockerhubusername>/<repositoryname>

Go to Jenkins dashboard > New Item

Enter an Item Name:spring-mongo-docker-k8s
Select : Pipeline
Click OK

Create Stage Git Clone

Use the following text in the script window and click pipeline syntax

Use the following in the Steps page

Sample Step: git:Git
Repositoru URL: https://github.com/your-github-username/spring-boot-mongo-docker.git
Branch: Master
Credentials: Use Dropdown Add> Jenkins
Username: your github username
Password: your Githubpassword
ID:GIT_Credentials
Description:GIT Credentials
Click Add
Now in the Credentials Select your username
Click Generate Pipeline Script
Copy this script and past in the pipeline window

your script looks like this now

Note: use your username in the <yourgithubusername> in the code

Click Apply and Save

Click Build now in the left Menu, you should see the following output

Image for post
Image for post

Create Stage Maven Clean Package

Go to Manage Jenkins > Global Tool Configuration
At the bottom select Maven > Add Maven
Name:maven-3.6.1
Version:
Apply and Save

Go configure > and Pipeline and use the following code in the Script windows

Note: use your username in the <yourgithubusername> in the code

Click — Apply and Save

The output should be like this

Image for post
Image for post
Image for post
Image for post
Check the console window and ensure it is success

Create Build Docker Image

docker pull dockerhandson/spring-boot-mango

Image for post
Image for post
Image for post
Image for post

Go configure > and Pipeline and use the following code in the Script windows

Note: use your username in the <yourgithubusername> in the code

Apply and Save. Click Build now. your Output should look like this

Image for post
Image for post

Push Docker Image

Before that you need to login

Go configure > and Pipeline and use the following code in the Script windows

click the Pipeline syntax > Sample step > withCredentials: Blind credentials to variables.

Under Binding> Secret text

Add: Jenkins

Kind: Secret text

Secret: hub.docker.com (password)

ID: DOCKER_HUB_CREDENTIALS

Description:Docker hub Credentials

Image for post
Image for post

Click ADD

Variable :DOCKER_HUB_CREDENTIALS

Generate Pipeline Script

Image for post
Image for post

Note: use your username in the <yourgithubusername> in the code

Image for post
Image for post

Deploy to Kubernetes Cluster

kubeadm init

Paste the following → Kubeadm join token in the worker machine …..←.

So that worker machine will join the master .

Image for post
Image for post

kubectl get nodes

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Go to jenkins dashboard > Manage Jenkins> Manage Plugins

Search: Kubernetes (Available tab)

Image for post
Image for post
Jackson plugin can be uploaded externally using Advaced tab

Install the following version of plugins to ensure the kubernetes deplyment work smoothly

  1. Kubernetes Continuous Deploy v2.3.0
  2. Jackson 2 API v2.9.10
  3. The previous version of jackson plugin can be found at the below link

https://updates.jenkins.io/download/plugins/

Select:Kubernetes Continuous Deploy and install without restart option.

Click jenkins> Manage credentials > Stores Scoped to Jenkins section > select Global drop down > Add Credentials

Image for post
Image for post

Kind: Kubernetes configuration (kubeconfig)

Kind: Select Kubernetes configuration
ID: KUBERNETES_CLUSTER_CONFIG
Description: Kubernetes config

Now connect to the kubernetes master machine
ls .kube/
cat .kube/config

Image for post
Image for post

Copy all the content starting from apiVersion: v1 to the end
and paste it in the Kubeconfig window

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

Before building the master machine check the pods in master machine As shown above

Image for post
Image for post
Image for post
Image for post

Written by

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store