# Ingress and load balancing

Kite publishes HTTP workloads through a Cloudflare Tunnel. When you create an `IngressClass` with the controller `cloudflare.com/tunnel`, Kite's tunnel controller creates the tunnel, runs `cloudflared` pods for it, and writes a DNS record for each hostname. To set it up, follow [Expose a workload](/docs/expose/).

The solid lines show the path of a request to a pod. The dashed lines show what
the tunnel controller writes:

```mermaid
graph TD
  client[Client] -->|HTTPS| edge[Cloudflare edge]
  edge -->|Cloudflare Tunnel| cfd[cloudflared pods]
  cfd -->|Service ClusterIP| pod[Pod]
  ctrl[Tunnel controller] -.->|tunnel rules, DNS| edge
  ctrl -.->|Deployment| cfd
```

## What is supported

The tunnel controller publishes HTTP rules on an `Ingress` and one HTTP port on a `LoadBalancer` Service. It does not publish raw TCP, UDP, or other non-HTTP Services.

Cloudflare serves public HTTPS at its edge, so the controller ignores `spec.tls` on an Ingress. The `cloudflared` pods open outbound connections to Cloudflare, and send each request to your Service over the [pod network](/docs/networking/). Publishing a hostname opens no inbound port on any node.

## Publish a LoadBalancer Service

The controller publishes a Service of `type: LoadBalancer` on one hostname and one port. The Service needs:

- the annotation `cloudflare.com/hostname`, set to the public hostname.
- exactly one HTTP port: one whose `appProtocol` is `http`, `https`, `kubernetes.io/ws` or `kubernetes.io/wss`, or whose name is `http` or `https`.
- a ClusterIP.

Kite publishes the Service through the IngressClass that its `cloudflare.com/ingress-class` annotation refers to. Without the annotation, Kite uses the default IngressClass. To publish several ports on one hostname, use an Ingress and route by path.

When the controller cannot publish a Service, it records a `Warning` Event on the Service. [Tunnel reasons](/docs/reference/events/#tunnel-reasons) lists each reason.

## Reach a TLS backend

`cloudflared` chooses the protocol for a backend from the Service port's `appProtocol`. A port named `https` does not turn on TLS.

| `appProtocol`                | `cloudflared`                                                           |
| ---------------------------- | ----------------------------------------------------------------------- |
| `https`, `kubernetes.io/wss` | Calls the port over TLS, with HTTP/2 if the backend offers it.          |
| `kubernetes.io/h2c`          | Refuses the port with an `UnsupportedProtocol` Event. HTTP/2 needs TLS. |
| Any other value, or none     | Sends plain HTTP/1.1.                                                   |

`cloudflared` accepts any certificate, so a self-signed one works.

### gRPC

For gRPC through the tunnel, the backend must serve TLS on a port with `appProtocol: https`. Turn on the zone's **gRPC** setting, under **Network** in the Cloudflare dashboard. Without that setting, Cloudflare refuses the call with 403.

## Credentials

The IngressClass's `spec.parameters` refers to a Secret that contains a Cloudflare API token. [Expose a workload](/docs/expose/) lists the token's permissions.

- In `spec.parameters`, set `kind: Secret`, `scope: Namespace`, and the Secret's `name` and `namespace`.
- Whoever creates or changes the class must be allowed to get that Secret, so nobody can make a class use a token that they could not read.
- To replace the token, write the Secret. The controller reads the Secret on every reconcile, and reconciles the class whenever you create, change or delete the Secret, so you need not edit the class.
- If the Secret is deleted or lacks a key, the controller records a `CredentialsUnusable` Event on the class.
- Use a token that expires: set an end date under **TTL** when you create it, or `expires_on` through the API. Replace the token in the Secret before that date.

When a token expires, the `cloudflared` pods keep serving their hostnames, because they run with the tunnel's own token. The controller records a `TunnelUnavailable` Event on the class, and leaves the tunnel and its DNS records as they are until the Secret contains a valid token.

So that you hear before then, the controller asks Cloudflare about the token once a day, and as soon as the Secret holds a different one. The [cluster summary](/docs/reference/resources/#read-the-cluster-summary) lists a class whose token Cloudflare refuses, as `TokenRefused`, and one whose token expires within 14 days, as `TokenExpiring`. Unlike an Event, which is deleted after an hour, each stays in the summary until the Secret holds a token that works.

:::caution
Anyone who can read the credential Secret can use its Cloudflare API token. See [Ingress credentials](/docs/security/#ingress-credentials).
:::

## What the controller creates

For each class, the controller creates:

- the tunnel,
- a Secret that contains the tunnel's token,
- a `cloudflared` Deployment with two replicas, and
- a proxied CNAME record for each hostname.

The controller overwrites any change that you make to these by hand. To change them, edit the IngressClass, the Ingress, or the `<class>-tunnel-override` ConfigMap.

The controller does not change a DNS record that it did not create, unless the record already points to the class's tunnel. If another record already uses a hostname, the controller records a `HostnameInUse` Event, and does not retry on its own. Delete the other record, then annotate the class so that the controller creates its own record:

```bash
kubectl annotate ingressclass <class> retried-at="$(date +%s)" --overwrite
```

:::caution
Deleting the IngressClass deletes its tunnel, its DNS records, its `cloudflared` Deployment, and its tunnel-token Secret.
:::

Cloudflare refuses to delete a tunnel while a `cloudflared` pod is still connected to it, so the controller retries the delete: after one minute, then with a doubling wait of at most five minutes, until it succeeds. A retry that fails records a `TunnelUnavailable` Event. The controller retries a failed delete after a [move](#move-a-class) on the same schedule.

If Cloudflare rejects the class's API token, because the token is revoked or lacks a permission, no retry can succeed. The controller then deletes the tunnel-token Secret and stops. It records a `TeardownAbandoned` Event that names the tunnel and the DNS records that may remain, with their account and zone. Delete them in the Cloudflare dashboard.

## Move a class

1. Point the class at its new place:
   - For another account or zone, change `accountId` or `zoneId` in its Secret, and the token if the old one cannot reach the new account or zone.
   - For another namespace, create its Secret in that namespace, then change `spec.parameters.namespace`.
2. The controller sets up the class in the new place, while the old place keeps serving:
   - A new zone gets a DNS record for each hostname. The tunnel and its `cloudflared` pods stay, because a tunnel belongs to an account.
   - A new account gets a tunnel of its own. The `cloudflared` pods restart to run it, and each pod keeps the old tunnel connected in a second container, `cloudflared-previous`. The DNS records and the Ingresses' addresses stay on the old tunnel.
   - A new namespace gets the `cloudflared` Deployment and the tunnel-token Secret, and the old namespace's pods keep serving. The controller reads the `<class>-tunnel-override` ConfigMap from the new namespace, so create it there if you use one.
3. The controller waits until the `cloudflared` Deployment in the new place has finished its rollout and has an available replica, which is a pod whose `cloudflared` has connected. It checks after 10 seconds, and then with a doubling wait of at most one minute.
4. The controller points the DNS records and the Ingresses' addresses at the new tunnel, deletes the old zone's records, and drops `cloudflared-previous` from the pods.
5. Once no pod runs the old tunnel, the controller deletes the old tunnel, and the old namespace's Deployment and tunnel-token Secret.

So the class's hostnames keep serving through the move. The controller deletes the old tunnel and DNS records with the credentials that it made them with, because the new token may not reach them. A failed delete retries, and can end in `TeardownAbandoned`, as [What the controller creates](#what-the-controller-creates) describes.

Until the class serves from the new place, the controller deletes nothing in the old one, and no DNS record points to a new tunnel. So if the new account or zone refuses the token, or the new pods never connect, the class keeps serving where it was, and the controller records an Event on the class:

| Event                 | Cause                                   |
| --------------------- | --------------------------------------- |
| `TunnelUnavailable`   | Cloudflare refused. The Event says why. |
| `CredentialsUnusable` | The new namespace has no Secret.        |

If you delete the class during a move, the controller deletes what the class has in both places at once.

## Next

- [Expose a workload](/docs/expose/): create credentials, a class, a Service, and an Ingress.
- [Compatibility and limitations](/docs/reference/compatibility/): the Ingress and Service behavior Kite supports.
