✦ For everyone, free.

Practical knowledge for real and everyday life

Home

Kubernetes Services and Discovery Scope

Kubernetes Services and Discovery Scope enables consistent service discovery across clusters through DNS and network policies, ensuring reliable container communication.

Kubernetes Services and Discovery Scope refers to the boundaries within which a Service object is visible, resolvable, and reachable, spanning the axes of namespace, cluster, and external network exposure. A Service's scope determines who can discover it by name, who can route traffic to it, and whether that traffic originates from within the same namespace, from elsewhere in the cluster, or from outside the cluster entirely, and choosing the correct scope for a given Service is a deliberate design decision rather than a default to be accepted unexamined.


The Service Abstraction and Why Scope Matters

Services as Stable Discovery Targets

A Service provides a stable virtual IP and DNS name in front of a shifting set of pod endpoints, so that consumers do not need to track individual pod IPs, which change on every rescheduling event. Discovery scope determines which consumers are even able to resolve and reach that stable name in the first place.

Scope as a Deliberate Exposure Decision

Every Service type implicitly makes an exposure decision: how far outward from the originating namespace its discoverability extends. Choosing the narrowest scope that satisfies a real consumer need is the general governing principle, since broader scope increases the attack surface and discovery noise without a corresponding benefit.


Namespace-Scoped Discovery

Short DNS Names Within a Namespace

Within the same namespace, a Service can be resolved using only its short name, without any namespace qualifier, because DNS search paths configured on the pod automatically append the local namespace and cluster domain.

curl http://ledger-api

This resolves identically to the fully qualified form when issued from a pod inside the same namespace as the ledger-api Service.


Cluster-Scoped Discovery

Fully Qualified Domain Names Across Namespaces

To reach a Service in a different namespace, a consumer must use the fully qualified domain name, which encodes the namespace explicitly, since the short name alone would resolve against the caller's own namespace rather than the target's.

curl http://ledger-api.prod-payments-ledger.svc.cluster.local

ClusterIP as the Default Cluster-Internal Scope

The default Service type, ClusterIP, allocates a virtual IP reachable only from within the cluster's pod network, making it the natural choice for internal service-to-service communication that should never be reachable directly from outside the cluster.

apiVersion: v1
kind: Service
metadata:
  name: ledger-api
  namespace: prod-payments-ledger
spec:
  type: ClusterIP
  selector:
    app: ledger-api
  ports:
    - port: 80
      targetPort: 8080

Headless Services for Direct Pod Discovery

Setting clusterIP: None produces a headless Service, which skips virtual-IP load balancing entirely and instead returns the individual pod IPs directly through DNS. This scope is used when a consumer needs to discover and address each backing pod individually, such as in a StatefulSet-backed database cluster where clients must connect to a specific member rather than an arbitrary one.

apiVersion: v1
kind: Service
metadata:
  name: ledger-db
  namespace: prod-payments-ledger
spec:
  clusterIP: None
  selector:
    app: ledger-db
  ports:
    - port: 5432

Externally-Scoped Discovery

NodePort Exposure

A NodePort Service extends discovery scope beyond the cluster network by opening a fixed port on every cluster node, making the Service reachable from outside using any node's IP address. This scope is coarse and rarely used directly in production, since it ties external reachability to node addressing rather than a stable external endpoint.

apiVersion: v1
kind: Service
metadata:
  name: ledger-api-external
spec:
  type: NodePort
  selector:
    app: ledger-api
  ports:
    - port: 80
      targetPort: 8080
      nodePort: 30080

LoadBalancer Exposure

A LoadBalancer Service requests a cloud provider-managed external load balancer with a stable external IP or hostname, which is the standard scope for Services intended to be reachable directly from the public internet or a corporate network outside the cluster.

Ingress as a Layer Above Service Scope

An Ingress resource does not itself change a Service's discovery scope but adds a routing layer in front of one or more ClusterIP Services, allowing many internally-scoped Services to share a single external entry point differentiated by hostname or path, which is generally preferred over granting each Service its own LoadBalancer scope.


Cross-Namespace Discovery Control

DNS Resolution Does Not Imply Authorization

DNS-based discovery across namespaces is unauthenticated by default: any pod that can resolve a fully qualified Service name can also attempt to connect to it, unless a NetworkPolicy restricts that reachability. Discovery scope and access control are therefore separate concerns, and a Service being technically discoverable across namespaces does not mean it should be reachable without an explicit policy permitting it.

ExternalName Services

An ExternalName Service maps a name inside the cluster's DNS scope to an external DNS name outside the cluster, allowing internal consumers to discover external dependencies through the same naming convention used for internal Services, without introducing an actual proxy or virtual IP.

apiVersion: v1
kind: Service
metadata:
  name: external-payments-gateway
  namespace: prod-payments-ledger
spec:
  type: ExternalName
  externalName: payments.partner-provider.com
External (LoadBalancer) Cluster (ClusterIP FQDN) Namespace