Jenkins Kubernetes- Integration

Srini
8 min readSep 18, 2020

--

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

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

Syntax: hostnamectl set-hostname <name of the host>hostnamectl set-hostname jenkins

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

Step-2 Add the Jenkins Debian repository.

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

 wget -q -O — https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -

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:

sudo echo "deb http://pkg.jenkins-ci.org/debian binary/" > /etc/apt/sources.list.d/jenkins.list

Step-3 Install Jenkins.

sudo apt updatesudo apt install jenkins -y

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

systemctl status jenkins

Step-4 Setting Up Jenkins

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:

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:

cat /var/lib/jenkins/secrets/initialAdminPassword
or
sudo cat /var/lib/jenkins/secrets/initialAdminPassword

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

Step-5 Setting Up Docker in Jenkins server

1. Install Docker

curl -fsSL get.docker.com | /bin/bash

2. Add Jenkins User to docker group

sudo usermod -aG docker jenkins

3. Restart Jenkins

sudo systemctl restart jenkins

Step-6 Install Git on Ubuntu

Update Default Packages

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

sudo apt update

Install Git

sudo apt install git

Confirm Successful Installation

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

git --version

Output >> git version 2.17.1

Set Up Git

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

Set Up Dockerhub

docker login — username=yourdockerhubusername

password: yourdockerhubpassword

Pull docker image

docker pull dockerhandson/spring-boot-mongo

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

docker tag <yourtagname> yourdockerhubusername/<yourrepositoryname>

docker push <yourdockerhubusername>/<repositoryname>

Step-7 Setting Up Jenkins Pipeline

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

node{

stage(“SCM Checkout”){

}
}

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

node{
//===============SCM CHECKOUT===============
stage('SCM Checkout'){
git credentialsId: 'GIT_CREDENTIALS', url: 'https://github.com/git_hub_username/spring-boot-mongo-docker.git',branch: 'master'
}

Click Apply and Save

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

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

node{
//===============SCM CHECKOUT===============
stage('SCM Checkout'){
git credentialsId: 'GIT_CREDENTIALS', url: 'https://github.com/git_hub_username/spring-boot-mongo-docker.git',branch: 'master'
}
//===============Maven Clean Package===============
stage(" Maven Clean Package"){
def mavenHome = tool name: "maven-3.6.1", type: "maven"
def mavenCMD = "${mavenHome}/bin/mvn"
sh "${mavenCMD} clean package"

}

Click — Apply and Save

The output should be like this

Check the console window and ensure it is success

Create Build Docker Image

docker pull dockerhandson/spring-boot-mango

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

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

node{
//===============SCM CHECKOUT===============
stage('SCM Checkout'){
git credentialsId: 'GIT_CREDENTIALS', url: 'https://github.com/git_hub_username/spring-boot-mongo-docker.git',branch: 'master'
}
//===============Maven Clean Package===============
stage(" Maven Clean Package"){
def mavenHome = tool name: "maven-3.6.1", type: "maven"
def mavenCMD = "${mavenHome}/bin/mvn"
sh "${mavenCMD} clean package"

}
//===============Build Docker Image===============

stage('Build Docker Image'){
withCredentials([string(credentialsId: 'DOCKER_HUB_CREDENTIALS', variable: 'DOCKER_HUB_CREDENTIALS')]) {
// some block
}
}
}

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

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

Click ADD

Variable :DOCKER_HUB_CREDENTIALS

Generate Pipeline Script

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

node{
//===============SCM CHECKOUT===============
stage('SCM Checkout'){
git credentialsId: 'GIT_CREDENTIALS', url: 'https://github.com/git_hub_username/spring-boot-mongo-docker.git',branch: 'master'
}
//===============Maven Clean Package===============
stage(" Maven Clean Package"){
def mavenHome = tool name: "maven-3.6.1", type: "maven"
def mavenCMD = "${mavenHome}/bin/mvn"
sh "${mavenCMD} clean package"

}
//===============Build Docker Image===============

stage('Build Docker Image'){
withCredentials([string(credentialsId: 'DOCKER_HUB_CREDENTIALS', variable: 'DOCKER_HUB_CREDENTIALS')]) {
// some block
}
}
//===============Push Docker Image===============
stage('Push Docker Image'){
withCredentials([string(credentialsId: 'DOCKER_HUB_CREDENTIALS', variable: 'DOCKER_HUB_CREDENTIALS')]) {
sh "docker login -u docker_hub_username -p ${DOCKER_HUB_CREDENTIALS}"
}
sh 'docker push docker_hub_username/docker_hub_repository_name'
}

}

Deploy to Kubernetes Cluster

apt-get update && apt-get install -y apt-transport-https
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://apt.kubernetes.io/ kubernetes-xenial main
EOF
apt-get update
apt-get install -y kubelet=1.15.4–00 kubeadm=1.15.4–00 kubectl=1.15.4–00 docker.io

kubeadm init

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

So that worker machine will join the master .

mkdir -p $HOME/.kubesudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/configsudo chown $(id -u):$(id -g) $HOME/.kube/config

kubectl get nodes

curl -o weave.yaml https://cloud.weave.works/k8s/v1.8/net.yamlkubectl apply -f weave.yamlkubectl get pods — all-namespaces

Go to jenkins dashboard > Manage Jenkins> Manage Plugins

Search: Kubernetes (Available tab)

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

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

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

node{
//===============SCM CHECKOUT===============
stage('SCM Checkout'){
git credentialsId: 'GIT_CREDENTIALS', url: 'https://github.com/git_hub_username/spring-boot-mongo-docker.git',branch: 'master'
}
//===============Maven Clean Package===============
stage(" Maven Clean Package"){
def mavenHome = tool name: "maven-3.6.1", type: "maven"
def mavenCMD = "${mavenHome}/bin/mvn"
sh "${mavenCMD} clean package"

}
//===============Build Docker Image===============

stage('Build Docker Image'){
withCredentials([string(credentialsId: 'DOCKER_HUB_CREDENTIALS', variable: 'DOCKER_HUB_CREDENTIALS')]) {
// some block
}
}
//===============Push Docker Image===============
stage('Push Docker Image'){
withCredentials([string(credentialsId: 'DOCKER_HUB_CREDENTIALS', variable: 'DOCKER_HUB_CREDENTIALS')]) {
sh "docker login -u docker_hub_username -p ${DOCKER_HUB_CREDENTIALS}"
}
sh 'docker push docker_hub_username/docker_hub_repository_name'
}
//===============Deploy To Kuberates Cluster===============
stage("Deploy To Kuberates Cluster"){
kubernetesDeploy(
configs: 'springBootMongo.yml',
kubeconfigId: 'KUBERNETES_CLUSTER_CONFIG',
enableConfigSubstitution: true
)
}

/**
stage("Deploy To Kuberates Cluster"){
sh 'kubectl apply -f springBootMongo.yml'
} **/

}

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

--

--

Responses (3)