Managing and Deploying WebApplicatons on kubernetes using Jenkins.
Arun Kumar D
Consultant @ Deloitte Digital | 7x Salesforce Certified | 9x Superbadges | Trailhead Ranger
Assigned Task :
Perform second task on top of Kubernetes where we use Kubernetes resources like Pods, ReplicaSet, Deployment, PVC and Service.
1. Create container image that’s has Jenkins installed using dockerfile Or You can use the Jenkins Server on RHEL 8/7
2. When we launch this image, it should automatically starts Jenkins service in the container.
3. Create a job chain of job1, job2, job3 and job4 using build pipeline plugin in Jenkins
4. Job1 : Pull the Github repo automatically when some developers push repo to Github.
5. Job2 :
1. By looking at the code or program file, Jenkins should automatically start the respective language interpreter installed image container to deploy code on top of Kubernetes ( eg. If code is of PHP, then Jenkins should start the container that has PHP already installed )
2. Expose your pod so that testing team could perform the testing on the pod
3. Make the data to remain persistent ( If server collects some data like logs, other user information )
6. Job3 : Test your app if it is working or not.
7. Job4 : if app is not working , then send email to developer with error messages and redeploy the application after code is being edited by the developer.
Here, Task 2 for you reference.
Now Lets Build a Docker Image which will contain Jenkins and Kubctl installed and configured in it.
Dockerfile
FROM centos RUN yum install sudo -y RUN yum install wget -y RUN yum install curl -y RUN yum install git -y RUN yum install net-tools -y RUN sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo RUN rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key RUN yum install jenkins -y RUN yum install java-11-openjdk.x86_64 -y RUN echo "jenkins ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers 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 sudo mv ./kubectl /usr/local/bin/kubectl RUN kubectl version --client RUN export PATH=/usr/local/bin/kubectl:$PATH COPY config /root/.kube/ COPY ca.crt /root/ COPY client.crt /root/ COPY client.key /root/ CMD java -jar /usr/lib/jenkins/jenkins.war EXPOSE 8080
for this you need to have the following 4 files to copy it into the image you are building.
and the config file is as same as the the one used for your base Linux on which the docker is running.
# docker build -t jenkins-kubectl:v1 .
now when the image is build successfully
docker run -it -p 8085:8080 -v /:/host/repo --name jenkube jenkins-kubectl:v1
when you launch this image you will get a admin password like this.
open the jenkins webUI using the IP of you Linux System with the Port number you have given and copy the password here and you are ready to use jenkins.
Before creating the jobs we need yml file to configure the pods in kubernetes...
Deployment.yml
apiVersion: v1 kind: Service metadata: name: myweb labels: app: webserver spec: ports: - port: 4949 nodePort: 31000 targetPort: 80 selector: app: webserver tier: frontend type: NodePort --- apiVersion: apps/v1 kind: Deployment metadata: name: myweb labels: app: webserver spec: selector: matchLabels: app: webserver tier: frontend strategy: type: Recreate template: metadata: labels: app: webserver tier: frontend spec: containers: - image: httpd name: myweb ports: - containerPort: 80 name: webserver volumeMounts: - name: webstorage mountPath: /usr/local/apache2/htdocs/ volumes: - name: webstorage persistentVolumeClaim:
claimName: webpvc
pvc.yml :
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: webpvc spec: accessModes: - ReadWriteOnce resources: requests: storage: 10Gi
now lets start creating the jobs in jenkins...
Job1 : Pull the Github repo automatically when some developers push repo to Github.
here im using the GitHub webhooks as the Build Triggers so that whenever the developer pushes something in the GitHub repository it automatically gets downloaded into the jenkins.
But for the WebHooks to work you need a public IP, to setup the webhooks and public ip to it refer to the below article.
Job2 :
1. By looking at the code or program file, Jenkins should automatically start the respective language interpreter installed image container to deploy code on top of Kubernetes ( eg. If code is of PHP, then Jenkins should start the container that has PHP already installed )
2. Expose your pod so that testing team could perform the testing on the pod
3. Make the data to remain persistent ( If server collects some data like logs, other user information )
Execute shell code :
if kubectl get pvc | grep webpvc then echo "pvc already exists" kubectl apply -f /kubecode/pvc.yml else kubectl create -f /kubecode/pvc.yml fi sleep 5 if kubectl get deployment | grep myweb then echo "deployment already exists" kubectl apply -f /kubecode/deployment.yml else kubectl create -f /kubecode/deployment.yml fi sleep 15 kubectl cp /webpage/index.html $(kubectl get pods -o=jsonpath='{.items[0].metadata.name}'):/usr/local/apache2/htdocs/
know if you check the in the minikube using the kubectl application, you will see the pod and a pvc for the storage created.
now check in the web for the webpage you have created using your minikube ip with the port you have defined in the service in the deployment.yml file
http//:<minikube IP>:31000
Job3 : im mixing both Job3 and Job4 into this job
Test your app if it is working or not. if the app is not working, the send email to the developer that the build was successful and if the app is not working , then send email to developer with error messages and redeploy the application after code is being edited by the developer.
But for this you need the email extension plugins and set the configuration in your linux system.
first go into the Docker Container using the command,
# Docker exec -it jenkube bash
the use this command to go into the file, you can you both vi or vim editor.
vim /etc/sysconfig/jenkins
then find and edit this line...
then exit from the Container.
now you have to restart the docker container...
# docker stop jenkube # docker start jenkube # docker attach jenkube
now, configure in the jenkins
go to Manage Jenkins >> Configure System and follow the steps show in the below article.
now lets build the job...
Build View :
Collaborated with Nishan Acharya