Kubernetes Services and Service Discovery

Kubernetes Services and Service Discovery

·

3 min read

Create a Deployment for the Web Application

To deploy a Job in Kubernetes, you need to define a YAML file that contains the Job configuration. Here is an example YAML file for deploying a Job that runs batch processing.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-app-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web-app
  template:
    metadata:
      labels:
        app: web-app
    spec:
      containers:
        - name: web-app-container
          image: your-web-app-image:tag
          ports:
            - containerPort: 8080

Create a Service to Expose the Web Application

In this example, we are creating a Service with the name web-app-service. The selector field specifies the Pods that the Service should route traffic to based on their labels. In this case, the label is app: web-app. The ports field defines the port mapping between the Service and the Pods. In this case, we are mapping port 80 on the Service to port 8080 on the Pods. Finally, the type field specifies the type of the Service as LoadBalancer, which will expose the Service outside of the Kubernetes cluster.

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

Apply the Deployments and Service

kubectl apply -f web-app-deployment.yaml
kubectl apply -f web-app-service.yaml

Access the Exposed Web Application

This will create the Service and expose the web application running on the Pods to the outside world.

kubectl get service web-app-service

Look for the "EXTERNAL-IP" field, which will provide the external IP address assigned to the Service. Open a web browser and enter the external IP address to access the web application.

Discovering Services and Pods using DNS

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}")

Discovering a Pod using DNS

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}")

Kubernetes API

from kubernetes import client, config

config.load_kube_config()
api_instance = client.CoreV1Api()

services = api_instance.list_service_for_all_namespaces().items
for service in services:
    print(f"Service Name: {service.metadata.name}")
    print(f"Service Namespace: {service.metadata.namespace}")
    print(f"Service IP: {service.spec.cluster_ip}")
    print("---")

Ingress:

from kubernetes i mport client, config

config.load_kube_config()
api_instance = client.ExtensionsV1beta1Api()

ingresses = api_instance.list_ingress_for_all_namespaces().items
for ingress in ingresses:
    print(f"Ingress Name: {ingress.metadata.name}")
    print(f"Ingress Namespace: {ingress.metadata.namespace}")
    for rule in ingress.spec.rules:
        print(f"Host: {rule.host}")
        for path in rule.http.paths:
            print(f"Path: {path.path}")
            print(f"Service: {path.backend.service_name}")
            print(f"Service Port: {path.backend.service_port}")
            print("---")

Conclusion :

In conclusion, Kubernetes Services provide a reliable and scalable way to expose your application to other services and users. With Services, you can easily route traffic to the appropriate Pods and scale your application as needed. Service Discovery is also an important aspect of Kubernetes Services, which allows services to discover and communicate with each other dynamically without hardcoding IP addresses.

In this blog, we have seen how to create a Service using Kubernetes YAML files and how to expose a web application running on a set of Pods. We have also seen how to use Service Discovery to discover and communicate with other services in a Kubernetes cluster.

By using Services and Service Discovery in your Kubernetes deployment, you can build reliable and scalable applications that can easily communicate with other services in your cluster.

480+ Devops Team Stock Photos, Pictures & Royalty-Free ...

Did you find this article valuable?

Support DevOpsculture by becoming a sponsor. Any amount is appreciated!