Kubernetes Services and Service Discovery

Kubernetes Services and Service Discovery

27th April Thursday

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: my-deployment
spec:
  selector:
    matchLabels:
      app: my-app
  replicas: 3
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: my-container
          image: nginx

---

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
    - name: http
      port: 80
      targetPort: 80
  type: NodePort

This YAML file creates a deployment with three replicas running an Nginx container and a NodePort service that exposes port 80 on the deployment. You can access the nginx container using the node's IP address and the NodePort.

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

Kubernetes provides built-in DNS-based service discovery, allowing services and pods to communicate with each other using DNS names. By default, each Service in a Kubernetes cluster is assigned a DNS name based on its name and namespace. Similarly, pods are assigned a DNS name based on their name and namespace. Assume we have a Kubernetes cluster running with Services and Pods.

DNS:

DNS is a widely used mechanism for service discovery in Kubernetes. Each Service in a Kubernetes cluster gets a DNS record created automatically.

Environment Variables:

Kubernetes automatically sets environment variables for each Service that you create.

👉Discover a service using DNS

Let's say we have a Service named "my-service" deployed in the "default" namespace. We can discover this Service using DNS. Here's an example Python code snippet to resolve the DNS and retrieve the IP address of the Service:

import socket

service_host = "my-service.default.svc.cluster.local"
service_port = 80

service_address = socket.gethostbyname(service_host)
print(f"Service IP: {service_address}")

In the above code snippet, replace my-service with the actual name of your Service, and modify the namespace and port accordingly. Running this code will resolve the DNS name my-service.default.svc.cluster.local and print the corresponding IP address of the Service.

👉Discover a Pod using DNS

Similarly, let's assume we have a Pod named "my-pod" running in the "default" namespace. We can discover this Pod using DNS. Here's an example Python code snippet to resolve the DNS and retrieve the IP address of the Pod:

import socket

pod_host = "my-pod.default.pod.cluster.local"
pod_port = 8080

pod_address = socket.gethostbyname(pod_host)
print(f"Pod IP: {pod_address}")

Replace my-pod with the actual name of your Pod, and adjust the namespace and port as necessary. Executing this code will resolve the DNS name my-pod.default.pod.cluster.local and display the IP address of the Pod.

To test the above code snippets, ensure that you have the necessary Python environment set up with the socket module available. Execute the code and observe the output, which will provide the IP addresses of the discovered Service and Pod.

Note: Remember to replace the placeholder values (my-service, my-pod, default, etc.) in the code snippets with the actual names and namespaces of your Services and Pods within the Kubernetes cluster.

👉Environment Variables:

Kubernetes sets environment variables for each service and pod within the cluster, allowing easy access to their information. You can access these environment variables from within a container to discover services and pods. Here's an example code snippet in Python to access environment variables:

import os

service_host = os.getenv("MY_SERVICE_HOST")
service_port = os.getenv("MY_SERVICE_PORT")

if service_host and service_port:
    print(f"Service Host: {service_host}")
    print(f"Service Port: {service_port}")
else:
    print("Service environment variables not found")

In the code snippet above, the os.getenv() function retrieves the values of environment variables such as MY_SERVICE_HOST and MY_SERVICE_PORT. Adjust the variable names to match the specific environment variables used in your deployment. If the environment variables are found, their values will be printed. Otherwise, a message indicating their absence will be displayed.

Thank you!!

~Ritul Gupta