Browse topics

Ingress

Version
1.37
Since
1.19
Review
Draft

Summary#

An Ingress describes HTTP and HTTPS routing from outside the cluster to Services, by host and path. The object only holds rules. An Ingress controller, installed separately, must act on them. The project recommends the Gateway API for new work, and the Ingress API is frozen but still generally available.

Syntax#

sh
kubectl create ingress NAME --class=CLASS --rule="HOST/PATH*=SERVICE:PORT"
kubectl get ingressclass
yaml
spec:
  ingressClassName: CLASS
  tls:
    - hosts: ["web.example.test"]
      secretName: web-tls
  rules:
    - host: web.example.test
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: web
                port:
                  number: 80

Example#

This example needs an installed Ingress controller. The defaults use the traefik IngressClass and the traefik Service on port 80 in the traefik namespace. Set INGRESS_CLASS, INGRESS_SERVICE (as service.namespace) or INGRESS_PORT to match your controller. The client sends the host header directly, so no external DNS record is needed.

sh
(
  set -eu
  ns="ref-ingress-$$"
  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

  ingress_class=${INGRESS_CLASS:-traefik}
  ingress_service=${INGRESS_SERVICE:-traefik.traefik}
  ingress_port=${INGRESS_PORT:-80}
  host="web.$ns.example.test"
  kubectl get ingressclass "$ingress_class" >/dev/null

  kubectl apply -n "$ns" -f - <<'EOF'
apiVersion: v1
kind: Pod
metadata:
  name: web
  labels:
    app: web
spec:
  terminationGracePeriodSeconds: 1
  containers:
    - name: web
      image: busybox:1.37
      command: ["sh", "-c", "mkdir /www && echo 'hello through ingress' > /www/greeting && exec httpd -f -p 8080 -h /www"]
      readinessProbe:
        tcpSocket:
          port: 8080
        periodSeconds: 1
---
apiVersion: v1
kind: Service
metadata:
  name: web
spec:
  selector:
    app: web
  ports:
    - port: 80
      targetPort: 8080
EOF
  kubectl create ingress -n "$ns" web --class="$ingress_class" \
    --rule="$host/greeting*=web:80"
  kubectl run -n "$ns" client --image=busybox:1.37 \
    --overrides='{"spec":{"terminationGracePeriodSeconds":1}}' --command -- sleep 3600 >/dev/null
  kubectl wait -n "$ns" --for=condition=Ready pod/web pod/client --timeout=120s >/dev/null
  kubectl exec -n "$ns" client -- sh -c '
    for attempt in 1 2 3 4 5 6 7 8 9 10; do
      if body=$(wget -T 2 -qO- --header "Host: $2" "$1" 2>&1); then
        printf "%s\n" "$body"
        exit 0
      fi
      sleep 1
    done
    printf "%s\n" "$body" >&2
    exit 1
  ' sh "http://$ingress_service:$ingress_port/greeting" "$host"
)

Expected output#

Text
pod/web created
service/web created
ingress.networking.k8s.io/web created
hello through ingress

Common mistakes#

A successful kubectl apply does not mean traffic works. Without a controller for the ingressClassName, nothing serves the rules. The Ingress spec defines Prefix by path element, so /foo matches /foo/bar and excludes /foobar. Traefik requires providers.kubernetesIngress.strictPrefixMatching=true for that behavior.

Use a TLS Secret in the same namespace. For Traefik, also enable TLS on the entrypoint or add the annotation traefik.ingress.kubernetes.io/router.tls: "true".

Version applicability#

The networking.k8s.io/v1 Ingress API is available since Kubernetes 1.19. The earlier beta versions are no longer served since 1.22, and v1 requires a pathType on every path. The example targets 1.37.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0