# Kubernetes Assignment

## 🚀 Deployment of a Microservices Application on K8s

* Do Mongo Db Deployment 📦
    
* Do Flask App Deployment 📦
    
* Connect both using Service Discovery 🔄
    

Clone the files to the local system

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693818310427/f4c13761-f11c-490a-bcc3-428918b1166a.png align="center")

Run ls to list all the files and go to the k8s folder to see all files of the Mongo DB file

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693818352676/d040c418-cfad-4d15-ab88-13c0c9a27a99.png align="center")

Create mongopv.yml to create the persistent volume

```basic
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mongo-pv
spec:
  capacity:
    storage: 256Mi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /tmp/db
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693818855427/aa70f113-ae54-4ad5-b407-9b7e3f61b4c1.png align="center")

Create a mongo-pvc.yml file to claim persistent volume

```basic

kind: PersistentVolumeClaim
metadata:
  name: mongo-pvc
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 256Mi
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693818988407/4aebaab3-976a-40d4-aaf8-678dfeb83b9c.png align="center")

Create mongo-svc.yml for service

```basic
apiVersion: v1
kind: Service
metadata:
  labels:
    app: mongo
  name: mongo
spec:
  ports:
    - port: 27017
      targetPort: 27017
  selector:
    app: mongo
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693819137993/27fbb80e-7499-478e-b614-031b9a5a588e.png align="center")

Create a mongo.yml file to create Mongo deployment

```basic
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongo
  labels:
      app: mongo
spec:
  selector:
    matchLabels:
      app: mongo
  template:
    metadata:
      labels:
        app: mongo
    spec:
      containers:
        - name: mongo
          image: mongo
          ports:
            - containerPort: 27017
          volumeMounts:
            - name: storage
              mountPath: /data/db
      volumes:
        - name: storage
          persistentVolumeClaim:
            claimName: mongo-pvc
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693819251682/25d96c48-784a-4f8c-b67c-7e7d271e000c.png align="center")

Get the pods

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693819447229/e3c09916-6900-427f-a38d-d83c28c83530.png align="center")

Create the taskmaster.yml for deployment.

```basic
apiVersion: apps/v1
kind: Deployment
metadata:
  name: taskmaster
  labels:
    app: taskmaster
spec:
  replicas: 1
  selector:
    matchLabels:
      app: taskmaster
  template:
    metadata:
      labels:
        app: taskmaster
    spec:
      containers:
        - name: taskmaster
          image: trainwithshubham/taskmaster-python:latest
          ports:
            - containerPort: 5000
          imagePullPolicy: Always
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693819584279/37ada33e-7edb-45eb-949e-adf8b359af74.png align="center")

Create taskmaster-svc.yml for service .

```basic
apiVersion: v1
kind: Service
metadata:
  name: taskmaster-svc
spec:
  selector:
    app: taskmaster
  ports:
    - port: 80
      targetPort: 5000
      nodePort: 30007
  type: NodePort
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693819657271/e1b99a10-4ecd-46ac-a569-815da5e45f94.png align="center")

Check whether the container is created or not on the worker node

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693819752377/f5d3e5e4-4a88-4b7a-8526-b1b44ab1d378.png align="center")

Get the pods

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693819774136/5c4e3bf7-bf98-4fb7-af88-db702ffbea78.png align="center")

Get the deployment

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693821580694/77400fb4-7311-4af9-a761-884713cbe408.png align="center")

Get the persistentvolume

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693821631502/1b0014e8-3521-4a3e-953e-6bdb4fc9652d.png align="center")

Open the browser and open the URL - http://&lt;public\_ip&gt;:30007

successfully access the flask-app

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693821379758/6e88a6f8-d0c9-4b55-a813-b0d28df29f55.png align="center")

## 🚀 Deployment of a Reddit-Clone Application

* Do Deployment of the Reddit Clone app 📦
    
* Write an ingress controller for the same to give a custom route 🛣️
    

Clone the Reddit repository to the local system

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693823298776/e2d95055-d146-4afa-a4ca-4ff1b671ed20.png align="center")

Run ls to list all the files in the flask-app repo

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693823424375/ba681275-bd66-49e0-9712-c5858e783e04.png align="center")

and create a deployment.yml file to create pods

```basic
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: rohanrustagi18/redditclone
        ports:
        - containerPort: 3000
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693823532636/30292337-1c65-422f-a2fc-41453da40612.png align="center")

Create service.yml

```basic
apiVersion: v1
# Indicates this as a service
kind: Service
metadata:
  # Service name
  name: reddit-clone-service
spec:
  selector:
    # Selector for Pods
    app: reddit-clone
  ports:
    # Port Map
  - port: 3000
    targetPort: 3000
    protocol: TCP
  type: LoadBalancer
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693823611237/7ef4d52e-e362-42b3-b81a-e4affe8aee3a.png align="center")

Create ingress.yml

```basic
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
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693824741148/1f0417ec-330c-4f25-9064-b5f203f2ba31.png align="center")

Get the service details and port number

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693826157795/bf60afec-23e7-40dc-832d-8ae34fca8656.png align="center")

Open the browser and run the URL - http://&lt;public\_ip&gt;:31687

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693825977152/fdd6e933-3581-4e88-b588-a29aab6f6f4a.png align="center")
