Working with Services in Kubernetes

Working with Services in Kubernetes | Day 34 of 90 Days of DevOps

Learn how to work with Services in Kubernetes, a key concept that allows you to expose and access your applications in a cluster, in this tutorial for Day 34 of 90 Days of DevOps.

Ajit Fawade
7 min readOct 1, 2023

--

Welcome to Day 34 of 90 Days of DevOps.

Today, we will learn how to work with Services in Kubernetes, a key concept that allows us to expose and access our applications in a cluster. We will also deploy and manage different types of Services for our sample ToDo app that we created on Day 32.

By the end of this blog post, you will be able to:

  • Understand what Services are and how they work in Kubernetes
  • Create and delete Services using kubectl
  • Deploy a Service for your ToDo app Deployment
  • Create a ClusterIP Service for internal cluster access
  • Create a LoadBalancer Service for external access

Let’s get started!

Understanding Services in Kubernetes

Services are an abstraction that defines a logical set of Pods and a policy to access them. Services allow us to expose our Pods to other Pods within the cluster or to external clients outside the cluster.

Services are useful for:

  • Providing a stable and reliable way of accessing our Pods, regardless of their location or status
  • Load balancing the traffic among our Pods, distributing the requests evenly, and avoiding overloading
  • Discovering our Pods dynamically, using DNS or environment variables

There are four types of Services in Kubernetes:

  • ClusterIP: This is the default type of Service that exposes our Pods within the cluster using an internal IP address. This type of Service is only accessible from within the cluster.
  • NodePort: This type of Service exposes our Pods on each node’s IP address using a static port. This type of Service is accessible from outside the cluster using <node-ip>:<node-port>.
  • LoadBalancer: This type of Service exposes our Pods using an external load balancer provided by our cloud provider or platform. This type of Service is accessible from outside the cluster using <load-balancer-ip>:<port>.
  • ExternalName: This type of Service maps our Pods to an external domain name using a CNAME record. This type of Service does not have any selectors or ports.

Creating and Managing Services

In this section, we will create and manage different types of Services for our sample ToDo app that we deployed on Day 32. We will use kubectl, the command-line tool that interacts with Kubernetes clusters, to perform various operations on our resources.

Creating a Service for your ToDo app Deployment

On Day 32, we deployed a ToDo app on Kubernetes using Deployment. The ToDo app is a simple web app that allows us to create and manage tasks. We used the Docker image ajitfawade14/node-todo-app as the source for our app.

To create a Service for our ToDo app Deployment, follow these steps:

1. Create a YAML file named service.yml with the following content:

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

This file defines a Service object named todo-service that exposes port 80 of our Pods as port 8000 on an internal IP address. The Service belongs to the todo Namespace and select the Pods that have the label app: todo.

2. Apply the YAML file to create the Service by running:

kubectl apply -f service.yml -n todo

The -n flag specifies the Namespace that we want to create the Service in.

This will create the Service and assign it an internal IP address.

3. Verify that the Service is created by running:

kubectl get services -n todo

You should see something like this:

This shows that you have one Service named todo-service that exposes port 80 of your Pods as port 8000 on an internal IP address 10.96.230.136 in the todo Namespace.

4. Access the ToDo app from your browser by going to http://<internal-ip>:8000/. You should see something like this:

This shows that you have successfully accessed your ToDo app via the Service within your Namespace.

Creating a ClusterIP Service for internal cluster access

A ClusterIP Service is the default type of Service that exposes our Pods within the cluster using an internal IP address. This type of Service is only accessible from within the cluster.

To create a ClusterIP Service for our ToDo app Deployment, follow these steps:

  1. Create a YAML file named cluster-ip-service.yml with the following content:
apiVersion: v1
kind: Service
metadata:
name: todo-cluster-ip-service
namespace: todo
spec:
type: ClusterIP
selector:
app: todo
ports:
- protocol: TCP
port: 80
targetPort: 8000

This file defines a Service object named todo-cluster-ip-service that exposes port 80 of our Pods as port 8000 on an internal IP address. The Service belongs to the todo Namespace and select the Pods that have the label app: todo. The type: ClusterIP specifies that this is a ClusterIP Service.

2. Apply the YAML file to create the Service by running:

kubectl apply -f cluster-ip-service.yml -n todo

This will create the Service and assign it an internal IP address.

3. Verify that the Service is created by running:

kubectl get services -n todo

You should see something like this:

This shows that you have one ClusterIP Service named todo-cluster-ip-service that exposes port 80 of your Pods as port 8000 on an internal IP address 10.96.249.227 in the todo Namespace.

4. Access the ToDo app from another Pod within the cluster by running:

kubectl run -it --rm curl --image=curlimages/curl -- sh

This will create a temporary Pod named curl that runs a shell in an interactive mode.

In the shell, run the following command:

curl http://todo-cluster-ip-service.todo.svc.cluster.local

This will send a request to the ClusterIP Service using its DNS name, which consists of <service-name>.<namespace-name>.svc.cluster.local.

This shows that you have successfully accessed your ToDo app via the ClusterIP Service from another Pod within your Namespace.

To exit the shell, press Ctrl+D.

Creating a LoadBalancer Service for external access

A LoadBalancer Service is a type of Service that exposes our Pods using an external load balancer provided by our cloud provider or platform. This type of Service is accessible from outside the cluster.

To create a LoadBalancer Service for our ToDo app Deployment, follow these steps:

  1. Create a YAML file named load-balancer-service.yml with the following content:
apiVersion: v1
kind: Service
metadata:
name: todo-load-balancer-service
namespace: todo
spec:
type: LoadBalancer
selector:
app: todo
ports:
- protocol: TCP
port: 80
targetPort: 8000

This file defines a Service object named todo-load-balancer-service that exposes port 80 of our Pods as port 8000 on an external IP address. The Service belongs to the todo Namespace and select the Pods that have the label app: todo. The type: LoadBalancer specifies that this is a LoadBalancer Service.

2. Apply the YAML file to create the Service by running:

kubectl apply -f load-balancer-service.yml -n todo

This will create the Service and assign it an external IP address.

3. Verify that the Service is created by running:

kubectl get services -n todo

You should see something like this:

This shows that you have one LoadBalancer Service named todo-load-balancer-service that exposes port 80 of your Pods as port 80 on an external IP address 192.168.49.2 in the todo Namespace.

4. Access the ToDo app from your browser by going to http://<external-ip>:80/. You should see something like this:

This shows that you have successfully accessed your ToDo app via the LoadBalancer Service from outside your cluster.

Conclusion

In this blog post, we learned how to work with Services in Kubernetes, a key concept that allows us to expose and access our applications in a cluster. We also deployed and managed different types of Services for our sample ToDo app that we created on Day 32.

I hope you enjoyed this blog post and learned something new. If you have any questions or feedback, please feel free to leave a comment below.

If you want to follow my journey of learning DevOps, you can check out my GitHub and my LinkedIn profile.

Thank you for reading and stay tuned for more!

--

--

Responses (1)