Services Basics
This section covers the everyday Service patterns you will use to give ephemeral pods a stable virtual IP and DNS name.
Search across all documentation pages
This section covers the everyday Service patterns you will use to give ephemeral pods a stable virtual IP and DNS name.
Each example is a small, complete manifest or command you can adapt directly.
kubectl matching your control plane minor version.app: web.Quick check that the pieces exist:
kubectl get pods -n kube-system -l k8s-app=kube-dns
kubectl get deploy -A | grep -i coredns || trueThe default Service type, reachable only inside the cluster.
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
ports:
- name: http
port: 80
targetPort: 8080selector matches pods labeled app: web; those become the backends.port is the port the Service listens on; targetPort is the container port.type means ClusterIP, so this VIP is cluster-internal only.name: http) so probes and other objects can reference them.Every Service gets a DNS record from CoreDNS.
kubectl run tmp --rm -it --image=busybox:1.36 --restart=Never -- \
nslookup web.default.svc.cluster.local<service>.<namespace>.svc.cluster.local.web alone resolves thanks to the search domain./etc/resolv.conf.Referencing the port by name decouples the Service from container port numbers.
spec:
selector:
app: web
ports:
- name: http
port: 80
targetPort: httptargetPort: http points at the container port named http.ports: [{name: http, containerPort: 8080}].A Service can map several ports at once.
spec:
selector:
app: web
ports:
- name: http
port: 80
targetPort: 8080
- name: metrics
port: 9090
targetPort: 9090name when there is more than one port.http for traffic, metrics for Prometheus scraping.TCP) unless you truly need UDP or SCTP.Confirm which pods a Service is actually routing to.
kubectl get endpointslices -l kubernetes.io/service-name=web
kubectl describe service webkubectl describe service summarizes the selector, ports, and endpoints.Test a Service from your laptop without exposing it publicly.
kubectl port-forward service/web 8080:808080 now tunnels to the Service port 80.Endpoints follow pod readiness, so define a probe.
readinessProbe:
httpGet:
path: /healthz
port: http
periodSeconds: 5Skip the VIP and return pod IPs directly, which StatefulSets need.
apiVersion: v1
kind: Service
metadata:
name: db
spec:
clusterIP: None
selector:
app: db
ports:
- name: pg
port: 5432clusterIP: None makes the Service headless.db-0.db.Keep the real caller IP for a LoadBalancer Service.
spec:
type: LoadBalancer
externalTrafficPolicy: Local
selector:
app: web
ports:
- name: http
port: 80
targetPort: 8080Local routes only to pods on the receiving node, avoiding a second hop.Cluster policy.Alias an out-of-cluster host behind a stable in-cluster name.
apiVersion: v1
kind: Service
metadata:
name: payments-api
spec:
type: ExternalName
externalName: api.payments.example.comExternalName returns a CNAME to the external host - no proxying or VIP.Keep a caller on the same backend for a window when you need stickiness.
spec:
selector:
app: web
sessionAffinity: ClientIP
sessionAffinityConfig:
clientIP:
timeoutSeconds: 10800
ports:
- name: http
port: 80
targetPort: 8080sessionAffinity: ClientIP keys routing on the source IP.Stack versions: This page was written for Kubernetes 1.36.2, Docker Engine 29.6.1 (BuildKit default), containerd (CRI runtime on nodes), Helm 3, Compose v2, Argo CD (latest - verify at build), and Gateway API (GA - verify controller support at build).
Reviewed by Chris St. John·Last updated Jul 19, 2026