Kubernetes Assignment on Microservices & Reddit

Kubernetes Assignment on Microservices & Reddit

Β·

5 min read

Introduction :

In this blog, I will guide you through the exhilarating process of setting up your very first Kubernetes cluster using Kubeadm and will go through the steps of deploying a web application resembling Reddit on an AWS-hosted Kubernetes cluster.

prerequisites :

Ubuntu OS

t2.medium instance

sudo privileges: sudo su

Kubeadm Installation :

Both Master & Worker Node

Run the following commands on both the master and worker nodes to prepare them for kubeadm.

sudo su
apt update -y
apt install docker.io -y

systemctl start docker
systemctl enable docker

curl -fsSL "https://packages.cloud.google.com/apt/doc/apt-key.gpg" | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/kubernetes-archive-keyring.gpg
echo 'deb https://packages.cloud.google.com/apt kubernetes-xenial main' > /etc/apt/sources.list.d/kubernetes.list

apt update -y
apt install kubeadm=1.20.0-00 kubectl=1.20.0-00 kubelet=1.20.0-00 -y

Master Node

  1. Initialize the Kubernetes master node.

     sudo su
     kubeadm init
    
  2. Set up local kubeconfig (both for root user and normal user):

     mkdir -p $HOME/.kube
     sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
     sudo chown $(id -u):$(id -g) $HOME/.kube/config
    
  3. Apply Weave network:

     kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml
    
  4. Generate a token for worker nodes to join:

     kubeadm token create --print-join-command
    
  5. Expose port 6443 in the Security group for the Worker to connect to Master Node


Worker Node

  1. Run the following commands on the worker node.

     sudo su
     kubeadm reset pre-flight checks
    
  2. Paste the join command you got from the master node and append --v=5 at the end.


Verify Cluster Connection

On Master Node:

kubectl get nodes

Kubeadm installation is done in the master and worker node.

git url for reference:-

https://github.com/mgitwork027/kubernetes-kickstarter/blob/main/kubeadm_installation.md

1. Deployment of a Microservices Application on K8s

- Do Mongo Db Deployment

- Do Flask App Deployment

- Connect both using Service Discovery

A. clone microservice-k8s in your master node

git clone github.com/LondheShubham153/microservices-k..

B. Go inside the repository

/home/ubuntu/microservices-k8s/flask-api/k8s

C. Check the list of files

ls

D. View files

Mongo-pvc.yml

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

Mongo-svc.yml

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

Mongo.yml

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

Taskmaster-svc.yml

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

Taskmaster.yml

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: nsparthu/microservice-k8s:latest
          ports:
            - containerPort: 5000
          imagePullPolicy: Always

E. Apply the YML Files πŸ“

Apply all these files using this command -

kubectl apply -f <file name>

F. Add a port in the security group

go to the master node instance in the AWS account, go inside the security and create a port number as per your given in taskmaster.

G. Open a new tab in browser and paste

ip:port

54.216.91.137:30007

Congratulations!!! The microservice project deployed and running successfully .

2. Deployment of a Redit-Clone Application

- Do Deployment of Redit Clone app

- Write ingress controller for the same to give a custom route

A. clone Reddit-clone-k8s-ingress in your master node

git clone https://github.com/mgitwork027/reddit-clone-k8s-ingress.git

B. go inside the repository and check the list of files

/home/ubuntu/reddit-clone-k8s-ingress

ls

C. View files

Create a Deployment YAML File πŸ“

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

Create a Kubernetes ServiceπŸ“

Now, create a Service to provide an IP address for your application within the cluster.

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

Create ingress.yml fileπŸ“

With the help of Ingress, we can streamline how external traffic reaches our services within the Kubernetes cluster. It acts as an entry point for external traffic to the application. It simplifies routing and load balancing, making your applications more accessible and reliable.

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

D. Apply the YML Files πŸ“

Apply all these files (Deployment, Service, ingress file) using this command -

kubectl apply -f <file name>

E. check the running pods

kubectl get pods

F. Add a port in the security group

go to the master node instance in the AWS account, go inside the security and create a port number as per your given in taskmaster.

G. Open a new tab in browser and paste

ip:port

54.216.91.137:32189

Congratulations!!! The Redit Application project deployed successfully.

Conclusion:

Our journey, from AWS instance provisioning to Ingress routing, highlights the power and potential of Kubernetes. It's a game-changer for modern app deployment and management. So, whether you're a developer, a sysadmin, or just curious about the tech world, embrace Kubernetes and watch your applications flourish. 🌟

With Kubernetes by your side, you're all set to conquer the world of container orchestration. Cheers to a bright future filled with smooth deployments and happy users! πŸŒπŸš€

Β