Working with Services in Kubernetes

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.

The commonly used Service types are:

ClusterIP:

  • This is the default service type.

  • Exposes the Service on an internal IP within the cluster.

  • The Service is only reachable from within the cluster.

NodePort:

  • Exposes the Service on each Node's IP at a static port.

  • A Service of the type NodePort is automatically created, which forwards to the selected pods.

LoadBalancer:

  • Exposes the Service externally using a cloud provider's load balancer.

  • The external load balancer will route traffic to the NodePorts.

ExternalName:

  • Maps the Service to the contents of the externalName field (e.g., a DNS name).

  • Allows Service resources to refer to external services by name.

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.

To break down the YAML manifest:

apiVersion: v1
kind: Service
metadata:
  name: todo-service
  namespace: my-todo-app
  • apiVersion: specifies the API version that the object uses

  • kind: defines the type of Kubernetes resource. Here, it is a Service

spec:
  type: NodePort
  selector:
    app: todo
  • spec: describes the desired state of the service

  • type: specifies the type of Service. In this case, it is a NodePort service.

  • NodePort: makes the service accessible on a static port on each node in the cluster. This allows external traffic to reach the service.

  • selector: specifies a set of labels used t identify the pods that this service will forward traffic to. Here, it selects pods with the label app:todo

ports:
    - protocol: TCP
      port: 8000
      targetPort: 8000
      nodePort: 30100
  • ports: specifies the ports that the service will use

  • protocol: specifies the protocol to use, here it is TCP

  • port: specifies the port to which the service will be exposed internally in the cluster

  • targetPort: specifies the port to which traffic will be forwarded to the pods matching the selector

  • nodePort: specifies the port on each node where the service will be accessible externally, here, it is set to 30100.

Apply the Service definition to your k8s(minikube) cluster using the below command:

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

To list Services in a Kubernetes cluster, use the kubectl get svc command. The -n flag is used to specify the namespace where the Service is located.

kubectl get svc -n my-todo-app

The minikube service command is the main command to interact with services in a minikube cluster. It is used to expose Kubernetes services outside of the cluster.

minikube service todo-service -n=my-todo-app --url

todo-service: is the name of the service you want to expose

n-my-todo-app: specifies the namespace in which the service is located, and

--url: is used to print the URL(s) of the exposed service(s)

When you run this command, Minikube will open the specified (todo-service) in your default browser, and the --url option will print the URL that you can use to access the service.

Task-2:

  • Create a LoadBalancer Service for accessing the todo-app from outside the cluster

  • Create a LoadBalancer Service definition for your todo-app Deployment in a YAML file.

Task-2 Service type: ClusterIP

  • Apply the ClusterIP Service definition to your k8s(minikube) cluster using the below command.
kubectl apply -f service.yml -n <namespace-name>

  • Get the ClusterIP Service IP address:
kubectl get services -n <namespace>

  • Identify all the pods in the same namespace:
kubectl get pods -n <namespace>

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

Exec into the pod: i.e go inside the container

kubectl exec -it <pod-name> -n <namespace> -- sh

Task-3:

  • Create a LoadBalancer Service for accessing the todo-app from outside the cluster

  • Create a LoadBalancer Service definition for your todo-app Deployment in a YAML file.

Task-3 Service type: LoadBalancer

  • Apply the LoadBalancer Service definition to your K8s (minikube) cluster
kubectl apply -f service.yml -n <namespace-name>

  • Get the load balancer service:
kubectl get services -n <namespace>

Get the LoadBalancer URL:

 minikube service list

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

This command minikube service todo-service -n my-todo-app will open the Service in your web browser

Thank you!