Sub topic 3
Probe mechanisms: httpGet, tcpSocket, exec, and gRPC
This chapter assumes you can write Pod and Deployment manifests, read YAML, and use kubectl apply and kubectl describe. It also assumes you know basic HTTP and TCP. The chapter covers how a probe checks a container. Later chapters cover what each probe type is for.
One check, four ways to run it
A probe is a check that the kubelet runs against a container. The kubelet is the agent on each node that manages Pods. Every probe has exactly one mechanism, which says how the check is performed. The mechanism is separate from the probe's role (liveness, readiness, or startup). You can use any mechanism in any role.
| Mechanism | Field | Succeeds when |
|---|---|---|
| HTTP GET | `httpGet` | The response status is 200 to 399 |
| TCP socket | `tcpSocket` | The kubelet opens a TCP connection to the port |
| Exec | `exec` | The command exits with status 0 |
| gRPC | `grpc` | The health service reports `SERVING` |
A probe must set one mechanism. Timing fields such as periodSeconds sit next to the mechanism and apply to all four. They are covered in the next chapter.
httpGet
An httpGet probe sends an HTTP GET request to the container's IP address. Any status from 200 to 399 counts as success. Anything else, or no response before the timeout, counts as a failure.
readinessProbe:
httpGet:
path: /ready
port: 8080
periodSeconds: 5You can also set scheme (HTTP or HTTPS), host, and httpHeaders. With HTTPS, the kubelet does not verify the certificate, so self-signed certificates work. Redirects matter: a 3xx response counts as success, so a redirect to a login page can hide a broken application.
tcpSocket
A tcpSocket probe only tries to open a connection. If the connection opens, the probe succeeds. It sends no data and reads no reply.
readinessProbe:
tcpSocket:
port: 5432
periodSeconds: 10Use it for servers that do not speak HTTP, such as databases. It is a weak signal. A process can accept connections while its application logic is deadlocked.
exec
An exec probe runs a command inside the container. Exit status 0 means success. Any other status means failure.
livenessProbe:
exec:
command:
- cat
- /tmp/healthy
periodSeconds: 5The command is not run in a shell. To use pipes or variables, call a shell yourself, for example ["sh", "-c", "..."]. The command must exist in the container image, which can be a problem for minimal images. Each run starts a new process, so frequent exec probes cost more than HTTP or TCP checks.
gRPC
A grpc probe calls the standard gRPC Health Checking Protocol on the given port.
readinessProbe:
grpc:
port: 9090
periodSeconds: 5Your server must implement that protocol. Without it, the probe fails. You can add a service field to check a named service rather than the whole server.
Choosing a mechanism
A quick check
Exercise
A container runs PostgreSQL and exposes no HTTP endpoint. Which mechanism is the simplest readiness probe, and what is its main weakness?
Show answer
Use tcpSocket on port 5432. It succeeds whenever the port accepts a connection, so it cannot detect a server that accepts connections but cannot serve queries. An exec probe running pg_isready gives a stronger signal.
Exercise
Why does exec: command: ["echo $HOME | grep root"] fail to work as intended?
Show answer
Exec probes do not run the command in a shell. The whole string is treated as a single program name, so it cannot be found. Use ["sh", "-c", "echo $HOME | grep root"] instead.