This guide covers scaling operations for SGShardedCluster, including horizontal scaling (adding shards or replicas) and vertical scaling (changing resources).
SGShardedCluster supports multiple scaling dimensions:
| Dimension | Component | Configuration |
|---|---|---|
| Horizontal - Shards | Number of shard clusters | spec.shards.clusters |
| Horizontal - Replicas | Replicas per shard | spec.shards.instancesPerCluster |
| Horizontal - Coordinators | Coordinator instances | spec.coordinator.instances |
| Vertical | CPU/Memory | spec.coordinator/shards.sgInstanceProfile |
To add more shard clusters, increase the clusters value:
apiVersion: stackgres.io/v1alpha1
kind: SGShardedCluster
metadata:
name: my-sharded-cluster
spec:
shards:
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":{"shards":{"clusters":5}}}'
After adding shards, 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 shard for better read scalability:
spec:
shards:
clusters: 3
instancesPerCluster: 3 # Increased from 2 to 3
Or patch:
kubectl patch sgshardedcluster my-sharded-cluster --type merge \
-p '{"spec":{"shards":{"instancesPerCluster":3}}}'
sync vs async) for consistency requirementsScale coordinator instances for high availability:
spec:
coordinator:
instances: 3 # Increased from 2 to 3
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
shards:
sgInstanceProfile: large-profile
spec:
coordinator:
sgInstanceProfile: coordinator-profile # Smaller, query routing
shards:
sgInstanceProfile: shard-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
shards:
autoscaling:
mode: horizontal
horizontal:
minInstances: 1
maxInstances: 3
Enable CPU/memory recommendations:
spec:
coordinator:
autoscaling:
mode: vertical
vertical:
# VPA will recommend resource adjustments
shards:
autoscaling:
mode: vertical
Reducing the number of shards requires data migration:
apiVersion: stackgres.io/v1
kind: SGShardedDbOps
metadata:
name: drain-shards
spec:
sgShardedCluster: my-sharded-cluster
op: resharding
resharding:
citus:
drainOnly: true
kubectl patch sgshardedcluster my-sharded-cluster --type merge \
-p '{"spec":{"shards":{"clusters":3}}}'
Reducing replicas is straightforward:
kubectl patch sgshardedcluster my-sharded-cluster --type merge \
-p '{"spec":{"shards":{"instancesPerCluster":1}}}'
# View overall status
kubectl get sgshardedcluster my-sharded-cluster
# Check individual shard 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