Here's a detailed breakdown of your Kubernetes Service YAML file:
1. apiVersion: v1
apiVersion: v1
- Specifies the API version used for this resource.
v1is 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.
LoadBalancermakes 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
containerPortfrom 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-servicewill 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).
- Description: Defines the API version (
-
kind
- Description: Specifies that this is a
Service.
- Description: Specifies that this is a
-
metadata.name
- Description: The name of the service (
notes-service).
- Description: The name of the service (
-
type
- Description:
LoadBalancermakes the service accessible externally.
- Description:
-
ports.port
- Description: External port for the service (
8100).
- Description: External port for the service (
-
ports.targetPort
- Description: Maps to the container’s port (
8100).
- Description: Maps to the container’s port (
-
selector
- Description: Matches Pods labeled
app: notes-service.
- Description: Matches Pods labeled
How It Works
- A user sends a request to the LoadBalancer at port
8100. - The Service forwards the request to one of the available Pods.
- 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