Service types and ports
Why a Service exists
A Pod can disappear during a rollout and its replacement can get a different IP address. A Service gives clients a consistent way to find an application without tracking every Pod. Imagine a bookstore frontend calling its catalog API: it should keep using the same address when the API Pods change. A normal ClusterIP Service supplies a virtual IP within the cluster; the backend set can change independently. A Service is not a process that starts the application.
The frontend connects to Service port 80. The Service networking implementation directs traffic to an eligible backend on port 8080. The backend must actually listen there. No connection is made in this offline example.
Write a ClusterIP Service
The Service below is a static manifest for the existing fictional book-lab namespace. The selector targets Pods labeled app: api. The port is the client-facing number; targetPort is the destination number. The protocol is TCP. A name such as http helps distinguish ports when a Service defines several of them.
apiVersion: v1kind: Servicemetadata: name: api namespace: book-labspec: type: ClusterIP selector: app: api ports: - name: http protocol: TCP port: 80 targetPort: 8080A permitted API submission could allocate a cluster IP. Saving the YAML has no effect on a cluster. A numeric targetPort is independent of the containerPort declaration; containerPort is descriptive metadata, not a way to start a listener or publish a host port. If omitted, targetPort defaults to port. A named targetPort instead resolves against the named container port on each selected Pod.
Choose the exposure type
ClusterIP is the default choice for internal communication. NodePort additionally exposes a port on node addresses subject to network configuration and firewalls; it is not automatically internet-accessible. LoadBalancer asks an installed implementation to supply a load balancer; a local cluster might not have one, and cloud implementations can incur costs. It often uses NodePorts, but some implementations support avoiding them. ExternalName is a DNS alias rather than a traffic-forwarding service. Headless discovery is discussed in the third lesson; it is not a fifth Service type.
Example NodePort access: node:30080 -> Service port 80 -> Pod port 808030080 is illustrative, within the usual default NodePort range; actual allocation and range are cluster-specific. Do not create a NodePort or cloud load balancer simply to test an internal API. The arrows are a conceptual mapping, not a claim about a particular packet forwarding implementation.
Troubleshoot port mismatches
A Service may have a valid address but still fail because targetPort is incorrect, the application binds only to localhost, the backend is not ready or a network policy blocks traffic. Check the intended listener before changing the Service. A DNS response or a declared port is not a health check.
Practice: choose a Service
Open lab-fixtures/services/port-cases.json. For each fictional scenario explain the exposure type, client port and destination port; identify which option requires an extra implementation and which is only a DNS alias. Use the internal API manifest to identify the selector and port mapping. Do not apply any files. Next: find which Pods the selector actually identifies.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Service types and ports — knowledge check
Apply the lesson concepts to original offline scenarios.
A frontend calls an internal API whose Pods may be replaced. Which default Service type fits?
The Service port is 80 and targetPort is 8080. Which port does the backend listen on for this mapping?
A local cluster has no load balancer implementation. What can you conclude from a LoadBalancer manifest?
Which field describes a container port but does not publish a host port or start a listener?
Which type supplies a DNS alias rather than a proxy?