Upgrading Task 2 with Kubernetes

Task2:

  • For this task, we have minikube installed in any O.S in a Virtual Machine.
  • I am using my ubuntu linux for doing this task.
  1. First start your minikube .

2. Create a Jenkins image with kubectl installed on it.

FROM centos:


RUN yum install wget -y
RUN yum install net-tools -y
RUN wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins.io/redhat/jenkins.repo
RUN rpm --import http://pkg.jenkins.io/redhat/jenkins.io.key
RUN yum install java -y
RUN yum install jenkins -y
RUN yum install git -y
RUN yum install python36 -y

RUN sed 's/JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true"/JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true -Dmail.smtp.starttls.enable=true -Dmail.smtp.ssl.protocols=TLSv1.2"/g' /etc/sysconfig/jenkins

RUN touch /etc/yum.repos.d/kubernetes.repo
RUN echo $'[kubernetes]\n\
name=Kubernetes\n\
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64\n\
enabled=1\n\
gpgcheck=1\n\
repo_gpgcheck=1\n\
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg' >> /etc/yum.repos.d/kubernetes.repo

RUN yum install -y kubectl

EXPOSE 8080
CMD java -jar /usr/lib/jenkins/jenkins.war
  • Build your image using
docker build -t skabhi003/jenkin-kubectl:v1 .
  • Run the container, and complete your login setup of Jenkins.
docker run -it -v /root/w1/:/root/.kube -p 9000:8080 skabhi003/jenkin-kubectl:v1 
  • Here my “root/w1/” contains :
> ca.crt
> client.crt
> client.key
> config
> my.py
  • Now our kubectl are completely configured and can run our commands on minikube.
  • Copy the marked password .
  • Paste the above copied password into adminstrator password.

Now we will going to create our jobs.

  1. Job1 will remain same as was in previous task.
  2. In Job2 , go to Execute shell and paste this commands:
export len1=$(ls /root/workspace | grep html | wc -l)
if [ $len1 -gt 0 ]
then
export len2=$(kubectl get deployments | grep webserver | wc -l)
if [ $len2 -gt 0 ]
then
kubectl delete deployment webserver
fi
export len3=$(kubectl get svc | grep webserver | wc -l)
if [ $len3 -gt 0 ]
then
kubectl delete svc webserver
fi
kubectl create deployment webserver --image=httpd
kubectl scale deployment webserver --replicas=3
kubectl expose deployment webserver --port=80 --type=NodePort
sleep 30
python3 /root/my.py
kubectl get svc
fi
  • Here “my.py” contains:
#!/usr/bin/python3
import os
cmd = os.popen("kubectl get pods | grep webserver")
cmd_op = cmd.read()
x = cmd_op.split('\n')
del x[-1]
for y in x:
z = y.split()
print(z)
os.system("kubectl cp /root/workspace/:/usr/local/apache2/htdocs/".format(z[0]))

3. Job3: I am leaving job3 as it is to send mail to developer if Job2 fails.

4. Here we don’t need any monitoring job as this will be handled by the Kubernetes itself.

  • My Job2 output :
  • After success of Job2 you can check it from minikube.

Now testing our web server:

  • Thanks, hope you guys will like my article. if you have any suggestion or query will free to ask.

#Happylearning #keepsharing #devops #integration

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

[AWS] How to restrict access of an IAM user to a specific region

Agave Development Update #2

Scaling Infrastructure Automation and Configuration Management Across the Enterprise

Python pathlib — an alternative to the OS module

Major Thorg release now supports a wider range of GPUs!

Python — Print colored text with ease

spectrogram error in matlab

Cost of Change Curves and Public Policy

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Skabhi

Skabhi

More from Medium

MLOps platform on Rancher RKE2 Kubernetes Cluster — Bare Metal environment.

RedHat Openshift Gitops

Chapter 18 — Distributed Computing

Red Hat Openshift Operator Certification on Power