This guide covers scaling operations for SGShardedCluster, including horizontal scaling (adding workers or replicas) and vertical scaling (changing resources).
SGShardedCluster supports multiple scaling dimensions:
| Dimension | Component | Configuration |
|---|---|---|
| Horizontal - Workers | Number of worker clusters | spec.workers.clusters |
| Horizontal - Replicas | Replicas per worker | spec.workers.instancesPerCluster |
| Horizontal - Coordinators | Coordinator instances | spec.coordinator.instances |
| Horizontal - Query routers (Citus only) | Number of query router clusters | spec.coordinator.queryRouterClusters |
| Vertical | CPU/Memory | spec.coordinator/workers.sgInstanceProfile |
To add more worker clusters, increase the clusters value:
apiVersion: stackgres.io/v1beta1
kind: SGShardedCluster
metadata:
name: my-sharded-cluster
spec:
workers:
clusters: 5 # Increased from 3 to 5
instancesPerCluster: 2
pods:
persistentVolume:
size: 50Gi
Apply the change:
kubectl apply -f sgshardedcluster.yaml
Or patch directly:
kubectl patch sgshardedcluster my-sharded-cluster --type merge \
-p '{"spec":{"workers":{"clusters":5}}}'
After adding workers, use SGShardedDbOps to rebalance data:
apiVersion: stackgres.io/v1
kind: SGShardedDbOps
metadata:
name: rebalance-after-scale
spec:
sgShardedCluster: my-sharded-cluster
op: resharding
resharding:
citus:
threshold: 0.1 # Rebalance if utilization differs by 10%
To increase replicas per worker for better read scalability:
spec:
workers:
clusters: 3
instancesPerCluster: 3 # Increased from 2 to 3
Or patch:
kubectl patch sgshardedcluster my-sharded-cluster --type merge \
-p '{"spec":{"workers":{"instancesPerCluster":3}}}'
sync vs async) for consistency requirementsScale coordinator instances for high availability:
spec:
coordinator:
instances: 3 # Increased from 2 to 3
For Citus sharded clusters, you can horizontally scale the number of read/write entrypoints by adding query router SGClusters. Query routers do not store sharded data — they route queries to the workers — so they can be added or removed without resharding.
Add or remove query routers by changing spec.coordinator.queryRouterClusters:
spec:
coordinator:
instances: 2
queryRouterClusters: 3 # Increased from 1 to 3
Or patch directly:
kubectl patch sgshardedcluster my-sharded-cluster --type merge \
-p '{"spec":{"coordinator":{"queryRouterClusters":3}}}'
Each new query router is created as a single-instance SGCluster named <cluster-name>-router<index> (or following the configured queryRouterClusterNameTemplate). The operator registers it in the Citus topology with shouldhaveshards set to false so the rebalancer never assigns workers to it.
For configuration details and connection guidance see Query Routers.
First, create an SGInstanceProfile with desired resources:
apiVersion: stackgres.io/v1
kind: SGInstanceProfile
metadata:
name: large-profile
spec:
cpu: "4"
memory: "16Gi"
Then reference it in the sharded cluster:
spec:
coordinator:
sgInstanceProfile: large-profile
workers:
sgInstanceProfile: large-profile
spec:
coordinator:
sgInstanceProfile: coordinator-profile # Smaller, query routing
workers:
sgInstanceProfile: worker-profile # Larger, data storage
Vertical scaling requires a restart. Use SGShardedDbOps for controlled rolling restart:
apiVersion: stackgres.io/v1
kind: SGShardedDbOps
metadata:
name: apply-new-profile
spec:
sgShardedCluster: my-sharded-cluster
op: restart
restart:
method: ReducedImpact
onlyPendingRestart: true
SGShardedCluster supports automatic scaling based on metrics.
Enable connection-based horizontal scaling:
spec:
coordinator:
autoscaling:
mode: horizontal
horizontal:
minInstances: 2
maxInstances: 5
# Scale based on active connections
cooldownPeriod: 300
pollingInterval: 30
workers:
autoscaling:
mode: horizontal
horizontal:
minInstances: 1
maxInstances: 3
Enable CPU/memory recommendations:
spec:
coordinator:
autoscaling:
mode: vertical
vertical:
# VPA will recommend resource adjustments
workers:
autoscaling:
mode: vertical
Reducing the number of workers requires data migration:
apiVersion: stackgres.io/v1
kind: SGShardedDbOps
metadata:
name: drain-workers
spec:
sgShardedCluster: my-sharded-cluster
op: resharding
resharding:
citus:
drainOnly: true
kubectl patch sgshardedcluster my-sharded-cluster --type merge \
-p '{"spec":{"workers":{"clusters":3}}}'
Reducing replicas is straightforward:
kubectl patch sgshardedcluster my-sharded-cluster --type merge \
-p '{"spec":{"workers":{"instancesPerCluster":1}}}'
# View overall status
kubectl get sgshardedcluster my-sharded-cluster
# Check individual worker clusters
kubectl get sgcluster -l stackgres.io/shardedcluster-name=my-sharded-cluster
# View pods
kubectl get pods -l stackgres.io/shardedcluster-name=my-sharded-cluster
kubectl get sgshardeddbops rebalance-after-scale -o yaml