Skip to content
Install the CLI

How-to guide Author packages and clusters

Add a post-deploy health check

These docs describe Kix's main branch, which can include changes not yet released. Read the docs for the latest release.

Use kix.healthCheck when Kubernetes readiness is necessary but not enough. For example, ready Pods do not prove that an application can reach its database or that a monitoring agent is successfully writing samples.

The helper adds a Job that runs after the package root and every endpoint used by the probe are ready. A non-zero exit fails the deployment, and the deploy output includes the last lines printed by the probe.

This guide assumes your package already has a workload, a Service exposed as self.service, and a list-valued build field.

Expose the standard options in the package:

how-to/application/web-package.nix
healthCheck = kix.options.healthCheck;

View source on GitHub ↗

This gives cluster authors two settings:

OptionDefaultPurpose
healthCheck.enabledfalseRender and run the probe
healthCheck.retryFor90Seconds the script may retry before failing

You can extend the option set with package-specific fields by merging another attribute set into kix.options.healthCheck.

Keep the script beside the package so it can be reviewed and tested on its own. This shell probe retries an HTTP endpoint for the period Kix supplies in RETRY_FOR:

how-to/application/health-check.sh
set -u
deadline=$(( $(date +%s) + RETRY_FOR ))
while true; do
if curl --fail --silent --show-error --max-time 10 "$APP_URL" >/dev/null; then
echo "ok GET $APP_URL"
exit 0
fi
echo "FAIL GET $APP_URL"
if [ "$(date +%s)" -ge "$deadline" ]; then
echo "gave up after ${RETRY_FOR}s"
exit 1
fi
sleep 5
done

View source on GitHub ↗

Keep one pass through the checks below 90 seconds. Kix reserves that much time after the retry window so the Job can report its final failure before its deadline expires.

Append kix.healthCheck to the package’s build list:

how-to/application/web-package.nix
(kix.healthCheck {
runtime = kix.healthCheck.runtimes.shell;
env =
{ self, ... }:
{
APP_URL = self.service.out.url { };
};
script = builtins.readFile ./health-check.sh;
})

View source on GitHub ↗

The callback value self.service.out.url { } matters. It gives the Job both the address to call and the dependency information attached to that address. Kix uses it to order the Job after the Service and to derive network-policy egress when network policy is enabled.

For a probe with several targets, add each target through env. The helper also provides deps, containing every dependency injected into any build entry in the package.

Use requires only for resources the probe must wait for but does not address. The package root is always included automatically.

Enable it on an instance:

how-to/application/cluster.nix
healthCheck = {
enabled = true;
retryFor = 60;
};

View source on GitHub ↗

Check the rendered cluster before deploying:

kix-examples/
❱ kix check how-to-application
 TOOL       RESULT  DETAILS                       
 eval       pass    15 manifests evaluated        
 scorecard  pass    0 errors, 11 warnings, 2 info 

governance.hasOwner (warning), 2:
  how-to-app/preview: package 'preview' has no meta.owner
  how-to-app/production: package 'production' has no meta.owner

reliability.boundedResources (warning), 2:
  how-to-app/preview/preview: container 'nginx' is missing resource settings: limits.cpu. Set CPU and memory requests and limits.
  how-to-app/production/production: container 'nginx' is missing resource settings: limits.cpu. Set CPU and memory requests and limits.

reliability.hasProbes (warning), 2:
  how-to-app/preview/preview: container 'nginx' has no livenessProbe
  how-to-app/production/production: container 'nginx' has no livenessProbe

security.hasNetworkPolicy (warning), 1:
  how-to-app: namespace 'how-to-app' has workloads but no network policy

security.nonRoot (warning), 2:
  how-to-app/preview/preview: container 'nginx' may run as root: neither it nor the pod sets securityContext.runAsNonRoot, and no non-zero runAsUser resolves for it
  how-to-app/production/production: container 'nginx' may run as root: neither it nor the pod sets securityContext.runAsNonRoot, and no non-zero runAsUser resolves for it

security.readOnlyRootfs (warning), 2:
  how-to-app/preview/preview: container 'nginx' does not set readOnlyRootFilesystem
  how-to-app/production/production: container 'nginx' does not set readOnlyRootFilesystem

2 info finding(s) not listed; run with -v to list them.

The output should include no health-check validation errors. Render the Job to confirm the helper is enabled:

kix-examples/ output excerpt offline capture
❱ kix build how-to-application -o json
Show outputHide output · 105 lines
{
  "apiVersion": "batch/v1",
  "kind": "Job",
  "metadata": {
    "annotations": {
      "kix.run/depends-on": "8767b7nzgc1x5bpfa9gv71cpk9z8h0iw,cd3l507fvaf0wg5azfza49x6wjsiiynz,dlzbv1nahmar8f7b9jm6rmyvmqczmzdb,requires:mkgdf3yajj0a92l7fspcy7ciykw4dsl1",
      "kix.run/identity-hash": "wpz10gpqzpm03j9h0brcczwjsgdrh5nx",
      "kix.run/package": "production",
      "kix.run/package-namespace": "how-to-app",
      "kix.run/rerun": "on-change",
      "kix.run/rerun-hash": "292bf91166ffd91051bafff4ba2a7a72fdbeede631f2da422b20c2b243fdf576"
    },
    "labels": {
      "app.kubernetes.io/component": "health-check",
      "app.kubernetes.io/instance": "production",
      "app.kubernetes.io/managed-by": "kix",
      "app.kubernetes.io/name": "production-health"
    },
    "name": "production-health",
    "namespace": "how-to-app"
  },
  "spec": {
    "activeDeadlineSeconds": 150,
    "backoffLimit": 0,
    "template": {
      "metadata": {
        "annotations": {
          "checksum/root": "670ec54e5945f89e2c6d0b47732acc448c40a80f51ef86c0d0eb2affd4924726"
        },
        "labels": {
          "app.kubernetes.io/component": "health-check",
          "app.kubernetes.io/name": "production-health"
        }
      },
      "spec": {
        "automountServiceAccountToken": false,
        "containers": [
          {
            "command": [
              "/bin/sh",
              "-eu",
              "/probe/health-check.sh"
            ],
            "env": [
              {
                "name": "APP_URL",
                "value": "http://production.how-to-app.svc.cluster.local:80"
              },
              {
                "name": "RETRY_FOR",
                "value": "60"
              }
            ],
            "image": "docker.io/curlimages/curl:8.14.1@sha256:9a1ed35addb45476afa911696297f8e115993df459278ed036182dd2cd22b67b",
            "name": "probe",
            "resources": {
              "limits": {
                "memory": "128Mi"
              },
              "requests": {
                "cpu": "50m",
                "memory": "64Mi"
              }
            },
            "securityContext": {
              "allowPrivilegeEscalation": false,
              "capabilities": {
                "drop": [
                  "ALL"
                ]
              },
              "readOnlyRootFilesystem": true,
              "runAsNonRoot": true
            },
            "terminationMessagePolicy": "FallbackToLogsOnError",
            "volumeMounts": [
              {
                "mountPath": "/probe",
                "name": "production-health-script",
                "readOnly": true
              }
            ]
          }
        ],
        "restartPolicy": "Never",
        "securityContext": {
          "runAsGroup": 65534,
          "runAsNonRoot": true,
          "runAsUser": 65534,
          "seccompProfile": {
            "type": "RuntimeDefault"
          }
        },
        "volumes": [
          {
            "configMap": {
              "name": "production-health-script"
            },
            "name": "production-health-script"
          }
        ]
      }
    }
  }
}

Deploy the cluster:

kix-examples/ live capture
❱ kix deploy how-to-application -y
Show outputHide output · 76 lines
Building cluster 'how-to-application'...
Cluster how-to-application: 15 manifests
Connecting to context kind-kix-docs (https://127.0.0.1:41673)...
No previous deployment record on the cluster. First deploy.
Note: Cluster 'how-to-application' does not set `cluster.kubernetes.uid`, so Kix continues without checking that context kind-kix-docs (https://127.0.0.1:41673) is the right Kubernetes cluster.
  If this is the right cluster, set `cluster.kubernetes.uid = "a5616cf1-7239-4ec7-aeef-10744af12935";` in the 'how-to-application' cluster definition. Once set, Kix refuses to deploy 'how-to-application' to any other cluster. See https://kix.run/docs/how-to/operate-a-cluster/bind-a-cluster-definition-to-its-kubernetes-cluster/.

  _cluster
    ~ cluster-level resources (1 changed, 1 added)
  how-to-app
    + preview 1.0.0 (3 resources)
    + production 1.0.0 (5 resources)
  kube-system
    + platform-dns (0 resources)
    + platform-storage (0 resources)

  Plan: 4 added, plus cluster-level changes
  Resources: 2 changed in content, 0 tracking annotation only (no spec change or restart)

Not checked by the dry run (13); their changes come from comparing builds:
  ? 5 uses a Kix CustomResourceDefinition that Kix installs when the deploy starts:
      Activation.kix.run/how-to-application-6n94gibgl7zv
      PackageInstance.kix.run/platform-dns@kube-system
      PackageInstance.kix.run/platform-storage@kube-system
      PackageInstance.kix.run/preview@how-to-app
      PackageInstance.kix.run/production@how-to-app
  ? 8 is in a Namespace created by this deploy:
      ConfigMap/preview@how-to-app
      ConfigMap/production-health-script@how-to-app
      ConfigMap/production@how-to-app
      Deployment.apps/preview@how-to-app
      Deployment.apps/production@how-to-app
      and 3 more (-o json lists them)

Not checked by any preview: whether Pods start, pull their images, and pass their readiness probes.

Objects: 15 to write, 0 to delete (sha256:07cee0a29c16dacc33ca673983cd67849f9543557982765268e4d34ac9e9f630)
plan: 15 nodes
  ~ Namespace/kube-system configured
  ✔ Namespace/kube-system ready
  + Namespace/how-to-app created
  ✔ Namespace/how-to-app ready
  + ConfigMap/preview@how-to-app created
  ✔ ConfigMap/preview@how-to-app ready
  + ConfigMap/production-health-script@how-to-app created
  ✔ ConfigMap/production-health-script@how-to-app ready
  + ConfigMap/production@how-to-app created
  ✔ ConfigMap/production@how-to-app ready
  + Deployment.apps/preview@how-to-app created
  + PackageInstance.kix.run/platform-storage@kube-system created
  + PackageInstance.kix.run/platform-dns@kube-system created
  + Deployment.apps/production@how-to-app created
  ✔ PackageInstance.kix.run/platform-storage@kube-system ready
  … Deployment.apps/preview@how-to-app waiting: Deployment/preview: 0/1 updated pods ready; pod preview-689b78858d-vhfzn cannot be scheduled: 0/1 nodes are available: 1 node(s) had untolerated taint(s). no new claims to deallocate, preemption: 0/1 nodes are available: 1 Preemption is not helpful for scheduling. (idle 10s) (14s of 180s); 3 objects wait for it: Service/preview@how-to-app and 2 more
  … PackageInstance.kix.run/platform-dns@kube-system waiting: Service exists, waiting for ready endpoints (14s of 180s); 3 objects wait for it: Activation.kix.run/how-to-application-6n94gibgl7zv, Job.batch/production-health@how-to-app, and 1 more
  … Deployment.apps/production@how-to-app waiting: Deployment/production: 0/2 updated pods ready; pod production-85577f7474-bbvch cannot be scheduled: 0/1 nodes are available: 1 node(s) had untolerated taint(s). no new claims to deallocate, preemption: 0/1 nodes are available: 1 Preemption is not helpful for scheduling. (idle 10s) (14s of 180s); 4 objects wait for it: Service/production@how-to-app and 3 more
  ✔ PackageInstance.kix.run/platform-dns@kube-system ready
  … Deployment.apps/preview@how-to-app waiting: Deployment/preview: 0/1 updated pods ready; pod preview-689b78858d-vhfzn/nginx is waiting: ContainerCreating (idle 18s) (34s of 180s); 3 objects wait for it: Service/preview@how-to-app and 2 more
  … Deployment.apps/production@how-to-app waiting: Deployment/production: 1/2 updated pods ready; pod production-85577f7474-h8s47/nginx is waiting: ContainerCreating (idle 0s) (34s of 180s); 4 objects wait for it: Service/production@how-to-app and 3 more
  ✔ Deployment.apps/preview@how-to-app ready
  + Service/preview@how-to-app created
  ✔ Service/preview@how-to-app ready
  + PackageInstance.kix.run/preview@how-to-app created
  ✔ PackageInstance.kix.run/preview@how-to-app ready
  ✔ Deployment.apps/production@how-to-app ready
  + Service/production@how-to-app created
  ✔ Service/production@how-to-app ready
  + recreate Job.batch/production-health@how-to-app created
  ✔ Job.batch/production-health@how-to-app ready
  + PackageInstance.kix.run/production@how-to-app created
  ✔ PackageInstance.kix.run/production@how-to-app ready
  ~ Activation.kix.run/how-to-application-6n94gibgl7zv configured
  ✔ Activation.kix.run/how-to-application-6n94gibgl7zv ready
  • activation 'how-to-application-6n94gibgl7zv' → Active

Deploy complete: 13 created, 2 configured, 0 unchanged, 0 failed

The deployment does not become active until the Job succeeds. If the script exits non-zero, Kix reports the Job as failed and prints its final output below the failure.

For example, a probe that reaches the application but rejects its response is reported with the final lines from the script:

kix-examples/
❱ kix deploy how-to-application -y
Building cluster 'how-to-application'...
Cluster how-to-application: 15 manifests
Connecting to context kind-kix-docs (https://127.0.0.1:41673)...
Current deployment: how-to-application-6n94gibgl7zv (build 6n94gibg...)

  how-to-app
    ~ production 1.0.0 (2 changed, 1 tracking annotation only)

  Plan: 1 updated, 3 unchanged
  Resources: 2 changed in content, 1 tracking annotation only (no spec change or restart)
  ↻ 1 Job(s) with kix.run/rerun changed their own content and will be deleted and recreated so they run again: Job.batch/production-health@how-to-app

Not checked by any preview: whether Pods start, pull their images, and pass their readiness probes.

Objects: 4 to write, 0 to delete (sha256:b9c708c587adbc6e6959b9ca8d4e937b0a6c2b33de7d10046bd3aa915e95a97d)
plan: 15 nodes
  ~ ConfigMap/production-health-script@how-to-app configured
  ✔ ConfigMap/production-health-script@how-to-app ready
  + recreate Job.batch/production-health@how-to-app created
  ⚠ Job.batch/production-health@how-to-app JobBackoffExceeded: Job failed 1 times (backoffLimit=0). Kix did not deploy the 2 objects that depend on it.
      ok    GET http://production.how-to-app.svc.cluster.local:80
      the application returned an unexpected response

Deploy of 'how-to-application' failed: 0 created, 1 configured, 11 unchanged, 1 failed (1 applied but not ready), 2 not started

  Failed resources (1):
    - Job.batch/production-health@how-to-app: JobBackoffExceeded: Job failed 1 times (backoffLimit=0)

  Cluster state:
    - 0 created and 1 configured object(s) from this run are ready on the cluster. 1 failed (1 applied but not ready; its new manifest is on the cluster), and 2 were never sent to the cluster.
    - Deployment record how-to-application-qhsf9zcji0j5 is marked Failed, and how-to-application-6n94gibgl7zv remains the Active deployment record.
    - The next `kix deploy how-to-application` on a machine with this build will compare against what this failed run applied.

  Next:
    Fix the failed resource(s) above and run `kix deploy how-to-application` again.
(exit code: 1)

The Job carries kix.run/rerun: on-change. When the Job’s own content changes, Kix deletes the existing Job in the foreground and recreates it. That content includes the script, env (along with any Kix-built Secrets or ConfigMaps referenced in env), and checksum/* annotations for self.root and each resource returned by requires. Changing any of these runs the probe again. When the Job is unchanged, kix deploy and kix rollback inspect the live Job: they skip it if its previous run succeeded and recreate it if its previous run failed (the in-cluster operator reports an unchanged failed Job without recreating it so a failing probe does not retry in a loop).