通过Terraform管理负载均衡服务
更新时间:
本文介绍如何使用Terraform创建一个负载均衡实例,并为其添加监听。
说明
本教程所含示例代码支持一键运行,您可以直接运行代码。一键运行
前提条件
在开始之前,请您确保完成以下操作:
使用Terraform,您需要一个阿里云账号和访问密钥(AccessKey)。 请在阿里云控制台中的AccessKey管理页面上创建和查看您的AccessKey。
已经安装并配置了Terraform,具体操作请参见在本地安装和配置Terraform和在Cloud Shell中使用Terraform。
背景信息
本文以创建一个名为slb_worder的公网实例,并为它配置TCP、UDP和HTTP监听为例。
操作步骤
创建一个负载均衡实例。
创建terraform.tf文件,输入以下内容,并保存在当前的执行目录中。
说明需要为每个Terraform项目创建一个独立的执行目录。在初始化配置之前至少需要有1个.tf文件。
resource "alicloud_slb_load_balancer" "instance" { load_balancer_name = "slb_worder" load_balancer_spec = "slb.s3.small" internet_charge_type = "PayByTraffic" address_type = "internet" }
运行terraform apply开始创建。出现类似下面的日志,说明创建成功。
alicloud_slb_load_balancer.instance: Creating... alicloud_slb_load_balancer.instance: Still creating... [10s elapsed] alicloud_slb_load_balancer.instance: Creation complete after 14s [id=lb-gw89kh14ra0htarr1ub8n] ...... Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
运行terraform show查看已创建的负载均衡实例。
添加监听。以下示例添加了TCP、UDP和HTTP三种协议的监听。
创建listener.tf文件,输入以下内容,并保存在当前的执行目录中。
说明在该目录下所有*.tf 文件都会被terraform自动加载。因此,用户可以按照实际用途将配置信息写入不同的文件中。
resource "alicloud_slb_listener" "tcp" { load_balancer_id = alicloud_slb_load_balancer.instance.id backend_port = "22" frontend_port = "22" protocol = "tcp" bandwidth = "10" health_check_type = "tcp" persistence_timeout = 3600 healthy_threshold = 8 unhealthy_threshold = 8 health_check_timeout = 8 health_check_interval = 5 health_check_http_code = "http_2xx" health_check_connect_port = 20 health_check_uri = "/console" established_timeout = 600 } resource "alicloud_slb_listener" "udp" { load_balancer_id = alicloud_slb_load_balancer.instance.id backend_port = 2001 frontend_port = 2001 protocol = "udp" bandwidth = 10 persistence_timeout = 3600 healthy_threshold = 8 unhealthy_threshold = 8 health_check_timeout = 8 health_check_interval = 4 health_check_connect_port = 20 } resource "alicloud_slb_listener" "http" { load_balancer_id = alicloud_slb_load_balancer.instance.id backend_port = 80 frontend_port = 80 protocol = "http" sticky_session = "on" sticky_session_type = "insert" cookie = "testslblistenercookie" cookie_timeout = 86400 health_check = "on" health_check_uri = "/cons" health_check_connect_port = 20 healthy_threshold = 8 unhealthy_threshold = 8 health_check_timeout = 8 health_check_interval = 5 health_check_http_code = "http_2xx,http_3xx" bandwidth = 10 request_timeout = 80 idle_timeout = 30 }
运行terraform apply开始创建资源。出现类似下面的日志,说明创建成功。
alicloud_slb_listener.udp: Creating... alicloud_slb_listener.http: Creating... alicloud_slb_listener.tcp: Creating... alicloud_slb_listener.http: Creation complete after 5s [id=lb-gw89kh14ra0htarr1ub8n:http:80] ...... Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
运行terraform show查看已创建的监听。
操作样例
说明
当前示例代码支持一键运行,您可以直接运行代码。一键运行
resource "alicloud_slb_load_balancer" "instance" {
load_balancer_name = "slb_worder"
load_balancer_spec = "slb.s3.small"
internet_charge_type = "PayByTraffic"
address_type = "internet"
}
resource "alicloud_slb_listener" "tcp" {
load_balancer_id = alicloud_slb_load_balancer.instance.id
backend_port = "22"
frontend_port = "22"
protocol = "tcp"
bandwidth = "10"
health_check_type = "tcp"
persistence_timeout = 3600
healthy_threshold = 8
unhealthy_threshold = 8
health_check_timeout = 8
health_check_interval = 5
health_check_http_code = "http_2xx"
health_check_connect_port = 20
health_check_uri = "/console"
established_timeout = 600
}
resource "alicloud_slb_listener" "udp" {
load_balancer_id = alicloud_slb_load_balancer.instance.id
backend_port = 2001
frontend_port = 2001
protocol = "udp"
bandwidth = 10
persistence_timeout = 3600
healthy_threshold = 8
unhealthy_threshold = 8
health_check_timeout = 8
health_check_interval = 4
health_check_connect_port = 20
}
resource "alicloud_slb_listener" "http" {
load_balancer_id = alicloud_slb_load_balancer.instance.id
backend_port = 80
frontend_port = 80
protocol = "http"
sticky_session = "on"
sticky_session_type = "insert"
cookie = "testslblistenercookie"
cookie_timeout = 86400
health_check = "on"
health_check_uri = "/cons"
health_check_connect_port = 20
healthy_threshold = 8
unhealthy_threshold = 8
health_check_timeout = 8
health_check_interval = 5
health_check_http_code = "http_2xx,http_3xx"
bandwidth = 10
request_timeout = 80
idle_timeout = 30
}
文档内容是否对您有帮助?