StaffCoder
Kubernetes
15 modules · 60 challengesSign up
Module 12 · Services (ClusterIP & NodePort)

NodePort & Named Ports

mediumNodePort ServicesNamed Container Ports—No data yet

Problem statement

Namespace edge-access runs storefront-web, two replicas serving on the port the app was told to bind. Nothing outside the cluster can reach it: there is no Service at all, and even a cluster IP would only be routable from inside. Inside is not much better - the container port is a bare number, so the day somebody moves the app to a different port, every object that wrote that number down goes on pointing at a port nothing listens on, and the only symptom is that requests stop being answered.

About this exercise

A ClusterIP Service is reachable on the cluster network and nowhere else. A NodePort Service is the next step out: it keeps the cluster IP and additionally reserves the same port on every node, so anything that can reach a node address can reach the Service through it.

The reserved port comes out of a fixed range - the cluster's node port range, which this cluster reports in the error you get when you ask for something outside it. You can name a port in that range yourself, or leave the field out and let the cluster allocate one. Pinning a value is what you do when something outside - a firewall rule, a load balancer, a colleague's bookmark - needs to know the number in advance; letting the cluster allocate is what you do when it does not, and it is the safer default because two Services cannot hold the same node port.

The other half of this exercise is the port name. A container port may carry a name, and a Service's targetPort may be that name instead of a number. The resolution happens per pod, at endpoint time: each selected pod is asked what number it has under that name. So a Service written against the name keeps working when the application is moved to a different port - the pods change, the Service does not. A Service written against the number keeps resolving to a port nothing is listening on, and fails silently: it still has endpoints, they just do not answer.

kubectl explain service.spec.ports and kubectl explain deployment.spec.template.spec.containers.ports list the fields.

Your shell here sits on the cluster's own network. A node's address is the address the cluster itself reports for the node, and a node port answers there from this shell exactly as it would from outside the cluster. Cluster DNS is the one thing that does not reach this far - a svc.cluster.local name resolves inside a pod, not in this shell.

How this project is graded

The grader reads both Services and the Deployment back off the live cluster and compares every field against the specification below, then sends real requests to the node address on each node port and checks what answers. It confirms the port name is what the endpoints resolve against, not the number, by creating a pod of its own in edge-access that carries the storefront labels and publishes the same name on a completely different number, and reading which port the Service publishes for it; the pod is deleted afterwards. It also asks the API for an out-of-range node port on a throwaway Service of its own to confirm the rejection you recorded is the cluster's own answer. Nothing you created is ever modified.

What you will build

storefront-web in edge-access keeps its container, image and the shape of its command - deployment.yaml holds it as it runs today - and moves to a named, renumbered port:

container port    9090, named http
                  the app takes its listen port from its own command line,
                  so the process has to move with the declaration

Two Services, both in services.yaml, applied to the cluster:

name          storefront-web              name          storefront-preview
namespace     edge-access                 namespace     edge-access
type          NodePort                    type          NodePort
selector      app: storefront             selector      app: storefront
port          80                          port          80
targetPort    http                        targetPort    http
nodePort      30080                       nodePort      left to the cluster

And one recorded value:

answers/rejected-nodeport.txt   what the API answers when it is asked for node
                                port 80 - the rejection exactly as printed

Step 1 - a name in front of the number

Give the container port the name http and move the app to 9090, declaration and process together.

named_container_port_declared_on_the_web_pods should pass

Step 2 - a door on every node

Create storefront-web on the node port the spec pins, so the storefront answers at the node address on 30080.

node_port_service_matches_the_required_spec and explicit_node_port_serves_at_the_node_address should pass

Step 3 - the range, and the one you do not pick

Create storefront-preview with no node port of its own and see what the cluster hands it. Then ask for node port 80 on a Service and record the refusal in answers/rejected-nodeport.txt - the request is rejected, so nothing changes.

allocated_node_port_lands_in_the_valid_range and out_of_range_node_port_is_rejected should pass

Step 4 - renumber without breaking

Both Services must reach the pods through the name, not through 9090. That is what makes the move from 8080 survivable, and what makes the next one survivable too.

named_target_port_survives_the_container_port_renumber and the_port_name_resolves_per_pod_not_per_service should pass

When every test passes

You have:

  • A storefront reachable from outside the cluster, at a node address on a port you chose
  • A second way in on a port the cluster allocated, and the reason to prefer that
  • The cluster's own answer about what a node port may be
  • Services written against a port name, so moving the application's port is a one-line change in one place instead of a silent outage

You do not have yet:

  • All of it working together. Stable internal addresses, external reach, only-ready backends and a port move with nothing dropped is the next exercise.

Verify

Click Test after every step.

More in Services (ClusterIP & NodePort)

See the module
ClusterIP BasicsGive a churning web tier one stable virtual address with a ClusterIP Service - selector, port and targetPort - and prove the endpoints track the ready pods while the address does not move.—No data yetmediumEndpoints & Readiness GatingGate Service membership on readiness so endpoints hold only servable pods - empty through the warm-up, withdrawn on a mid-life failure without a restart, republished on recovery.—No data yetmediumStorefront Traffic LayerBuild a storefront's whole traffic layer - a stable internal address, endpoints gated on readiness, a node port for the outside world and both Services written against a named port that survives a renumber - and audit the layer of any namespace.—No data yetmedium

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.