Spring Boot HandBook

    Services in Kubernetes

    Introduction#

    We all know that Kubernetes has revolutionized container orchestration, making it easier to manage, scale, and deploy applications. However, one of the biggest challenges in Kubernetes is handling communication between different application components, especially since pods are ephemeral. This is where Kubernetes Services come into play.

    Why Do We Need Services?#

    Pods in Kubernetes are constantly created and destroyed due to scaling events, updates, or failures. This means their IP addresses are dynamic they keep on changing, this makes direct communication unreliable. Kubernetes Services provide a stable endpoint for communication, ensuring that requests are always routed to the appropriate pods.

    A Kubernetes Service acts as an abstraction layer over a set of pods, allowing seamless communication between them. It provides critical functionalities such as:

    • Load Balancing: Distributing traffic across multiple pod replicas.
    • Service Discovery: Enabling pods to find and communicate with each other.
    • Zero Downtime Deployments: Ensuring new pod instances can seamlessly replace old ones.
    • External Exposure: Allowing applications to be accessed outside the cluster.

    Understanding Kubernetes Service Ports#

    Services in Kubernetes

    A Kubernetes Service has three main port-related attributes:

    • TargetPort: The port on the pod that the service forwards requests to.
    • Port: The internal port within the Kubernetes cluster that the service listens on.
    • NodePort: An optional port that exposes the service externally on each node.

    For example:

    apiVersion: v1 kind: Service metadata: name: my-service spec: selector: app: my-app ports: - protocol: TCP port: 80 # The service port inside the cluster targetPort: 8080 # The port the application inside the pod is listening on nodePort: 30008 # The external port exposed on each node (optional) type: NodePort

    Types of Kubernetes Services#

    Kubernetes provides different types of services depending on how you want to expose your application.

    1. ClusterIP (Default)#

    A ClusterIP service is only accessible within the Kubernetes cluster. This is ideal for internal communication between different services.

    apiVersion: v1 kind: Service metadata: name: internal-service spec: selector: app: my-app ports: - port: 80 targetPort: 8080 type: ClusterIP

    2. NodePort#

    A NodePort service exposes the application on a static port on each node. This allows external users to access the application via <NodeIP>:<NodePort>.

    apiVersion: v1 kind: Service metadata: name: nodeport-service spec: selector: app: my-app ports: - port: 80 targetPort: 8080 nodePort: 30008 type: NodePort

    3. LoadBalancer#

    A LoadBalancer service integrates with cloud provider load balancers (AWS ELB, GCP LB, Azure LB) to distribute external traffic.

    apiVersion: v1 kind: Service metadata: name: loadbalancer-service spec: selector: app: my-app ports: - port: 443 targetPort: 8443 type: LoadBalancer

    Managing Kubernetes Services#

    Here are some essential kubectl commands for managing services:

    • List all services in the current namespace:
    kubectl get svc
    • Get detailed information about a specific service:
    kubectl describe svc <service-name>
    • Create a service from a deployment:
    kubectl expose deployment <deployment-name> --port=80 --target-port=8080 --type=ClusterIP
    • Apply a service from a YAML file:
    kubectl apply -f service.yaml
    • Delete a service:
    kubectl delete svc <service-name>

    Service Discovery and DNS Resolution#

    Kubernetes has a built-in DNS service (CoreDNS or kube-dns) that allows services to be discovered by name. Each service gets an internal DNS name in the format:

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

    For example, if you have a service named my-app in the default namespace, pods can access it using:

    <http://my-app.default.svc.cluster.local>

    Conclusion#

    Kubernetes Services play a crucial role in enabling reliable communication between pods. By understanding the different service types and their use cases, you can design scalable and resilient Kubernetes applications. Whether you're exposing an internal microservice or making an application publicly accessible, Kubernetes Services provide the necessary abstraction and stability.

    Last updated on Feb 20, 2025