Kubernetes Services and Service Discovery

Kubernetes Services and Service Discovery

KubeWeek Challenge Day : 4

Kubernetes Service

Kubernetes services are a fundamental building block of distributed applications deployed on Kubernetes clusters. They provide a stable IP address and DNS name for a set of pods in the cluster, allowing other applications within the cluster to communicate with them.

There are several types of Kubernetes services, including:

  • ClusterIP: Exposes the service on a cluster-internal IP address. This is the default service type.

  • NodePort: Exposes the service on a node-specific port, which can be accessed externally.

  • LoadBalancer: Exposes the service externally using a cloud provider’s load balancer.

  • ExternalName: Maps service to a DNS name external to the cluster.

Refer to this for a brief about services.

Kubernetes Service Discovery

Kubernetes provides built-in service discovery, which is the process by which applications in a distributed system can locate and communicate with each other. This is achieved through the use of Kubernetes services.

Kubernetes services provide a stable IP address and DNS name for a set of pods in the cluster. Services can discover other services by their DNS name, allowing for seamless communication between services within a Kubernetes cluster, even as the number of instances of those services changes dynamically.

Kubernetes uses labels and selectors to define sets of pods that a service targets. When a new pod is created or an existing pod is terminated, Kubernetes automatically updates the service’s endpoints to ensure that traffic is always directed to a healthy pod.

There are several ways to access a Kubernetes service from within the cluster, including:

  • DNS name: Services are automatically assigned a DNS name, which can be used to access the service from within the cluster.

  • Environment variables: Kubernetes automatically sets environment variables for each service, which can be used by applications to access the service.

  • Service discovery libraries: Libraries such as Kubernetes client libraries and service mesh tools like Istio can be used to access and manage services within a Kubernetes cluster.

Exposing an External IP Address to Access an Application in a Cluster

Objectives

  • Run five instances of a Hello World application

  • Create a Service object that exposes an external IP address

  • Use the Service object to access the running application

Run five instances of a Hello World application

Run a Hello World application in your cluster by creating the following deployment file (load-balancer-example.yaml)

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app.kubernetes.io/name: load-balancer-example
  name: hello-world
spec:
  replicas: 5
  selector:
    matchLabels:
      app.kubernetes.io/name: load-balancer-example
  template:
    metadata:
      labels:
        app.kubernetes.io/name: load-balancer-example
    spec:
      containers:
      - image: gcr.io/google-samples/node-hello:1.0
        name: hello-world
        ports:
        - containerPort: 8080

The proceeding command creates a Deployment and an associated ReplicaSet. The ReplicaSet has five Pods each of which runs the Hello World application.

kubectl apply -f load-balancer-example.yaml

Display information about the deployment

kubectl get deployments hello-world 
kubectl describe deployments hello-world

Display information about the Kubernetes deployment

Display information about your ReplicaSet objects

kubectl get replicasets
kubectl describe replicasets

Display information about your ReplicaSets

Create a Service object that exposes the deployment

The proceeding command will create a Service object that exposes Hello World application for access from the Internet. It will declaratively provision a classic load balancer within your AWS account.

kubectl expose deployment hello-world --type=LoadBalancer --name=my-service

AWS Classic Load Balancer

Display information about the Service using the proceeding commands

kubectl get services my-service
kubectl describe services my-service

Display the Kubernetes service

Make notes of the LoadBalancer Ingress's DNS name and the value of the Port and NodePort exposed by the Service. Use the DNS address and port number to access the Hello World application.

curl http://ae5cb07ebfaf14fb283c20d988a0da90-1987687362.eu-west-1.elb.amazonaws.com:8080

The response to a successful request is a hello message:

Hello Kubernetes!

Cleaning up

To delete the Service, enter this command:

kubectl delete services my-service

To delete the Deployment, the ReplicaSet, and the Pods that are running the Hello World application, enter this command:

kubectl delete deployment hello-world

Discover services and pods within Kubernetes cluster using DNS and other methods

Kubernetes provides several ways to discover services and pods within a cluster, including DNS, environment variables, and Kubernetes client libraries. Here's an overview of each method:

  1. DNS: Kubernetes provides a built-in DNS service that allows you to discover services and pods using their DNS names. For example, if you have a service named "web" in the "default" namespace, you can access it from another pod in the same namespace using the DNS name "web.default.svc.cluster.local". You can also use the DNS name to access the service from outside the cluster by setting up a DNS server or using a DNS provider.

  2. Environment variables: Kubernetes automatically sets environment variables for each service and pod, which you can use to discover their IP addresses and ports. For example, if you have a service named "web" with port 80, you can access it from a pod using the environment variable "WEB_SERVICE_HOST" for the IP address and "WEB_SERVICE_PORT" for the port.

  3. Kubernetes client libraries: You can also use Kubernetes client libraries, such as kubectl or the Kubernetes API, to discover services and pods within a cluster. For example, you can use the "kubectl get pods" command to list all the pods in a namespace, or use the Kubernetes API to programmatically retrieve information about services and pods.

Here are some examples of commands to discover services and pods within a Kubernetes cluster using DNS, environment variables, and Kubernetes client libraries:

  1. DNS:

To discover services and pods using DNS in a Kubernetes cluster, you can use the following commands:

  • To list all services in a namespace:
kubectl get services -n <namespace>
  • To resolve the IP address of a service:
nslookup <service_name>.<namespace>.svc.cluster.local
  • To resolve the IP address of a pod:
nslookup <pod_name>.<namespace>.pod.cluster.local
  1. Environment Variables:

To discover services and pods using environment variables in a Kubernetes cluster, you can use the following commands:

  • To list all environment variables for a pod:
kubectl exec <pod_name> -n <namespace> -- printenv
  • To get the IP address and port of a service using environment variables:
echo $<service_name>_SERVICE_HOST
echo $<service_name>_SERVICE_PORT
  • To get the IP address and port of a pod using environment variables:
echo $HOSTNAME
echo $PORT
  1. Kubernetes Client Libraries:

To discover services and pods using Kubernetes client libraries in a Kubernetes cluster, you can use the following commands:

  • To list all pods in a namespace:
kubectl get pods -n <namespace>
  • To get information about a specific pod:
kubectl describe pod <pod_name> -n <namespace>
  • To get information about a specific service:
kubectl describe service <service_name> -n <namespace>
  • To get information about a specific node:
kubectl describe node <node_name>
  • To get information about a specific namespace:
kubectl describe namespace <namespace>

Thank you Shubham Londhe for KubeWeek challenge. This inspires all learners to showcase their learning and improve their knowledge.

#devops #kubeweek #kubernetes #kubeweekchallenge #trainwithshubham

Thank You!!