Kubernetes Cheatsheet
Services Reference
Use this Kubernetes reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
Service Types
| Type | Accessibility | Use case |
|---|---|---|
ClusterIP | Inside cluster only | Default; Pod-to-Pod communication |
NodePort | NodeIP:port from outside | Dev/testing; exposes on 30000-32767 |
LoadBalancer | External via cloud LB | Production ingress (costs money) |
ExternalName | DNS alias to external FQDN | Route to external service by name |
Headless | Direct Pod DNS (no VIP) | StatefulSets, service-discovery sidecars |
ClusterIP Service (default)
apiVersion: v1
kind: Service
metadata:
name: api
spec:
selector:
app: api # matches Pod labels
ports:
- name: http
port: 80 # port the Service listens on
targetPort: 8080 # port on the Pod (or named port)
protocol: TCP
type: ClusterIPDNS: api.default.svc.cluster.local (short form: api within same namespace).
NodePort Service
apiVersion: v1
kind: Service
metadata:
name: api-nodeport
spec:
selector:
app: api
ports:
- port: 80
targetPort: 8080
nodePort: 31000 # omit to auto-assign (30000-32767)
type: NodePortAccess via <NodeIP>:31000.
LoadBalancer Service
apiVersion: v1
kind: Service
metadata:
name: api-lb
annotations:
service.beta.kubernetes.io/aws-load-balancer-type: "nlb" # AWS NLB
spec:
selector:
app: api
ports:
- port: 443
targetPort: 8080
type: LoadBalancerkubectl get svc api-lb # EXTERNAL-IP column shows the LB address once provisionedHeadless Service
No ClusterIP; DNS returns individual Pod IPs. Required by StatefulSets.
apiVersion: v1
kind: Service
metadata:
name: postgres
spec:
clusterIP: None # headless
selector:
app: postgres
ports:
- port: 5432DNS for StatefulSet pod: postgres-0.postgres.default.svc.cluster.local
ExternalName Service
apiVersion: v1
kind: Service
metadata:
name: external-db
spec:
type: ExternalName
externalName: my-rds.us-east-1.rds.amazonaws.comPods resolve external-db → CNAME → the external hostname.
Multi-Port Service
spec:
ports:
- name: http
port: 80
targetPort: 8080
- name: metrics
port: 9090
targetPort: 9090Port names are required when more than one port is defined.
Session Affinity
spec:
sessionAffinity: ClientIP
sessionAffinityConfig:
clientIP:
timeoutSeconds: 3600kubectl Service Commands
| Command | What it does |
|---|---|
kubectl get svc | List services |
kubectl get svc -o wide | Show selector and endpoints |
kubectl describe svc api | Endpoints, selectors, events |
kubectl get endpoints api | List the backing Pod IPs |
kubectl port-forward svc/api 8080:80 | Forward local 8080 → service 80 |
kubectl expose deployment api --port=80 --target-port=8080 | Quick ClusterIP |
kubectl expose deployment api --type=LoadBalancer --port=80 | Quick LB |
kubectl delete svc api | Delete service |
Service DNS Naming
<service>.<namespace>.svc.<cluster-domain>
| Reference | Resolves from |
|---|---|
api | Same namespace only |
api.production | Any namespace |
api.production.svc.cluster.local | Always (FQDN) |
Endpoint Slices
Modern replacement for Endpoints; auto-managed by the controller:
kubectl get endpointslices -l kubernetes.io/service-name=api