# etcd

The `etcd` driver stores shared [cluster state](https://almaforge.com/storage.md#cluster-state) for multiple Auth instances. Use it when your team operates an etcd cluster on VMs or Kubernetes. Choose separate drivers for [audit events](https://almaforge.com/storage.md#audit-events) and [session recordings](https://almaforge.com/storage.md#session-recordings).

| Storage concern                         | Supported |
| --------------------------------------- | --------- |
| Cluster state (`name: state`)           | yes       |
| Audit events (`name: audit-*`)          | no        |
| Session recordings (`name: recordings`) | no        |

## TL;DR[​](#tldr "Direct link to TL;DR")

1. Use etcd only for `name: state`. You still need separate drivers for audit events and session recordings.
2. Pick a required key prefix, then write a `StorageConfig` with `spec.etcd` that points at your etcd client endpoints.
3. Restart the Auth Service and verify with `alma get storageconfig/state`.

## Prerequisites[​](#prerequisites "Direct link to Prerequisites")

Before you start, make sure you have:

* etcd 3.5 or newer.
* A working etcd cluster with peer TLS already configured.
* Client credentials for AlmaForge: either mTLS files or username plus password file.
* A planned key prefix for AlmaForge data. `prefix` is required.

The [etcd security guide](https://etcd.io/docs/v3.5/op-guide/security/) covers the TLS setup procedure, and the [`etcd-io/etcd/hack/tls-setup`](https://github.com/etcd-io/etcd/tree/master/hack/tls-setup) script is a quick way to bootstrap a CA, peer certificates, and client certificates if you do not already have a TLS hierarchy in place.

## Configuration[​](#configuration "Direct link to Configuration")

### Step 1. Choose the key prefix and endpoints[​](#step-1-choose-the-key-prefix-and-endpoints "Direct link to Step 1. Choose the key prefix and endpoints")

Use etcd when you already trust etcd as part of your control-plane stack and want AlmaForge on the same operational footing. That is common in Kubernetes-heavy environments because etcd already sits behind the Kubernetes API server.

Plan the `prefix` before the first start. There is no default, and AlmaForge refuses to start without one.

### Step 2. Write the state backend resource[​](#step-2-write-the-state-backend-resource "Direct link to Step 2. Write the state backend resource")

For mTLS authentication:

[`mtls.yaml`](https://almaforge.com/examples/storage/etcd/mtls.yaml):

examples/storage/etcd/mtls.yaml

```yaml
# Shared etcd state backend authenticated with a client certificate.

apiVersion: almaforge.com/v1

kind: StorageConfig

metadata:

  name: state

spec:

  etcd:

    peers:

      - https://etcd-1.example.com:2379

      - https://etcd-2.example.com:2379

      - https://etcd-3.example.com:2379

    prefix: /almaforge/

    tlsCAFile: /etc/almaforge/etcd/ca.pem

    tlsCertFile: /etc/almaforge/etcd/client.pem

    tlsKeyFile: /etc/almaforge/etcd/client-key.pem
```

For password authentication instead of mTLS:

[`password.yaml`](https://almaforge.com/examples/storage/etcd/password.yaml):

examples/storage/etcd/password.yaml

```yaml
# Shared etcd state backend authenticated with a password file.

apiVersion: almaforge.com/v1

kind: StorageConfig

metadata:

  name: state

spec:

  etcd:

    peers:

      - https://etcd-1.example.com:2379

    prefix: /almaforge/

    tlsCAFile: /etc/almaforge/etcd/ca.pem

    username: almaforge

    passwordFile: /etc/almaforge/etcd/password
```

| Field                   | Required                    | Default             | Notes                                                                                |
| ----------------------- | --------------------------- | ------------------- | ------------------------------------------------------------------------------------ |
| `peers`                 | yes                         | none                | One or more etcd client endpoints.                                                   |
| `prefix`                | yes                         | none                | Key prefix for AlmaForge data. No default. AlmaForge refuses to start without it.    |
| `tlsCAFile`             | yes unless `insecure: true` | none                | CA bundle used to verify the etcd server.                                            |
| `tlsCertFile`           | no                          | none                | Client certificate for mTLS auth.                                                    |
| `tlsKeyFile`            | no                          | none                | Client key for mTLS auth.                                                            |
| `username`              | no                          | none                | Username for password auth when not using mTLS.                                      |
| `passwordFile`          | no                          | none                | Path to a file containing the etcd password. Newlines are trimmed on read.           |
| `insecure`              | no                          | `false`             | Disables normal TLS validation requirements. Do not use this in production.          |
| `dialTimeout`           | no                          | `30s`               | Connection timeout.                                                                  |
| `bufferSize`            | no                          | `1024`              | Event buffer capacity.                                                               |
| `maxClientMsgSizeBytes` | no                          | etcd client default | Raise only when message size errors force it. Keep it in sync with the server limit. |
| `clientPoolSize`        | no                          | `3`                 | Number of concurrent etcd clients AlmaForge uses.                                    |

### Step 3. Restart and verify[​](#step-3-restart-and-verify "Direct link to Step 3. Restart and verify")

Restart the Auth Service after updating the bootstrap file, then verify the active resource:

******Terminal

```bash
sudo systemctl restart almad
alma get storageconfig/state -o yaml
```

The backend is active when the command shows the intended endpoints and prefix and all Auth instances are ready. See [Monitoring](https://almaforge.com/monitoring.md) for readiness checks.

### URI form[​](#uri-form "Direct link to URI form")

Equivalent URI string, suitable for an environment variable or CLI flag. Peer endpoints are comma-separated in the URI host, or in a `peers=` query parameter for additional peers. Other fields go in the query string:

text

```text
etcd://etcd-1.example.com:2379,etcd-2.example.com:2379,etcd-3.example.com:2379?prefix=/almaforge&tls_ca_file=/etc/almaforge/etcd-ca.pem&tls_key_file=/etc/almaforge/etcd-client-key.pem&tls_cert_file=/etc/almaforge/etcd-client-cert.pem&dial_timeout=30s
```

Canonical query keys are snake\_case (`tls_ca_file`, `tls_key_file`, `tls_cert_file`, `dial_timeout`, `password_file`). The typed JSON-tag form (`tlsCAFile`, etc.) is accepted as an alias.

## Troubleshooting[​](#troubleshooting "Direct link to Troubleshooting")

### AlmaForge refuses to start without `prefix`[​](#almaforge-refuses-to-start-without-prefix "Direct link to almaforge-refuses-to-start-without-prefix")

That is expected. `prefix` is required and has no default.

### etcd reports gRPC message size errors[​](#etcd-reports-grpc-message-size-errors "Direct link to etcd reports gRPC message size errors")

Raise `maxClientMsgSizeBytes` on AlmaForge and `--max-request-bytes` on the etcd servers together. Keep the numbers aligned.

### You want to store audit events or recordings in etcd[​](#you-want-to-store-audit-events-or-recordings-in-etcd "Direct link to You want to store audit events or recordings in etcd")

Do not. etcd is state only. Plan separate drivers for [audit events](https://almaforge.com/storage.md#audit-events) and [session recordings](https://almaforge.com/storage.md#session-recordings).

## Operations[​](#operations "Direct link to Operations")

### Cluster sizing[​](#cluster-sizing "Direct link to Cluster sizing")

Use three etcd members for a quorum that tolerates one member loss. Five members tolerate two member losses. Two members require both to remain available and provide no member-failure tolerance. See [High availability](https://almaforge.com/high-availability.md#how-replicas-coordinate).

### Hardware[​](#hardware "Direct link to Hardware")

Follow the [etcd operational guide](https://etcd.io/docs/v3.5/op-guide/hardware/). SSD or NVMe storage is the safe default.

### Driver scope[​](#driver-scope "Direct link to Driver scope")

etcd only covers cluster state. You still need separate drivers for audit events and session recordings.

## References[​](#references "Direct link to References")

* [Storage backends overview](https://almaforge.com/storage.md).
* [High availability](https://almaforge.com/deployment/kubernetes.md).
* [Break-glass access](https://almaforge.com/break-glass.md).
* [etcd security guide](https://etcd.io/docs/v3.5/op-guide/security/).
* [etcd operational guide](https://etcd.io/docs/v3.5/op-guide/hardware/).
