Monitor container health with probes

In this tutorial, you’ll learn the process of setting up startupProbe, livenessProbe, and readinessProbe using Score and the Humanitec extension.

  • startupProbe adds a probe that runs only once at container startup before the others.

  • initialDelaySeconds delays the first run of livenessProbe and readinessProbe from container start.

  • livenessProbe and readinessProbe run periodically after their initial delay.

Define Probes

Begin by defining the necessary probes in your score.yaml:

apiVersion: score.dev/v1b1 # Score API version

metadata:
  name: test # Name of the workload

containers: # Define containers
  test: # Container name
    image: nginx:latest # Container image
    # Define a liveness probe
    livenessProbe:
      httpGet: # HTTP GET probe
        path: /healthz # Probe path
        port: 8080 # Probe port
    # Define a readiness probe
    readinessProbe:
      httpGet: # HTTP GET probe
        path: /readyz # Probe path
        port: 8080 # Probe port

The liveness and readiness probes allow Kubernetes to monitor the container health and restart it if needed.

The probes hit different endpoints to differentiate between liveness and readiness.

  • livenessProbe and readinessProbe define the probes.
  • httpGet specifies this is an HTTP GET probe.
  • path is the endpoint path for health/ready checks.
  • port specifies the port for requests.

Now, set up the Humanitec extension file to configure the probes for the application.

Use an extensions file

The score.humanitec.yaml file provides a way to customize the configuration when deploying an application to the Humanitec Platform Orchestrator.

Create a score.humanitec.yaml file in the root of your repository.

This file will be detected and applied when deploying the application.

# Humanitec API version
apiVersion: humanitec.org/v1b1

# Use the default Humanitec module profile
profile: humanitec/default-module

# Override spec for the 'test' container
spec:
  containers:
    test:
      # Add a startup probe
      startup_probe:
        # Probe type is HTTP
        type: "http"
        # Endpoint path for startup check
        path: /healthz
        # Port for startup check
        port: 8080
        # Consecutive failures needed to fail startup
        failureThreshold: 10
        # Initial delay before performing startup check
        initialDelaySeconds: 5
        # Interval between startup checks
        periodSeconds: 5
      # Customize liveness probe
      liveness_probe:
        # Initial delay before liveness check
        initialDelaySeconds: 3
        # Interval between checks
        periodSeconds: 10
        # Probe timeout
        timeoutSeconds: 2
      # Customize readiness probe
      readiness_probe:
        # Initial delay before readiness check
        initialDelaySeconds: 3
        # Interval between checks
        periodSeconds: 10
        # Probe timeout
        timeoutSeconds: 2

This overrides the test container config to add a startup probe and customize the liveness and readiness probes.

Generate a Deployment Delta

Use the score-humanitec CLI tool to prepare a new Humanitec deployment delta:

score-humanitec run -f ./score.yaml --extensions ./score.humanitec.yaml --env test-env

This command will produce a JSON output, which can be used as a payload for the Create a new Delta API call in Humanitec.

The following is the expected output:

{
  "metadata": {
    "env_id": "test-env",
    "name": "Auto-deployment (SCORE)"
  },
  "modules": {
    "add": {
      "test": {
        "profile": "humanitec/default-module",
        "spec": {
          "containers": {
            "test": {
              "id": "test",
              "image": "nginx:latest",
              "liveness_probe": {
                "initialDelaySeconds": 3,
                "path": "/healthz",
                "periodSeconds": 10,
                "port": 8080,
                "timeoutSeconds": 2,
                "type": "http"
              },
              "readiness_probe": {
                "initialDelaySeconds": 3,
                "path": "/readyz",
                "periodSeconds": 10,
                "port": 8080,
                "timeoutSeconds": 2,
                "type": "http"
              },
              "startup_probe": {
                "failureThreshold": 10,
                "initialDelaySeconds": 5,
                "path": "/healthz",
                "periodSeconds": 5,
                "port": 8080,
                "type": "http"
              }
            }
          }
        }
      }
    }
  }
}

Deploy

Once you’ve generated the deployment delta, you can proceed to deploy your service with the Humanitec Platform Orchestrator.

score-humanitec run -f ./score.yaml --extensions ./score.humanitec.yaml --env test-env --deploy

Ensure that all configurations are correctly set to avoid deployment issues.

Conclusion

By following this tutorial, you’ve successfully set up probes with Score and integrated with Humanitec Platform Orchestrator allowing you to monitor your application’s health and readiness during deployments.

Top