0
0
KubernetesHow-ToBeginner · 4 min read

How to Configure Ingress Rules in Kubernetes

To configure ingress rules in Kubernetes, define an Ingress resource with rules that specify hostnames and paths to route traffic to backend Services. Each rule maps a request path to a service and port, enabling external access to your cluster applications.
📐

Syntax

An Ingress resource uses rules to define how incoming requests are routed. Each rule includes a host and http paths. Each path specifies a path pattern and a backend service name and port.

The main parts are:

  • host: The domain name to match.
  • http.paths: List of paths and their backend services.
  • backend.service.name: The Kubernetes service to route to.
  • backend.service.port.number: The port on the service.
yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
spec:
  rules:
  - host: example.com
    http:
      paths:
      - path: /path1
        pathType: Prefix
        backend:
          service:
            name: service1
            port:
              number: 80
      - path: /path2
        pathType: Prefix
        backend:
          service:
            name: service2
            port:
              number: 8080
💻

Example

This example shows an ingress that routes requests for myapp.example.com. Requests to / go to web-service on port 80, and requests to /api go to api-service on port 8080.

yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: myapp-ingress
spec:
  rules:
  - host: myapp.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: web-service
            port:
              number: 80
      - path: /api
        pathType: Prefix
        backend:
          service:
            name: api-service
            port:
              number: 8080
Output
ingress.networking.k8s.io/myapp-ingress created
⚠️

Common Pitfalls

  • Missing pathType: Always specify pathType (e.g., Prefix or Exact) to avoid errors.
  • Incorrect service name or port: The backend service must exist and the port must be correct.
  • Host mismatch: Requests to hosts not listed in rules won't be routed.
  • No ingress controller: An ingress resource needs a running ingress controller to work.
yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: bad-ingress
spec:
  rules:
  - host: wronghost.com
    http:
      paths:
      - path: /test
        # Missing pathType causes error
        backend:
          service:
            name: missing-service
            port:
              number: 80
---
# Corrected version
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: good-ingress
spec:
  rules:
  - host: correcthost.com
    http:
      paths:
      - path: /test
        pathType: Prefix
        backend:
          service:
            name: existing-service
            port:
              number: 80
📊

Quick Reference

Remember these tips when configuring ingress rules:

  • Always specify pathType to define how paths are matched.
  • Use correct service names and ports matching your cluster services.
  • Define hosts to control which domains the ingress responds to.
  • Ensure an ingress controller is installed and running in your cluster.

Key Takeaways

Define ingress rules with hosts and paths to route traffic to services.
Always specify pathType (Prefix or Exact) for each path.
Ensure backend services and ports exist and match your ingress rules.
Ingress resources require a running ingress controller to function.
Test your ingress by accessing the defined host and paths externally.