Skip to content

Health and Monitoring

Liveness and Readiness Probes

A probe is a diagnostic performed periodically by the kubelet on a container. Each probe uses one of four mechanisms:

exec: Runs a command inside the container. The check succeeds if the command exits with status code 0.

tcpSocket: Opens a TCP connection to the container's IP address on a specified port. The check succeeds if the port is open.

httpGet: Sends an HTTP GET request to the container's IP address on a specified port and path. The check succeeds if the response status code is at least 200 and below 400.

grpc: Calls the standard gRPC health checking protocol on a specified port. The check succeeds if the service reports SERVING.

The kubelet can run three kinds of probes on each container:

startupProbe: Indicates whether the application inside the container has started. Liveness and readiness probes don't run until the startup probe succeeds. If it keeps failing, the container is restarted. Use it for slow-starting applications instead of a long initialDelaySeconds.

livenessProbe: Indicates whether the container is still working. Runs for the lifetime of the container. If it fails failureThreshold times in a row, the kubelet kills the container, and it's restarted according to the pod's restartPolicy.

readinessProbe: Indicates whether the container is ready to serve requests. It also runs for the lifetime of the container. While it fails, the pod is removed from the endpoints of every Service that selects it. The container is not restarted.

The timing of every probe is tuned with initialDelaySeconds, periodSeconds (default 10), timeoutSeconds (default 1), failureThreshold (default 3) and successThreshold (default 1).

Resources

  • Application Health


    A health check periodically performs diagnostics on a running container using any combination of the readiness, liveness, and startup health checks.

    Learn more

  • Virtual Machine Health


    Use readiness and liveness probes to detect and handle unhealthy virtual machines (VMs).

    Learn more

  • Container Probes


    To perform a diagnostic, the kubelet either executes code within the container, or makes a network request.

    Learn more

  • Configure Probes


    Read about how to configure liveness, readiness and startup probes for containers.

    Learn more

References

Exec liveness probe
apiVersion: v1
kind: Pod
metadata:
  name: exec-probe
spec:
  containers:
    - name: app
      image: busybox
      command: ["sh", "-c", "echo Hello, Kubernetes! && sleep 3600"]
      livenessProbe:
        exec:
          command: ["echo", "alive"]
TCP liveness and HTTP readiness probes
apiVersion: v1
kind: Pod
metadata:
  name: web-probes
spec:
  containers:
    - name: app
      image: quay.io/nginx/nginx-unprivileged:1.29
      ports:
        - containerPort: 8080
      livenessProbe:
        tcpSocket:
          port: 8080
        initialDelaySeconds: 10
      readinessProbe:
        httpGet:
          path: /
          port: 8080
        periodSeconds: 10
Startup probe for a slow-starting app
apiVersion: v1
kind: Pod
metadata:
  name: slow-start
spec:
  containers:
    - name: app
      image: quay.io/nginx/nginx-unprivileged:1.29
      ports:
        - containerPort: 8080
      startupProbe:          # allow up to 30 x 10s = 5 minutes to start
        httpGet:
          path: /
          port: 8080
        failureThreshold: 30
        periodSeconds: 10
      livenessProbe:         # only starts once the startup probe has passed
        httpGet:
          path: /
          port: 8080

Container Logging

Application and systems logs can help you understand what is happening inside your cluster. The logs are particularly useful for debugging problems and monitoring cluster activity.

Kubernetes provides no native storage solution for log data, but you can integrate many existing logging solutions into your Kubernetes cluster.

Resources

  • Logs Command


    Read about the descriptions and example commands for OpenShift CLI (oc) developer commands.

    Learn more

  • Cluster Logging


    As a cluster administrator, you can deploy logging on an OpenShift Container Platform cluster, and use it to collect and aggregate node system audit logs, application container logs, and infrastructure logs.

    Learn more

  • Logging Collector


    The collector collects log data from each node, transforms the data, and forwards it to configured outputs.

    Learn more

  • Logging


    Application logs can help you understand what is happening inside your application and are particularly useful for debugging problems and monitoring cluster activity.

    Getting started

References

Pod Example
apiVersion: v1
kind: Pod
metadata:
  name: counter
spec:
  containers:
    - name: count
      image: busybox
      command:
        [
          "sh",
          "-c",
          'i=0; while true; do echo "$i: $(date)"; i=$((i+1)); sleep 5; done',
        ]
Get Logs
oc logs counter                  # current logs
oc logs -f counter               # follow new lines
oc logs counter --previous       # logs from the previous (crashed) container
oc logs counter -c count         # a specific container in a multi-container pod
oc logs deployment/my-deployment # logs from a pod of a deployment
Use Stern to View Logs
brew install stern
stern . -n default
Get Logs
kubectl logs counter
kubectl logs -f counter
kubectl logs counter --previous
kubectl logs counter -c count
kubectl logs deployment/my-deployment
Use Stern to View Logs
brew install stern
stern . -n default

Monitoring Applications

To scale an application and provide a reliable service, you need to understand how the application behaves when it is deployed. You can examine application performance in a Kubernetes cluster by examining the containers, pods, services, and the characteristics of the overall cluster. Kubernetes provides detailed information about an application’s resource usage at each of these levels. This information allows you to evaluate your application’s performance and where bottlenecks can be removed to improve overall performance.

Prometheus, a CNCF project, can natively monitor Kubernetes, nodes, and Prometheus itself. OpenShift includes a preconfigured Prometheus-based monitoring stack, and can also monitor your own applications' metrics (user workload monitoring).

Resources

  • Monitoring Application Health


    OpenShift Container Platform applications have a number of options to detect and handle unhealthy containers.

    Learn more

  • Monitoring Resource Usage


    You can examine application performance in a Kubernetes cluster by examining the containers, pods, services, and the characteristics of the overall cluster.

    Learn more

  • Resource Metrics


    For Kubernetes, the Metrics API offers a basic set of metrics to support automatic scaling and similar use cases.

    Learn more

References

The following pods use the Kubernetes resource-consumer test image to burn a fixed amount of CPU, so you can watch resource usage:

cpu-500m.yaml
apiVersion: v1
kind: Pod
metadata:
  name: cpu-500m
spec:
  containers:
    - name: app
      image: registry.k8s.io/e2e-test-images/resource-consumer:1.13
      resources:
        requests:
          cpu: 700m
          memory: 128Mi
    - name: load-generator
      image: registry.access.redhat.com/ubi9/ubi-minimal
      command:
        - /bin/sh
        - -c
        - until curl -s localhost:8080/ConsumeCPU -d "millicores=500&durationSec=3600"; do sleep 5; done && sleep infinity
cpu-200m.yaml
apiVersion: v1
kind: Pod
metadata:
  name: cpu-200m
spec:
  containers:
    - name: app
      image: registry.k8s.io/e2e-test-images/resource-consumer:1.13
      resources:
        requests:
          cpu: 300m
          memory: 64Mi
    - name: load-generator
      image: registry.access.redhat.com/ubi9/ubi-minimal
      command:
        - /bin/sh
        - -c
        - until curl -s localhost:8080/ConsumeCPU -d "millicores=200&durationSec=3600"; do sleep 5; done && sleep infinity

OpenShift ships with cluster monitoring (Prometheus) and the metrics API enabled. You can also see pod metrics in the web console under Observe in the developer perspective.

Resource usage of pods and nodes
oc adm top pods
oc adm top pods --containers
oc adm top nodes
Explore the cluster
oc get projects
oc api-resources -o wide
oc get nodes,ns,po,deploy,svc
oc describe node <node-name>

Resource metrics require the metrics-server, which most managed Kubernetes services install for you.

Verify metrics are enabled
kubectl get --raw /apis/metrics.k8s.io/
Check resource usage
kubectl top pods
kubectl top pods --containers
kubectl top nodes
Get node description
kubectl describe node <node-name>

Activities

Lab Description
Lab 2 - Probes Add liveness and readiness probes to recover from an unhealthy app