使用Terraform搭建VPS集群后,如何实现垂直扩容?
Hey there! Glad to hear your Terraform-managed VPS cluster is running smoothly. Let's break down how to perform vertical scaling (upgrading the instance size) for your 3 DigitalOcean Droplets.
Step 1: Update the Instance Size Configuration
You've defined the swarm_instance_size variable to control your Droplet size, so we'll start here. You have two flexible options to adjust this:
Option 1: Modify the Variable's Default Value
Edit your Terraform variable definition to set the new desired size (e.g., 2gb, 4gb, or any valid DigitalOcean instance size):
variable "swarm_instance_size" { default = "2gb" # Replace with your target instance size }
Option 2: Pass the New Size via Command Line (No Code Change)
If you prefer not to modify your code (for example, a one-off scaling test), use the -var flag when running terraform apply:
terraform apply -var "swarm_instance_size=2gb"
Step 2: Preview the Change (Always Do This First!)
Before making any live changes, run terraform plan to verify exactly what Terraform will do:
terraform plan
You should see output indicating that all 3 digitalocean_droplet.server resources will be updated in-place. This is critical—it means Terraform will resize your existing Droplets instead of destroying and recreating them, so your data and setup stay intact.
Important: While DigitalOcean's resize operation is generally safe, it's smart to take snapshots of your Droplets before scaling, just as a precaution.
Step 3: Execute the Scaling
Once you're comfortable with the plan, run the apply command to trigger the resize:
terraform apply
Confirm the change when prompted, and Terraform will handle resizing all 3 Droplets to the new size. Keep in mind your Droplets may restart during this process, so schedule this during a maintenance window if your application can't tolerate brief downtime.
Key Notes to Keep in Mind
- Region Availability: Ensure your target instance size is available in the
sfc1region you're using. Some sizes are region-specific, so double-check compatibility first. - Billing: Resizing will adjust your billing rate to match the new instance size—review DigitalOcean's pricing for your target size beforehand.
- Partial Scaling: If you only want to resize a subset of Droplets, you'll need to split your resource definition. For example, create a separate
digitalocean_dropletblock usingfor_eachinstead ofcountto specify individual sizes for different instances.
内容的提问来源于stack exchange,提问作者Snowball

