> For the complete documentation index, see [llms.txt](https://docs.cennso.com/components/upg/3.2/llms.txt) (all documentation: [llms.txt](https://docs.cennso.com/llms.txt)). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.cennso.com/components/upg/3.2/deployment-guides/upf-high-availability.md).

# Deploy UPF high availability

> [!IMPORTANT]
> UPF Session Controller (UPF-SC) and the User Plane Function (UPF) load balancer (UPF-LB) are experimental, and this deployment is not suitable for production. Their configuration can change in a later release.

UPF Session Controller (UPF-SC) presents a group of User Plane Gateway (UPG) instances to Session Management Functions (SMFs) as one UPF. It communicates with the SMFs and the UPG instances over the Packet Forwarding Control Protocol (PFCP) and places each session on one of the instances. If an instance fails or you drain it, UPF-SC moves its sessions to other instances. The SMF still sees the same tunnel endpoints and User Equipment (UE) IP address for a moved session.

A UPF-SC deployment consists of the following components:

- UPF-SC controllers: one active controller and one or more standby controllers
- PFCP NATS Gateways: a pair that shares the PFCP virtual IP addresses (VIPs) toward the SMFs and the UPG instances
- MySQL: stores the session state
- NATS with JetStream: carries PFCP between the gateways and the controllers, and holds the forwarding entries for UPF-LB
- UPF-LB: forwards User Plane traffic to the UPG instance that serves the session
- UPG instances: handle the User Plane of the sessions

## Prerequisites

Before you begin, make sure you have the following:

- [Helm](https://helm.sh/docs/intro/install/) v3+ and [kubectl](https://kubernetes.io/docs/tasks/tools/) configured to access the Core Cluster
- The `upf-sc` and `upf-sc-support` Helm charts from `oci://registry.cennso.com/cennso/charts`, version 3.2.0
- An existing MySQL database whose user can create tables, because UPF-SC creates its schema but not the database
- NATS 2.12 or later with JetStream
- UPG instances deployed with [`UPGDeployment`](https://docs.cennso.com/components/upg/3.2/concepts/upgdeployment.md), reachable from the PFCP NATS Gateways
- Fast Path Provider (FPP) 3.2.0 with a `CGWDeployment` resource for [UPF-LB](https://docs.cennso.com/components/fpp/3.2/deployment-guides/upflb.md). UPF-LB supports IPv4 only.

> [!CAUTION]
> The admin HTTP API and NATS do not use TLS. Keep the admin port and NATS on a trusted network.

## Steps

In the commands, `{UPF_SC_CHART}` is the `upf-sc` chart.

To deploy UPF-SC, follow these steps:

1. Create the namespace:

   ```bash
   kubectl create namespace {NAMESPACE}
   ```

2. Add a `Secret` resource with the MySQL Data Source Name (DSN):

   ```bash
   kubectl -n {NAMESPACE} create secret generic upf-sc-database-dsn \
     --from-literal=dsn='{DB_USER}:{DB_PASSWORD}@tcp({MYSQL_HOST}:3306)/{DB_NAME}?parseTime=true'
   ```

3. Store the admin token in a second `Secret` resource:

   ```bash
   kubectl -n {NAMESPACE} create secret generic upf-sc-admin-token \
     --from-literal=token={ADMIN_TOKEN}
   ```

4. Create a values file named `upf-sc-values.yaml`. The following example deploys two controllers and a gateway pair for two UPG instances:

   ```yaml
   sessionController:
     replicas: 2
     adminTokenSecret: upf-sc-admin-token
     databaseDSNSecret: upf-sc-database-dsn
     config:
       database:
         driver: mysql
       pfcp:
         node_id: upf-sc.example.net
         external:
           transport: nats
           udp: {address: 192.0.2.20:8805}
           nats: {uri: nats://nats:4222, subject_prefix: uplbc.pfcp}
         internal:
           transport: nats
           udp: {address: 192.0.2.21:8805}
           nats: {uri: nats://nats:4222, subject_prefix: uplbc.pfcp}
       load_balancer:
         nats: {uri: nats://nats:4222, kv_bucket: upflb}
         nwis:
           - {name: epc, role: access, external_addr: 198.51.100.20}
           - {name: sgi, role: sgi}
       dynamic_config:
         source: sql
         bootstrap:
           upfs:
             - pfcp_address: 192.0.2.101
               nwis:
                 - {name: epc, role: access, address: 198.51.100.101}
                 - {name: sgi, role: sgi, address: 203.0.113.201}
             - pfcp_address: 192.0.2.102
               nwis:
                 - {name: epc, role: access, address: 198.51.100.102}
                 - {name: sgi, role: sgi, address: 203.0.113.202}
           pools: [{name: subscribers, prefix: 203.0.113.0/25}]
           nwis: [{name: epc, pools: [subscribers]}, {name: sgi, pools: [subscribers]}]
   pfcpGateway:
     config:
       pfcp:
         node_id: upf-sc.example.net
         external:
           transport: nats
           udp: {address: 192.0.2.20:8805, require_local_address: true}
           nats: {uri: nats://nats:4222, subject_prefix: uplbc.pfcp}
         internal:
           transport: nats
           udp: {address: 192.0.2.21:8805, require_local_address: true}
           nats: {uri: nats://nats:4222, subject_prefix: uplbc.pfcp}
     vip:
       enabled: true
       address: 192.0.2.20
       upfAddress: 192.0.2.21
       interface: n4
       virtualRouterID: 82
     instances:
       a:
         vipPriority: 120
         podAnnotations:
           k8s.v1.cni.cncf.io/networks: {N4_NETWORK_A}@n4
       b:
         vipPriority: 110
         podAnnotations:
           k8s.v1.cni.cncf.io/networks: {N4_NETWORK_B}@n4
   ```

5. Adjust the values file to your network:

   - List each UPG instance under **dynamic_config.bootstrap.upfs**.
   - Set **pfcp_address** to the address of the **spec.instances.{NAME}.networking.sx** network of the instance.
   - Use the addresses of the **spec.instances.{NAME}.networking.grx** and **spec.instances.{NAME}.networking.sgi** networks as the `access` and `sgi` addresses.
   - Keep **pfcp.node_id** and the **udp.address** values the same in the controllers and the gateways.
   - Give the gateways the same **nats** settings as the controllers.
   - Set **require_local_address** only in the gateway configuration. The controllers reject it.
   - Set **pfcpGateway.vip.virtualRouterID** to a Virtual Router Redundancy Protocol (VRRP) ID that no other VRRP group on the segment uses.

   UPF-SC uses **dynamic_config.bootstrap** only at the first start, when the database has no saved configuration. To change the UPG instances later, use the `upf-sc` CLI. See [Manage UPG instances](#manage-upg-instances).

6. Install the `upf-sc` chart:

   > [!CAUTION]
   > A newer UPF-SC version upgrades the database schema when it starts. You cannot downgrade after that.

   ```bash
   helm upgrade --install upf-sc {UPF_SC_CHART} \
     --namespace {NAMESPACE} -f upf-sc-values.yaml --wait
   ```

7. Deploy UPF-LB in a `CGWDeployment` resource. In **spec.upflbConfig.spec**, set the following parameters:

   - **nats.bucket** to the **load_balancer.nats.kv_bucket** value
   - **nwis[].ownIP** of the `access` Network Instance (NWI) to the **load_balancer.nwis[].external_addr** value
   - **uePools[]** to the prefixes of **dynamic_config.bootstrap.pools**

   For the other parameters, see [Set up UPF load balancing](https://docs.cennso.com/components/fpp/3.2/deployment-guides/upflb.md).

8. Configure the SMFs to use `192.0.2.20`, the address of **pfcp.external.udp.address**, as the UPF address.

The SMFs associate with UPF-SC, and UPF-SC places new sessions on the UPG instances.

## Verification

To check the deployment, follow these steps:

1. Check that the pods are running:

   ```bash
   kubectl get pods --namespace {NAMESPACE}
   ```

   Sample output:

   ```text
   NAME                          READY   STATUS    RESTARTS   AGE
   upf-sc-0                      1/1     Running   0          1m
   upf-sc-1                      1/1     Running   0          1m
   upf-sc-gateway-a-{SUFFIX}     2/2     Running   0          1m
   upf-sc-gateway-b-{SUFFIX}     2/2     Running   0          1m
   ```

2. Find the active controller. Run the following command for each controller pod:

   ```bash
   kubectl -n {NAMESPACE} exec {POD} -- upf-sc status
   ```

   Sample output of the active controller:

   ```text
   FIELD                   VALUE
   component               controller
   holder                  {HOLDER}
   lease epoch             {LEASE_EPOCH}
   lease held              true
   ...
   ```

   The active controller reports `lease held` as `true`. The other controllers are standby.

3. Check the UPG instances in the active controller pod, `{ACTIVE_POD}`:

   ```bash
   kubectl -n {NAMESPACE} exec {ACTIVE_POD} -- upf-sc get upfs
   ```

   Sample output:

   ```text
   N4           STATE   ASSOCIATED  USABLE  WEIGHT  UPF SESSIONS
   192.0.2.101  active  true        true    1       0
   192.0.2.102  active  true        true    1       0
   ```

   For each UPG instance, `STATE` is `active` and `ASSOCIATED` is `true`.

## Manage UPG instances

Run the `upf-sc` CLI in the active controller pod to change the group of UPG instances. The SMFs do not see these changes. To change an existing instance, get the current revision, `{REVISION}`, with the following command:

```bash
kubectl -n {NAMESPACE} exec {ACTIVE_POD} -- upf-sc config get
```

### Add a UPG instance

To add a UPG instance, run the following command with the addresses of the instance:

```bash
kubectl -n {NAMESPACE} exec {ACTIVE_POD} -- \
  upf-sc create upf {PFCP_ADDRESS} \
  --nwi epc/access={ACCESS_ADDRESS} --nwi sgi/sgi={SGI_ADDRESS} --wait
```

UPF-SC places new sessions on the new instance until all instances hold the same number of sessions.

### Drain a UPG instance

To stop placing sessions on a UPG instance and move its sessions to other instances, run the following command:

```bash
kubectl -n {NAMESPACE} exec {ACTIVE_POD} -- \
  upf-sc drain upf {PFCP_ADDRESS} --revision {REVISION} --wait --timeout 10m
```

UPF-SC moves all sessions of the instance to the other instances. If the other instances have no capacity, the sessions stay on the instance. To place sessions on the instance again, get the new revision with `upf-sc config get`. Then run the following command:

```bash
kubectl -n {NAMESPACE} exec {ACTIVE_POD} -- \
  upf-sc undrain upf {PFCP_ADDRESS} --revision {REVISION} --wait
```

### Remove a UPG instance

> [!CAUTION]
> `upf-sc delete upf` moves all sessions of the instance to other instances and then removes the instance. Make sure that the other instances have capacity for these sessions.

To remove a UPG instance from the group, run the following command:

```bash
kubectl -n {NAMESPACE} exec {ACTIVE_POD} -- \
  upf-sc delete upf {PFCP_ADDRESS} --revision {REVISION} --wait --timeout 10m
```

UPF-SC no longer lists the instance in `upf-sc get upfs`.
