This is#90DaysofDevopschallenge under the guidance of Shubham Londhe sir.
Understanding Kubernetes Services
In Kubernetes, Services act as facilitators for communication among Pods and external users. They provide stable network addresses to Pods, ensuring seamless message exchange without the hassle of changing IP addresses. Let's delve into the technical intricacies of creating and managing Services in Kubernetes.
What are Services?
In Kubernetes, Services are like signposts for Pods, helping them communicate with each other and with external users. They give Pods consistent network addresses, making it easier to send and receive messages without worrying about changing IP addresses. Services ensure that Pods can talk to each other and to external sources smoothly.
Task-1:
Create a Service for your todo-app Deployment from Day-32.
Create a Service definition for your todo-app Deployment in a YAML file
apiVersion: v1 kind: Service metadata: name: django-todo-services namespace: python-django-app1 spec: type: NodePort selector: app: django-app ports: - protocol: TCP port: 8000 targetPort: 30007
In this example, we're setting up a Service in Kubernetes using the NodePort type. This means we're making the Service reachable from outside the cluster through any node's IP address and a specific nodePort, which here is set to 30080.
Here's a breakdown of the configuration:
apiVersion: v1
: This tells Kubernetes which version of the API to use, which is v1 in this case.Kind: Service
: This indicates that we're defining a Service resource.metadata: name: django-todo-service
: This assigns a name to our Service, which is 'django-todo-service'.spec: selector: app: django-app
: This specifies which Pods the Service should target based on the app label being set to 'django-app'.type: NodePort
: Here, we're defining the type of Service as NodePort, making it accessible from outside the cluster.ports: - protocol: TCP port: 8001 targetPort: 8001 nodePort: 30080
: This section maps the ports for the Service. It listens on port 8001 and forwards traffic to port 8001 on the Pods. The nodePort 30080 makes the Service reachable externally using any node's IP address and the specified nodePort.To create a namespace before deploying
kubectl create namespace python-django-app1
kubectl get namespace
-
Now that we've made the services and namespace, it's time to set up the deployment.
kubectl create -f service.yaml -n python-django-app1 kubectl get all -n python-django-app1
Now, check if the deployment and service are working correctly.
Test if the todo-app is accessible by using the IP address and port of the Service in your Namespace.
Task-2:
Creating a ClusterIP Service for the deployment.
apiVersion: v1 kind: Service metadata: name: new-service-name namespace: python-django-app spec: type: ClusterIP selector: app: django-app ports: - protocol: TCP port: 8000 targetPort: 8000
Now, we're going to set up a deployment.
kubectl apply -f cluster-service.yaml --namespace=python-django-app kubectl get all -n python-django-app
Let's confirm that the deployment and service are working as expected.
Task-3:
Create a LoadBalancer service for the deployment.
Step 1: Create a file named
loadbalancer-service.yaml
and add the following code for the LoadBalancer service:apiVersion: v1 kind: Service metadata: name: django-todo-lb-services namespace: python-django-app spec: selector: app: django-app ports: - protocol: TCP port: 8000 targetPort: 8000 type: LoadBalancer
Step 2: Apply the LoadBalancer service definition to your Kubernetes cluster:
kubectl apply -f loadbalancer-service.yaml -n python-django-app
Step 3: Check if the service is created:
kubectl get svc -n python-django-app
You can also use Minikube to list all services:
minikube service list
Step 4: Verify the deployment and service:
kubectl get all -n python-django-app
This command will show you all resources (pods, services, deployments, etc.) in the
python-django-app
namespace, allowing you to verify that the deployment and service are correctly configured and running.