Day 34 Working with Services in Kubernetes

Day 34 Working with Services in Kubernetes

Β·

3 min read

Day 34 of #90daysofdevops

Hey Techies! Welcome to this blog

In this blog, we are going to start working with Services in Kubernetes

What are Services in K8s

In Kubernetes, Services are objects that provide stable network identities to Pods and abstract away the details of Pod IP addresses. Services allow Pods to receive traffic from other Pods, Services, and external clients.

This is like a phone directory for Pods. Since Pods can came and go , a service provides a stable "address" so that other parts of your application can find them.

Task-1:

Creating a Service for todo-app Deployment

Step 1: Service Definition

Create a Service definition for your todo-app Deployment. Open a new file, service.yml, and add the following configuration:

apiVersion: v1
kind: Service
metadata:
  name: todo-app-service
spec:
  selector:
    app: todo-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8000
  type: NodePort

Step 2: Applying the Service Definition

Apply the Service definition to your Kubernetes cluster using the following command:

kubectl apply -f service.yml -n <namespace-name>

Step 3: Verifying the Service

Verify that the Service is working by accessing the todo-app using the Service's IP and Port within your Namespace.

kubectl get svc -n <namespace-name>

Access your todo-app using the provided IP and Port.

Task-2:

Creating a ClusterIP Service for Internal Access

Understanding ClusterIP Service

The ClusterIP service type exposes the Service on a cluster-internal IP. This type of Service is accessible only within the cluster, making it ideal for internal communication between Pods.

Step 1: ClusterIP Service Definition

Create a ClusterIP Service definition for your todo-app Deployment. Open a new file, cluster-ip-service.yml, and add the following configuration:

apiVersion: v1
kind: Service
metadata:
  name: todo-app-cluster-ip-service
spec:
  selector:
    app: todo-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8000
  type: ClusterIP

Step 2: Applying the ClusterIP Service Definition

Apply the ClusterIP Service definition to your Kubernetes cluster using the following command:

kubectl apply -f cluster-ip-service.yml -n <namespace-name>

Step 3: Verifying the ClusterIP Service

Verify that the ClusterIP Service is working by accessing the todo-app from another Pod in the cluster within your Namespace.

kubectl get svc -n <namespace-name>

Access your todo-app from another Pod using the ClusterIP.

Task-3:

Creating a LoadBalancer Service for External Access

Step 1: LoadBalancer Service Definition

Create a LoadBalancer Service definition for your todo-app Deployment. Open a new file, load-balancer-service.yml, and add the following configuration:

apiVersion: v1
kind: Service
metadata:
  name: todo-app-load-balancer-service
spec:
  selector:
    app: todo-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8000
  type: LoadBalancer

Step 2: Applying the LoadBalancer Service Definition

Apply the LoadBalancer Service definition to your Kubernetes cluster using the following command:

kubectl apply -f load-balancer-service.yml -n <namespace-name>

Step 3: Verifying the LoadBalancer Service

Verify that the LoadBalancer Service is working by accessing the todo-app from outside the cluster within your Namespace.

kubectl get svc -n <namespace-name>

Access your todo-app from outside the cluster using the provided external IP.

That's it! You've just completed the task. πŸŽ‰

Thank you so much for taking the time to read till the end! Hope you found this blog informative and helpful.

Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.

Happy Learning!

~kritika :)

Connect with me: linkedin.com/in/kritikashaw

Β