Launching Website on top of KUBERNETES

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.

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

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 .

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

Step 4: Job for Testing the webapp

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

Step 5: Job for Informing the Developer about the error

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

Let us see our webapp ,

No alt text provided for this image

I manully failed the webserver too see what will happen , then I received this mail ,

No alt text provided for this image

Let us have look our deployments of K8S .

No alt text provided for this image

I also made a jenkins pipeline considering all the jobs for overview of all tasks and their status .

No alt text provided for this image

Hurray !! Done

This task I have done with my friend Atharva Patil .

Speacial Thanks to Vimal Daga Sir and whole Linuxworld Team .

Happy Learning !!

Aaditya Joshi

Software Engineer - Cloud Engineer at CRISIL Limited

4 年

Great work shyam sulbhewar bro ????

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

Shyam Sulbhewar的更多文章

  • Website Deployment Using Jenkins via Job Creation Using Groovy code

    Website Deployment Using Jenkins via Job Creation Using Groovy code

    Jenkins is an incredibly powerful tool for DevOps that allows us to execute clean and consistent builds and deployments…

    6 条评论
  • Metrics Collection and Monitoring Using Prometheus and Grafana

    Metrics Collection and Monitoring Using Prometheus and Grafana

    What is Metrics ? Metrics represent the raw measurements of resource usage or behavior that can be observed and…

    8 条评论
  • CI/CD PIPELINE

    CI/CD PIPELINE

    The CI/CD pipeline is one of the best practices for devops teams to implement, for delivering code changes more…

    10 条评论
  • Launching A Secure Wordpress site

    Launching A Secure Wordpress site

    Task Description : Write an Infrastructure as code using Terraform, which automatically creates a VPC. In that VPC we…

    9 条评论
  • Launching Website Using AWS with EFS using Terraform

    Launching Website Using AWS with EFS using Terraform

    What is AWS ? Amazon web service is a platform that offers flexible, reliable, scalable, easy-to-use and cost-effective…

    8 条评论
  • Face Recognition Using Transfer Learning (VGG16)

    Face Recognition Using Transfer Learning (VGG16)

    Face Recognition is a method to identify the identity of an individual using their face. It is capable of identifying a…

    8 条评论
  • Amazon EKS

    Amazon EKS

    Cloud computing is an internet-based computing service in which large groups of remote servers are networked to allow…

    6 条评论
  • MLOPS & DEVOPS TASK2

    MLOPS & DEVOPS TASK2

    1. Create container image that’s has Jenkins installed using dockerfile 2.

    4 条评论
  • MLOPS & DEVOPS Task1

    MLOPS & DEVOPS Task1

    Integration of git , github ,jenkins ,docker . Task Description : JOB#1 If Developer push to master branch then Jenkins…

    2 条评论
  • Integrating aws with Terraform

    Integrating aws with Terraform

    Task 1 : How to create/launch Application using Terraform on the top of aws. 1.

社区洞察

其他会员也浏览了