Tuesday, June 9, 2026

K8S Reference - Service YAML

The table below summarizes all important elements used when defining a Kubernetes Service resource.

Element Required / Optional Description Example Syntax
apiVersion Required The API group. For Services, this is always v1. apiVersion: v1
kind Required Defines the resource type. kind: Service
metadata.name Required The DNS name that other applications inside the cluster will use to talk to this service. name: my-db-service
spec.ports Required A list of network ports to expose. Includes the port (the Service's port) and targetPort (the Pod's actual application port).
- port: 80

  targetPort: 8080
spec.selector Optional (Recommended) A key-value label pair used to target which Pods receive traffic. Crucial for connecting the Service to your application.
selector:

  app: my-database
spec.type Optional Controls how the Service is exposed: ClusterIP (internal only), NodePort (exposes via host ports), or LoadBalancer (cloud provider external IP). Defaults to ClusterIP. type: ClusterIP
spec.clusterIP Optional Can be set to None to create a Headless Service, which is strictly required when pairing with a StatefulSet to handle direct pod addressing. clusterIP: None
Tip: The most commonly used Service types are ClusterIP, NodePort, and LoadBalancer. For StatefulSets, remember that a Headless Service (clusterIP: None) is typically required to provide stable network identities to Pods.

No comments:

Post a Comment

K8S - Important Reference Articles on this Blog

The following articles provide a neat and concise reference for creating YAML files for some of the most commonly use...