Browse topics

Services

Version
1.37
Review
Draft

Summary#

A Service gives a stable name to a changing set of Pods chosen by a label selector. The default type, ClusterIP, is reachable inside the cluster. Clients connect to port, and traffic goes to targetPort on a ready Pod. EndpointSlice objects record the backends.

Syntax#

sh
kubectl expose deployment NAME --port=80 --target-port=8080
kubectl get endpointslices -l kubernetes.io/service-name=NAME
yaml
apiVersion: v1
kind: Service
metadata:
  name: web
spec:
  selector:
    app: web
  ports:
    - port: 80
      targetPort: 8080

Example#

sh
(
  set -eu
  ns="ref-services-$$"
  kubectl create namespace "$ns" >/dev/null
  trap 'kubectl delete namespace "$ns" >/dev/null' EXIT
  kubectl wait -n "$ns" --for=create serviceaccount/default --timeout=60s >/dev/null

  kubectl apply -n "$ns" -f - >/dev/null <<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 2
  selector:
    matchLabels:
      app: web
  template:
    metadata:
      labels:
        app: web
    spec:
      terminationGracePeriodSeconds: 1
      containers:
        - name: server
          image: busybox:1.37
          command: ["sh", "-c", "mkdir /www && echo 'hello from web' > /www/index.html && exec httpd -f -p 8080 -h /www"]
          ports:
            - name: http
              containerPort: 8080
          readinessProbe:
            tcpSocket:
              port: http
            periodSeconds: 1
---
apiVersion: v1
kind: Service
metadata:
  name: web
spec:
  selector:
    app: web
  ports:
    - port: 80
      targetPort: http
---
apiVersion: v1
kind: Pod
metadata:
  name: client
spec:
  terminationGracePeriodSeconds: 1
  containers:
    - name: client
      image: busybox:1.37
      command: ["sleep", "3600"]
EOF
  kubectl rollout status -n "$ns" deployment/web --timeout=120s >/dev/null
  kubectl wait -n "$ns" --for=condition=Ready pod/client --timeout=120s >/dev/null

  ready_backends() {
    kubectl get -n "$ns" endpointslices -l kubernetes.io/service-name=web \
      -o jsonpath='{range .items[*]}{range .endpoints[*]}{.conditions.ready}{"\n"}{end}{end}' |
      grep -c true || true
  }
  tries=0
  until [ "$(ready_backends)" -eq 2 ]; do
    tries=$((tries + 1)); [ "$tries" -lt 60 ]; sleep 1
  done

  kubectl get -n "$ns" service web \
    -o jsonpath='type={.spec.type} port={.spec.ports[0].port} targetPort={.spec.ports[0].targetPort}{"\n"}'
  echo "ready backends: $(ready_backends)"
  kubectl exec -n "$ns" client -- sh -c \
    'for i in 1 2 3 4 5 6 7 8 9 10; do wget -T 2 -qO- http://web && exit 0; sleep 1; done; exit 1'
)

Expected output#

Text
type=ClusterIP port=80 targetPort=http
ready backends: 2
hello from web

Common mistakes#

If the selector does not match the Pod labels, the Service is created without error but has no backends. Check its EndpointSlices and compare the selector with the labels. An unready Pod is normally excluded from ready backends, except when publishNotReadyAddresses is set. Use SERVICE.NAMESPACE to reach a Service from another namespace by DNS.

Version applicability#

The example targets Kubernetes 1.37. EndpointSlices are stable since 1.21. The older Endpoints API is deprecated since 1.33, so scripts should read EndpointSlices.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0