straits-1w3w6tc has market-web (3 replicas), market-api (1 replica) and a client Pod. One objective adds something; two fix something.
Part one: a headless Service
A headless Service has clusterIP: None. It allocates no virtual IP and does no forwarding. DNS for its name returns the Pod IPs themselves, and its Endpoints object lists them. Use one when a client needs to address each Pod directly.
normal market-web ──▶ 10.96.x.x ──▶ one of the Pods
headless web-headless ──▶ 10.42.0.8, 10.42.0.9, 10.42.0.10kubectl apply -n straits-1w3w6tc -f - <<EOF
apiVersion: v1
kind: Service
metadata:
name: web-headless
spec:
clusterIP: None
selector:
app: market-web
ports:
- port: 80
EOFCount the addresses it lists:
kubectl describe svc web-headless -n straits-1w3w6tc
kubectl get pods -n straits-1w3w6tc -l app=market-web -o jsonpath='{.items[*].status.podIP}'The Endpoints: line and the Pod IPs are the same list.
Part two: the port that answers nothing
kubectl exec client -n straits-1w3w6tc -- wget -qO- market-api:8080/healthz
kubectl describe svc market-api -n straits-1w3w6tcConnection refused, and the Endpoints are not empty. Read Port: and TargetPort:. The container listens on 8080; the Service forwards to a different port. Apply a corrected Service with targetPort: 8080:
kubectl apply -n straits-1w3w6tc -f - <<EOF
apiVersion: v1
kind: Service
metadata:
name: market-api
spec:
selector:
app: market-api
ports:
- port: 8080
targetPort: 8080
EOFCheck with nc:
kubectl exec client -n straits-1w3w6tc -- nc -zv market-api 8080