Project - DevOps (Implementation)

Project - DevOps (Implementation)

  1. Goto AWS console and provision two Instances, named CI-Server and Deployment Server. CI-Server - (AMI- Ubuntu, Type- t2.micro) Deployment Server - (AMI- Ubuntu, Type- t2.medium)

    No alt text provided for this image

  1. Clone the code on CI-Server.

  2. Prerequisites

· On CI-Server install:

\> Install updates. (sudo apt-get update)

\> Install Docker. (sudo apt-get install docker.io -y)

\> Give permission to Docker (sudo usermod -aG docker $USER && newgrp docker)

· On Deployment Server:

\> Install updates. (sudo apt-get update)

\> Install Docker. (sudo apt-get install docker.io -y)

\> Give permission to Docker (sudo usermod -aG docker $USER && newgrp docker)

\> Minikube and Kubectl installation.

(curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64

sudo install minikube-linux-amd64 /usr/local/bin/minikube

sudo snap install kubectl --classic

minikube start --driver=docker)

  1. Write a Docker file for the project.

FROM node:19-alpine3.15

WORKDIR /reddit-clone

COPY . /reddit-clone

RUN npm install

EXPOSE 3000

CMD ["npm","run","dev"]

  1. Now, we need to build an image from the Dockerfile.

“docker build . -t trainwithshubham/reddit-clone”

  1. After building the image, push it to the docker hub, by running:

“docker login” give username and password.

“docker push trainwithshubham/reddit-clone”

  1. Now connect to the second instance “Deployment-server”.

  2. Install minikube and kubectl.

Now, we will deploy the docker image from “Docker Hub”.

  1. Create a folder named K8s.

  2. Inside the K8s folder, We will create a Deployment.yml file as,

apiVersion: apps/v1

kind: Deployment

metadata:

name: reddit-clone-deployment

labels:

app: reddit-clone

spec:

replicas: 2

selector:

matchLabels:

app: reddit-clone

template:

metadata:

labels:

app: reddit-clone

spec:

containers:

  • name: reddit-clone

    image: trainwithshubham/reddit-clone

    ports:

    • containerPort: 3000
  1. To apply the Deployment file,

“kubectl apply -f Deployment.yml”

  1. To verify the Deployments, run

“kubectl get deployments”

  1. Suppose, Deployment created 4 pods and those pods will have separate IP addresses now we need to access the application which has 4 replica pods. For that, we use “Service.yml” file.

  2. Service basically creates an IP for a cluster.

  3. Inside the K8s folder, create a file named “Service.yml” and add,

apiVersion: v1

kind: Service

metadata:

name: reddit-clone-service

labels:

app: reddit-clone

spec:

type: NodePort

ports:

- port: 3000

targetPort: 3000

nodePort: 31000

selector:

app: reddit-clone

  1. Now, apply the “Service.yml” file.

kubectl apply -f Service.yml

  1. To check the Service. Run,

kubectl get services

  1. Now, you can access the application that you have created with Deployment.yml. (2 Replicas). Run,

minikube service reddit-clone-service --url

It will give you an URL by which you can access the application from your Linux server itself.

  1. To check if we can access it. Run,

curl -L https://192.168.49.2:31000

  1. It is accessible from inside the VM but we want to run it via the Internet. For that, we need to expose our deployment. Run,

kubectl expose deployment reddit-clone-deployment --type=NodePort

  1. Now, you need to add a new rule for Port: 3000 in Inbound Rules.

  1. Now, we need to do the port forwarding for Port 3000. As

kubectl port-forward svc/reddit-clone-service 3000:3000 –address 0.0.0.0

  1. Now run, the public ip of youre instance and ":3000" eg: http://3.108.41.198:3000 The Reddit Clone is live and good to go!!!

  1. After this here comes the concept of ingress widely used in kubernetes

  2. Ingress is basically a combination of Nginx which uses reverse proxy and HA proxy in layman's language

  3. Definition:What is Ingress?

    * Ingress exposes HTTP and HTTPS routes from outside the cluster to services within the cluster. Traffic routing is controlled by rules defined on the Ingress resource.

  4. Now, we need to enable ingress from minikube. Run,

    minikube addons enable ingress

  5. Now, create a file named “Ingress.yml”

    apiVersion: networking.k8s.io/v1

    kind: Ingress

    metadata:

    name: ingress-reddit-app

    spec:

    rules:

    • host: "domain.com"

      http:

      paths:

      • pathType: Prefix

        path: "/test"

        backend:

        service:

        name: reddit-clone-service

        port:

        number: 3000

    • host: "*.domain.com"

      http:

      paths:

      • pathType: Prefix

        path: "/test"

        backend:

        service:

        name: reddit-clone-service

        port:

        number: 3000

      1. To check if your ingress is running fine. Run,

        kubectl get ingress ingress-reddit-app

        I expect that you find this a bit helpful.

        Uzair Bagwan

        Credits : Shubham Londhe

        #devops #devopscommunity # kubernetes #linux #AWS #thanks to Chetan Rakhra

        LinkedIn url: linkedin.com/in/uzair-bagwan-devops