0
0
KubernetesHow-ToBeginner · 4 min read

How to Use Kubernetes Ingress with Multiple Services

Use a single Ingress resource with multiple rules or paths to route traffic to different services. Each rule defines a host or path that directs requests to a specific Service in your cluster.
📐

Syntax

An Ingress resource defines rules to route external HTTP(S) traffic to Kubernetes Services. You specify rules with host and http.paths. Each path maps to a backend.service.name and backend.service.port.number.

Key parts:

  • host: Domain name to match (optional).
  • http.paths: List of URL paths to match.
  • backend.service.name: Target service name.
  • backend.service.port.number: Target service port.
yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /service1
        pathType: Prefix
        backend:
          service:
            name: service1
            port:
              number: 80
      - path: /service2
        pathType: Prefix
        backend:
          service:
            name: service2
            port:
              number: 80
💻

Example

This example shows an Ingress routing traffic to two services: service1 and service2. Requests to example.com/service1 go to service1, and requests to example.com/service2 go to service2.

yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: multi-service-ingress
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /service1
        pathType: Prefix
        backend:
          service:
            name: service1
            port:
              number: 80
      - path: /service2
        pathType: Prefix
        backend:
          service:
            name: service2
            port:
              number: 80
Output
Ingress resource "multi-service-ingress" created. Traffic to example.com/service1 routes to service1, and example.com/service2 routes to service2.
⚠️

Common Pitfalls

  • Forgetting to specify pathType causes errors; use Prefix or Exact.
  • Not matching the host in requests will cause routing to fail if host is set.
  • Using the same path for multiple services causes conflicts; paths must be unique per host.
  • Ingress controller must be installed and running; otherwise, Ingress rules won't work.
yaml
Wrong example (missing pathType):

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: bad-ingress
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /service1
        backend:
          service:
            name: service1
            port:
              number: 80

Right example (with pathType):

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: good-ingress
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /service1
        pathType: Prefix
        backend:
          service:
            name: service1
            port:
              number: 80
📊

Quick Reference

Tips for using Ingress with multiple services:

  • Use host to separate domains.
  • Use path and pathType to route URLs.
  • Each path must point to one service and port.
  • Check your Ingress controller supports your configuration.

Key Takeaways

Use a single Ingress resource with multiple rules or paths to route to different services.
Always specify pathType (Prefix or Exact) for each path to avoid errors.
Ensure your Ingress controller is installed and running to apply rules.
Paths must be unique per host to prevent routing conflicts.
Use host rules to separate traffic by domain when needed.