High Availability
Run two or more gateway replicas when the OpenShell control plane must remain available during a gateway pod failure or rolling update. Every replica can serve API requests. The replicas share persistent state through PostgreSQL and relay session-bound requests to the replica connected to each sandbox supervisor.
Gateway replicas do not make PostgreSQL highly available. Use a managed PostgreSQL service or a separately operated PostgreSQL cluster with its own backup and failover plan.
Requirements
An HA gateway deployment requires:
- Two or more schedulable Kubernetes nodes or failure domains.
- PostgreSQL reachable from every gateway pod.
- A Kubernetes Secret in the OpenShell namespace with the PostgreSQL connection
URI in a key named
uri. workload.kind: deployment. The default StatefulSet and per-pod SQLite database are intended for a single gateway replica.- An ingress or load balancer that routes clients to the gateway Service. Refer to Ingress for a Gateway API configuration.
- PostgreSQL connection capacity for every replica, including pods that are starting or terminating during a rollout. Refer to Size PostgreSQL Connections.
The Helm chart rejects replicaCount values above 1 unless
server.externalDbSecret is set. It also rejects a multi-replica StatefulSet
unless workload.allowMultiReplicaStatefulSet is explicitly enabled. Use a
Deployment for the standard HA configuration.
Create the PostgreSQL Secret
Provision PostgreSQL outside the OpenShell chart, then create the connection Secret. The chart does not install PostgreSQL.
Use your PostgreSQL provider’s required TLS parameters in the connection URI. Keep the Secret in the same namespace as the gateway release.
Configure Gateway Replicas
Create values-ha.yaml:
The anti-affinity rule places the two replicas on different nodes. The
app.kubernetes.io/instance value must match the Helm release name. Change the
topology key to a zone label when replicas must span availability zones. A
required rule leaves a replica Pending when the cluster does not have enough
matching failure domains; use preferred anti-affinity when scheduling the
replica is more important than strict separation.
Install or upgrade OpenShell:
The chart shares its retained sandbox JWT signing material, TLS material, and default credential-storage encryption key across the gateway pods. Do not create different copies of those Secrets for individual replicas.
Verify the Deployment
Wait for all gateway replicas to become ready:
Confirm that the client-facing Service and headless peer Service have ready endpoints:
If you changed the chart’s generated names with nameOverride or
fullnameOverride, use the rendered Service names instead.
After registering the gateway with the CLI, verify that requests succeed:
Protect Voluntary Disruptions
Add a PodDisruptionBudget so a voluntary disruption, such as node maintenance, does not evict every ready gateway pod at once:
Apply the manifest separately from the OpenShell chart. Match the namespace and release labels to your installation. A PodDisruptionBudget controls voluntary evictions; it does not protect against an unavailable node or failure domain.
How Peer Routing Works
A sandbox supervisor keeps one active control stream to one gateway replica. That replica records ownership in PostgreSQL. When Kubernetes sends a client request to another replica, the receiving gateway relays exec, forwarding, file-sync, service, and status traffic to the owner through the headless peer Service.
Peer RPCs use a projected, pod-bound ServiceAccount token with the
openshell-gateway-peer audience. The receiving gateway validates the token
with the Kubernetes TokenReview API, verifies the live pod identity and release
labels, and authorizes only peer methods. When gateway TLS is enabled, peer
connections also use the chart’s TLS materials.
You do not need session affinity at the ingress or client-facing Service.
Failure and Rollout Behavior
When a non-owner gateway pod stops, Kubernetes removes it from ready Service endpoints and new requests go to another replica. Existing requests through the stopped pod disconnect and must be retried.
When an owner gateway pod stops gracefully, it re-reads gateway membership and redirects its connected supervisors to the replicas that now own them, including replicas that started moments earlier. After a crash, supervisors reconnect through the gateway Service. Either way, they publish new ownership. Other replicas re-read ownership while waiting to relay a request. A request fails if no fresh, reachable owner appears before its normal deadline.
A stopping owner waits up to five seconds for a redirected supervisor to
publish its new session before it marks the sandbox not ready, so a sandbox
stays Ready through a graceful handoff. Supervisors reconnect with
exponential backoff that restarts after each accepted session, so repeated
gateway restarts do not lengthen later reconnects.
Established streams do not move between gateway replicas. An interrupted exec, forwarding, or service stream must reconnect. File sync retries with a new SSH session instead of resuming the interrupted byte stream. Rolling updates can temporarily concentrate supervisor sessions on the replicas that stayed up; client requests remain routable through peer relay.
Monitor Capacity
Each gateway replica exposes Prometheus metrics on port 9090
(service.metricsPort). Scrape every gateway pod, not the Service, because
these signals describe one replica. Refer to
Gateway Metrics for the full catalog, scrape
configuration, and access control.
These queries assume that Prometheus labels each series with namespace and
pod:
Relay capacity is used on the replica that owns the sandbox’s supervisor session, including relays that other replicas request through peers, so adding replicas does not relieve a busy owner. A relay whose client gave up stays counted for up to about 40 seconds, the 10-second claim timeout plus the 30-second cleanup interval. Routed request counts include retries during rollouts.
Scale the Gateway
Scale the gateway by changing a fixed replica count or by letting a HorizontalPodAutoscaler choose one. In both cases, keep at least two ready replicas when availability must survive one gateway pod failure, and size PostgreSQL and the cluster nodes for the largest replica count.
Scale Manually
Change replicaCount in values-ha.yaml, then apply the release again:
Autoscale with a HorizontalPodAutoscaler
Set autoscaling.enabled to render an autoscaling/v2
HorizontalPodAutoscaler for the gateway Deployment, or for a StatefulSet with
workload.allowMultiReplicaStatefulSet. Add these values to
values-ha.yaml:
With autoscaling enabled, the chart stops setting spec.replicas and ignores
replicaCount. Kubernetes computes CPU and memory utilization against the
container request, so those targets need a matching resources.requests
entry. A resources.limits entry also works, because Kubernetes copies a
limit into an unset request. The chart rejects an autoscaling.maxReplicas
above 1 without server.externalDbSecret. Each scale-down hands the removed
replica’s supervisor sessions to the remaining replicas, so the default behavior removes at most one pod every two minutes
after a five-minute stabilization window. Override it with
autoscaling.behavior, or set autoscaling.behavior.scaleDown to null to
drop the default. New replicas do not take existing sessions.
A scale-down moves every supervisor session on the removed pod at once, and
requests through that pod must be retried. To choose when that happens,
set autoscaling.behavior.scaleDown.selectPolicy to Disabled so the
autoscaler only adds replicas, and remove replicas yourself with
kubectl scale at a quiet time.
Check the autoscaler after you apply the release:
Enabling autoscaling on an existing release removes spec.replicas from the
workload in that upgrade. Kubernetes can reset the workload to one replica
until the HPA scales it back to at least autoscaling.minReplicas, which moves
supervisor sessions from the other gateway pods to the remaining replica. Enable autoscaling
when you install the chart, or during a maintenance window. On an existing
release, upgrade with --reset-then-reuse-values, which needs Helm 3.14 or
later. With an older Helm, save the values with
helm get values <release> -o yaml > values.yaml and upgrade with
--reset-values -f values.yaml. --reuse-values keeps the
previous chart version’s defaults, which lack the autoscaling values,
including the scale-down behavior, when that release predates them. For background, refer to
Migrating Deployments and StatefulSets to horizontal autoscaling.
Disabling autoscaling renders spec.replicas from replicaCount again,
which defaults to 1, so set replicaCount to the replica count you want in
that upgrade.
Autoscale on Custom Metrics
The HPA can also scale on a per-pod metric that a metrics adapter publishes.
The chart passes autoscaling.metrics to the HPA unchanged and does not
install Prometheus or an adapter. A custom metric needs these pieces:
- Prometheus scrapes every gateway pod and labels each series with
namespaceandpod. - A metrics adapter, such as prometheus-adapter, maps those labels to
Kubernetes resources and publishes a per-pod metric through the
custom.metrics.k8s.ioAPI. autoscaling.metricsreferences that metric as aPodsmetric with anAverageValuetarget.
Scale on signals that fall when you add replicas, such as CPU utilization. Do not scale on supervisor sessions, relay utilization, relay claim latency, or peer request rate. New replicas do not take existing sessions, relay load stays on the replica that owns each session, and peer traffic grows with the replica count. Alert on those signals instead.
Replace <metric-name> with the name your adapter publishes, and set
averageValue to the per-pod value at which you want another replica. Check
that the adapter serves the metric for the gateway pods:
Size PostgreSQL Connections
Each gateway pod opens up to 10 PostgreSQL connections on demand. The chart uses the Kubernetes default rolling update strategy for each workload kind, so the number of pods that run during a rollout depends on the kind.
A Deployment rollout adds up to 25 percent of the replica count, rounded up,
as surge pods. Kubernetes does not count terminating pods against that surge,
and each replaced pod can keep its connections throughout its termination
grace period. A rollout can therefore run up to twice the replica count at
once, and an eviction or an autoscaler scale-down during the rollout adds more
terminating pods. Set PostgreSQL max_connections to at least
(2 × replicas + surge) × 10, which leaves one surge of margin for those
pods, plus headroom for your other clients and administration. Use
autoscaling.maxReplicas as the replica count when autoscaling is enabled.
With a Deployment, five or more replicas exceed what the PostgreSQL default of
100 leaves for the gateway, because PostgreSQL reserves 3 connections for
superusers. Raise max_connections or use a larger managed instance. Let one
rollout finish before you start another, because each overlapping rollout adds
its own terminating pods.
If you run several replicas as a StatefulSet with
workload.allowMultiReplicaStatefulSet, a rollout adds no surge pods. It
replaces one pod at a time and creates each replacement only after the old pod
exits. Set max_connections to at least replicas × 10, using
autoscaling.maxReplicas when autoscaling is enabled, plus headroom for your
other clients and administration.
If a connection pooler sits between the gateway and PostgreSQL, use session pooling. The gateway holds session-level advisory locks, which transaction pooling breaks.
Next Steps
- To expose the gateway through a highly available data path, refer to Ingress.
- To configure automatic certificate renewal, refer to Managing Certificates.
- To configure user authentication and authorization, refer to Access Control.
- To scrape and alert on gateway metrics, refer to Gateway Metrics.