Deployment on dynamic Jenkins Cluster with Build Configuration & Rolling Updates Automatically

Deployment on dynamic Jenkins Cluster with Build Configuration & Rolling Updates Automatically

In this fast growing world, to sustain in business we need to adopt the agile process or say Devops environment. The future of Devops is Devops assembly line. To become agile we need to have robust architecture system and many resources. The best way to manage is by using container technology. In my previous articles we have seen that using container technology we can cut down the testing and production time with a huge difference and it's even automated.

Few things which we can even do more better is to make it sustain the changing requirements of the customer. It's very tedious and hard process to do updates in real life with zero downtime and even not disturbing the on going actions on the sever. So for that Kubernetes provides us with a very prominent strategy to do updates or in industry it's called roll out.

Even if we go more in-depth we can see that deployment is still very efficient but the process which we have in the production is still not that efficient. To even make it more scale-able we can have a concept of build configuration which will automatically fetch the code once the developer commits it to the SCM and make an image out of it. This image will be further uploaded to the public or private repository like docker hub. Now the deployment process is the same, it will download form the docker hub or any other repository and deploy the code. Even if the new version or patch is to be implemented, developer will only make changes and commit it again. Our build configuration will again make the image out of it and upload it. Now we will use rolling update strategy to roll out because we have already made deployment. This whole process would be taking place on dynamic slave of the Jenkins which will be the image which we built and launched on top of Kubernetes.

How to create the Jenkins Dynamic Cluster?

For this we have some pre-requisite to be done.

  1. We need to update Jenkins to the latest version because we will install some plugins which need the newer version. To update run this code
yum update jenkins -y

Let it update then restart the services

systemctl restart jenkins

2. Now we need to download some plugins as mentioned bellow

  1. SSH Agent
  2. Docker
  3. Yet Another Docker

manage jenkins -> manage plugins -> available -> search and download

No alt text provided for this image

3. Start the minikube which will in turn help us create kubernetes single node cluster as server. For this you require minikube which you can download from internet.

minikube start
No alt text provided for this image

4. Now in the docker internal service file we need to make some changes because we want to control it using Jenkins, hence we want to make Jenkins as docker client and docker installed in host as server. Here we have to enable tcp connection as docker will accept it with the protocol tcp.

vim /usr/lib/systemd/system/docker.service
No alt text provided for this image

Here we appended this line which will allow to control docker server from anyone who contacts it

-H tcp://0.0.0.0:6969

Here you can give any port number.

5. Now we need to make come configuration in Jenkins so that it can dynamically launch cluster.

Go to manage jenkins -> Manage Nodes & Cloud -> Configure Clouds

No alt text provided for this image

Then choose docker from the dropbox appeared. Then do the following configuration

No alt text provided for this image

Here you will give local host IP with the port number which we edited.

No alt text provided for this image

Here you are seeing an error because some failure as to demonstrate this, I have stopped some services but you won't see this error. Here Remote File System is the path where you will have your all code files, so it's a good practice to make a directory, for me it's /jenkins-dynamic

No alt text provided for this image

Then click save. You wont see any node come up right now because its cloud, this means on demand or dynamic.

So, we have created our dynamic slave which will come up when its needed or called in use using labels. We will call it using labels, that's why it's import to give labels.

Jenkins Job Configuration

JOB slave-github

This will download code from the GitHub account we provide and then do build configuration.

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

Now let's see how our Dockerfile to create image of our code and configure it as kubernetes client should be.

FROM centos
RUN yum install sudo -y 
RUN yum install java -y 
RUN yum install openssh-server -y 
RUN /usr/sbin/sshd -D & 
RUN ssh-keygen -A 
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 mkdir /root/.kube /root/jenkins 
RUN sudo mv ./kubectl /usr/local/bin/kubectl 
COPY ca.crt client.crt client.key config deployment.yml /root/
RUN mv /root/config /root/.kube/config &&\ 
    yum install httpd -y &&\
    yum install php -y 
COPY index.html /var/www/html/
EXPOSE 80
CMD /usr/sbin/httpd -DFOREGROUND

For this you have to take your keys and certificates from host system to RHELv8 machine and keep in the same directory as the Dockerfile

No alt text provided for this image

This is after running this jobs that's why you are seeing index.html and README.md files. You don't have to create this.

apiVersion: v1
kind: Config
clusters:
- cluster:
    server: https://192.168.99.100:8443
    certificate-authority: /root/ca.crt
  name: minikube
contexts:
- context:
    cluster: minikube
    user: shubham
users:
- name: shubham
  user:
    client-key: /root/client.key
    
    client-certificate: /root/client.crt

This is the config file. This will configure the slave node to communicate to kubernetes server.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myweb
spec:
  replicas: 2
  selector:
    matchLabels:
      env: slave
  template:
    metadata:
      name: myweb-pod
      labels:
        env: slave
    spec:
      containers:
      - name: myweb-con
        
        image: shubhambhalala/slave

This is the deployment.yml file which will deploy our application on top of dynamic jenkins.

JOB slave-deployment

This job will run on dynamic jenkins nodeand will deploy our application on top of kubernetes cluster. Now if the deployment is already created then it will do rolling update for the new version on fly. This will be done using rolling strategy hence there will be no downtime or latency.

No alt text provided for this image

Here we have to specify the label we set for the slave node. NOTE: This option will only come after you have configured the slave node, until then you won't see this option.

No alt text provided for this image
No alt text provided for this image

This will finally launch our application and expose it so that the client can have access to this.

Finally we will create build pipeline and run this.

No alt text provided for this image

You can see in console output that how it does everything. Let's see what happens in job1.

No alt text provided for this image

It successfully downloaded code from github.

No alt text provided for this image

Built the image successfully using Dockerfile

No alt text provided for this image

Downloaded necessary software

No alt text provided for this image

Finally uploaded to docker hub.

Now let's see for the job2

No alt text provided for this image

You will see this if you are running it for first time. Next time you change comthing for update and run it, you will see its doing rolling update.

No alt text provided for this image

Finally you can take the port number and view your website deployed using dynamic jenkins cluster on kubernetes with rolling update.

No alt text provided for this image

??GitHub: if you like it hit star

Thank you for reading this article??Feel free to connect and comment.





要查看或添加评论,请登录

Shubham Bhalala的更多文章

社区洞察

其他会员也浏览了