Setting Up Load Balancers Using Terraform

简介: In this tutorial, we will learn how to set up Bolt on Alibaba Cloud ECS using Terraform load balancers and ApsaraDB for RDS.

By Alberto Roura, Alibaba Cloud Tech Share Author

In this tutorial, I will show you how to set up a CMS, in this case Bolt, on Alibaba Cloud using a Load Balancer and RDS with 3 ECS instances attached. We will be doing this based on a DevOps approach using Terraform and the official Alibaba Cloud (Alicloud) provider.

If you heard of the term "Load Balancer" but don't have a clear idea of the concept, sit tight, as I'm going to develop (pun intended) it a bit more.

What is Load Balancing?

Load balancing is a means to distribute workload across different resources. Let's say you own a very busy website; having a single server dealing with all queries will overload it. Instead, you can have an additional server to help cope with the requests.

The most common approach is to clone the web hosting server and put it behind a load balancer. The load balancer is just another server that distributes the load, sending the request from visitor to one server or another. Using load balancers also increases redundancy, so it's also handy to keep the data safe.

How Does a Load Balancer Distribute Load?

There are different scheduling methods to do it, and the most popular is Round Robin (RR), as it is very simple and effective. Another way to do it is using a similar approach called Weighted Round Robin (WRR), which is a fine-tuned version of RR.

Round-Robin Balancing (RR)

You might have heard the term Round Robin from sporting events, such as soccer tournaments. This technique name comes the original term meaning "signing petitions in circular order so that the leaders could not be identified". This leads to the current meaning in computing terms, where the load balancer rotates the attached servers, one at a time.

The biggest advantage is its simplicity. Load is also distributed evenly across all servers in a network. RR has one bad downside, however, as this algorithm doesn't care how different are servers between them and their capacity. That's why there is another version of this called Weighted Round-Robin.

Weighted Round-Robin (WRR)

This algorithm is based in the standard Round-Robin but with the difference of "having in mind" how different the resources are. In WRR, the resources are given priorities (weight) in the queue based on the capacity. For example, a 100GB server would be given a larger weight over a 20GB server. This approach gives the network admin more control in which servers should be used first and which ones later. WRR is better than RR for complex networks, such as in a hybrid cloud environment.

Weighted Least-Connections (WLC)

Similar to WRR, WLC is an approach that assigns different weights to the servers in a network. But unlike RR and WRR, WLC is dynamic. This scheduling algorithm sends the requests to the server with least active connections in a weighted resource list. This is handy when, apart from assigning a performance weight to each server, you want to control how busy, network-wise, a resource can get. The downside of this approach is that it requires more computations for it to work effectively.

Setting Up Terraform

With the Alibaba Cloud (Alicloud) official terraform provider we can choose between Weighted Round-Robin (WRR) and Weighted Least-Connections (WLC). It is completely up to you which one you use. In the example I provide, I have used WRR, but with no specific reasons.

Install Terraform

Terraform is very easy to install using Homebrew. If you do not have Homebrew already installed on your computer, please find install instructions here.

Run brew install terrafrom the below command in your terminal to install Terraform.

To verify Terraform installation type terraform version.

Install Alibaba Cloud Official provider

As Hashicorp is not actively updating the provider for us, Alibaba Cloud has a really good and active developed GitHub repository of its official provider, which is the one you should get and install. Go to the releases tab and get the latest one for your platform.

After downloading it, you should place the binary file in the plugins folder of terraform. On Windows, in the terraform.d/plugins beneath your user's "Application Data" directory. On all other systems, as Linux or Mac, in ~/.terraform.d/plugins in your user's home directory. Its also a good practice to version the binary, so you should rename it to terraform-provider-alicloud_v1.8.2, given that the version you downloaded is the 1.8.2, you should change that depending in which one you get.

Get Alibaba Cloud Access Keys

Once you log into your Alibaba Cloud console, go to the top Menu and click “accesskeys” located directly under your email address.

1

Once in the keys screen, copy the Access Key ID and the Access Key Secret into a safe place. To show the Secret Key to need to click on “Show“. Be careful where you save this data, as it is very sensitive. Also you should consider creating more limited keys using their policies.

Prepare the Terraform File

For this example, we will put all the config in one single file, but you are recommended to separate the different parts of the config in their own .tf files. This is a good practice that improves the maintainability and readability over time.

Having that clear, lets create a folder, and inside that folder a file called main.tf that we will edit in the next step.

main.tf

provider "alicloud" {
  access_key = "KEY"
  secret_key = "SECRET"
  region = "ap-southeast-2"
}

variable "vswitch" {
  type = "string"
  default = "vsw-xxxxxxxxx"
}

variable "sgroups" {
  type = "list"
  default = [
    "sg-xxxxxxxxxxx"
  ]
}

variable "app_name" {
  type = "string"
  default = "my_app"
}

variable "ecs_password" {
  type = "string"
  default = "Test1234!"
}

resource "alicloud_db_instance" "default" {
  engine = "MySQL"
  engine_version = "5.6"
  instance_type = "rds.mysql.t1.small"
  instance_storage = 5
  vswitch_id = "${var.vswitch}"
  security_ips = [
    "0.0.0.0/0"
  ]
}

resource "alicloud_db_database" "default" {
  instance_id = "${alicloud_db_instance.default.id}"
  name = "bolt_site"
  character_set = "utf8"
}

resource "alicloud_db_account" "default" {
  instance_id = "${alicloud_db_instance.default.id}"
  name = "bolt_user"
  password = "boltdb1234"
}

resource "alicloud_db_account_privilege" "default" {
  instance_id = "${alicloud_db_instance.default.id}"
  account_name = "${alicloud_db_account.default.name}"
  privilege = "ReadWrite"
  db_names = [
    "${alicloud_db_database.default.name}"
  ]
}

resource "alicloud_db_connection" "default" {
  instance_id = "${alicloud_db_instance.default.id}"
  connection_prefix = "bolt-app"
  port = "3306"
}

data "template_file" "user_data" {
  template = "${file("user-data.sh")}"
}

data "alicloud_images" "default" {
  name_regex = "^ubuntu_16.*_64"
}

data "alicloud_instance_types" "default" {
  instance_type_family = "ecs.xn4"
  cpu_core_count = 1
  memory_size = 1
}

resource "alicloud_instance" "app" {
  count = 3
  instance_name = "${var.app_name}-${count.index}"
  image_id = "${data.alicloud_images.default.images.0.image_id}"
  instance_type = "${data.alicloud_instance_types.default.instance_types.0.id}"

  vswitch_id = "${var.vswitch}"
  security_groups = "${var.sgroups}"
  internet_max_bandwidth_out = 100

  password = "${var.ecs_password}"

  user_data = "${data.template_file.user_data.template}"
}

resource "alicloud_slb" "default" {
  name = "${var.app_name}-slb"
  vswitch_id = "${var.vswitch}"
  internet = true
}

resource "alicloud_slb_listener" "http" {
  load_balancer_id = "${alicloud_slb.default.id}"
  backend_port = 80
  frontend_port = 80
  health_check_connect_port = 80
  bandwidth = -1
  protocol = "http"
  sticky_session = "on"
  sticky_session_type = "insert"
  cookie = "testslblistenercookie"
  cookie_timeout = 86400
}

resource "alicloud_slb_attachment" "default" {
  load_balancer_id = "${alicloud_slb.default.id}"
  instance_ids = [
    "${alicloud_instance.app.*.id}",
  ]
}

output "app_id" {
  value = "${alicloud_instance.app.*.public_ip}"
}


output "slb_ip" {
  value = "${alicloud_slb.default.address}"
}

output "rds_host" {
  value = "${alicloud_db_instance.default.connection_string}"
}

user-data.sh

In this example, we are going to rely in the cloud-init program that comes bundled in Ubuntu and runs whatever script to pass at the moment of resource creation. In this case, the script is going to install the needed software packages tu run Docker containers and to connect the app to the proper database. The file user-data.sh needs to be in the same path next to our main.tf. Be aware of the MYSQL_HOST variable, you'll need to adjust that to fit your database instance internet host.

#!/bin/bash -v

export MYSQL_HOST=bolt-app.mysql.australia.rds.aliyuncs.com

apt-get update && apt-get install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
apt-get update && apt-get install -y docker-ce docker-compose
curl -L https://github.com/docker/compose/releases/download/1.20.1/docker-compose-`uname -s`-`uname -m` -o /usr/bin/docker-compose

cd ~/
curl https://raw.githubusercontent.com/roura356a/bolt/master/with-mysql/docker-compose.yml -o docker-compose.yml
sed -i "s/=db/=$MYSQL_HOST/g" docker-compose.yml

docker-compose up -d    

Launch Terraform

terraform init

We are ready to take off! Type the init command for Terraform to get the project ready to apply.

terraform init

terraform plan

In order to verify that everything is fine, it is good practice to run the plan command, so you can get an overview of the job without actually applying it.

terraform plan

terraform apply

Seatbelts on. We are now deploying our machine! Run the apply command and wait until it finishes. It will take about 3 to 5 minutes depending on the Internet connection and the conditions of the datacenter you are connecting to.

terraform apply

After the job finishes, you will get a message in the terminal confirming the IP address of your new ECS instances, RDS host and Load Balancer IP:

Apply complete! Resources: 11 added, 0 changed, 0 destroyed.

Outputs:

app_id = [
    xx.xx.xx.xx,
    xx.xx.xx.xx,
    xx.xx.xx.xx
]
rds_host = rm-xxxxxxxxxxx.mysql.australia.rds.aliyuncs.com
slb_ip = xx.xx.xx.xxx

If the security group you selected has the port 80 opened, you can now type the IP of the balancer in your browser and see how Bolt web-based installation comes up to customize your new website.

Conclusion

You have now successfully set up Bolt on Alibaba Cloud ECS using Load Balancers and RDS. Go and do something fun with them! Because this is a fresh Bolt installation, you'll need to manually visit one of the instances (not the slb) and create the first user, so this way the health check will pass in the 3 backend servers. After that, visiting the SLB IP directly or thought a domain should be enough to make it work.

Enjoy you newly-created SLB-backed Bolt web application!

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
目录
相关文章
|
7月前
|
传感器 人工智能 自然语言处理
2026 AI 元年:人工智能从工具属性迈向原生智能的历史拐点
2026 年之所以被定义为 AI 元年,并非因为某一款模型的参数规模突破,而是因为人工智能首次完成了从“工具系统”向“原生智能系统”的整体跃迁。
737 12
|
11月前
|
自然语言处理 搜索推荐 Java
Spring i18n:@LocaleResolver 和 @RequestToViewName 指南
国际化是设计支持多语言和区域适配的应用程序的关键,有助于扩大市场覆盖、提升用户体验。Spring 提供了丰富的内置支持,如 `MessageSource`、`LocaleResolver` 和 `RequestToViewNameTranslator`,帮助开发者高效实现多语言切换和区域设置管理。通过结合 `@LocaleResolver` 识别用户语言环境,并配合 `@RequestToViewNameTranslator` 动态渲染视图,可构建高度本地化、灵活且用户友好的全球应用。
390 2
Spring i18n:@LocaleResolver 和 @RequestToViewName 指南
|
6月前
|
人工智能 自然语言处理 小程序
给AI拜年差点翻车后,我悟了:RAG和微调,到底谁更懂“人情世故”?
大家好,我是AI伙伴狸猫算君!本文以“AI写春节祝福”为切入点,深入剖析RAG与微调的技术差异:RAG依赖检索拼凑,难捕获独特人情;微调则通过高质量关系感知数据,将“称呼、细节、风格”内化为模型本能。手把手演示30分钟用LLaMA-Factory完成Qwen3微调,让祝福真正有温度、有梗、有你。
405 13
|
6月前
|
人工智能 自然语言处理 运维
企业建设智能客服系统要多少钱(2026年2月最新)
2026年智能客服成企业数字化标配,全球市场规模将超680亿美元。建设成本因部署模式(SaaS/专属云/私有化)、对话复杂度、渠道覆盖、系统集成、并发量及合规要求而异,年投入从2万元到150万元不等。科学规划预算、聚焦业务价值,方能实现高效ROI。(239字)
|
7月前
|
人工智能 安全 搜索推荐
2.62亿美元的警钟:AI驱动下的账户接管风暴席卷全球,中国如何筑牢反钓鱼防线?
2025年,AI驱动的账户接管攻击激增,全球损失超2.62亿美元。钓鱼攻击从“广撒网”转向精准“狙击”,传统短信验证码防线濒临失效。中国面临仿冒链接、SIM劫持等威胁,亟需推动密码less认证、行为生物识别与零信任架构,构建全民参与的反钓鱼防线。
449 3
|
JSON API 数据格式
淘宝商品评论API接口,json数据示例参考
淘宝开放平台提供了多种API接口来获取商品评论数据,其中taobao.item.reviews.get是一个常用的接口,用于获取指定商品的评论信息。以下是关于该接口的详细介绍和使用方法:
|
7月前
|
存储 人工智能 安全
AI智能体的开发费用
2026年AI智能体开发成本两极分化:低代码工具使简单Agent成本降至5千-2万元,而复杂企业级系统仍需30万以上。费用涵盖开发、API、算力及维护,建议从中级智能体切入,结合开源平台与工作流模式降本增效。#AI智能体 #降本策略
|
8月前
|
自然语言处理 算法 数据可视化
DeepInsight x ChatBI:“智能歧义识别+知识沉淀”,化解模糊查询
本文针对自然语言数据分析中的语义歧义问题,提出“智能澄清-知识沉淀-动态召回”闭环方案,通过精准识别、最少提问、结构化留存用户意图,实现一次澄清、长期复用,显著提升查询效率与体验一致性。
|
消息中间件 缓存 弹性计算
纯PHP+MySQL手搓高性能论坛系统!代码精简,拒绝臃肿
本内容分享了一套经实战验证的社交系统架构设计,支撑从1到100万用户的发展,并历经6次流量洪峰考验。架构涵盖客户端层(App、小程序、公众号)、接入层(API网关、负载均衡、CDN)、业务服务层(用户、内容、关系、消息等服务)、数据层(MySQL、Redis、MongoDB等)及运维监控层(日志、监控、告警)。核心设计包括数据库分库分表、多级缓存体系、消息队列削峰填谷、CQRS模式与热点数据动态缓存。同时提供应对流量洪峰的弹性伸缩方案及降级熔断机制,并通过Prometheus实现全链路监控。开源建议结构清晰,适合大型社交平台构建与优化。
576 11
|
数据可视化 流计算 Python
Python创意爱心代码大全:从入门到高级的7种实现方式
本文分享了7种用Python实现爱心效果的方法,从简单的字符画到复杂的3D动画,涵盖多种技术和库。内容包括:基础字符爱心(一行代码实现)、Turtle动态绘图、Matplotlib数学函数绘图、3D旋转爱心、Pygame跳动动画、ASCII艺术终端显示以及Tkinter交互式GUI应用。每种方法各具特色,适合不同技术水平的读者学习和实践,是表达创意与心意的绝佳工具。
10409 0

热门文章

最新文章