# Install Cilium / network policy support

Use the `cilium` package to make Kix responsible for the Cilium CNI and the
CiliumNetworkPolicy API.

This guide uses a new Kind cluster. Cilium must be the cluster's CNI from the
start, so create Kind without its default CNI before deploying the example.

## Create the Kind cluster

Create this Kind configuration beside the `kix-examples` checkout:

```yaml title="kind-cilium.yaml"
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
networking:
  disableDefaultCNI: true
  podSubnet: "10.244.0.0/16"
```

Create the cluster:

<Command commands={["kind create cluster --config kind-cilium.yaml"]} />

The control-plane node remains `NotReady` until Cilium is deployed. That is
expected for a cluster with no CNI.

## Make the CNI role available

Tell Kix that the Kind environment is not providing a CNI, then enable network
policy generation:

<Snippet {...ciliumRoleAndPolicy} />

The role setting must describe the cluster you actually created. Do not mark
the role absent on a Kind cluster that is already running kindnet.

## Add the Cilium instance

Install Cilium in `kube-system`:

<Snippet {...ciliumInstance} />

The instance needs no `values`. It keeps Kind's kube-proxy (the chart default)
and takes its IPAM pool from `cluster.network.podCidr`, which the kind flavour
sets to Kind's default `podSubnet`, `10.244.0.0/16`. A Kind configuration with
another `podSubnet` sets `cluster.network.podCidr` to match. With network
policy on, the package sets Cilium's `policyEnforcementMode` to `always`, so a
pod without an allow rule loses all traffic except cluster DNS. The package
also writes two CiliumClusterwideNetworkPolicies: `cilium-cluster-dns-egress`
keeps cluster DNS open for every pod, and `cilium-health-probes` lets Cilium's
health probes reach the other nodes.

## Check the generated resources

Evaluate the cluster before deploying it:

<Command {...check} />

Inspect the main Cilium components and a generated default-deny policy:

<Command {...components} />

Kix builds the Cilium DaemonSet and operator from the package, then connects
generated policies to Cilium's custom resource definitions in the deployment
graph.

## Deploy and verify Cilium

Deploy the cluster and wait for the Cilium pods to become ready:

<Command
  commands={[
    "kix deploy how-to-package-cilium",
    "kubectl rollout status -n kube-system daemonset/cilium",
    "kubectl get nodes",
  ]}
  cwd="kix-examples/"
/>

The Kind node should report `Ready` after the Cilium DaemonSet is running.
Check the generated policies with:

<Command commands={["kubectl get ciliumnetworkpolicies --all-namespaces"]} />

If the node remains `NotReady`, compare the pod CIDR in the Kind configuration
with `cluster.network.podCidr`, then inspect the Cilium pod logs in
`kube-system`.

## When RKE2 installs Cilium

RKE2 installs its own Cilium chart when its server config sets `cni: cilium`.
Tell Kix the same thing with the rke2 flavour, and declare the instance in
`kube-system`:

```nix
cluster.flavor.rke2.cni = [ "cilium" ];
instances.kube-system.cilium.package = packages.experimental.cilium;
```

The flavour declares RKE2's Cilium as the platform import
`kube-system/platform-cilium`, and the instance takes it as a dependency. The
instance does not install a second Cilium. It writes its values into the
HelmChartConfig `kube-system/rke2-cilium`, which the import names and RKE2's
helm-controller merges
into the bundled chart: policy enforcement, operator replicas, and rollouts.
RKE2's chart uses `kubernetes` IPAM, so the package sets no pool. A cluster
that wants cluster-pool IPAM sets `values.ipam.mode = "cluster-pool"` and the
pool together.

To replace kube-proxy on RKE2, set `values.kubeProxyReplacement = true` with
`values.k8sServiceHost` and `values.k8sServicePort`, and configure every RKE2
server with `cni: cilium` and `disable-kube-proxy: true`. Kix cannot read the
server config, so it cannot check that the two agree.

With network policy on, the rke2 flavour declares platform intents for the
pods RKE2's bundled charts run in `kube-system` (CoreDNS, metrics-server, the
ingress controllers, the snapshot controller, and the chart install Jobs).

Kix then owns `kube-system/rke2-cilium`, which has two consequences:

- RKE2 also applies any `rke2-cilium` HelmChartConfig in the server's
  manifests directory (`/var/lib/rancher/rke2/server/manifests`). Remove that
  file, or RKE2 and Kix write the same object.
- Removing the instance makes the next `kix deploy --prune` delete the
  HelmChartConfig, and helm-controller then reinstalls RKE2's Cilium with its
  chart defaults.

### Keep Cilium running if a Helm upgrade fails

On RKE2 releases before 4 August 2026 (earlier than 1.33.13, 1.34.10, 1.35.7,
or 1.36.3), set `failurePolicy` to `"abort"`:

```nix
instances.kube-system.cilium = {
  package = packages.experimental.cilium;
  config.failurePolicy = "abort";
};
```

When RKE2's Helm job fails to install or upgrade Cilium, `helm-controller`
follows a failure policy. When `failurePolicy` is unset, Kix omits
`spec.failurePolicy` from `kube-system/rke2-cilium` and RKE2's default applies:

| RKE2 release | Default policy | What happens on failure |
|---|---|---|
| 1.33.13, 1.34.10, 1.35.7, 1.36.3 (4 August 2026) and later | `retry` | `helm-controller` retries the install or upgrade. Cilium stays installed. |
| Earlier releases | `reinstall` | `helm-controller` uninstalls Cilium and installs it again. |

Under `reinstall`, no node runs Cilium until the reinstall succeeds. Open
connections keep working, while new pods get no networking. During an RKE2
minor upgrade, the reinstall can fail too. With `"abort"`, `helm-controller`
leaves the failed Helm release for you to fix and keeps the running Cilium
installation unchanged.

`failurePolicy` accepts `"abort"`, `"reinstall"`, or `"retry"` and writes that
value to `spec.failurePolicy` on the `HelmChartConfig`. RKE2 releases before
4 August 2026 do not recognize `"retry"`, and their API server rejects the
`HelmChartConfig`.

Only an instance that writes a `HelmChartConfig` accepts `failurePolicy`. If
the `cilium` instance installs the Helm chart directly, or if its
`platform-cilium` import sets `out.helmChartConfig = null`, setting
`failurePolicy` fails evaluation.

### Control when Cilium pods restart after a values change

The Cilium agent reads most settings, such as `loadBalancer.algorithm`, only at
startup. By default, the `cilium` package sets `rollOutCiliumPods = true` and
`operator.rollOutPods = true`, so any change to `values` that updates Cilium's
ConfigMap restarts the Cilium agent on every node and restarts the operator.
This applies whether RKE2 or Kix installs the Cilium chart.

Restarting the agent on a node that holds an L2 announcement moves that IP
address to another node. To control when the agents restart, set
`values.rollOutCiliumPods = false`. `kix deploy` then finishes while the agents
keep running with their previous settings. Restart them when you are ready:

<Command commands={["kubectl -n kube-system rollout restart ds/cilium"]} />

The Cilium operator still restarts during `kix deploy` unless you also set
`values.operator.rollOutPods = false`.

### Adopt an existing HelmChartConfig

If your cluster already has a `kube-system/rke2-cilium` HelmChartConfig, Kix
replaces all of `spec.valuesContent` on the first deploy. Any setting you do
not copy into `values` is removed. Read the live settings first:

<Command
  commands={[
    "kubectl -n kube-system get helmchartconfig rke2-cilium -o jsonpath='{.spec.valuesContent}'",
  ]}
/>

Copy each setting into the instance's `values`. If the live `HelmChartConfig`
also sets `spec.failurePolicy`, set `failurePolicy` to the same value. Run
`kix plan` before your first deploy. The plan shows `valuesContent` as changed
even when your settings match, because Kix writes JSON and includes the
package's defaults: `policyEnforcementMode`, `policyAuditMode`,
`operator.replicas`, `operator.rollOutPods`, and `rollOutCiliumPods`. Check the
diff to confirm that none of your existing settings were dropped.

If another field manager owns fields on `rke2-cilium`, `kix plan` reports a
field ownership conflict and names that owner. Stop that tool from writing the
object again first. On RKE2, remove any `rke2-cilium` manifest from
`/var/lib/rancher/rke2/server/manifests`, because RKE2 reapplies files in that
directory each time it starts. Then run `kix deploy --force-conflicts` once to
transfer field ownership to Kix. Because the package enables
`rollOutCiliumPods` by default, expect the first deploy to restart the Cilium
agents once unless you set `values.rollOutCiliumPods = false`.

## When the environment installed Cilium

Where something outside Kix installed and configures Cilium, declare that
the environment provides it, as a platform import the `cilium` instance
depends on, and leave its configuration alone:

```nix
cluster.roles.cni.binding = "shadowable";
instances.cilium-policy = {
  cilium.package = packages.experimental.cilium;
  platform-cilium.package = kix.mkImport {
    roles = [ "cni" ];
    out.helmChartConfig = null;
  };
};
```

`out.helmChartConfig = null` says the environment merges no HelmChartConfig
into its Cilium. Set it to `{ name; namespace; }` when it does, and the
instance writes its values there, as on RKE2.

Declare the instance in a namespace of its own, not in `kube-system`. With
network policy on, every namespace Kix manages gets a default-deny policy.
Nothing here declares platform intents for the environment's `kube-system`
pods, so a default deny there would cut off CoreDNS, and with it DNS for every
pod.

The instance then emits only the CRD references that generated policies wait
on, and the evaluation fails if it is given `values`.