1. apiVersion: v1

Here's a detailed breakdown of your Kubernetes Service YAML file:

TrackKubernetes Learning Journey
Current SectionServices and Discovery
Progress29 of 271

Here's a detailed breakdown of your Kubernetes Service YAML file:


1. apiVersion: v1

apiVersion: v1
  • Specifies the API version used for this resource.
  • v1 is the API version for Service objects.

2. kind: Service

kind: Service
  • Defines the type of Kubernetes resource being created.
  • A Service exposes a set of Pods to other services or external users.
  • It provides a stable endpoint (IP and DNS name) for accessing the application.

3. metadata

metadata:
  name: notes-service
  • name: notes-service
    • The unique name of this Service.
    • Other resources (like Deployments or Ingress) can reference this service by name.

4. spec

Defines the configuration of the Service.

4.1 type: LoadBalancer

type: LoadBalancer
  • Defines how the service is exposed.
  • LoadBalancer makes the service externally accessible via a cloud provider’s load balancer.
  • Common Service types:
    • ClusterIP (default): Internal service accessible only within the cluster.
    • NodePort: Exposes the service on each node’s IP at a static port.
    • LoadBalancer: Provisions a cloud load balancer (AWS ELB, GCP LB, etc.).
    • ExternalName: Maps the service to an external DNS name.

Example:

  • On AWS, this will create an ELB (Elastic Load Balancer).
  • On Google Cloud, this will create a GCP Load Balancer.
  • On Minikube, it doesn’t work unless you use MetalLB.

4.2 ports

ports:
  - port: 8100
    targetPort: 8100
  • Defines the networking configuration.

  • port: 8100

    • The external port on which the service is exposed.
    • When using LoadBalancer, this is the port accessible from outside.
  • targetPort: 8100

    • The port on the pod that the traffic will be forwarded to.
    • Should match the container’s containerPort from the Deployment.

Flow:
External Request → LoadBalancer (port 8100) → Service (port 8100) → Pod’s Container (targetPort 8100)


4.3 selector

selector:
  app: notes-service
  • Important! This binds the Service to the correct Pods.

  • Only Pods with the label app: notes-service will receive traffic from this Service.

  • The related Deployment has:

    metadata:
      labels:
        app: notes-service
    
  • This ensures that traffic is routed only to the correct set of Pods.


Summary of Key Components

  • apiVersion

    • Description: Defines the API version (v1).
  • kind

    • Description: Specifies that this is a Service.
  • metadata.name

    • Description: The name of the service (notes-service).
  • type

    • Description: LoadBalancer makes the service accessible externally.
  • ports.port

    • Description: External port for the service (8100).
  • ports.targetPort

    • Description: Maps to the container’s port (8100).
  • selector

    • Description: Matches Pods labeled app: notes-service.

How It Works

  1. A user sends a request to the LoadBalancer at port 8100.
  2. The Service forwards the request to one of the available Pods.
  3. The request reaches the container running the application inside the Pod.

Best Practices & Considerations

Use ClusterIP for internal services
Use NodePort if you don’t need a cloud load balancer
For production, use an Ingress controller instead of LoadBalancer for better flexibility