CI/CD: Jenkins,Kubernetes With DockerHub
As always I'll explain some basic terminologies that we need to know before getting started and jumping onto the project..... so that if anybody who is not comfortable with these devops stuff will learn something from the project also be able to link on to the project.
What is Docker and DockerHub ?
- Docker is one of the most demanding technology that is growing very much fastly in terms of improvement in the form of different technogies like PodMan , CRI- O
- All these tools uses containerization technology and thus each of them is very effective in launching a operating system inside any system in less than 10 seconds which was som interesting to know
- Now since every operating system needs some images to get booted , so in docker , we normally store all of our docker images into a single registry like SCM and this registry is none other than DockerHub
What is Kubernetes?
- Kubernetes is one of the great DevOps tools that we use for launching containers, monitoring them , keeping their storage permanent and all about containerization technology
- It's unique quality to intricate with different types of technology i.e Docker, Podman and CRI-O .So, one doesn't have to learn different technologies.
- Only getting comfortable with Kubernetes will work.
What is Jenkins ?
- It is the main program using which we are able to achieve automation in this agile world and can preform various tasks involving commands to get onto work in the form of chain of jobs that we also call mainly by pipelines
Why this task ?
- To help others and those who are in need of cool stuffs like this and don't know much about this technology to get them a flavour of what's the great here in this tool
What's the motto ?
The situation is like that suppose a developer pushes some code to our company's SCM and we as operator guys wants to update the code onto our company's site our maybe we will want code to be deployed onto testing environment so that QA team can take actions on it.
Here we want to automate things by making use of docker images such that we will copy the code to our docker container, make an image out of it and then finally upload it onto our production environment.
Here are some prerequisites that one should have inorder to get onto the project :
- Basics of Kubernetes
- Minikube setup already installed and configured with kubectl
- A Basic knowledge on linux interface like Redhat
So lets get started without wasting further more time.............
Here is the Dockerfile for the image that we have to use as our dynamic jenkins slave
#!/bin/bash FROM centos:latest #Basic downloading commands RUN dnf install wget -y #Root priviledge giving command RUN dnf install sudo -y RUN dnf install java-11-openjdk.x86_64 -y RUN dnf install net-tools -y RUN dnf install git -y #ssh for key injection RUN dnf install openssh-server -y #generating ssh key for starting the sshd environment RUN ssh-keygen -A #config file for kubectl COPY config /root/.kube/config COPY ca.crt /root/ca.crt COPY client.crt /root/client.crt COPY client.key /root/client.key 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 sudo mv ./kubectl /usr/bin/kubectl RUN sudo chmod +x /usr/bin/kubectl EXPOSE 8080 #starting sshd environment for ssh RUN bash
CMD [ "/usr/sbin/sshd", "-D" ]
Here I have used the credentials for my minikube setup and created an image named ssjnm/ssjnmkube and that thing depends on your keys ,certificates and client certificates
Important thing to notice here is that I have also installed and started SSH daemon (environment) so that I can use it to login to the new OS created and thereby we have to inject some SSH keys for getting connectivity
Use the following command to start building the docker image
docker build -t <your image name>:<version> <PATH>
So here we have Image ready and If we will want the image to run dynamically then there''s one more thing we have to do
We have to host our docker server running inside our linux VM so that external docker clients can also get connected to it which is a need for dynamic slave setup
But there's a special port 4243 only on which we will be able to get request
Of course this can be changed by using docker main configuration file.
Now just reload the environment by using the following commands:
systemctl daemon-reload systemctl restart docker
Now we can see in the status of docker server that external requests are allowed.
So let's use my ssjnmjenkins image to launch a jenkins on Docker onto which we will we working and creating the CI/CD setup
So, first unlock Jenkins and Try to install the basic plugins because it covers almost every plugin that we will be needing in our current setup
Here, we have to download one more plugin for docker cloud provisioning
Now that we have downloaded the plugins, its time to setup the Redhat node and Cloud provisioner. If you are unfamiliar with the setup for static slave agents then have a look onto my previous article for task 3
Here , we have used SSH method to log onto our Redhat slave agent and I have used a label docker to represent that slave
So, lets configure cloud for getting started.
Here make sure that you have enabled the cloud and exported DOCKER_HOST to set the IP address of host internally
This was about configuring docker for clod provisioning but if we want a specific image to work as a slave then we have one option for templated using which we will specify to use ssjnm/ssjnmkube image for kubectl command to run
And here at connectivity use option for SSH key injection to get access to OS launched byy images
So, all the work for configuration is done and now its time for starting our jobs
Job1 : To copy the developer codes inside our environment images
Here I have used the docker plugin to automatically build the code by making use of Dockerfile and the codes in the same directory. Just give your credentials to the plugin so that it can upload the image to DockerHub
Job2: To launch or to update the previously running docker images servers onto our Kubernetes Clusters. Here we will use the dynamic slave node for kubectl
Here I have written a code expressing my needs for current setup or the setup-that was already running ,we just have to update our deployment or create new ones my making use of these.
Before getting to job3 , lets us configure our smtp mail server so that if a build will be unstable then a email will be send to us to get notified
Job3: To check the status of site whether its working or not
Everything's ready now , just start a build pipeline for all of our jobs and if you don't know that, then refer my previous article.
So let me create hooks for post commit program and I'll be using triggers by links inorder to start our build so that whenever the code is changed by developer, the new code will be deployed onto our environment.
Here I have edited the file index.html and then finally commited the changes as a developer
Here we can see that our Pipeline is started Once we have triggered our change in code
Yeeeah! The build is Successful and thus achieved what I wanted
For checking manually we can use the console output and take a look onto the site.
See here , exactly same code as that of the changed code.
This was the output I was getting during build failure .
But when my Build has returned to stable , I have received this email from Jenkins :)
This way the project is completed and that's all for now.............