# Check a cluster

Use `kix status` to check a cluster after a deploy, or at any time later. For
every object of the cluster's last deploy, it checks three things: the object
is on the cluster, it still holds what Kix applied, and it is ready.

`kix status` does not build the cluster definition, so you can run it from any
directory, even on a machine without the project repository. Inside the project,
pass the cluster's name from `flake.nix` (or its deployed `name`, if the cluster
definition sets a different one). Outside the project, pass the deployed `name`.

{/* The output below is maintained by hand until the live captures in
    docs/nix/captures.nix (how-to/application-status and
    how-to/application-drift) are regenerated against `kix status`. */}

## Check the cluster

Run the command with the cluster name:

<Command commands={["kix status how-to-application"]} cwd="kix-examples/" />

When everything is in order, the output looks like this:

```text
Cluster how-to-application on Kubernetes v1.35.0 (context kind-kix-demo)
Last deploy: how-to-application-6rhi3wpk9iml  Active  2m ago

  how-to-app/preview              4 resources  ok
  how-to-app/production           6 resources  ok
  kube-system/platform-dns        1 resource   ok
  kube-system/platform-storage    1 resource   ok
  (cluster-level)                 4 resources  ok

16 resources: 16 ok.
```

Kix reads the Kubernetes cluster of the current kubectl context. Pass
`--context` to read another one:

<Command
  commands={["kix status how-to-application --context kind-kix-demo"]}
  cwd="kix-examples/"
/>

Leave out the cluster name to check every Kix cluster that has deployed to
that Kubernetes cluster.

## Read the package lines

The output has one line for each package, named `namespace/instance`, with
the number of objects the package deployed. Objects that belong to no
package, such as Namespaces and CustomResourceDefinitions, are counted under
`(cluster-level)`.

A package line reads `ok` or counts its problems: `missing`, `changed`, or
`not ready`. Below the package lines, Kix lists each object with a problem,
grouped by package. Add `-v` to list every object:

<Command commands={["kix status how-to-application -v"]} cwd="kix-examples/" />

Add `-q` to print only the objects with a problem, or `--package production`
to look at one package.

## Find missing objects

An object is missing when the last deploy made it and it is gone from the
cluster, for example after someone ran `kubectl delete`:

```text
  how-to-app/production           6 resources  1 missing

how-to-app/production
  ConfigMap how-to-app/production  missing, needed by Deployment how-to-app/production

16 resources: 15 ok, 1 missing.
Next: run `kix deploy how-to-application` to restore what is missing or changed, or `kix plan how-to-application` to preview the changes first.
```

The line names the objects that depend on the missing one. Those objects may
keep running, but can fail when they next need it, for example when a Pod
restarts and mounts the ConfigMap.

Run `kix deploy` (shown on the `Next:` line) to recreate the missing object.
`kix deploy` always recreates any object in the build that is missing from the
cluster.

When Kix cannot determine a missing object's kind and name, it shows
`Unknown/(hash: ...)`. This happens on a machine that does not have the
deployed build locally when an earlier version of Kix wrote the deployment
record without object names and no deploy on this version has converted it yet.

## Find changed objects

An object is changed when a field Kix applied no longer holds the value Kix
applied. Kix reads the object's field ownership data to say which field
changed and which tool changed it. To run only this check:

<Command commands={["kix status how-to-application --check drift"]} cwd="kix-examples/" />

In this example, someone scaled the production Deployment from two replicas
to one with `kubectl scale`:

```text
Cluster how-to-application on Kubernetes v1.35.0 (context kind-kix-demo)
Last deploy: how-to-application-6rhi3wpk9iml  Active  5m ago

  how-to-app/preview              4 resources  ok
  how-to-app/production           6 resources  1 changed
  kube-system/platform-dns        1 resource   ok
  kube-system/platform-storage    1 resource   ok
  (cluster-level)                 4 resources  ok

how-to-app/production
  Deployment how-to-app/production  changed since deploy (spec.replicas by kubectl)

16 resources: 15 ok, 1 changed.
Next: run `kix deploy how-to-application` to restore what is missing or changed, or `kix plan how-to-application` to preview the changes first. The deploy takes back the fields that kubectl owns.
```

Only the fields Kix set count. A field that a controller adds, or one the
package cedes to a controller, does not make the object changed.

Before you put the value back, decide whether the change should stay. If it
should, change the cluster definition to match and deploy. If it should not,
follow [Reconcile drift](/docs/how-to/operate-a-cluster/reconcile-drift/).

An object with no recorded applied hash shows as "not checked for changes".
The next `kix deploy` reapplies the object and records its applied hash. Objects that are not checked do not
count as problems.

## Investigate an object that is not ready

An object is not ready when it fails the readiness rules `kix deploy` waits
on. The line says why:

```text
how-to-app/production
  Deployment how-to-app/production  not ready (1/2 ready, 2/2 updated)
```

`kix status` gives a summary. To find the cause, use `kubectl`:

1. Note the object's kind, namespace, and name from the output.
2. Inspect it with `kubectl describe`.
3. Read Pod or Job logs where they apply.

For example:

<Command expandable {...describeDeployment} />

The health-check Job's logs say whether the post-deploy probe passed:

<Command {...jobLogs} />

## When the last deploy failed

When a deploy fails, the previous deploy stays active. `kix status` shows the
failed deploy first, along with when and why it failed, followed by the current
deploy. Any objects that belong only to the failed deploy appear under
`Only in the failed deploy`, and `kix status` exits `2` until a new deploy
succeeds. Fix the cause of the failure and run `kix deploy` again.

## What readiness does not show

Kix checks the status that Kubernetes and cluster controllers report for each
object. A Deployment is ready when its pods are updated and pass their
readiness probes. A `LoadBalancer` Service is ready once its load balancer
gives it an address. A custom resource is ready when its controller sets the
ready state its package declares. Because Kix does not test the external
services your pods call, every object can report ready while part of a workload
is failing:

- **Rejected cloud identities.** A pod can start and pass its readiness probes
  even if the cloud provider rejects its identity. Only its requests to cloud
  APIs fail, for example when an IAM role is missing permissions or when a
  trust policy or Workload Identity binding names the wrong ServiceAccount.
  Check the pod logs for errors such as `AccessDenied` or `403`.
- **Operator features reported in separate conditions.** An operator can mark a
  custom resource ready while one of its features is failing. For example, a
  CloudNativePG `Cluster` stays `Ready=True` when archiving to its backup store
  fails, and reports the failure in its `ContinuousArchiving` condition
  instead:

  ```sh
  kubectl get cluster.postgresql.cnpg.io <name> -n <namespace> \
    -o jsonpath='{.status.conditions[?(@.type=="ContinuousArchiving")]}'
  ```

## Use the check in a script

`kix status` exits with a code a script or a cron job can branch on:

| Code | Meaning |
| --- | --- |
| `0` | Every selected check passed. |
| `2` | An object is missing, changed, or not ready. |
| `1` | Kix could not answer, for example because it could not reach the cluster or the cluster has no deploy. |

Pass `--check` to run only the checks you care about. Repeat it to run more
than one. For example, to alert when an object is gone or someone changed one:

<Command
  commands={["kix status how-to-application --check presence --check drift -q"]}
  cwd="kix-examples/"
/>

Objects that Kix could not check do not change the exit code. For example,
when the account may not list Secrets, Kix says so on a closing line and
leaves those objects unchecked.

To store the result or read it with another program, use JSON:

<Command
  commands={["kix status how-to-application --check drift -o json > status.json"]}
  cwd="kix-examples/"
/>

The document lists every object with its `problems`, and has a `summary` with
counts. See [`kix status`](/docs/reference/cli/status/#json-and-yaml-output)
for every field.

`kix status` only reads. It does not apply anything or change the cluster's
Activation records.

:::note[Reference]
See [`kix status`](/docs/reference/cli/status/) for every flag and what each
check does.
:::