Sub topic 11
Exposing an app: Services, port-forward and port mappings
This chapter assumes you have a running kind or minikube cluster, a working kubectl context, and the web Deployment from the previous chapters. You should be able to read YAML and run kubectl get.
Why pods are not enough
Pods get new IP addresses whenever they are replaced, and those addresses are reachable only inside the cluster. A Service gives a set of pods one stable name and virtual IP. It finds its pods by matching a label selector against pod labels.
A Deployment and a NodePort Service
The manifest below runs two nginx pods labeled app: web. The Service selects that label and opens port 30080 on every node.
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 2
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.27
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: web
spec:
type: NodePort
selector:
app: web
ports:
- port: 80
targetPort: 80
nodePort: 30080Three port fields are easy to confuse:
| Field | Meaning |
|---|---|
| `port` | The port the Service listens on inside the cluster |
| `targetPort` | The port on the pod that receives the traffic |
| `nodePort` | The port opened on each node, in the range 30000-32767 |
Apply and test with port-forward
kubectl apply -f web.yaml
kubectl rollout status deployment/web
kubectl get pods -o wide
kubectl port-forward service/web 8080:80port-forward tunnels a local port to the cluster through the API server. It works on any cluster, with no extra configuration. While it runs, open http://localhost:8080 from a second terminal. It ends when you press Ctrl+C. Note that it attaches to one pod behind the Service, so it does not balance load.
Reaching a NodePort
A NodePort is open on the node, but the node is not your machine's network. How you reach it depends on the tool.
- kind: nodes are Docker containers, so the port is not published to your host by default. You must declare it when you create the cluster.
- minikube: run
minikube service web -p dev --urlto get a reachable address. With the Docker driver on macOS and Windows, this command opens a tunnel, so keep it running.
kind port mappings
The kind configuration below maps host port 8080 to NodePort 30080 on the control-plane node container.
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
extraPortMappings:
- containerPort: 30080
hostPort: 8080
protocol: TCP
- role: workerkind create cluster --name dev --config kind-config.yamlAfter you apply web.yaml, http://localhost:8080 reaches the Service. kube-proxy opens the NodePort on every node, so the control-plane mapping works even if the pods run on the worker.
Common errors
- Empty endpoints: if the Service responds with nothing, run
kubectl get endpoints web. An empty list means the selector does not match the pod labels. - Port already in use:
port-forwardfails if local port 8080 is taken. Pick another local port, such as8081:80. - Connection refused on 8080 with kind: the cluster was probably created without the config file.
Exercise
Your kind cluster was created without extraPortMappings, and the Service is type NodePort. How can you still open the app at http://localhost:8080 without recreating the cluster?
Show answer
Run kubectl port-forward service/web 8080:80. It tunnels local port 8080 to the Service through the API server and needs no cluster-level configuration. Keep the command running while you browse.