Deploy a Reddit Clone with Kubernetes Ingress

Deploy a Reddit Clone with Kubernetes Ingress

Prerequisites for K8s ingress

Create 2 Ubuntu EC2 Servers

CI server - t2 micro and for Deployment - t2 medium

CI Server (t2 micro)

On t2 micro instance, install Docker and copy the clone from GitHub

New docker for Ubuntu from the official page link

Run the following command to uninstall all conflicting packages
$ for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do sudo apt-get remove $pkg; done

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

# Add the repository to Apt sources:
echo \
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

# Install latest version
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo usermod -aG docker $USER && newgrp docker
sudo reboot

Clone the Reddit project from GitHub into an

Instance

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

Containerize the Application using the Docker file

You will find the docker file inside the src

FROM node:19-alpine3.15
WORKDIR /reddit-clone
COPY . /reddit-clone
RUN npm install 
EXPOSE 3000
CMD ["npm","run","dev"]

Reddit application is based on nodejs.

Create a container from the docker file with the DockerHub tag. Once the image is created we can push the same image into DockerHub

docker build . -t rjthapaa/redditimage

Now login to DockerHub (Signup if you are a new user) and push the images into the registry by using the below commands

docker login
docker push rjthapaa/redditimage

Check the DockerHub registry for the newly pushed image

Deployment server (t2 medium)

Install Docker, Minikube cluster and Kubectl

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl gnupg
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

# Add the repository to Apt sources:
echo \
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

# Install latest version
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo usermod -aG docker $USER && newgrp docker
sudo reboot
# Minikube & Kubectl
curl -LO 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

Kubernetes manifest file

To deploy any application on K8S resources like Pods, replica-set, replica-controller, deployment etc. We need to create and write a manifest.yaml file.

We need Deployment.yaml to deploy an application and service.yaml to access the application services. create deployment and service.yaml files and run them to deploy the application and access it

mkdir ingress
cd ingress
nano Deployment.yaml

Deployment.yaml

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: rjthapaa/redditimage      # replace with your own image
        ports:
        - containerPort: 3000

Service.yaml

nano service.yaml
apiVersion: v1
kind: Service
metadata:
  name: reddit-clone-service
  labels:
    app: reddit-clone
spec:
  type: NodePort
  selector:
    app: reddit-clone
  ports:
  - port: 3000
    targetPort: 3000
    nodePort: 31000

Run the below commands for deployment and service

kubectl apply -f deployment.yml
kubectl apply -f service.yml

Check the running Deployment, Pods and svc(services)

kubectl get deployment 
kubectl get pods
kubectl get svc

Access the Service using the below command

minikube service < service name > --url
minikube service reddit-clone-service --url
# Gives you http://192.168.49.2:31000

# Use below command to access the application inside Minikube
curl -L http://192.168.49.2:31000

Expose the application globally using the below command

kubectl expose deployment reddit-clone-deployment --type=NodePort
kubectl port-forward svc/reddit-clone-service 3000:3000 --address 0.0.0.0 &

Ingress

# Enable the Ingress
minikube addons enable ingress

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

Edit, apply and access the ingress Commands

vim ingress.yml
kubectl apply -f ingress.yml
curl -L domain.com/test

Learn more about ingress

https://www.youtube.com/watch?v=9tl0A_rwgu4&t=279s

https://www.youtube.com/watch?v=hGG8cq70XQ0