credits: TrainwithShubham/youtube
💠Create master node and worker node instance
Instance type should be t2.medium - Cluster requires atleast 4 GB ram and 2 CPUs because of multi-node architecture.
⭐Prerequisite
install all the dependencies using the script provided by Rushikesh Mashidkar.
https://github.com/RishikeshOps/Scripts/blob/main/k8sss.sh
💠Deployment.yml/Taskmaster
For setting up the application pod will use taskmaster.yml which is a Deployment configuration file and apply.
💠Scaling
Scaling up to 3 Pods for Load Balancing the Traffic.
docker ps to check your containers running in applications pods.
💠Service.yml/Taskmaster
Apply services for applications pod ( creates a Nord port which is mapped to service to access application pods (target port 5000) for External Access) Also creates a Cluster IP to for internal communication
Expose port 30007 in inbound rules to access our Application.
💠Postman/TestApplication
microservice application is deployed in the Kubeadm cluster on port 30007.
copy the ip of the worker node and paste in Postman to check application is running or not
💠Persistent volume
Creating Persistent volume claim to claim persistent volume that has created.
💠Deployment.yml/Mongodb
Inside MongoDB deployment.yml file will specify the Persistent volume claim to give a claim of persistent volume to pods.
💠Service.yml/Mongodb
Headless service
DNS-Based Service Discovery: When you create a headless service, Kubernetes sets up DNS entries for each pod associated with the service. Each pod gets its own DNS record in the form of <pod-name>.<service-name>.<namespace>.svc.cluster.local
. This allows other pods in the same namespace to discover and connect to individual pods in a stateful manner.
to check the mongo db container
"docker exec -it id bash"
"mongosh" its a shell command for mongo.
Means Mongodb container is deployed and our database is working fine.
💠Connecting Microservices
If you want to establish a connection from an application pod to a MongoDB pod in a microservices architecture in Kubernetes:
you need to use ClusterIP and ExternalName services for DNS resolution.
➡️Create ClusterIP Service for MongoDB:
Create a ClusterIP service that targets the MongoDB service name and port.
apiVersion: v1
kind: Service
metadata:
name: mongodb-clusterip-svc
spec:
selector:
app: mongodb
ports:
- protocol: TCP
port: 27017
targetPort: 27017
➡️Create ExternalName Service for Application:
Create an ExternalName service for your application pod(s) to connect to MongoDB. This ExternalName service will map to the ClusterIP service .
yamlCopy codeapiVersion: v1
kind: Service
metadata:
name: mongodb-external-svc
spec:
type: ExternalName
externalName: mongodb-clusterip-svc.default.svc.cluster.local # Replace with your namespace if not in 'default'
ports:
- protocol: TCP
port: 27017
➡️Configure Your Application Pods:
Inside your application pods, configure the MongoDB connection string to use the DNS name provided by the ExternalName service. The connection string will be in the format of
mongodb-external-svc.<namespace>.svc.cluster.local:<port>
.For example, in your application configuration:
yamlCopy codemongodb_uri: mongodb-external-svc.default.svc.cluster.local:27017
Replace
<namespace>
with your actual namespace if it's not in the "default" namespace.
With these configurations in place, your application pods can connect to MongoDB using the DNS name provided by the ExternalName service. Kubernetes will handle DNS resolution and routing, ensuring that your application communicates with the MongoDB ClusterIP service, which, in turn, routes traffic to the MongoDB StatefulSet pods.
📍CONCLUSION
Deployed Microservice Application on Kubernetes using Kubeadm k8s cluster.Flask/MongoDB.