# Install the agent

> Install the read-only LevelFour agent in one Kubernetes cluster with Helm, check that it reports, and choose which namespaces receive recommendations.

Source: https://docs.levelfour.ai/kubernetes/install


## Prerequisites

* **A LevelFour admin**, to connect the cluster or create an install key on **Connect Providers > Kubernetes** in the dashboard.
* Kubernetes 1.23 or later.
* `helm` 3.8 or later and `kubectl` access to the cluster, with rights to create a namespace, a ClusterRole and a ClusterRoleBinding. With GitOps, the tool that deploys the chart needs the same rights.
* **For costs:** the cloud account that runs the cluster connected to LevelFour with its billing data, or node prices you set for a cluster with no cloud bill. [How clusters are priced](/kubernetes/pricing) lists what each cloud needs. Without it, LevelFour shows configuration and usage with no prices.

## Review the chart before you install

The chart and the agent image are public on Amazon ECR Public. You can read them before you install, with no login and no access to a cluster:

```bash
helm show chart  oci://public.ecr.aws/levelfour/charts/levelfour-agent --version 0.8.6
helm show values oci://public.ecr.aws/levelfour/charts/levelfour-agent --version 0.8.6
helm pull oci://public.ecr.aws/levelfour/charts/levelfour-agent --version 0.8.6 --untar
helm template levelfour-agent oci://public.ecr.aws/levelfour/charts/levelfour-agent --version 0.8.6 \
  --namespace levelfour --set clusterName=review --set token=review
```

`helm pull` saves the chart's templates in a `levelfour-agent` folder. `helm template` prints the six objects the install creates (ServiceAccount, ClusterRole, ClusterRoleBinding, Secret, Deployment and Service) and applies none of them. To check that LevelFour published the chart and the image, [verify their signatures](/kubernetes/configuration#verify-the-signatures-and-sbom). A `403` from `public.ecr.aws` comes from an old login saved on your machine: see [Helm cannot pull the chart](/kubernetes/troubleshooting#helm-cannot-pull-the-chart).

## Pick a way to install

The **Connect Kubernetes** dialog asks which way you install. Both install the same chart and the same read-only agent.

| [One cluster, with Helm](#install-with-helm)                         | [Many clusters, with GitOps](/kubernetes/gitops)                                       |
| -------------------------------------------------------------------- | -------------------------------------------------------------------------------------- |
| You name the cluster in the dashboard, and it shows the Helm command | You create one install key for your organization                                       |
| The token in the command works for that cluster only                 | Each cluster that runs the agent with the key registers itself under its `clusterName` |
| You run the command once per cluster                                 | Argo CD, Flux or Terraform deploys the chart, and your secret manager supplies the key |

## Install with Helm

### Name the cluster

Open **Connect Providers > Kubernetes** in the dashboard and click **Connect cluster** (**Add a cluster** once you have one). In the **Connect Kubernetes** dialog, choose **One cluster, with Helm** and name the cluster. A name uses lowercase letters, digits and hyphens, starts and ends with a letter or digit, and has at most 63 characters.

The dialog shows the install command for the cluster. The token in the command (`l4k_...`) sends data for this one cluster and cannot read anything from your LevelFour organization.

### Run the install command

Run the command with your `kubectl` context set to the cluster:

```bash
helm upgrade --install levelfour-agent oci://public.ecr.aws/levelfour/charts/levelfour-agent \
  --version 0.8.6 \
  --namespace levelfour --create-namespace \
  --set token=YOUR_TOKEN \
  --set clusterName=YOUR_CLUSTER_NAME
```

The command pins chart version 0.8.6 and creates the `levelfour` namespace. The chart adds a service account, a read-only ClusterRole and its binding, a Secret holding the token, one Deployment and a Service for the agent's metrics port. It adds no CRD, no webhook and no persistent volume.

### Wait for the agent

The dashboard turns green when the agent first reports. To check from the cluster side, run the commands in [Verify the install](#verify-the-install).

### Choose the namespaces that get recommendations

Label each namespace you want recommendations for, or turn it on from the dashboard. Start with one and add more when you are ready. [Choose namespaces](#choose-namespaces) covers both ways, the opt-out and the namespaces that never get recommendations.

To change the proxy, Prometheus, image registry or any other setting, see [Configure the agent](/kubernetes/configuration).

## Verify the install

```bash
kubectl -n levelfour get pods
kubectl -n levelfour logs deploy/levelfour-agent
```

The pod reaches `Running` within a minute and reports to LevelFour as soon as it starts. It turns ready once it has read the cluster's objects, which takes longer on a large cluster. Its first log line names the agent version, the cluster name and the address it sends to.

When LevelFour refuses a report, the agent logs the status, the code and LevelFour's message, such as `401 AUTHENTICATION_ERROR: Unknown ingest token`. It logs each answer once, and logs again when LevelFour accepts reports. [Troubleshooting](/kubernetes/troubleshooting) explains each code.

> **Success**
>
> The cluster turns green on **Connect Providers > Kubernetes** when its agent first reports.

## Choose namespaces

Recommendations are opt-in per namespace. The agent reads the whole cluster from the start, so costs and usage cover every namespace. LevelFour recommends changes only in the namespaces you opt in, with a label in the cluster or a switch in the dashboard. A [configuration object](/kubernetes/automation#configure-it-in-the-cluster) that sets `automationMode` also opts in the namespaces it covers, unless a label or the switch turned them off. A namespace you opt in later already has its history, so its recommendations arrive without a new wait.

### From the dashboard

Turn a namespace on or off with the switch in the cluster's **Namespaces** tab, or at the top of the namespace's page. Admins and members can change it; viewers see it.

A label in the cluster wins over the switch. A namespace labelled `enabled` or `disabled` shows its switch locked, with the label as the reason. Remove the label to decide from the dashboard again.

### With a label

**kubectl**

```bash
kubectl label namespace payments levelfour.ai/recommendations=enabled
```

**Manifest**

```yaml title="namespace.yaml"
apiVersion: v1
kind: Namespace
metadata:
  name: payments
  labels:
    levelfour.ai/recommendations: enabled
```

To keep one workload out of an opted-in namespace, set the same key to `disabled` on the workload, as a label or an annotation:

```bash
kubectl -n payments label deployment checkout-api levelfour.ai/recommendations=disabled
```

To stop new recommendations in a namespace, remove the label:

```bash
kubectl label namespace payments levelfour.ai/recommendations-
```

These namespaces never get recommendations, whatever their labels or switches: `kube-system`, `kube-public`, `kube-node-lease`, and the namespace the agent runs in (`levelfour` by default).
