Kubernetes Services and Service Discovery

Kubernetes Services and Service Discovery

Kubernetes Services and Service Discovery

Kubernetes Services

A Kubernetes service is a logical abstraction for a deployed group of pods in a cluster (which all perform the same function). Services enable communication between various components within an application and make it easy to expose an application externally.

Services can be defined using YAML or JSON manifests and can be created, updated, and deleted using the Kubernetes API or command-line tools like kubectl. Services also define policies for their access. There are four types of services that Kubernetes supports: ClusterIP, NodePort, LoadBalancer, and ExternalName.

Read more about services by clicking on this link

Service Discovery

Service discovery is the process of discovering and connecting to Services in a Kubernetes cluster. Kubernetes provides two primary mechanisms for Service discovery:

  • DNS: Kubernetes maintains a DNS record for each Service, mapping the Service name to the IP address of the corresponding Service. Applications can use this DNS record to resolve the Service name to an IP address and connect to the corresponding Service.

  • Environment variables: Kubernetes sets environment variables for each Service in the form of <SERVICE_NAME>_SERVICE_HOST and <SERVICE_NAME>_SERVICE_PORT, which applications can use to connect to the Service. This method is commonly used in languages that don't support DNS resolution, such as some versions of Java.

How to expose Kubernetes workloads to the outside world using Services

To expose a Kubernetes workload to the outside world using services, you can follow these steps:

  1. Create a deployment: A deployment is a Kubernetes resource that manages a set of pods. You can create a deployment using a YAML or JSON file that describes the desired state of your deployment.

  2. Create a service: A service is a Kubernetes resource that provides a stable IP address and DNS name for your deployment. You can create a service using a YAML or JSON file that describes the desired state of your service.

  3. Expose the service: Once you have created the service, you can expose it to the outside world using a service type. There are several service types available in Kubernetes, including ClusterIP, NodePort, and LoadBalancer.

Here's an example YAML file for simple deployment and service:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapp
spec:
  replicas: 3
  selector:
    matchLabels:
      app: webapp
  template:
    metadata:
      labels:
        app: webapp
    spec:
      containers:
      - name: webapp
        image: my/webapp:latest
        ports:
        - containerPort: 80

---

apiVersion: v1
kind: Service
metadata:
  name: webapp-service
spec:
  selector:
    app: webapp
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer

How to discover Services and Pods within a Kubernetes cluster using DNS and other mechanisms.

Discover Services and Pods using DNS

Kubernetes provides a built-in DNS service that allows you to discover Services and Pods using their DNS names. The DNS service is automatically created when you create a cluster.

The DNS service uses the following naming convention:

<service-name>.<namespace>.svc.cluster.local

For example, if you have a Service named myapp in the default namespace, you can access it using the DNS name myapp.default.svc.cluster.local.

Discover Services and Pods using Environment Variables

Kubernetes also provides environment variables that allow you to discover Services and Pods from within a container. When a container is created, Kubernetes sets several environment variables that provide information about the container's namespace, hostname, and IP address.

apiVersion: v1
kind: Service
metadata:
  name: webapp
  labels:
    app: webapp
spec:
  type: ClusterIP
  selector:
    app: webapp
  ports:
  - name: http
    port: 80
    targetPort: 8080

---
apiVersion: v1
kind: Pod
metadata:
  name: webapp-pod
  labels:
    app: webapp
spec:
  containers:
  - name: webapp-container
    image: nginx
    env:
    - name: WEBAPP_SERVICE_HOST
      value: "webapp.default.svc.cluster.local"
    - name: WEBAPP_POD_IP
      valueFrom:
        fieldRef:
          fieldPath: status.podIP

Thank you for reading!!

Irfan