Automation with Jenkins and Kubernetes

kanav Gupta
4 min readAug 4, 2020

Task Overview:

1. Create a container image that has Linux and other basic configuration required to run Slave for Jenkins. ( example here we require kubectl to be configured )

2. When we launch the job it should automatically starts job on slave based on the label provided for dynamic approach.

3. Create a job chain of job1 & job2 using build pipeline plugin in Jenkins

4. Job1 : Pull the Github repo automatically when some developers push repo to Github and perform the following operations as:

4.1 Create the new image dynamically for the application and copy the application code into that corresponding docker image

4.2 Push that image to the docker hub (Public repository)

( Github code contain the application code and Dockerfile to create a new image )

5. Job2 ( Should be run on the dynamic slave of Jenkins configured with Kubernetes kubectl command): Launch the application on the top of Kubernetes cluster performing following operations:

5.1 If launching first time then create a deployment of the pod using the image created in the previous job. Else if deployment already exists then do rollout of the existing pod making zero downtime for the user.

5.2 If Application created first time, then Expose the application. Else don’t expose it.

Project Description

First we have to create a dynamic cluster in Jenkins using Docker containers.

  • I use two VM’s here, one for docker service and another for docker client.
  • In docker service,First we have to open this (/usr/lib/systemd/system/docker.service) using vim and then we need some changes

Now stop the docker services using systemctl stop docker in another VM and then we need to export the DOCKER_HOST, so that we can use this VM as a docker client.

export DOCKER_HOST=IP_of_VM1:port_you_give_in_docker_service_file

We have to install Docker plugin and then Go to Configure -> Manage Node and Clouds -> configure cloud.

1. Create a container image that has Linux and other basic configuration required to run Slave for Jenkins:

Port 22

Protocol 2

HostKey /etc/ssh/ssh_host_rsa_key

HostKey /etc/ssh/ssh_host_dsa_key

HostKey /etc/ssh/ssh_host_ecdsa_key

HostKey /etc/ssh/ssh_host_ed25519_key

LoginGraceTime 2m

PermitRootLogin yes

PubkeyAuthentication yes

UsePAM yes

Save this file as sshd_config

FROM ubuntu

RUN mkdir -p /var/run/sshd

RUN apt -y update

RUN apt install -y openjdk-8-jdk

RUN apt install -y openssh-server

RUN ssh-keygen -A

RUN ADD ./sshd_config /etc/ssh/sshd_config

RUN echo root:root | chpasswd

RUN apt-get update && apt-get install -y apt-transport-https gnupg2

RUN apt-get install -y curl

RUN apt-get install -y git-all

RUN curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl

RUN chmod +x ./kubectl

RUN mv ./kubectl /usr/local/bin/kubectl

RUN COPY .kube /root/.kube

CMD ["/usr/sbin/sshd", "-D"]

Save this file as Dockerfile

Build this image using command “docker build -t <image name> .

2. Jobs in jenkins:

  • Job1: (Copy code and Dockerfile from github)

Job2: (Launch the application deployed on top of kubernetes cluster…)

This code is deployed web-services and when developer push any code again then it rollout the update without any downtime.

--

--