What are Kubernetes services and service discovery?

What are Kubernetes services and service discovery?

Kubernetes is a powerful platform for deploying, scaling, and managing containerized applications. However, once you deploy your application, you need a way to expose it to external traffic so that users can access it over the internet. In this tutorial, we will go through different ways of exposing applications in Kubernetes using Services.

💠What is a Service in Kubernetes?

In Kubernetes, a Service is a method for exposing a network application that is running as one or more Pods in your cluster. Services enable a loose coupling between dependent Pods and allow for external traffic exposure, load balancing, and service discovery for those Pods.

A Service in Kubernetes is defined using YAML or JSON manifests. The set of Pods targeted by a Service is usually determined by a label selector, which selects all the Pods in the cluster that have a particular set of labels attached to them.

Sample service.yml

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app.kubernetes.io/name: MyApp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376

Applying this manifest creates a new Service named "my-service", which targets TCP port 9376 on any Pod with the app.kubernetes.io/name: MyApp label.

There are different types of Services in Kubernetes: ClusterIP, NodePort, LoadBalancer, and ExternalName:

  • ClusterIP (default): Exposes the Service on an internal IP in the cluster. This type makes the Service only reachable from within the cluster.

  • NodePort: Exposes the Service on the same port of each selected Node in the cluster using NAT. Makes a Service accessible from outside the cluster using <NodeIP>:<NodePort>.

  • LoadBalancer: Creates an external load balancer in the current cloud (if supported) and assigns a fixed, external IP to the Service. Superset of NodePort.

  • ExternalName: Maps the Service to the contents of the externalName field (e.g. foo.bar.example.com), by returning a CNAME record with its value. No proxying of any kind is set up.

💠How to Expose an Application in Kubernetes

To demonstrate how to expose an application in Kubernetes, we will use the kubectl command-line tool and a sample application called Kubernetes-boot camp. The kubernetes-bootcamp application is a simple web server that listens on port 8080 and returns a message to the client.

  1. Start Minikube Before we can start deploying our application, we need to have a running Kubernetes cluster. We will use Minikube to create a local, single-node Kubernetes cluster for development purposes. To start Minikube, run the following command:
minikube start
  1. Deploy the Sample Application Next, we need to deploy the Kubernetes-boot camp application to the Kubernetes cluster. We will use a Deployment object to manage a set of replicas, and a Service object to expose the application to external traffic.

Use the following command to deploy the sample application:

kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1

This command creates a Deployment with one replica of the kubernetes-bootcamp Pod running the v1 image.

  1. Expose the Application using a NodePort Service Now that we have deployed our application, we need to expose it to external traffic so that users can access it. We will create a NodePort Service to achieve this.

Use the following command to create a NodePort Service:

kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080

This command creates a new Service called kubernetes-bootcamp and exposes it on a randomly assigned port on all Nodes in the cluster.

  1. Access the Application To access the kubernetes-bootcamp application, we need to find out the IP address and port number of the Service that was created in the previous step.

Use the following command to get the IP address and port number of the Service:

minikube service kubernetes-bootcamp --url

This command shows the URL to access the application. Open a web browser and go to the URL to see the running application.

💠Conclusion

In this tutorial, we have learned how to expose a sample application in Kubernetes using Services. Services are an essential component of Kubernetes and provide a way for users to access applications running in the cluster. By defining a Service, you can abstract away the underlying network topology and make your application easily accessible to users.