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

Storefront Traffic Layer

mediumService CompositionTraffic Layer Auditing—No data yet

Problem statement

Namespace storefront runs the shop and nothing can reach it properly. storefront-web is two replicas that bind their listen port instantly and then serve a cold-start page for ten seconds; the port number comes off the container's own command line and the port it declares carries no name. storefront-batch is somebody else's tier, sharing the app label and answering nothing. There is no Service of any kind: the other services inside the cluster hold pod IPs that expire on every deploy, and from outside the cluster there is no way in at all. Build the traffic layer, and then produce the scan that proves what it is doing.

About this exercise

This brings together the topic's three exercises:

  • ClusterIP - one virtual address for a tier, computed from labels and outliving every pod behind it (ClusterIP Basics)
  • Endpoints and readiness gating - the published backend set is the selected pods that can serve, and it is rewritten as that changes (Endpoints & Readiness Gating)
  • NodePort and named ports - a door on every node, and a Service written against a port name so the application can move (NodePort & Named Ports)

One idea ties them together: every part of a Service is late-bound. The address is allocated once and never moves; membership is recomputed continuously from the selector and the Ready condition; the target port is resolved per pod, at endpoint time, against whatever that pod declares. Nothing in a Service is a copy of a fact about a pod, which is why the pods can all be replaced underneath it without anything that talks to the Service noticing.

Each layer fails differently when it is wrong, and all three failures are quiet. A selector one label too loose puts a tier that answers nothing into rotation. A pod that is running but not warm is a backend the moment it starts, so a deploy routes users onto a cold cache. A numeric targetPort goes on resolving to a number nothing is listening on after the application moves, and the Service still has endpoints - they just do not answer.

A readiness probe is what gives the application the vote: its result drives the container's ready flag and nothing else, so a failing check withdraws the address and leaves the process running, its restart count untouched, whatever it warmed still warm. kubectl explain service.spec.ports, kubectl explain deployment.spec.template.spec.containers.ports and kubectl explain deployment.spec.template.spec.containers.readinessProbe list every field of the three.

Your shell here sits on the cluster's own network. A cluster IP answers from it exactly as it would from a pod, and a node port answers at the node's own address - the address the cluster reports for the node - 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, so from here a Service is reached at its address. A Service with no ready endpoints queues nothing - the connection is refused at once, which is the correct behaviour and looks nothing like a cold-start page.

How this project is graded

The grader reads the Deployment and both Services back off the live cluster and compares every field against the specification below, sends real requests to the cluster IP and to the node address on the node port, and compares the published endpoints against the pods the cluster reports as ready. Then it runs two drills. In storefront it creates a pod of its own carrying the web tier's labels, checks that it joins and leaves the endpoint list while the cluster IP holds its value and goes on serving, and deletes it again. In a scratch namespace of its own it rebuilds the same tier carrying your probe, your port name and copies of your Services, and watches it from the first second through a whole warm-up, upstream outage and recovery - sampling the endpoints and what the address answers throughout - and then rolls that tier onto a completely different container port behind the same name, without editing either Service, and requires both of them to keep serving every request across the roll. Finally it runs your audit twice: once against storefront, where every field must match what the cluster reports at that moment, and once against a second scratch namespace it builds holding a different tier. Both scratch namespaces are removed afterwards, and nothing in storefront is created, patched or deleted by the grader.

What you will build

storefront-web keeps the container, image and the shape of its command it has now - deployment.yaml holds it as it runs today - and gains a named port and one check:

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

readinessProbe HTTP GET on path /ready, at the port named http, initial delay 2s, period 2s, timeout 1s, failure threshold 2 and no liveness probe and no startup probe

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

name          storefront-internal         name          storefront-edge
namespace     storefront                  namespace     storefront
type          ClusterIP                   type          NodePort
selector      app: storefront             selector      app: storefront
              tier: web                                 tier: web
              (both, and nothing else)                  (both, and nothing else)
port          80                          port          80
targetPort    http                        targetPort    http
                                          nodePort      30090

And traffic-audit.sh, a read-only scan that takes an optional namespace argument and defaults to storefront:

traffic-audit.sh [namespace]

It prints one line per Service in that namespace, sorted by Service name, and then one count:

TRAFFIC <service> <type> <port>:<targetPort> <nodePort> <endpoints>
READY <n>

type the Service type port the port the Service answers on targetPort the target port as the Service spells it - a name stays a name nodePort the node port the Service holds, or a single - when it holds none endpoints the addresses the Service currently publishes, sorted and comma-separated with no spaces, or a single - when it publishes none n how many pods in the namespace the cluster currently reports as ready

So a namespace whose only Service selects nothing audits as one - in the endpoints field.

The tests

  • ClusterIP_internal_address_holds_and_serves_through_churn - one address for the tier, unchanged and still serving while pods come and go
  • Endpoints_track_exactly_the_ready_web_pods_through_every_flap - empty, both, empty, both - and never an address that is not a selected web pod
  • NodePort_serves_at_the_node_address_within_the_valid_range - the storefront answers at the node address on a port inside the cluster's range, for every request across the drill
  • NamedPorts_renumber_behind_the_name_serves_without_interruption - the container moves to another port, the endpoints follow it, the Services are not touched and nothing stops answering
  • Readiness_every_answer_came_from_a_ready_backend - no cold-start page is ever served, refusals happen instead, and the audit agrees about who was in rotation
  • Composition_service_and_port_specs_read_back_as_written - every field of the probe, the named port and both Services
  • Composition_audit_reports_the_live_traffic_layer - the audit matches the live cluster line for line
  • Composition_audit_run_against_a_namespace_it_has_never_seen - the same scan, on a namespace the grader builds, reports what is really there

When every test passes

You have:

  • One internal address for the storefront that outlives every pod behind it, with the batch tier correctly excluded
  • A backend set that is the instances able to serve, so a deploy makes traffic wait instead of landing on a cold cache
  • A way in from outside the cluster, on a node port you chose out of the range the cluster accepts
  • Both Services written against a port name, so moving the application's port is a one-line change and not a silent outage
  • One command that reports the whole traffic layer of a namespace, and works on a namespace it has never seen
  • The next topic unlocked

Verify

Click Test.

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 yetmediumNodePort & Named PortsOpen a storefront to the outside with NodePort Services - one pinned, one allocated, one rejected out of range - and decouple the Service from the container port with a named target port that survives a renumber.—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.