The AlloyDB Autoscaler is an application that automatically scales the number of read replicas of an AlloyDB cluster based on CPU and memory usage metrics.
The following environment variables are essential for the application to function:
GOOGLE_APPLICATION_CREDENTIALS
: Path to the Google Cloud credentials fileGCP_PROJECT
: Google Cloud project IDCLUSTER_NAME
: AlloyDB cluster nameINSTANCE_NAME
: AlloyDB read instance nameREGION
: Region where the AlloyDB cluster is locatedLOG_LEVEL
: Log level for the application (debug, info, warn, error)CPU_THRESHOLD
: CPU usage threshold for scaling (in percentage)MEMORY_THRESHOLD
: Memory usage threshold for scaling (in percentage)CHECK_INTERVAL
: Time interval between checks (in seconds)EVALUATION
: Time window to evaluate checks before scaling up or down (in seconds)MIN_REPLICAS
: Minimum number of replicas allowedMAX_REPLICAS
: Maximum number of replicas allowedTIMEOUT_SECONDS
: GCP API timeout (in seconds)
GOOGLE_APPLICATION_CREDENTIALS=/app/key.json
GCP_PROJECT=my-gcp-project
CLUSTER_NAME=my-alloydb-cluster
INSTANCE_NAME=my-read-instance
REGION=us-central1
LOG_LEVEL=info
CPU_THRESHOLD=70
MEMORY_THRESHOLD=70
CHECK_INTERVAL=60
EVALUATION=300
MIN_REPLICAS=1
MAX_REPLICAS=5
TIMEOUT_SECONDS=120
- Configured Google Cloud credentials file (key.json)
- Access to an existing AlloyDB cluster in Google Cloud
- Configuration of environment variables (via
.env
file for local execution or via Kubernetes deployment for cloud environments)
Since the application is containerized in Docker, it is not necessary to have Go installed locally or the Google Cloud SDK, as these components are included in the Docker image.
- Create a service account in the Google Cloud Console with the required permissions (listed below)
- Generate and download the service account key file (JSON format)
- Rename the downloaded file to
key.json
and place it in the root directory of the project - Make sure the path in
GOOGLE_APPLICATION_CREDENTIALS
environment variable points to this file
The application follows this workflow:
- Reads the environment variables and configures the Google Cloud client
- Checks CPU and memory usage in GCP Cloud Monitoring of the AlloyDB cluster at each specified time interval
- Evaluates all checks in a time window before making scaling decisions
- If CPU or memory usage exceeds the specified threshold, scales up the number of cluster replicas by 1
- If CPU and memory usage is below the threshold and there is more than one replica, reduces the number of replicas by 1 until it reaches the minimum value
IMPORTANT: The rule for scaling down only applies if the current replica count is greater than the minimum replicas setting. If there is only the minimum number of replicas, the application will only consider the possibility of scaling up.
To use the application with Docker:
- Fill in the
.env
file with the necessary environment variables - Place your service account key file as
key.json
in the project root directory - Build the Docker image:
docker build -t alloydb-autoscaler .
- Run the container:
docker run -d --name alloydb-autoscaler \ -v $(pwd)/.env:/app/.env \ -v $(pwd)/key.json:/app/key.json \ alloydb-autoscaler:latest
To deploy the application on Kubernetes:
- Create a ConfigMap for the environment variables:
apiVersion: v1
kind: ConfigMap
metadata:
name: alloydb-autoscaler-config
data:
GCP_PROJECT: "my-gcp-project"
CLUSTER_NAME: "my-alloydb-cluster"
INSTANCE_NAME: "my-read-instance"
REGION: "us-central1"
LOG_LEVEL: "info"
CPU_THRESHOLD: "70"
MEMORY_THRESHOLD: "70"
CHECK_INTERVAL: "60"
EVALUATION: "300"
MIN_REPLICAS: "1"
MAX_REPLICAS: "5"
TIMEOUT_SECONDS: "120"
- Create a Secret for the service account key:
kubectl create secret generic alloydb-sa-key --from-file=key.json=./key.json
- Create a Deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: alloydb-autoscaler
labels:
app: alloydb-autoscaler
spec:
replicas: 1
selector:
matchLabels:
app: alloydb-autoscaler
template:
metadata:
labels:
app: alloydb-autoscaler
spec:
containers:
- name: alloydb-autoscaler
image: [YOUR_REGISTRY]/alloydb-autoscaler:latest
envFrom:
- configMapRef:
name: alloydb-autoscaler-config
env:
- name: GOOGLE_APPLICATION_CREDENTIALS
value: "/var/secrets/google/key.json"
volumeMounts:
- name: google-cloud-key
mountPath: /var/secrets/google
volumes:
- name: google-cloud-key
secret:
secretName: alloydb-sa-key
- Apply the configuration:
kubectl apply -f configmap.yaml
kubectl apply -f deployment.yaml
The Google Cloud service account used by the application needs the following permissions:
alloydb.clusters.get
alloydb.clusters.list
alloydb.instances.get
alloydb.instances.list
alloydb.instances.update
alloydb.locations.get
alloydb.locations.list
alloydb.operations.get
alloydb.operations.list
alloydb.users.get
alloydb.users.list
monitoring.timeSeries.list
Alternatively, you can assign the following predefined roles:
roles/alloydb.admin
roles/monitoring.viewer
For more detailed logs, set LOG_LEVEL=debug
in the .env
file or ConfigMap.
To check logs in Kubernetes:
kubectl logs -f deployment/alloydb-autoscaler
Ensure that the container has internet access to communicate with Google Cloud APIs.
For Kubernetes deployments, verify that the cluster has outbound internet access or appropriate firewall rules.
If you encounter permission errors, verify that the service account has all the necessary permissions listed above.
- "Cannot find credentials": Ensure the key.json file is correctly mounted and the GOOGLE_APPLICATION_CREDENTIALS path is correct.
- "Permission denied": Verify that the service account has all required permissions.
- "Cluster not found": Check that the CLUSTER_NAME and REGION values are correct.
Contributions are welcome! Feel free to open issues or send pull requests.
This project is licensed under the MIT License - see the LICENSE file for details.