Project: Deployment of Reddit Clone App with Ingress on K8s Cluster Using Kubeadm

Ahmed NisarAhmed Nisar
4 min read

💥Introduction:

In this project, we have a Reddit clone app 🌐 which is an exact replicate of the original Reddit app. For sake of learning, I will be developing this app via the K8s Ingress manifest file. Now, as a DevOps engineer 👷‍♂️, our task is to deploy this application on a Kubernetes cluster using Kubeadm .🚀

💥Installation Guide K8s:

Please proceed with the following instructions to configure the Kubernetes cluster.

  • 👉🏻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 the 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 the 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:

ahmed@master:~/TWS-Project/flask_mongodb$ kubectl get nodes
NAME     STATUS   ROLES                  AGE   VERSION
master   Ready    control-plane,master   14h   v1.20.0
node-1   Ready    <none>                 14h   v1.20.0

💥Reddit-Microservice- Project:

  1. Run the following commands on the worker node.

     https://github.com/ahmednisarhere/K8s-Clone-Reddit-Ingress.git
    
  2. Create a Dockerfile for the Flask app.

     FROM node:19-alpine3.15
    
     WORKDIR /reddit-clone
    
     COPY . /reddit-clone
     RUN npm install 
    
     EXPOSE 3000
     CMD ["npm","run","dev"]
    
  3. Build the Dockerfile and push it to DockerHub for K8s deployment.

     docker build . -t reddit-app
     docker login
     docker tag <your-dockerhub-id>/microservice:latest
     docker push <your-dockerhub-id>/microservice:latest
    
  4. Create a microservice "deployment.yml" for the microservice container.

     apiVersion: apps/v1
     kind: Deployment
     metadata:
       name: reddit-clone-deployment
       labels:
         app: reddit-clone
     spec:
       replicas: 1
       selector:
         matchLabels:
           app: reddit-clone
       template:
         metadata:
           labels:
             app: reddit-clone
         spec:
           containers:
           - name: reddit-clone
             image: ahmed0001/reddit-clone
             ports:
             - containerPort: 3000
    
  5. To access the reddit app from outside the cluster we have to create a service manifest file "service.yml". In K8s, three types of services were used that is node port, ClusterIP, and LoadBalancer. Here in microappservice.yml port was mapped target port and the port was assigned with node port 30001 for microservice

     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
         nodePort: 31000
         protocol: TCP
       type: NodePort
    
  6. Now we have to create an "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
    
  7. Now all the necessary manifest files are created and we have to deploy the all manifest files

     kubectl apply -f deployment.yml,services.yml,ingress.yml
    
  8. Check all manifests individually if they are in running state

     kubectl get pods
     kubectl get deployments
     kubectl get ingress
     kubectl get services
    
  9. Go the Web browser and type the below link

     http://localhost:31000/
    

  10. First We need to expose our deployment

    kubectl expose deployment reddit-deployment --type=NodePort --port=500
    
  11. Then We have to expose our app service

    kubectl port-forward svc/reddit-clone-service 3000:3000 --address 0.0.0.0 &
    
  12. Go to the browser and type in URL http://<Public_IP>:3000 or edit the /etc/host and add the (localhost domain.com/test) to access via URL.

Conclusion:

In summary, deploying the Reddit app on Kubernetes (K8s) with Ingress has streamlined web traffic management, improved user experience, and demonstrated Kubernetes' effectiveness in hosting modern web applications.

0
Subscribe to my newsletter

Read articles from Ahmed Nisar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ahmed Nisar
Ahmed Nisar

AWS Devops | Linux | Docker | Jenkins | Terraform | Kubernetes | Ansible |