Kubernetes Assignment🕸️Microservices & Reddit

·

4 min read

The following prerequisites need to be installed before we can start the Kubernetes Project:

  • Ubuntu OS (Xenial or later)

  • sudo privileges

  • Internet access

  • t2.medium instance type or higher


Kubeadm Installation


Both master and worker nodes ⬇️


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

# To connect with cluster execute above commands on master node and worker node respectively

Master node⬇️


sudo su
kubeadm init

# To start using your cluster, you need to run the following as a regular user:
  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

# Alternatively, if you are the root user, you can run:
  export KUBECONFIG=/etc/kubernetes/admin.conf

kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml

kubeadm token create --print-join-command

Note:-Expose port 6443 in the Security group for the Worker to connect to the Master Node


Worker node⬇️


sudo su
kubeadm reset pre-flight checks
-----> Paste the Join command on worker node and append `--v=5` at end

#To verify cluster connection


On master node⬇️


kubectl get nodes

Let's start the project:-

1. Deployment of a Microservices Application on K8s

- Do Mongo Db Deployment

- Do Flask App Deployment

- Connect both using Service Discovery

  • After that enter into that repository of microservices-k8s, flask-api, k8s if you list ls that file you will find some yml files which we call manifest file

  • Create a Kubernetes deployment and service by running the following command:

    kubectl apply -f <yml files.yml> briefly you can understand in photos. Before adding photos let's see yml file of MongoDB and Taskmaster

  • Mongo-pv.yml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mongo-pv
spec:
  capacity:
    storage: 256Mi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: /tmp/db
  • 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
  • After that apply them all using this command kubectl apply -f <yml files.yml>

  • After that go to your AWS account click on master node go to security group and create a port number as per your given in taskmaster.yml

    Example:- http://ip.adress:30007 http://3.82.213.122:30007

  • After that open a new tab and past your IP and port number

Congratualtion💥💥 Your microservice project completed

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

  • After that enter into that repository of Reddit-clone-k8s-ingress if you list ls that file you will find some yml files which we call manifest file

  • Create a Kubernetes deployment and service by running the following command:

    kubectl apply -f <yml files.yml> briefly you can understand in photos. Before adding photos let's see yml file of Reddit-clone-k8s-ingress

  • Deployment.yml

  • Service.yml

  • Ingress.yml

  • After that apply them all using this command kubectl apply -f <yml files.yml>

  • After that go to your AWS account click on master node go to a security group and create a port number Example:- http://ip.adress32189 http://3.88.137.167:32189

  • After that open a new tab and past your IP and port number Reddit will open

Congratualtion💥💥 Your Application project is completed and your Reddit account created


Happy Learning

Thanks For Reading! :)

-Sriparthu💝