Skip to content
Kubernetes

Ingress Routing

Route external HTTP traffic to services by host and path.

By EZ4Code Team
ingressroutingtls

Code

# ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: api-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
    - host: api.example.com
      http:
        paths:
          - path: /v1
            pathType: Prefix
            backend:
              service:
                name: api-v1
                port: { number: 80 }
          - path: /
            pathType: Prefix
            backend:
              service:
                name: api-v2
                port: { number: 80 }
  tls:
    - hosts: [api.example.com]
      secretName: api-tls

Explanation

An Ingress object routes HTTP/HTTPS traffic to services based on host and path, typically backed by a controller like ingress-nginx. pathType Prefix matches URL prefixes, and the rewrite-target annotation strips the matched path before forwarding. The tls section references a Secret containing the TLS certificate.

More Kubernetes Snippets