# 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](https://media.licdn.com/dms/image/D4E12AQEL8n2kYOZJWg/article-inline_image-shrink_1500_2232/0/1678894646022?e=1684972800&v=beta&t=ag1Q_X6qkzVnR_QBEY_9guBNilVBFzIIk0uBHIYu8YI align="left")
    

2.     Clone the code on CI-Server.

3.     Prerequisites

·       On CI-Server install:

\&gt; Install updates. (sudo apt-get update)

\&gt; Install Docker. (sudo apt-get install [docker.io](http://docker.io) -y)

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679469684806/a52fc2d3-13c7-4441-a030-180825ff3960.png align="left")

·       On Deployment Server:

\&gt; Install updates. (sudo apt-get update)

\&gt; Install Docker. (sudo apt-get install [docker.io](http://docker.io) -y)

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

\&gt; Minikube and Kubectl installation.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679469768848/8145fc6a-e824-4bad-ab8d-d59bdf8809f9.png align="center")

(curl -LO [https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64](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)

4.     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"\]

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

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679469823579/9a25a12e-3825-4a80-9751-0864b67ae558.png align="center")

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

“docker login” give username and password.

“docker push trainwithshubham/reddit-clone”

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679469923847/fc687fba-7b1a-41db-b66d-2c2165e2189e.png align="center")

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

8.     Install minikube and kubectl.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679469983714/01eddd2b-72b6-4a28-b615-ce95dfd6723e.png align="center")

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

10.  Create a folder named K8s.

11.  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

12.  To apply the Deployment file,

`“kubectl apply -f Deployment.yml”`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679470136545/1c237717-c0eb-4cfa-b94f-93feab01f94a.png align="center")

13.  To verify the Deployments, run

`“kubectl get deployments”`

14.  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.

15.  Service basically creates an IP for a cluster.

16.  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**

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

“`kubectl apply -f Service.yml`”

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679470378395/6d299be2-8010-47aa-857d-47f1337e0d1b.png align="center")

18.  To check the Service. Run,

“`kubectl get services`”

19.  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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679470566874/f2a32d45-e1ce-4e66-939d-b95aa2000d17.png align="center")

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

“`curl -L` [`https://192.168.49.2:31000`”](https://192.168.49.2:31000”)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679470665440/d2511829-8576-4bdb-bdff-de15bcae8b4b.png align="center")

21.  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`”

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679470756934/75489336-45ea-4979-b46d-11a26166663f.png align="center")

23.  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`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679470838922/f8418b89-9129-454e-b2f7-aa825970c215.png align="center")

24.  Now run, the public ip of youre instance and ":3000" eg: [http://3.108.41.198:3000](http://54.193.214.183:3000/) The Reddit Clone is live and good to go!!!

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679471024907/b6759c68-56ad-44c5-9118-f9df302e8dce.png align="center")

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](https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.26/#ingress-v1-networking-k8s-io) exposes HTTP and HTTPS routes from outside the cluster to [services](https://kubernetes.io/docs/concepts/services-networking/service/) 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](http://networking.k8s.io/v1)
    
    kind: Ingress
    
    metadata:
    
     name: ingress-reddit-app
    
    spec:
    
     rules:
    
     - host: "[domain.com](http://domain.com)"
    
       http:
    
         paths:
    
         - pathType: Prefix
    
           path: "/test"
    
           backend:
    
             service:
    
               name: reddit-clone-service
    
               port:
    
                 number: 3000
    
     - host: "\*.[domain.com](http://domain.com)"
    
       http:
    
         paths:
    
         - pathType: Prefix
    
           path: "/test"
    
           backend:
    
             service:
    
               name: reddit-clone-service
    
               port:
    
                 number: 3000
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679471307875/c4410ca3-bd1a-468a-bfb2-0f1c108d2b0e.png align="center")
    
    1.  To check if your ingress is running fine. Run,
        
        “`kubectl get ingress ingress-reddit-app`”
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679471543906/07e5f851-2ae1-48a0-b5a2-6cef03402cee.png align="center")
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1679471680354/f5cde24f-4fd7-44fd-9c9b-860552acf476.png align="center")
        
        I expect that you find this a bit helpful.
        
        Uzair Bagwan
        
        Credits : @[Shubham Londhe](@TrainWithShubham)
        
        #devops #devopscommunity # kubernetes #linux #AWS #thanks to @[Chetan Rakhra](@chxtan)
        
        LinkedIn url: [**https://www.linkedin.com/in/uzair-bagwan-devops**](https://www.linkedin.com/in/uzair-bagwan-devops)
