Sub topic 1
Recap: Services as the targets of Ingress
Prerequisites
This chapter assumes you can already do the following:
- Run Pods and Deployments in Kubernetes.
- Write and apply YAML manifests with
kubectl. - Read basic HTTP terms such as hosts, paths, and status codes.
- Recognize TLS terms such as certificates and private keys. They matter in later chapters.
Why Services come first
An Ingress never sends traffic to a Pod directly. Every rule in an Ingress points at a Service. The controller that implements the Ingress then reads that Service and its endpoints to find the Pods.
Pods are replaced often and get new IP addresses each time. A Service gives them one stable name and a virtual IP. If the Service is wrong, the Ingress in front of it cannot work, however correct the Ingress is.
The working model
A Service uses a label selector to pick Pods. Kubernetes keeps a list of the ready Pod addresses for that Service. These are called endpoints. A Service has two port fields that people often mix up:
portis the port the Service listens on inside the cluster.targetPortis the port the container listens on.
The default Service type is ClusterIP. It is reachable only from inside the cluster, which is what Ingress needs. The Ingress controller runs inside the cluster, so it can reach a ClusterIP Service. Only the controller needs an outside address.
A Service to use as a backend
This Service is named web. It accepts traffic on port 80 and forwards it to port 8080 on Pods labeled app: web.
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: web
ports:
- port: 80
targetPort: 8080In an Ingress, you will refer to this Service by name (web) and by its port (80). You do not use targetPort there. The Service translates 80 to 8080.
How a request reaches the Pod
The diagram below shows where the Service sits. The Ingress layer is covered in later chapters. For now, note that the last hop always goes through the Service.
Common errors to rule out early
Check these before you suspect the Ingress:
| Symptom | Likely cause |
|---|---|
| Service has no endpoints | The selector does not match the Pod labels, or no Pod is ready. |
| Connection refused through the Service | `targetPort` does not match the port the container listens on. |
| Ingress cannot find the backend | The Ingress names a different Service, or a different port, than the Service defines. |
| Backend not found | The Service is in another namespace. An Ingress can only refer to Services in its own namespace. |
Checking a Service
Run these commands to confirm that a Service has ready Pods behind it:
kubectl get service web
kubectl get endpoints webIf the ENDPOINTS column is empty, fix the selector or the Pod readiness first. An Ingress pointing at a Service with no endpoints will usually return a 503 error.
Exercise
A Service has port: 80 and targetPort: 8080. In an Ingress rule, which port number do you write for this Service, and why?
Show answer
Write 80. The Ingress refers to the Service's own port. The Service then forwards traffic to the container's targetPort, 8080.
Exercise
kubectl get endpoints web shows <none> in the ENDPOINTS column. Name two possible causes.
Show answer
The Service selector does not match the labels on the Pods, or the matching Pods are not ready (for example, a failing readiness probe). Either one leaves the Service with no addresses to send traffic to.