Deploying web-server using Jenkins and Kubernetes
Gaurav Gupta
M.tech(AI)@IIITL | Django Developer | Web Developer | Freelancer | DevOps & Cloud Expert | Blogger
Task Overview:
- Create a container image that’s has Jenkins installed using Dockerfile.
- When we launch this image, it should automatically start the Jenkins service in the container.
- Create a job chain of job1, job2, job3 and job4 using build pipeline plugin in Jenkins
- Job1: Pull the Github repo automatically when some developers push the repo to Github.
- Job2 :
1. By looking at the code or program file, Jenkins should automatically start the respective language interpreter installed image pod 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 )
- Job3: Test your app if it is working or not.
- Job4: if the app is not working, then send an email to the developer with error messages and redeploy the application after code is being edited by the developer.
Tools used:
- Jenkins
- Kubernetes
- Docker
I'm using Kubernetes here if you want to launch Kubernetes locally, refer to this article.
Project Description:
- Create Jenkins installed Dockerfile:
First I need to create an image which has Jenkins installed also running Kubernetes commands. This is the dockerfile for this...
FROM centos:latest RUN yum install wget -y RUN 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 java-11-openjdk.x86_64 -y RUN yum install jenkins -y RUN yum install git -y RUN yum install python3 net-tools -y RUN echo -e "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 mv ./kubectl /usr/local/bin/kubectl COPY .kube /root/.kube RUN mkdir -p /home/jenkins EXPOSE 8080 CMD [ "java", "-jar", "/usr/lib/jenkins/jenkins.war" ]
Save this as Dockerfile and run this command...
Only thing you need to change is files of .kube folder.
docker build -t dockerhub_username/img_name:tag
Now your image is created and now push it to in docker hub using command... docker push img_name:tag
Before that, make sure you are doing login first using docker login command. You can pull my image from docker-hub.
Now my image is created.
- Running Jenkins server on top of Kubernetes with persistent storage.
When you work on Kubernetes, creating a YAML file for this is always better than doing it manually using the command. So here I creating one Dockerfile.
apiVersion: v1 kind: Service metadata: name: jenkins labels: app: jenkins spec: ports: - port: 8080 selector: app: jenkins type: NodePort --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: jenkins-claim labels: app: jenkins spec: accessModes: - ReadWriteOnce resources: requests: storage: 10Gi --- apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2 kind: Deployment metadata: name: jenkins labels: app: jenkins spec: selector: matchLabels: app: jenkins strategy: type: Recreate template: metadata: labels: app: jenkins spec: containers: - image: gaurav0417/jenkins name: jenkins ports: - containerPort: 8080 name: jenkins volumeMounts: - name: jenkins-storage mountPath: /root/.jenkins/ volumes: - name: jenkins-storage persistentVolumeClaim: claimName: jenkins-claim
To run this, we using the command...
kubectl apply -f file_name
Now your jenkins_server is running.
You can see that my Jenkins server is running... Run minikube_ip:port in your browser.
For getting the password, Run kubectl logs pod_name
Now my Jenkins server is running.
2. Jobs in Jenkins:
- Job1(Copy Github Code):
Whenever the developer pushes any code in Github, this job automatically detects and copy in host OS.
This job creates a directory mycode and copy the GitHub code in that directory.
- Job2(Deploy deployment for website):
If my job1 is successfully built, it triggers job2 and launches the container. Here I have taken the example of webpages so I have used the apache webserver.
The YAML code is launching for the website is below...
apiVersion: v1 kind: Service metadata: name: httpd labels: app: httpd spec: ports: - nodePort: 80 port: 80 protocol: TCP targetPort: 80 selector: app: httpd type: NodePort --- apiVersion: v1 kind: PersistentVolumeClaim metadata: name: httpd-claim labels: app: httpd spec: accessModes: - ReadWriteOnce resources: requests: storage: 10Gi --- apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2 kind: Deployment metadata: name: httpd labels: app: httpd spec: selector: matchLabels: app: httpd strategy: type: Recreate template: metadata: labels: app: httpd spec: containers: - image: gaurav0417/http-server name: httpd ports: - containerPort: 8080 name: httpd volumeMounts: - name: httpd-storage mountPath: /var/www/html/ volumes: - name: httpd-storage persistentVolumeClaim: claimName: httpd-claim
- Job3(Testing that website is running or not) and Job4(Sent a mail to the developer):
After successfully build, Job2 will trigger Job3 and it checks the website that is working or not. If not working, it triggers job4 and sent a notification to the developer.
Finally...Done??
Github Link for all YAML file: https://github.com/gaurav-gupta-gtm/web
Feel free to DM me if you have any queries regarding the project.
Thanks for reading...
Let's connect to each other for make this platform more beautiful...??
Ex- Client Support Specialist at Amazon || Ex- Process Expert at Provana || Ex- Sr. Client support associate at Amazon || Ex-Software developer trainee at Wbcoms designs || Blogger || Content writer ||
4 年Well done bro keep it up.... ??
? Android Developer ? Kotlin ? Java ? React Native Developer ?
4 年Great Gaurav Gupta brother
Graduate intern @ Dell | Mtech(CS) @ IIITL
4 年Great work Gaurav ??