Lesson 7 of 11 · 11 min · Cluster networking
Service types and Ingress
What you will learn
- The four Service types and when to use each
- Why Ingress exists, and the difference between an Ingress resource and an Ingress controller
- Host-based and path-based routing, tested
- TLS termination at the Ingress
- The traps: path handling, IngressClass, and the move toward the Gateway API
In simple words
A Service can be private to the cluster or opened to the outside world in a few different ways. For websites, an Ingress works like the reception desk of a building: one entrance, which sends each visitor to the right office according to the web address they asked for.
Words to know
- ClusterIP: the default kind of Service: reachable only from inside the cluster.
- LoadBalancer: a kind of Service that asks the cloud for a public address.
- Ingress: rules that send web requests to different Services by host name or path.
- Ingress controller: the program that actually carries out those rules. Without one, an Ingress does nothing.
Service types
A Service's type decides how it is reachable:
| Type | Reachable from | How | Typical use |
|---|---|---|---|
| ClusterIP (default) | inside the cluster only | virtual IP + DNS name | internal services (databases, back-end APIs) |
| NodePort | outside, on every node | opens the same high port (30000-32767) on every node's IP and forwards to the Service | quick tests, on-premises setups, or as the target of an external load balancer |
| LoadBalancer | outside, via a cloud load balancer | asks the cloud provider to create a load balancer with a public IP/name pointing at the nodes | exposing one service directly (TCP/UDP, non-HTTP) |
| ExternalName | inside | DNS CNAME to an external name | alias to an outside dependency |
Our lab results:
api-ci ClusterIP 10.43.188.171 <none> 80/TCP
api-np NodePort 10.43.38.89 <none> 80:30918/TCP
For NodePort the output 80:30918/TCP means Service port 80 is also reachable at port 30918 on every node. Each type includes the previous one (a LoadBalancer also gets a ClusterIP and a NodePort). On a managed cloud cluster a type: LoadBalancer Service gets an EXTERNAL-IP after a minute or two; on a bare local cluster with no provider it stays <pending> (in our lab it stayed <pending> because the single node's port 80 was already taken by the ingress controller's own load balancer). On premises, tools such as MetalLB provide LoadBalancer addresses.
Cost and sprawl: each LoadBalancer Service usually creates a separate cloud load balancer (money, plus a public IP). Ten HTTP services should not mean ten load balancers. That is what Ingress is for.
Ingress
An Ingress is a set of HTTP/HTTPS routing rules: "requests for shop.example.com go to Service shop; /api goes to Service api". It is Layer 7 routing: one public entry point, many back-end services.
Two separate pieces:
- The Ingress resource: your YAML rules. By itself it does nothing.
- The Ingress controller: a running component (a Deployment of a reverse proxy) that watches Ingress resources and configures itself to implement them. Examples: ingress-nginx, Traefik (bundled with k3s), HAProxy, Envoy-based controllers, cloud-native ones (AWS Load Balancer Controller creates an ALB, GKE's Ingress creates a Google load balancer). A cluster may have several, chosen by IngressClass (
ingressClassName: traefik). Without a controller your Ingress objects are ignored: "I created an Ingress but nothing happens" very often means no controller or the wrong class.
internet ──▶ load balancer ──▶ ingress controller Pods ──▶ Service shop ──▶ shop Pods
(reads Ingress rules) └▶ Service blog ──▶ blog Pods
Host-based routing
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: sites
spec:
rules:
- host: shop.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service: {name: shop, port: {number: 80}}
- host: blog.example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service: {name: blog, port: {number: 80}}
kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS
sites traefik shop.example.com,blog.example.com 172.17.0.3 80
The ADDRESS is the controller's entry point. Without real DNS, send the Host header yourself:
curl -H "Host: shop.example.com" http://172.17.0.3/
shop.example.com -> SHOP home [200]
blog.example.com -> BLOG home [200]
unknown.example.com -> 404 page not found [404]
no Host header -> [404]
One IP, several sites, chosen by the HTTP Host header: name-based virtual hosting, as in the Networking course. A request whose host matches no rule gets the controller's default 404. In production you point real DNS records (A or CNAME) for all these hosts at the load balancer in front of the controller.
Path-based routing, and the prefix trap
rules:
- host: www.example.com
http:
paths:
- {path: /shop, pathType: Prefix, backend: {service: {name: shop, port: {number: 80}}}}
- {path: /blog, pathType: Prefix, backend: {service: {name: blog, port: {number: 80}}}}
Our test:
/shop -> 404 (from the nginx backend)
/blog -> 404
Routing worked: the request reached the right Service. The 404 came from the backend, because the Ingress passes the full original path (/shop) to the backend, and nginx has no /shop file. The application must serve under that prefix, or the controller must strip/rewrite the prefix (controller-specific annotations or middleware: nginx rewrite-target, Traefik StripPrefix). This "404 behind a path prefix" is one of the most common Ingress problems. Where possible prefer host-based routing, which avoids rewriting.
pathType matters: Prefix matches the path and everything below it (/shop, /shop/cart), Exact only that URL, ImplementationSpecific leaves it to the controller.
TLS termination
Create a Secret with the certificate and key and name it in the Ingress; the controller terminates HTTPS:
kubectl create secret tls secure-tls --cert=tls.crt --key=tls.key
spec:
tls:
- hosts: [secure.example.com]
secretName: secure-tls
rules:
- host: secure.example.com
http:
paths:
- {path: /, pathType: Prefix, backend: {service: {name: shop, port: {number: 80}}}}
With a self-signed certificate (as in the TLS lesson), curl -k --resolve secure.example.com:443:172.17.0.3 https://secure.example.com/ returned the shop page, and without -k curl failed with exit code 60 (untrusted certificate). In real clusters cert-manager requests and renews Let's Encrypt certificates automatically for annotated Ingresses, so nobody handles key files. Traffic from the controller to the Pods is usually plain HTTP inside the cluster (TLS terminates at the edge; use a service mesh or backend TLS if policy requires encryption everywhere).
Other controller features (via annotations or CRDs)
Redirect HTTP to HTTPS, rate limiting, request size limits, timeouts, authentication, CORS, rewrites, canary weights. These differ per controller, so read your controller's documentation.
Failure behaviour
A rule pointing at a Service that does not exist routes to nothing: our broken.example.com rule returned 404. Check kubectl describe ingress, the controller's logs, kubectl get endpoints for the Service, and the IngressClass.
Gateway API
Ingress is simple and widely used but limited (HTTP only, features hidden in annotations). The Gateway API is its modern, more expressive successor (Gateway, HTTPRoute, with roles split between platform and application teams, traffic splitting, header matching). New controllers support both; for new platforms evaluate the Gateway API.
Common mistakes
- Creating Ingress without a controller or with the wrong
ingressClassName. - A LoadBalancer Service per app instead of one Ingress.
- Path prefixes the backend doesn't know → 404.
- Forgetting DNS records for the hosts.
- Missing readiness probes, so the controller sends traffic to unready Pods (it uses Service endpoints).
- Wrong Service port or a Service in a different namespace (an Ingress can only reference Services in its own namespace).
- Certificates not covering the host or expired.
Try it yourself
- Install an ingress controller on a local cluster (or use k3s with Traefik). Route two hosts to two Deployments and test with
curl -H "Host: ...". - Add path-based routing and see the prefix problem; fix it with your controller's strip-prefix feature.
- Create a self-signed TLS Secret and enable HTTPS on one host.
- Break the Service name in an Ingress rule and find the failure in the controller's logs and events.
Check yourself
What is the difference between an Ingress and an Ingress controller?
The Ingress is a resource containing routing rules. The controller is the software that reads those rules and actually routes traffic. Without a controller the rules have no effect.
Why use Ingress instead of a LoadBalancer Service per application?
One shared entry point (and often one cloud load balancer) can route to many services by host and path, with TLS handled centrally, which saves cost and simplifies DNS and certificates.
A request to /shop through the Ingress reaches the right Service but returns 404. Why?
The Ingress forwards the full path; the backend does not serve /shop. Make the app serve under the prefix or strip the prefix at the controller.
What do ClusterIP, NodePort and LoadBalancer have in common?
Each builds on the previous: a NodePort Service also has a ClusterIP, and a LoadBalancer Service also has a NodePort and ClusterIP.
Interview questions
How does external traffic reach a Pod in a typical cloud Kubernetes setup?
DNS points to a cloud load balancer; it forwards to the ingress controller Pods (via NodePort or directly); the controller matches host and path to a Service; kube-proxy or the dataplane sends it to one of the Service's ready Pods.
How do you add HTTPS to an application on Kubernetes?
Terminate TLS at the Ingress (or Gateway) with a certificate in a TLS Secret, ideally issued and renewed automatically by cert-manager (Let's Encrypt), and redirect HTTP to HTTPS.
When would you use NodePort?
For simple on-premises setups or tests, or as the backend of an external load balancer you manage yourself; rarely as the public interface in production.
Summary
- Service types: ClusterIP (internal), NodePort (node port), LoadBalancer (cloud LB), ExternalName (DNS alias).
- Ingress resources hold HTTP routing rules; an Ingress controller implements them; pick it with IngressClass.
- Host-based routing is simple; path-based routing forwards the full path and often needs prefix stripping.
- TLS terminates at the Ingress with a TLS Secret (cert-manager automates it); consider the Gateway API for new designs.
Going deeper
- Service types: ClusterIP, NodePort, LoadBalancer, ExternalName.
- Ingress: resources, path types and TLS.
- Ingress controllers: the available implementations.
- Gateway API: the successor to Ingress.
- cert-manager: automatic certificates in Kubernetes.
See how much of this you can explain
A free mock interview asks you new questions on Kubernetes for DevOps Engineer and scores every answer, with what to fix.