Goto AWS console and provision two Instances, named CI-Server and Deployment Server. CI-Server - (AMI- Ubuntu, Type- t2.micro) Deployment Server - (AMI- Ubuntu, Type- t2.medium)
Clone the code on CI-Server.
Prerequisites
· On CI-Server install:
\> Install updates. (sudo apt-get update)
\> Install Docker. (sudo apt-get install docker.io -y)
\> Give permission to Docker (sudo usermod -aG docker $USER && newgrp docker)
· On Deployment Server:
\> Install updates. (sudo apt-get update)
\> Install Docker. (sudo apt-get install docker.io -y)
\> Give permission to Docker (sudo usermod -aG docker $USER && newgrp docker)
\> Minikube and Kubectl installation.
(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)
- Write a Docker file for the project.
FROM node:19-alpine3.15
WORKDIR /reddit-clone
COPY . /reddit-clone
RUN npm install
EXPOSE 3000
CMD ["npm","run","dev"]
- Now, we need to build an image from the Dockerfile.
“docker build . -t trainwithshubham/reddit-clone”
- After building the image, push it to the docker hub, by running:
“docker login” give username and password.
“docker push trainwithshubham/reddit-clone”
Now connect to the second instance “Deployment-server”.
Install minikube and kubectl.
Now, we will deploy the docker image from “Docker Hub”.
Create a folder named K8s.
Inside the K8s folder, We will create a Deployment.yml file as,
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: trainwithshubham/reddit-clone
ports:
- containerPort: 3000
- To apply the Deployment file,
“kubectl apply -f Deployment.yml”
- To verify the Deployments, run
“kubectl get deployments”
Suppose, Deployment created 4 pods and those pods will have separate IP addresses now we need to access the application which has 4 replica pods. For that, we use “Service.yml” file.
Service basically creates an IP for a cluster.
Inside the K8s folder, create a file named “Service.yml” and add,
apiVersion: v1
kind: Service
metadata:
name: reddit-clone-service
labels:
app: reddit-clone
spec:
type: NodePort
ports:
- port: 3000
targetPort: 3000
nodePort: 31000
selector:
app: reddit-clone
- Now, apply the “Service.yml” file.
“kubectl apply -f Service.yml
”
- To check the Service. Run,
“kubectl get services
”
- Now, you can access the application that you have created with Deployment.yml. (2 Replicas). Run,
“minikube service reddit-clone-service --url
”
It will give you an URL by which you can access the application from your Linux server itself.
- To check if we can access it. Run,
“curl -L
https://192.168.49.2:31000
”
- It is accessible from inside the VM but we want to run it via the Internet. For that, we need to expose our deployment. Run,
“kubectl expose deployment reddit-clone-deployment --type=NodePort
”
- Now, you need to add a new rule for Port: 3000 in Inbound Rules.
- Now, we need to do the port forwarding for Port 3000. As
“kubectl port-forward svc/reddit-clone-service 3000:3000 –address 0.0.0.0
- Now run, the public ip of youre instance and ":3000" eg: http://3.108.41.198:3000 The Reddit Clone is live and good to go!!!
After this here comes the concept of ingress widely used in kubernetes
Ingress is basically a combination of Nginx which uses reverse proxy and HA proxy in layman's language
Definition:What is 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.
Now, we need to enable ingress from minikube. Run,
“
minikube addons enable ingress
Now, create a file named “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
To check if your ingress is running fine. Run,
“
kubectl get ingress ingress-reddit-app
”I expect that you find this a bit helpful.
Uzair Bagwan
Credits : Shubham Londhe
#devops #devopscommunity # kubernetes #linux #AWS #thanks to Chetan Rakhra