Deployments and Services
Run replicas and expose them with Services. Results appear in the same fence: same-line # comments when short, multiline # blocks below the sample when not. Fences are bash, dockerfile, or yaml as appropriate.
Search across all documentation pages
Run replicas and expose them with Services. Results appear in the same fence: same-line # comments when short, multiline # blocks below the sample when not. Fences are bash, dockerfile, or yaml as appropriate.
Declarative replica management.
apiVersion: apps/v1
kind: Deployment
metadata:
name: api
spec:
replicas: 3
selector:
matchLabels: { app: api }
template:
metadata:
labels: { app: api }
spec:
containers:
- name: api
image: ghcr.io/acme/api:1.0
# 3 pods desiredScale desired count.
spec:
replicas: 5
# kubectl scale deploy/api --replicas=5Max unavailable/surge during rollout.
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 0
maxSurge: 1
# zero-downtime style rolloutStable virtual IP inside the cluster.
apiVersion: v1
kind: Service
metadata:
name: api
spec:
selector: { app: api }
ports:
- port: 80
targetPort: 8080
# ClusterIP allocatedExternal exposure types.
spec:
type: LoadBalancer
ports:
- port: 80
targetPort: 8080
# cloud LB or NodePort fallbackNo ClusterIP for direct pod DNS.
spec:
clusterIP: None
selector: { app: zk }
# dns: pod-ip.svcDNS CNAME Service.
spec:
type: ExternalName
externalName: db.example.com
# resolves to external hostClientIP stickiness.
spec:
sessionAffinity: ClientIP
# same client IP -> same pod (best effort)Labels on Deployment vs pod template.
metadata:
labels: { app: api }
spec:
template:
metadata:
labels: { app: api, version: v1 }
# Service selects pod labelsDelay availability during rollout.
spec:
minReadySeconds: 10
# pod must be ready 10s to countFail rollout if stuck.
spec:
progressDeadlineSeconds: 600
# ProgressDeadlineExceeded conditionHow many old ReplicaSets to keep.
spec:
revisionHistoryLimit: 5
# rollout undo targetsNamed ports for clarity.
ports:
- name: http
port: 80
targetPort: http
# targetPort can be name or numberService without selector - manual Endpoints.
# Service with no selector + Endpoints object
# for external backendsInclude not-ready pods in Endpoints.
publishNotReadyAddresses: true
# useful for stateful joinersStack versions: Kubernetes 1.36.2 · Docker Engine 29.6.1 · Helm 3 · Compose v2 · containerd via CRI
Reviewed by Chris St. John·Last updated Jul 18, 2026