To define liveness and readiness probes for a container in a Kubernetes pod, you need to include the probe configurations in the pod's YAML definition under the spec section. Here's how to define them:
-
Liveness Probe:
To define a liveness probe, you need to specify the livenessProbe field for the container. The liveness probe determines if the container is still running as expected. If the liveness probe fails, Kubernetes restarts the container.
Here's an example of how to define a liveness probe using an HTTP GET request to a specific path:
apiVersion: v1 kind: Pod metadata: name: mypod spec: containers: - name: mycontainer image: myimage ports: - containerPort: 80 livenessProbe: httpGet: path: /healthz port: 80 initialDelaySeconds: 15 periodSeconds: 10
In this example:
- httpGet specifies an HTTP GET request to /healthz on port 80.
- initialDelaySeconds specifies the number of seconds after the container has started before the probe is initiated.
- periodSeconds specifies how often (in seconds) Kubernetes should perform the probe.
-
Readiness Probe:
To define a readiness probe, you need to specify the readinessProbe field for the container. The readiness probe determines if the container is ready to serve traffic. If the readiness probe fails, Kubernetes removes the pod from service endpoints until it passes again.
Here's an example of how to define a readiness probe using an HTTP GET request:
apiVersion: v1 kind: Pod metadata: name: mypod spec: containers: - name: mycontainer image: myimage ports: - containerPort: 80 readinessProbe: httpGet: path: /ready port: 80 initialDelaySeconds: 5 periodSeconds: 10
In this example:
- httpGet specifies an HTTP GET request to /ready on port 80.
- initialDelaySeconds specifies the number of seconds after the container has started before the probe is initiated.
- periodSeconds specifies how often (in seconds) Kubernetes should perform the probe.
You can customize these probes based on your application's requirements, such as using TCP checks, command execution, or defining custom parameters like timeoutSeconds or failureThreshold. Adjust the parameters according to your application's behavior and needs.