通過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
}
文檔內容是否對您有幫助?