Kubernetes Ingress & Services Guide: Master Traffic Routing

Kubernetes Ingress & Services

Photo by Growtika on Unsplash

When you run a micro‑service stack on Kubernetes, every request that comes in has to find its way to the right pod. That routing happens inside the cluster via Services and outside the cluster via Ingress controllers. Understanding how these two layers work together is essential for any platform engineer who wants predictable load balancing, zero‑downtime upgrades, and secure traffic paths.

This article dives into the practical side of Ingress and Services—how they fit in the Kubernetes networking stack, how to pick the right controller, how to expose services safely, and how to troubleshoot common pitfalls. It’s written for engineers who already know the basics of Pods and Deployments but want a deeper, hands‑on view of traffic routing.


1. The Two Faces of Traffic: Services vs Ingress

Layer What it does Typical use case
Service (ClusterIP / NodePort / LoadBalancer) Internal DNS name that points to a set of pods. Implements kube‑proxy load balancing and optional session affinity. Expose a pod internally, or expose it via a cloud LB (LoadBalancer).
Ingress External HTTP/HTTPS routing into the cluster. Uses an Ingress controller (NGINX, Traefik, Istio, etc.) to terminate TLS, perform path‑based routing, and apply rate limits or authentication. Publicly expose multiple services behind a single IP or hostname.

Key takeaway: Services are the “virtual endpoints” inside Kubernetes; Ingress is the “gateway” that brings external traffic into those endpoints.


2. Choosing an Ingress Controller

Controller Strengths Common pitfalls
NGINX (open‑source) Mature, highly configurable, supports HTTP/HTTPS, websockets, TCP/UDP via annotations. Requires careful tuning of worker processes; can be heavy if you run many replicas.
Traefik Auto‑discovery from Kubernetes resources, built‑in Let’s Encrypt support, lightweight. Less mature for complex TLS policies; may need custom middlewares for advanced auth.
Istio Gateway (Envoy) Rich feature set: traffic splitting, mirroring, retries, circuit breaking. Adds significant overhead; steep learning curve.

When selecting a controller, ask:

  1. Do I need advanced traffic shaping? If yes, consider Envoy‑based solutions.
  2. How much external TLS termination is required? Traefik’s built‑in cert manager can simplify this.
  3. What is the operational footprint? NGINX may be heavier but offers proven stability.

3. Building a Service‑to‑Ingress Pipeline

Below is a minimal, reproducible example that shows how to expose an HTTP service via Ingress on a local Minikube cluster.

3.1 Deploy a Sample App

# hello.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world
spec:
  replicas: 2
  selector:
    matchLabels:
      app: hello
  template:
    metadata:
      labels:
        app: hello
    spec:
      containers:
      - name: web
        image: hashicorp/http-echo:v0.2.3
        args: ["-text=Hello, Kubernetes!"]
        ports:
        - containerPort: 5678
---
apiVersion: v1
kind: Service
metadata:
  name: hello-service
spec:
  selector:
    app: hello
  ports:
  - port: 80
    targetPort: 5678
kubectl apply -f hello.yaml

Tip: The hashicorp/http-echo image is a lightweight echo server that’s great for demos.

3.2 Install NGINX Ingress

# Add the official chart repo
helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
helm repo update

# Install into its own namespace
kubectl create ns ingress-nginx
helm install nginx-ingress ingress-nginx/ingress-nginx \
  --namespace ingress-nginx \
  --set controller.publishService.enabled=true

The publishService.enabled flag ensures that the Ingress controller creates a Service exposing the LB IP, which is handy on cloud providers.

3.3 Create an Ingress Resource

# hello-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: hello-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
  - host: hello.local
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: hello-service
            port:
              number: 80
kubectl apply -f hello-ingress.yaml

3.4 Test It

On Minikube, you can get the external IP:

minikube ip
# e.g., 192.168.49.2

Add a host entry to /etc/hosts:

192.168.49.2 hello.local

Now visit http://hello.local in your browser or curl:

curl http://hello.local
# Hello, Kubernetes!

You’ve just wired an external domain (hello.local) to a Service (hello-service) via Ingress.


4. Advanced Traffic Management Patterns

4.1 Path‑Based Routing

If you run multiple services under the same host:

rules:
- host: api.example.com
  http:
    paths:
    - path: /users
      backend:
        serviceName: users-service
        servicePort: 80
    - path: /orders
      backend:
        serviceName: orders-service
        servicePort: 80

Ingress controllers will match the longest prefix, so /users and /orders are routed to their respective services.

4.2 TLS Termination

spec:
  tls:
  - hosts:
    - api.example.com
    secretName: api-tls

Create a Kubernetes Secret with your certificate:

kubectl create secret tls api-tls --cert=fullchain.pem --key=privkey.pem

The controller will terminate TLS at the edge, forwarding plain HTTP to pods (unless you enable secureBackend).

4.3 Rate Limiting & IP Whitelisting

Annotations let you tweak NGINX behavior:

metadata:
  annotations:
    nginx.ingress.kubernetes.io/limit-connections: "10"
    nginx.ingress.kubernetes.io/whitelist-source-range: "192.168.0.0/16,10.0.0.0/8"

For more complex policies (e.g., OAuth2), use an Ingress controller that supports middleware or integrate a service mesh.


5. Common Failure Modes & How to Spot Them

Symptom Likely cause Quick check
Ingress shows “Pending” status Controller not ready, missing ingressClassName, or no external IP provisioned kubectl get pods -n ingress-nginx
Requests return 502/503 from Ingress Backend pod is unhealthy or unreachable kubectl describe svc hello-service and kubectl logs <pod>
DNS resolves but you get “Connection refused” Service selector mismatch (no matching pods) kubectl get pods -l app=hello
TLS handshake fails Secret missing or wrong cert/key kubectl describe secret api-tls

Pro Tip: Use kube-proxy logs (/var/log/kubelet.log) to see if kube‑proxy is updating iptables correctly when new pods join.


6. Performance & Scaling Considerations

  1. Ingress Controller Replicas – Deploy multiple replicas for high availability, but remember that some controllers (e.g., NGINX) share a single iptables table; you may need to enable ingress‑controller sidecar or use an external load balancer.
  2. Connection Limits – By default, NGINX allows 1 000 connections per worker. For bursty traffic, increase worker_connections.
  3. TLS Offloading vs End‑to‑End – Terminating TLS at the controller reduces CPU on pods but introduces a trust boundary. If you need end‑to‑end encryption, use an Envoy sidecar or service mesh.
  4. Service Mesh Integration – Istio’s Gateway can replace Ingress for advanced traffic policies, but it adds ~200 MB of extra resources per pod.

7. Security Checklist

  • [ ] Use TLS certificates from a reputable CA (Let’s Encrypt via cert‑manager).
  • [ ] Enable HTTP/2 only if supported by all clients.
  • [ ] Apply nginx.ingress.kubernetes.io/auth-url for external authentication when needed.
  • [ ] Restrict Ingress access to known IP ranges with whitelist-source-range.
  • [ ] Keep the controller image up‑to‑date; monitor CVE feeds (e.g., NGINX’s security advisories).

8. When to Use a Service vs an Ingress

Scenario Recommendation
Internal micro‑service communication Expose via ClusterIP or Headless Service, no Ingress needed.
Public API with multiple backends Create one Ingress per domain; route paths to services.
Legacy application that needs TCP/UDP Use the controller’s annotations for TCP/UDP load balancing (nginx.ingress.kubernetes.io/configuration-snippet).
Zero‑downtime blue/green deployments Leverage Ingress’ weighted routing or use a service mesh for traffic splitting.

9. Wrap‑Up

  • Services are the glue that keeps pods reachable inside Kubernetes.
  • Ingress controllers bring those services to the outside world, handling TLS, path routing, and load balancing.
  • Pick a controller that matches your feature needs and operational constraints.
  • Test early: deploy a minimal app, expose it with Ingress, verify DNS, TLS, and metrics.
  • Keep an eye on logs and health endpoints; most traffic issues surface there first.

By mastering these building blocks, you’ll be able to build robust, scalable, and secure ingress paths for any Kubernetes workload—whether it’s a single micro‑service or a full multi‑tenant platform. Happy routing!

Frequently Asked Questions

What is the difference between a Service and an Ingress in Kubernetes?

A Service is an internal virtual endpoint that load‑balances traffic to a set of pods, while an Ingress acts as an external gateway that routes HTTP/HTTPS requests into the cluster using an Ingress controller. Services expose pods inside or outside the cluster; Ingress consolidates multiple services behind a single IP and handles TLS termination.

Which Ingress controller should I choose for my workload?

If you need advanced traffic shaping, retries, or circuit breaking, go with Istio Gateway (Envoy). For most use cases where stability and maturity matter, NGINX is reliable; if you want lightweight auto‑discovery and built‑in Let’s Encrypt support, try Traefik. Consider operational footprint and TLS needs when deciding.

How do I expose a service with HTTPS using NGINX Ingress?

Create an Ingress resource that references your Service and specify a tls block pointing to a Kubernetes Secret containing the certificate and key. The NGINX controller will then terminate TLS at the edge and forward traffic to the backend Service.

Why does my Ingress not show a public IP after installation?

Most cloud providers expose an external LoadBalancer automatically, but local setups (e.g., Minikube) may require manual port forwarding or --set controller.publishService.enabled=true during Helm install. Verify that the Ingress controller’s Service has an external IP or use kubectl port-forward to access it locally.

Related Articles

0 0 votes
Article Rating
guest
0 Comments
Oldest
Newest Most Voted
Scroll to Top