In the previous blog, we have worked with namespaces in K8s: Create a Namespace for Deployment.👈
Now, today we will work with Services in K8s.✨
Service
enables communication between various components within and outside of the application or front-end and backend pods and helps establishing connectivity to an external data source. It helps us to connect application together with other applications or users.
There are different types of services:
ClusterIP
: For communication between different parts of your app inside the cluster.NodePort
: If you want people outside the cluster to access your app.LoadBalancer
: When you need an external load balancer to distribute traffic to your app.ExternalName
: To connect your app to an external service by name.
For more details on Services you can refer to the given link: Services.👈
Create a Service for your todo-app Deployment
Step 1: We are taking todo-app from our Day-32 blog. Refer to Deploy a sample todo app.
Step 2: Create a Service definition for your todo-app Deployment in a YAML file. Now, Create a service.yml
where we will deploy NodePort.
apiVersion: v1
kind: Service
metadata:
name: my-django-app-service
namespace: my-django-app
spec:
type: NodePort
selector:
app: django-app
ports:
# By default and for convenience, the 'targetPort' is set to the same value as the 'port' field.
- port: 80
targetPort: 8000
# Optional field
# By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767)
nodePort: 30009
Step 3: Apply the Service definition to your K8 cluster using the kubectl apply -f service.yml.
kubectl apply -f service.yml
We got the above error, so when you want to create any pod, deployment, or service file then once start your machine if you are working on minikube.
Step 4: Start the minikube and again try to apply the service definition.
minikube start
You can see in the above diagram the service has been applied successfully.
Step 5: Get the service details by the below command:
# Getting the service. (svc is short form of service)
kubectl get svc -n=my-django-app
Step 6: Now go to your instance machine go into security and edit inbound rules expose the port that you describe in the service file
So edit the inbound rule of the worker node and add port number 30009 so that we can access the pod outside.
Step 7: Verify that the Service is working by accessing the todo-app using the Service’s IP and Port in your Namespace.
Create a ClusterIP Service for accessing the todo-app
Step 1: Create a ClusterIP Service for accessing the todo-app from within the cluster.
vim cluster-ip-service.yml
apiVersion: v1
kind: Service
metadata:
name: my-django-app-cluster
namespace: my-django-app
spec:
type: ClusterIP
selector:
app: django-app
ports:
- name: http
protocol: TCP
port: 8000
targetPort: 8000
Step 2: Apply the Service definition to the cluster using the following command:
kubectl apply -f cluster-ip-service.yml -n my-django-app
Step 3: Verify that the service is running by running the following command:
kubectl get svc -n my-django-app
Step 4: Deploy another Pod in the my-django-app namespace to test the service. You can use the following YAML definition to create a simple test Pod:
apiVersion: v1
kind: Pod
metadata:
name: test-pod
namespace: my-django-app
spec:
containers:
- name: busybox
image: busybox
command: ['sh', '-c', 'while true; do wget -q -O- my-django-app-cluster-ip:8000; done']
Step 5: Apply the Pod definition using the following command:
kubectl apply -f test-pod.yml -n my-django-app
Step 6: Now enter into this pod :
kubectl exec -it test-pod -n my-django-app -- /bin/sh
Step 7: Edit inbound rule in security group. Add 10250 port.
Step 8: Verify that the ClusterIP Service is working by accessing the todo-app from another Pod in the cluster in your Namespace.
Step 9: Test Todo app.
# Inside the test pod execute the wget commad.
wget -qO- http://<ip-of-my-django-app-cluster-ip>:<port
Now you successfully Access it through another Port:
Create a LoadBalancer Service for accessing the todo-app
Step 1: Create a YAML file named load-balancer-service.yml with the following contents:
apiVersion: v1
kind: Service
metadata:
name: my-django-app-cluster-ip
namespace: my-django-app
spec:
selector:
app: django-app
ports:
- port: 80
targetPort: 8000
type: LoadBalancer
Step 2: Apply the LoadBalancer Service definition to your K8s cluster using the following command:
kubectl apply -f load-balancer-service.yml -n my-django-app
Step 3: Verify that the LoadBalancer Service is working by accessing the todo-app from outside the cluster in your namespace.
kubectl get service -n=my-django-app
Step 4: Get the IP from the service and then open the port in the Worker Node.
Step 5: Copy the Public IPv4 DNS and access through the browser by providing the port number in the URL.
Public-IPv4-DNS:<loadbalancer-port-number>
Conclusion
In Conclusion, Services
in Kubernetes enable communication and discovery between different parts of an application or between applications. They abstract the underlying network details, providing a stable endpoint for communication. Services can be exposed internally or externally, allowing for flexible connectivity within the cluster or from external sources.
So, In this blog we have looked:
1. First, we created a service to access our todo-app, highlighting the significance of service abstraction in Kubernetes.
2. Next, we delved deeper by setting up a ClusterIP service, optimizing internal communication within our application.
3. Lastly, we explored the power of LoadBalancer services, ensuring seamless external access to our todo-app.
Hope you find it helpful🤞 So I encourage you to try this on your own and let me know in the comment section👇 about your learning experience.✨
*👆The information presented above is based on my interpretation. Suggestions are always welcome.*😊
~Smriti Sharma✌