CKAD field manual

k8s Service YAML example

A Service load-balances to Pods whose labels match selector. port is the Service, targetPort is the container.

A fixed comms buoy; craft take turns docking to the same ring. A fixed comms buoy; craft take turns docking to the same ring.
Create
kubectl expose deploy/web --name=web --port=80 --target-port=80
kubectl expose po/web --name=web --port=80 --target-port=80 --type=NodePort
ClusterIP
apiVersion: v1
kind: Service
metadata:
  name: web
  namespace: default
spec:
  type: ClusterIP
  selector:
    app: web
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 80
NodePort
apiVersion: v1
kind: Service
metadata:
  name: web
spec:
  type: NodePort
  selector:
    app: web
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30080
Headless
spec:
  clusterIP: None
  selector:
    app: web
  ports:
    - port: 80
      targetPort: 80
Inspect
kubectl get svc,ep web
kubectl describe svc web

Fields

selector
Matches Pod labels (template.metadata.labels). Not the Deployment name.
port
The Service port. Clients use this.
targetPort
The container port. Often the same number as port. Often not.
nodePort
Only on type NodePort or LoadBalancer.
clusterIP
None = headless. DNS returns Pod IPs.
kubectl expose
--port is the Service. --target-port is the container.

Watch

  • selector matches Pod labels (Deployment: template.metadata.labels), not the Deployment name.
  • port = Service. targetPort = container. nodePort only on NodePort / LoadBalancer.
  • Default type is ClusterIP. clusterIP: None is headless — DNS returns Pod IPs.

Official docs Services

Practice these objects on a live cluster →