Launching Website on top of KUBERNETES
What is Kubernetes ?
Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation. It has a large, rapidly growing ecosystem. Kubernetes services, support, and tools are widely available.
Why you need Kubernetes and what it can do
Containers are a good way to bundle and run your applications. In a production environment, you need to manage the containers that run the applications and ensure that there is no downtime. For example, if a container goes down, another container needs to start. Wouldn't it be easier if this behavior was handled by a system?
That's how Kubernetes comes to the rescue! Kubernetes provides you with a framework to run distributed systems resiliently. It takes care of scaling and failover for your application, provides deployment patterns, and more. For example, Kubernetes can easily manage a canary deployment for your system.
So Let me tell you about the task we are performing :
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 .
So let us start our task :
Step 1: Creating a Docker image with kubectl configured .
What is kubectl ?
From a user’s point of view, kubectl is your cockpit to control Kubernetes. It allows you to perform every possible Kubernetes operation.
From a technical point of view, kubectl is a client for the Kubernetes API.
The Kubernetes API is an HTTP REST API. This API is the real Kubernetes user interface. Kubernetes is fully controlled through this API. This means that every Kubernetes operation is exposed as an API endpoint and can be executed by an HTTP request to this endpoint.
Consequently, the main job of kubectl is to carry out HTTP requests to the Kubernetes API:
While creating this image we have to copy the client.crt , client.key, ca.crt files in docker image , we also need to copy config file in docker image it can be also created there inside image . While configuring kubectl command we also installed jenkins in docker image which will help us in automation .
FROM centos RUN yum install sudo -y RUN sudo yum install wget -y RUN yum install git -y #Jenkins RUN sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo RUN sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key RUN yum install java-11-openjdk.x86_64 -y RUN yum install initscripts.x86_64 -y RUN yum install jenkins -y RUN echo -e "jenkins ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers #Kubernetes 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 cp ./kubectl /usr/local/bin/kubectl RUN cp ./kubectl /usr/bin/ RUN mkdir /root/.kube COPY client.key /root/.kube COPY client.crt /root/.kube COPY ca.crt /root/.kube COPY config /root/.kube CMD /etc/rc.d/init.d/jenkins start && /bin/bash
This dockerfile is used to create image .
command to build docker image : docker build -f /path/to/a/Dockerfile .
Step 2 : Creating Jenkins Github Job
This job will download the github repo and will copy all the files to task3 directory.
Step 3: Launching the deployments
This job will see the code files that developer has uploaded to github and will create the deployment according to that code(if code files contains the code of html then it will launch the pod with html required environment , and if the code contain php related code then php environment will be launched) .
For creating deployment it is more easy to create .yml files then use it to create deployment.
Here is the .yml file I created to launch the environment for html environment.
#TO Expose webserver Pod apiVersion: v1 kind: Service metadata: name: webserver-task3 labels: app: webserver spec: ports: - port: 80 selector: app: webserver type: NodePort --- #PVC to make Data Persistent of webserver apiVersion: v1 kind: PersistentVolumeClaim metadata: name: webserver-vol spec: accessModes: - ReadWriteOnce resources: requests: storage: 2Gi --- #To Create a Deployment From webserver Image apiVersion: apps/v1 kind: Deployment metadata: name: webserver-deploy labels: app: webserver spec: replicas: 1 selector: matchLabels: app: webserver strategy: type: Recreate template: metadata: labels: app: webserver spec: containers: - image: httpd name: webserver ports: - containerPort: 8000 name: webserver volumeMounts: - name: webserver-vol mountPath: /usr/local/apache2/htdocs/ volumes: - name: webserver-vol persistentVolumeClaim: claimName: webserver-vol
Here is the .yml file I created for php environment .
#TO Expose Php Pod apiVersion: v1 kind: Service metadata: name: php-server-task3 labels: app: php spec: ports: - port: 8080 targetPort: 90 nodePort: 31000 selector: app: php type: NodePort --- #PVC to make Data Persistent of php apiVersion: v1 kind: PersistentVolumeClaim metadata: name: php-vol spec: accessModes: - ReadWriteOnce resources: requests: storage: 2Gi --- #To Create a Deployment From php Image apiVersion: apps/v1 kind: Deployment metadata: name: php-deploy labels: app: php spec: replicas: 1 selector: matchLabels: app: php strategy: type: Recreate template: metadata: labels: app: php spec: containers: - image: vimal13/apache-webserver-php name: php ports: - containerPort: 80 name: php volumeMounts: - name: php-vol mountPath: /var/www/html volumes: - name: php-vol persistentVolumeClaim: claimName: php-vol
Here starts our jenkins Job2 for creating deployment .
Step 4: Job for Testing the webapp
Step 5: Job for Informing the Developer about the error
Let us see our webapp ,
I manully failed the webserver too see what will happen , then I received this mail ,
Let us have look our deployments of K8S .
I also made a jenkins pipeline considering all the jobs for overview of all tasks and their status .
Hurray !! Done
This task I have done with my friend Atharva Patil .
Speacial Thanks to Vimal Daga Sir and whole Linuxworld Team .
Happy Learning !!
Software Engineer - Cloud Engineer at CRISIL Limited
4 年Great work shyam sulbhewar bro ????