Deployed a Reddit Clone with Kubernetes Ingress
Prerequisite
VM (Ubuntu, Type - Standard B2s (2 vcpus, 4 GiB memory) )
Docker
Minikube
kubectl
Clone the source code
Clone the GitHub repo: https://github.com/Ibrahimsi/reddit-clone-k8s-ingress.git
Write a docker file
Push the Image To DockerHub
Now it's time to build Docker Image from this Dockerfile. docker build -t <DockerHub_Username>
Push the Image To DockerHub
Now push this Docker Image to DockerHub so our Deployment file can pull this image & run the app in Kubernetes pods.
- First login to your DockerHub account using Command i.e. docker login and give your username & password.
Then use docker push <DockerHub_Username>/ for pushing to the DockerHub.
You can use an existing docker image i.e. ibrahimsi/reddit-clone for deployment.
Now it's time to build Docker Image from this Dockerfile.
docker build -t ibrahimsi/reddit-clone .
Login into your Deployment Server
When you are going to deploy to Kubernetes or create Kubernetes resources like a pod, replica-set, config map, secret, deployment, etc, you need to write a file called manifest that describes that object and its attributes either in yaml or json.
- Create a deployment file:
We have a deployment file for our app and we have a running Kubernetes cluster, we can deploy the app to Kubernetes. To deploy the app you need to run following the command: kubectl apply -f Deployment.yml
Now, to provide access to the application we will create a service.yml file:
Just Like this create a Service using kubectl apply -f Service.yml
If You want to check your deployment & Service use the command kubectl get deployment
& kubectl get services
Let's Configure Ingress
Ingress
Ingress exposes HTTP and HTTPS routes from outside the cluster to services within the cluster. Traffic routing is controlled by rules defined on the Ingress resource.
Let's write ingress.yml and put the following code in it:
Minikube doesn't enable ingress by default; we have to enable it first using minikube addons enable ingress
command.
- If you want to check the current setting for addons in minikube use
minikube addons list
command.
Now you can able to create ingress for your service. kubectl apply -f ingress.yml
use this command to apply ingress settings.
- Verify that the ingress resource is running correctly by using
kubectl get ingress ingress-reddit-app
command.
Expose the app
First We need to expose our deployment so use kubectl expose deployment reddit-clone-deployment --type=NodePort
command.
You can test your deployment using curl -L http://10.0.0.5:32000. 10.0.0.5: is a minikube ip & Port 32000 is defined in service.yml
- Then We have to expose our app service
kubectl port-forward svc/reddit-clone-service 3000:3000 --address 0.0.0.0 &
Copy public IP of your VM and add “ :3000”. Webpage will open.
Successfully Deployed a Reddit Copy on Kubernetes with Ingress Enabled.