DEV Community

Udoh Deborah
Udoh Deborah

Posted on

Day 34: Working with Services in Kubernetes

Our day 34 task is all about working with Services, which is the next logical step after managing Deployments. Services are how we make our applications accessible and robust.

Here is a breakdown of our tasks.

Task 1: Create a Service for your todo-app Deployment

The first task is a general service creation. For simplicity, we'll create a simple ClusterIP service.

Here is a YAML file for the service. You can save this as todo-service.yml.

---
apiVersion: v1
kind: Service
metadata:
  name: todo-app-service
  namespace: todo-space # Use the same namespace as your Deployment
spec:
  selector:
    app: todo-app # This must match the 'app' label in your Pods/Deployment
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000 # The port your todo-app is running on inside the container
Enter fullscreen mode Exit fullscreen mode

Apply the Service Definition

First, ensure you have the todo-space namespace created:

kubectl create namespace todo-space
Enter fullscreen mode Exit fullscreen mode

Now, apply the service:

kubectl apply -f todo-service.yml -n todo-space
Enter fullscreen mode Exit fullscreen mode

Verify the Service

To verify, get the service details and find its ClusterIP.

kubectl get services -n todo-space
Enter fullscreen mode Exit fullscreen mode

You should see an output.

Task 2: Create a ClusterIP Service

This task is a more explicit version of Task 1. The YAML is identical to the one above since ClusterIP is the default Service type.

Save the content below to cluster-ip-service.yml.

---
apiVersion: v1
kind: Service
metadata:
  name: todo-app-clusterip
  namespace: todo-space
spec:
  selector:
    app: todo-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000
  type: ClusterIP # Explicitly declaring the type
Enter fullscreen mode Exit fullscreen mode

Apply and Verify

Apply it with the same command as before:

kubectl apply -f cluster-ip-service.yml -n todo-space
Enter fullscreen mode Exit fullscreen mode

To verify, you can get the service's details. The CLUSTER-IP will be a unique IP only accessible from within the cluster.

kubectl get services -n todo-space
Enter fullscreen mode Exit fullscreen mode

To test, you would typically exec into an existing Pod and use curl to the ClusterIP or the service's DNS name (todo-app-clusterip.todo-space.svc.cluster.local).

Task 3: Create a LoadBalancer Service

The LoadBalancer Service type is used to expose your application externally. On a cloud provider like GCP or AWS, this automatically provisions a cloud load balancer.

Save the content below to load-balancer-service.yml.

---
apiVersion: v1
kind: Service
metadata:
  name: todo-app-loadbalancer
  namespace: todo-space
spec:
  selector:
    app: todo-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000
  type: LoadBalancer # Changed from ClusterIP
Enter fullscreen mode Exit fullscreen mode

Apply and Verify

Apply the file:

kubectl apply -f load-balancer-service.yml -n todo-space
Enter fullscreen mode Exit fullscreen mode

To verify, get the service details. This time, you'll see an EXTERNAL-IP.

kubectl get services -n todo-space
Enter fullscreen mode Exit fullscreen mode

In a cloud environment, it may take a few moments for the EXTERNAL-IP to be provisioned. Once it appears, you can access your todo-app from outside the cluster using that IP address in your web browser. In minikube, the EXTERNAL-IP will often be pending, and you'll need to use the minikube service command to get the external URL.

minikube service todo-app-loadbalancer -n todo-space
Enter fullscreen mode Exit fullscreen mode

This will open your web browser to the correct URL for the todo-app.

Top comments (0)