Table of contents
Hello everyone in this blog we gonna do a project on Kubernetes that brings the reddit clone image from the docker hub and deploy through Kubeadm cluster with the help of deployment.yml and service.yml file let's do it step-by-step.
Prerequisite
Make two instance naming Kubernetes-master and Kubernetes-worker with instance type both set to t2.medium as shown in the below image
-
Make a Kubernetes cluster in this if you don't know how to do it please check this blog.
To confirm that your are connected run these commands in the master server
kebectl get nodes
You should see like this in the image shown below
Step 1: Create a namespace called reddit-clone-ns by running these commands
kubectl create namespace reddit-clone-ns
In the below image you can see we have created "reddit-clone-ns" namespace
Step 2: Make a directory called deployment_k8s in master server and change directory to it
Step 3: Create a deployment.yml file in that directory
Step 4: In deployment.yml run these commands
apiVersion: apps/v1
kind: Deployment
metadata:
name: reddit-clone-deployment
namespace: reddit-clone-ns
labels:
app: reddit-clone
spec:
replicas: 2
selector:
matchLabels:
app: reddit-clone
template:
metadata:
labels:
app: reddit-clone
spec:
containers:
- name: reddit-clone
image: gurucharan22/reddit-clone-app:latest
ports:
- containerPort: 3000
here containerPort is 3000 because our reddit clone image we have expose port number 3000 and "gurucharan22/reddit-clone-app:latest" is our docker hub image id from that we are pulling that image
Step 5: Run these commands on master node
kubectl apply -f deployment.yml
Step 6: Run these commands on master node to confirm that what are the pods running in reddit-clone-ns namespace.
kubectl get pods -n=reddit-clone-ns
Step 7: Create a service.yml file in that directory
Step 8: In service.yml run these commands
apiVersion: v1
kind: Service
metadata:
name: reddit-clone-service
namespace: reddit-clone-ns
spec:
type: NodePort
selector:
app: reddit-clone
ports:
# By default and for convenience, the `targetPort` is set to the same value as the `port` field.
- port: 80
targetPort: 3000
# Optional field
# By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767)
nodePort: 30007
Step 9: Run these commands on master node
kubectl apply -f service.yml
Step 10: Run these commands on master node to confirm that what are the services running in reddit-clone-ns namespace.
kubectl get service -n reddit-clone-ns
Step 11: Run these commands in worker node
sudo docker ps
here you can see two replicas are running
Step 12: In the worker node open port 30007 in the security group and copy its Public IP address and paste it to google chrome
Now you see we have successfully deployed our application reedit clone through Kubernetes
Thank you for reading this blog and if any queries or if any corrections to be done in this blog please let me know.
contact us in Linkedin ,Twitter or email-id gurucharanu716@gmail.com