Back to Terraform

Service Exposure: Cluster Networking, Ingress and Gateway Controllers

November 6, 2026 Wasil Zafar 17 min read

Connect workload selectors to traffic routing without assuming API objects create a working data plane

Table of Contents

  1. Trace a Request Through Service Selectors and Endpoints
  2. Choose ClusterIP, LoadBalancer or an Explicit Gateway Path
  3. Ingress and Gateway APIs Require the Right Controllers
  4. Separate Certificate and DNS Controllers from Terraform Ownership
  5. Verify Routes and Track Cloud-Created Resources

Trace a Request Through Service Selectors and Endpoints

A Service is an identity; the data plane is an implementation. The object declares a stable virtual IP, a DNS name and a port mapping, and no field in that declaration moves a packet. A node-level data plane programs the translation, and an endpoint controller fills in the destinations. Terraform creates the identity; the endpoint controller and the node agents create the behaviour.

The selector is the join, and readiness is the filter. An endpoint slice lists only the addresses of pods whose labels match the selector and whose readiness check passes. A running pod that is not ready is not a destination. That is why a clean apply can be followed by a name that resolves and connections that are refused.

One request crosses five boundaries. A client resolves the name to the virtual IP, a node rewrites the destination to a ready pod, the pod answers on its container port, and the reply is translated back. Each boundary fails differently: no record, no endpoints, a wrong port, or a policy drop.

A named target port is a promise to every selected pod. When the Service references a port by name, each selected container must declare that name, or the endpoint carries no port to forward to. Numbers are more forgiving, which is why a named port deserves an explicit check rather than a copied manifest.

Headless and ExternalName answer different questions. A headless Service removes the virtual IP so DNS returns pod addresses for peer discovery, and publish_not_ready_addresses decides whether unready peers appear in the answer. ExternalName returns a CNAME without proxying, so there is no load balancing or port mapping left to verify.

Figure 1 follows one request through those boundaries and marks where each failure begins.

Trace a Request Through Service Selectors and Endpoints
%%{init: {'theme':'base','themeCSS':'.edgeLabel .label text, .edgeLabel text, .edgeLabel span { fill:#132440 !important; color:#132440 !important; }','themeVariables': {'primaryColor':'#16476A','primaryTextColor':'#ffffff','primaryBorderColor':'#132440','secondaryColor':'#3B9797','secondaryTextColor':'#ffffff','secondaryBorderColor':'#132440','tertiaryColor':'#c4873b','tertiaryTextColor':'#132440','tertiaryBorderColor':'#132440','lineColor':'#5a6e82','textColor':'#132440','titleColor':'#132440','mainBkg':'#16476A','nodeBorder':'#132440','clusterBkg':'#eef3f7','clusterBorder':'#132440','edgeLabelBackground':'#ffffff','actorBkg':'#16476A','actorTextColor':'#ffffff','actorBorder':'#132440','activationBkgColor':'#3B9797','activationBorderColor':'#132440','signalColor':'#5a6e82','signalTextColor':'#132440','noteBkgColor':'#c4873b','noteTextColor':'#132440','noteBorderColor':'#132440','labelBoxBkgColor':'#a8dede','labelBoxBorderColor':'#132440','labelTextColor':'#132440','loopTextColor':'#132440','altBackground':'#f8f9fa','fontFamily':'DM Sans, sans-serif'}}}%%
flowchart TD
    C["Client request
inside or outside the cluster"] --> D{"Name resolves?"} D -->|"no record"| X1["NXDOMAIN
nothing to connect to"] D -->|"yes"| S["Service
virtual IP and port"] S --> G{"Selector matches
pod labels?"} G -->|"no"| X2["Empty endpoint slice
connection refused"] G -->|"yes"| R{"Pod passes
readiness?"} R -->|"no"| X3["Running but excluded
no traffic sent"] R -->|"yes"| P{"Named target port
declared?"} P -->|"no"| X4["Endpoint without port
forwarding fails"] P -->|"yes"| E["EndpointSlice
pod IP and port"] E --> N["Node data plane
rewrites destination"] N --> O["Pod answers"] O --> K["Client receives response"] classDef primary fill:#132440,color:#ffffff,stroke:#0b172b; classDef secondary fill:#16476A,color:#ffffff,stroke:#0f314a; classDef ok fill:#3B9797,color:#ffffff,stroke:#2a6f6f; classDef danger fill:#BF092F,color:#ffffff,stroke:#7a0420; classDef warn fill:#c4873b,color:#132440,stroke:#8a5a1f; class C,S primary; class D,G,R,P warn; class E,N,O secondary; class K ok; class X1,X2,X3,X4 danger;

Figure 2 separates the object Terraform plans from the work a node performs. The Service and its slice are cluster objects; the translation rule and the pod are runtime. A selector that matches nothing still produces a valid Service with an address and an empty endpoint set.

A Service identity, an endpoint slice and a node data plane translate one request to a ready pod Terraform writes a Service with a selector and port. The endpoint controller writes a slice containing only ready pods. A node data plane rewrites the virtual IP to a pod IP. An unready pod is dashed back to the slice because readiness excluded it, and two notes record that an empty selector and an unready pod both leave the Service valid but unroutable. Identity, endpoints, data plane Terraform plans Service selector app=web port 80 to http A virtual IP and a DNS name, no packet handling. Status shows the address, not the endpoint list. Endpoint controller EndpointSlice only ready pod addresses rebuilt on every change Node data plane virtual IP to pod IP reply translated back Selected pods 10.0.1.7 ready in the slice answers on http 10.0.1.8 running readiness false not an endpoint ready IPs A selector that matches nothing is a valid Service with an address and no endpoints. Readiness removes a running pod from the slice. The Service object does not change.

Choose ClusterIP, LoadBalancer or an Explicit Gateway Path

Choose the exposure contract before the resource type. Four questions decide it: who the client is, whether the platform terminates TLS, whether one address must serve several hosts, and who pays for the address. The resource follows the answer; picking a type first and justifying it later is how a cluster ends up with a load balancer per preview branch.

ClusterIP is the default and the right answer for most Services. It is reachable only from inside the cluster, adds no cloud cost, and puts a stable name in front of a changing pod set. Most Services that look like they need external exposure are called by another workload.

NodePort trades an address for a port on every node. It needs no cloud integration, which makes it the fallback on bare metal and the debugging path when a managed load balancer is suspect. The cost is an infrastructure contract: every node must be reachable and the port range open in the firewall.

LoadBalancer asks the cloud for one address per Service. The provider's wait_for_load_balancer setting blocks until at least one ingress point appears, turning a silent pending state into a failed apply. Without a controller and a cloud account the object stays pending, and the Service spec itself reports no error.

Source-IP and CIDR controls belong to the same decision. An external traffic policy of Local preserves the client address but routes only to nodes holding a local endpoint, while source ranges narrow who may connect. Both are enforced by the implementation rather than the manifest, so they belong in the review.

Gateway API is the shared entry point. One Gateway owns the address and the listeners, and HTTPRoutes attach hosts and paths for many Services. The saving is concrete: one cloud load balancer and one certificate for a set of routes instead of one per Service.

Figure 3 turns the contract into a decision path and names the failure that follows a missing requirement.

Choose ClusterIP, LoadBalancer or an Explicit Gateway Path
%%{init: {'theme':'base','themeCSS':'.edgeLabel .label text, .edgeLabel text, .edgeLabel span { fill:#132440 !important; color:#132440 !important; }','themeVariables': {'primaryColor':'#16476A','primaryTextColor':'#ffffff','primaryBorderColor':'#132440','secondaryColor':'#3B9797','secondaryTextColor':'#ffffff','secondaryBorderColor':'#132440','tertiaryColor':'#c4873b','tertiaryTextColor':'#132440','tertiaryBorderColor':'#132440','lineColor':'#5a6e82','textColor':'#132440','titleColor':'#132440','mainBkg':'#16476A','nodeBorder':'#132440','clusterBkg':'#eef3f7','clusterBorder':'#132440','edgeLabelBackground':'#ffffff','actorBkg':'#16476A','actorTextColor':'#ffffff','actorBorder':'#132440','activationBkgColor':'#3B9797','activationBorderColor':'#132440','signalColor':'#5a6e82','signalTextColor':'#132440','noteBkgColor':'#c4873b','noteTextColor':'#132440','noteBorderColor':'#132440','labelBoxBkgColor':'#a8dede','labelBoxBorderColor':'#132440','labelTextColor':'#132440','loopTextColor':'#132440','altBackground':'#f8f9fa','fontFamily':'DM Sans, sans-serif'}}}%%
flowchart TD
    Q["Who must reach the workload?"] -->|"only cluster clients"| CIP["ClusterIP
stable name, no cloud cost"] Q -->|"clients outside"| L{"Need host or
path routing?"} L -->|"no, one TCP service"| I{"Load-balancer
implementation installed?"} I -->|"yes"| LB["LoadBalancer Service
one address per Service"] I -->|"no"| NP["NodePort
node IP plus firewall rule"] L -->|"yes"| A{"Which API does the
controller implement?"} A -->|"Gateway API CRDs"| GW["Gateway and HTTPRoute
shared address and certificate"] A -->|"Ingress"| IN["Ingress with ingressClassName"] A -->|"neither"| X1["No data plane
spec accepted, no route"] NP -.->|"port blocked"| X2["Connection times out
before reaching a pod"] LB -.->|"no controller"| X3["Pending address
apply still succeeds"] IN -.->|"class mismatch"| X4["No address, no rules
default backend answers"] classDef primary fill:#132440,color:#ffffff,stroke:#0b172b; classDef secondary fill:#16476A,color:#ffffff,stroke:#0f314a; classDef ok fill:#3B9797,color:#ffffff,stroke:#2a6f6f; classDef danger fill:#BF092F,color:#ffffff,stroke:#7a0420; classDef warn fill:#c4873b,color:#132440,stroke:#8a5a1f; class Q,L,I,A primary; class CIP,LB,NP,GW,IN secondary; class X1,X2,X3,X4 danger;

The type is not a maturity ladder. A Gateway is not a better LoadBalancer Service; it is a different ownership model with a shared address and one route object per team. Pick the smallest object that satisfies the contract, and record why the next one was not needed.

Ingress and Gateway APIs Require the Right Controllers

An Ingress is a request, not a router. It names hosts, paths, a backend Service and optionally a TLS Secret. A controller watching that class turns the request into proxy configuration. Without one, the API accepts the object and nothing listens.

The class is the ownership link. ingress_class_name points at an IngressClass whose controller field names the implementation. A controller reconciles only its own class, and a default class annotation decides the ambiguous case. An Ingress with no class at all is ignored by every controller that requires one, not rejected.

A missing controller looks like a missing address. The Ingress is created, its status stays empty, and requests either fail to connect or reach a different default backend. Terraform reports success because the object exists; no field records that the route was never programmed.

Gateway API splits the same job across three objects. A GatewayClass names the implementation, a Gateway owns listeners and the address, and an HTTPRoute owns matching and backends. That split is the point: the platform team owns the address, and the application team owns the route.

CRDs must exist before the plan, not just before the apply. Gateway API types are custom resources, so the provider reads their schema during planning. A missing CRD fails the plan with an unknown type, which is a cleaner failure than storing an object no controller understands.

Namespaces are part of the routing contract. A route attaches to a Gateway only when the listener's allowed routes admit its namespace, and a backend in another namespace needs a ReferenceGrant. The same-namespace case works without either, so the omission survives local testing.

Pin the API channel deliberately. Ingress uses the networking group at v1, while Gateway API publishes a standard channel and an experimental channel. Naming the channel in the module turns a cluster upgrade into a reviewed change rather than a plan-time surprise.

Separate Certificate and DNS Controllers from Terraform Ownership

Terraform declares intent; three controllers produce the result. A route references a host and a TLS Secret name, cert-manager issues the certificate, external-dns writes the record, and the cloud controller creates the load balancer. Each has a retry loop and a status, and none of them appears in the Terraform plan.

Ownership is a matrix, not a slogan. Terraform owns the Service, Ingress, Gateway and HTTPRoute. cert-manager owns the Certificate status and the Secret contents. external-dns owns the DNS record. The cloud controller owns the load balancer and any security groups it creates.

Expressed as dependencies, the order becomes a cycle. Certificate issuance often needs the host to resolve, DNS needs the load-balancer address, and the address comes from the route. Making the route depend on the certificate closes the loop, so the certificate must be allowed to converge after the route exists.

An annotation is an API for a controller you do not own. Issuer and hostname annotations are untyped strings, so a typo is accepted and ignored. Keep them in one module, define them once, and verify the derived object rather than the annotation text.

Never let Terraform read the secret value. Reference the Secret by name and let the controller write it. A certificate in state is a private key in state, and the provider has no reason to read it.

Deletion runs in the opposite order and is where resources leak. Removing the route lets external-dns delete the record, but the cloud load balancer can outlive the object when its controller is gone. Audit the address inventory before calling teardown complete.

Figure 4 contrasts the order that converges with the dependency cycle that stalls.

The converging ownership order compared with the dependency cycle that blocks certificate issuance The left panel lists a converging order: Terraform applies the route, the cloud controller assigns an address, external-dns writes the record, and cert-manager issues the certificate. The right panel shows the same four steps as a cycle, where a route that waits for the certificate prevents the address, DNS record and certificate from ever being created. Two notes explain that the certificate must converge after the route and that each derived resource has its own owner. One route, four writers Order that converges 1 Terraform applies the route Service, Ingress or Gateway 2 Cloud controller assigns an address status ingress point or Gateway address 3 external-dns writes the record host points at the assigned address 4 cert-manager issues the certificate Secret becomes Ready for the listener Order that stalls route applied needs a Ready cert address assigned needs the route DNS record needs the address certificate needs the host to resolve waits for Every edge is a dependency someone added. The certificate must converge after the route exists, not before Terraform creates it. Terraform owns the objects. Controllers own the addresses, records and secrets derived from them.

Verify Routes and Track Cloud-Created Resources

Acceptance needs a request from outside the cluster. A zero exit, a populated status address and a Ready certificate prove three controllers converged. They do not prove the host resolves to that address or that the route returns the expected body.

Test one allowed path and one denied path. The allowed request uses the real host name and a path present in the rules, and should return the application's status. The denied request uses a host or path with no rule and should return the controller's not-found response, which proves the default backend handles the miss.

A cloud health check is a fourth gate. The load balancer probes a node port or a dedicated health endpoint, and when every target fails the request never reaches the controller. The client sees the provider's error page, which looks like an application outage but is a target-group decision. Read that probe beside the readiness probe, because the two answer different questions.

Read the status to identify the layer. A miss on every rule usually returns the controller's 404, a matched route with no ready endpoints returns 503, and a TLS name mismatch fails before any HTTP response. The status narrows the search before a log is opened.

Teardown is an inventory exercise. List Services of type LoadBalancer with their addresses, then routes with theirs, then the DNS records and certificates derived from them. Anything that survives destroy is controller-owned and needs an owner.

The lab below exercises both paths and checks the address inventory before and after destroy.

Lab

Verify Routes and Track Cloud-Created Resources

Execution status: design-only. The procedure is written for Terraform CLI 1.17.1 with hashicorp/kubernetes 3.5.0 against a disposable namespace, but this article does not claim the commands were run. Nothing here creates billable infrastructure beyond the cluster and load balancer you already have.

Prerequisites. A reachable cluster with a load-balancer implementation and an ingress or Gateway controller, a hostname you may point at it, and permission to create Services and routes. If no DNS controller is installed, create the record by hand and note that it is now outside Terraform.

  1. Apply the Service and the route with the provider's load-balancer wait enabled. Expect a zero exit and a status carrying one address or hostname.
  2. Resolve the host from outside the cluster and record the answer. Expect the load-balancer address, not a node address.
  3. Request the allowed path with the real host name. Expect the application's status and a body marker that identifies the running release.
  4. Request a host or path with no rule. Expect the controller's 404 from its default backend rather than a connection reset.
  5. Point a matched rule at a Service with no ready endpoints. Expect 503, which separates routing from endpoint health.
  6. Inspect the certificate and the DNS record, then destroy. Compare the address inventory before and after, and expect the load balancer to disappear with the route.
EvidenceWhere it comes fromWhat it provesWhat it cannot prove
Apply exit statusTerraformobjects accepted, address waited forthat DNS or TLS is correct
Resolved addresspublic DNSthe name reaches the load balancerthat the certificate matches the host
Allowed responseone real requestroute and backend servethat other hosts are denied
Denied responseone real requestthe default backend handles the missthat no other path leaks
Post-destroy inventorycluster and cloudderived resources are gonethat resolver caches have expired

Expected observation. The allowed request returns the application's marker, the unrouted host returns the controller's 404, and the empty backend returns 503. After destroy the address inventory is empty; a surviving load balancer means its controller was removed before the route, not that Terraform missed an object.

Next in the Series

In ConfigMaps and Secrets: Configuration Delivery Without False Security Claims, we'll explore Separate application configuration, secret values and Terraform artifact exposure.