Step-by-Step Guide to Deploying a Chat Application on Kubernetes Using Minikube
Table of contents
- Step 1: Create a Minikube Cluster
- Step 2: Clone the Application Locally
- Step 3: Login to DockerHub
- Step 4: Build Docker Image for the Backend
- Step 5: Build Docker Image for the Frontend
- Step 6: Verify Docker Images
- Step 7: Push Images to DockerHub
- Step 8: Create Kubernetes Manifests
- Step9: Apply Manifests
- Step 10: Verify Pods and Services
- Step 11: Access the Application
- Step 12: Host on a Custom Domain (Optional)
- Conclusion
Setting up a chat application on Kubernetes might seem like a daunting task, but with the right steps, it becomes manageable and even enjoyable. This blog will walk you through creating a Minikube cluster, building Docker images, and deploying your chat application using Kubernetes manifests.
Step 1: Create a Minikube Cluster
To get started, ensure Minikube is installed on your system. Then, start a Minikube cluster using Docker as the driver:
minikube start --driver=docker
Step 2: Clone the Application Locally
Clone the chat application repository to your local system.
Step 3: Login to DockerHub
Login to DockerHub from the CLI so you can push the Docker images later:
docker login
Step 4: Build Docker Image for the Backend
Navigate to the backend directory and build the Docker im
docker build -t harshsoni777/chatapp-backend:latest .
Step 5: Build Docker Image for the Frontend
Similarly, navigate to the frontend directory and build the image:
docker build -t harshsoni777/chatapp-frontend:latest .
Step 6: Verify Docker Images
List all Docker images to verify both images were built successfully:
docker images
Step 7: Push Images to DockerHub
Push both images to DockerHub:
Step 8: Create Kubernetes Manifests
Create the necessary YAML files for Kubernetes resources. Below are the manifests for namespace, deployments, services, and secrets.
Namespace
Create a namespace.yml
file:
apiVersion: v1
kind: Namespace
metadata:
name: chat-app
Frontend-Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend-deployment
namespace: chat-app
spec:
replicas: 1
selector:
matchLabels:
app: frontend
template:
metadata:
name: frontend-pod
namespace: chat-app
labels:
app: frontend
spec:
containers:
- name: chat-app-frontend
image: harshsoni777/chatapp-front:latest
ports:
- containerPort: 80
env:
- name: NODE_ENV
value: production
Backend-Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: backend-deployment
namespace: chat-app
spec:
replicas: 1
selector:
matchLabels:
app: backend
template:
metadata:
name: backend-pod
namespace: chat-app
labels:
app: backend
spec:
containers:
- name: chat-app-backend
image: harshsoni777/chatapp-backend:latest
ports:
- containerPort: 5001
env:
- name: NODE_ENV
value: production
- name: MONGODB_URI
value: "mongodb://mongoadmin:secret@mongodb:27017/dbname?authSource=admin"
- name: JWT_SECRET
valueFrom:
secretKeyRef:
name: chatapp-secrets
key: jwt
- name: PORT
value: "5001"
resources:
limits:
memory: "512Mi"
cpu: "500m"
requests:
memory: "256Mi"
cpu: "250m"
MongoDB Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongodb-deployment
namespace: chat-app
spec:
replicas: 1
selector:
matchLabels:
app: mongodb
template:
metadata:
name: mongodb-pod
namespace: chat-app
labels:
app: mongodb
spec:
containers:
- name: chat-app-mongodb
image: mongo:latest
ports:
- containerPort: 27017
env:
- name: MONGO_INITDB_ROOT_USERNAME
value: mongoadmin
- name: MONGO_INITDB_ROOT_PASSWORD
value: secret
volumes:
- name: mongodb-data
persistentVolumeClaim:
claimName: mongodb-pvc
Persistent Volumes
Define storage for MongoDB with mongodb-pv.yml
and mongodb-pvc.yml
files.
Persistent Volume:
apiVersion: v1
kind: PersistentVolume
metadata:
name: mongodb-pv
spec:
capacity:
storage: 5Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /mnt/data
Persistent Volume Claim:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mongodb-pvc
namespace: chat-app
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
Backend Service:
apiVersion: v1
kind: Service
metadata:
name: backend
namespace: chat-app
spec:
selector:
app: backend
ports:
- port: 5001
targetPort: 5001
Frontend Service:
apiVersion: v1
kind: Service
metadata:
name: frontend
namespace: chat-app
spec:
selector:
app: frontend
ports:
- port: 80
targetPort: 80
MongoDB Service:
apiVersion: v1
kind: Service
metadata:
name: mongodb
namespace: chat-app
spec:
selector:
app: mongodb
ports:
- port: 27017
targetPort: 27017
Create secrets.yml for storing JWT secrets
- Generate the code using an online JWT Generator, then create a base64 encoded key and paste it here.
apiVersion: v1
kind: Secret
metadata:
name: chatapp-secrets
namespace: chat-app
type: Opaque
data:
jwt: <base64 encoded jwt code>
Step9: Apply Manifests
Apply Persistent Volume and PVC
Apply all the Kubernetes manifests to set up the application.
kubectl apply -f mongodb-deployment.yml
kubectl apply -f backend-deployment.yml
kubectl apply -f backend-service.yml
kubectl apply -f frontend-deployment.yml
kubectl apply -f frontend-service.yml
Step 10: Verify Pods and Services
Check the status of your pods and services:
kubectl get pods -n chat-app
kubectl get services -n chat-app
Pods
Services
Step 11: Access the Application
Port forward the services to access the application:
kubectl port-forward service/backend -n chat-app 5001:5001 &
kubectl port-forward service/frontend -n chat-app 81:80 &
Access the application at http://localhost:81
.
Step 12: Host on a Custom Domain (Optional)
Edit the /etc/hosts
file to use a custom domain:
sudo vim /etc/hosts
Add the following line:
127.0.0.1 chatCraze.com
Now, you can access the app at http://www.chatCraze.com
.
Conclusion
By following this guide, you have successfully deployed a fully functional chat application on Kubernetes using Minikube. This hands-on approach not only strengthens your understanding of Kubernetes concepts but also prepares you for deploying applications in real-world scenarios. If you have any questions or suggestions, feel free to share them in the comments below!