Kubernetes · Chapter 8 of 28
Reaching Services from outside
By the end of this chapter you can reach an app in the cluster from your own terminal with kubectl port-forward, open it on every node with a NodePort Service, give it an address of its own with a LoadBalancer Service, and say what ExternalName, a Service without a selector and a headless Service are for. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Running apps
- 4 tasks
- Kubernetes
- In the all labs plan
The first 2 chapters of Kubernetes are free. This one comes with the all labs plan.
Why you need this
Jhatpat's services now run in the cluster, and in chapter 7 the storefront found the catalog by its Service name. But Jhatpat's people are not inside the cluster. Bhola's pickers in 14 dark stores open the storefront on their tablets to check what a customer sees; the tablets sit on the same private network as the nodes. 1,900 riders send their position to the rider app from their phones, over the internet, every 10 seconds. And Tushar wants to open one storefront pod's page from his laptop while he looks into a bug.
The Services you know so far are ClusterIP Services: an address that exists only inside the cluster. From your terminal, which is outside like Bhola's tablet, neither the name nor the address works:
$ lab curl http://catalog/products
curl: (6) Could not resolve host: catalog
lab: outside the cluster, only Ingress hosts resolve; catalog is not a host in any Ingress (Service names work only inside the cluster: kubectl exec into a pod, or kubectl port-forward)
$ lab curl http://10.96.119.124/products
curl: (28) Failed to connect to 10.96.119.124 port 80 after 5002 ms: Timeout was reached
lab: 10.96.119.124 is a Service ClusterIP: it is reachable only from inside the cluster (use kubectl exec, kubectl port-forward, a NodePort or an Ingress)
lab curl sends a request from outside the cluster, the way a customer's phone would. The name is unknown outside (cluster DNS answers only pods), and nothing outside knows how to route to 10.96.x.x. Kubernetes has a few ways to open a door, each for a different visitor.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
page.txt: the storefront's page through port-forward
Your terminal is outside the cluster, so open a tunnel to the storefront with port-forward.
1. Run kubectl port-forward to the storefront Deployment, or to one of its pods. Use a local port other than 8080.
2. Run it in the background with &, or in a second terminal tab. It keeps running.
3. While it runs, run curl on localhost at your local port, with the path /.
4. Add what curl prints to page.txt, below the notes. Then stop the port-forward.
To pass: a curl through port-forward reached a storefront pod and got status 200, and page.txt has that page below the notes. Port 8080 does not work on your side, because the lab uses it.
- Task 2
storefront-nodeport.yaml: a NodePort for the storefront
Open the storefront on one fixed port of every node.
1. Open storefront-nodeport.yaml with nano. Set the Service type to NodePort.
2. On its port (80, going to the pods' 8080), set the node port to 30500. Save the file.
3. Apply the file with kubectl apply -f.
4. Run lab curl on a node's IP at port 30500. kubectl get nodes -o wide shows the IPs.
To pass: the Service storefront in ch08 is a NodePort, sends port 80 to 8080, and has node port 30500. The Check sends a request to two nodes' IPs on port 30500, and both must answer 200.
- Task 3
rider-lb.yaml: a LoadBalancer for the rider app
Give the rider app its own outside address with a LoadBalancer Service.
1. Open rider-lb.yaml with nano. Set the type to LoadBalancer.
2. Add a port 80 that goes to the pods' port 8080. Keep the selector app=rider-tracker, and add no other label to it.
3. Apply the file. List the Services until rider-tracker shows an EXTERNAL-IP.
4. Run lab curl on that address with the path /riders. The path / answers 404.
To pass: the Service rider-tracker in ch08 is a LoadBalancer, selects only app=rider-tracker, sends port 80 to 8080, and has an address. You ran lab curl on that address and got status 200.
- Task 4 · interview question
Interview: NodePort, LoadBalancer or Ingress
Interview question: why would you put an Ingress in front of Jhatpat's customer traffic?
1. Read the notes in answer.txt. Customers use one name and HTTPS, and there are several Services behind it.
2. Say which one you would use: a NodePort, a LoadBalancer for each Service, or an Ingress.
3. Say why. Think about addresses, host names, paths, HTTPS and cost.
4. Write one or two sentences of your own below the notes in answer.txt.
To pass: answer.txt names the Ingress, and says what it gives that a NodePort or a LoadBalancer for each Service does not.
What you will learn
- kubectl port-forward: a private tunnel from your machine to one pod, through the API server.
- NodePort: a Service that opens the same port on every node.
- LoadBalancer: a Service that gets its own address from the cluster's load balancer provider.
- ExternalName: a Service name that points at a host outside the cluster.
- A Service without a selector: you write its EndpointSlice yourself.
- Headless Services: DNS that answers with the pods' own IPs (chapter 22 needs them).
More in Running apps
Build "Reaching Services from outside" yourself
Jhatpat delivers groceries in ten minutes from 14 dark stores across Lucknow, and its hand-run servers fall over on festival evenings. You move it onto Kubernetes. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Kubernetes are free. This one comes with the all labs plan.