Integration of GitHub, Docker, Kubernetes using dynamic Jenkins cluster

Divya Kurothe
4 min readAug 29, 2020

--

Create A dynamic Jenkins cluster and perform task-3 using the dynamic Jenkins cluster.
Steps to proceed as:

1.Create container image that’s 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:
a. Create the new image dynamically for the application and copy the application code into that corresponding docker image
b. 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:
a. 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.
b. If Application created first time, then Expose the application. Else don’t expose it.

So let’s start with the solution part:

During development here we need to allow remote access to a Docker deamon. When the daemon runs we’ll have to modify /lib/systemd/system/docker.service. The key thing to do is to add a command line parameter: -H tcp://127.0.0.1:4243 in the Service section of the configuration file.

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
BindsTo=containerd.service
After=network-online.target firewalld.service
Wants=network-online.target
Requires=docker.socket

[Service]
Type=notify
ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:4243 ExecReload=/bin/kill -s HUP
$MAINPID
TimeoutSec=0
RestartSec=2
Restart=always

"/usr/lib/systemd/system/docker.service"

Now, Reload the daemon configuration and we are good to go:

$ systemctl daemon-reload
$ systemctl restart docker

Next we need to create a Dockerfile which will install webserver for us to run the code.

FROM centos
RUN yum install sudo -y
RUN yum install httpd -y
RUN yum install /sbin/service -y
COPY *.html /var/www/html
CMD /usr/sbin/httpd DFOREGROUND &&/bin/bash
EXPOSE 80

We will then create a jenkins job which will pull the Github repo automatically when some developers push repo to Github and create the new image dynamically for the application and copy the application code into that corresponding docker image.

Next we will configure kubectl by creating configuration file

Create a local repo of kubernetes in our system. Kubernetes packages are not available from official CentOS 7 repositories. This step needs to be performed on the Master Node, and each Worker Node you plan on utilizing for your container setup. Enter the following command to retrieve the Kubernetes repositories.

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF

Dockerfile:

FROM centos:latest

RUN yum install java-1.8.0-openjdk-devel -y
RUN yum install python3 -y
RUN yum install sudo -y
RUN yum install git -y
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/bin
RUN mkdir /root/.kube/
COPY config /root/.kube/
COPY ca.crt /root/
COPY client.key /root/
COPY client.crt /root/
RUN mkdir /jenkins
RUN yum install openssh-server openssh-clients passwd initscripts net-tools -y
RUN /usr/bin/ssh-keygen -A

EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"] && /bin/bash/

Now we will install docker plugin in jenkins to configure cloud for docker

At last we have to create a jenkins job to deploy the image with desired replicas.

That’s all geeks. Any suggestions and feedback will be appreciated.

--

--

No responses yet