跳转到内容
搜索文档

4 – 提高性能

最后更新 查看 MarkdownAgent 设置

在本教程中,您将添加第二个源服务器进行一些基本的轮询,然后使用 Cloudflare 负载平衡 (Load Balancing) 产品在需要时对流量进行故障转移。您还将通过使用“地理位置引导 (geo steering)”来增强您的负载平衡配置,以便从地理位置最接近最终用户的源服务器提供结果。

先决条件

1. 为 www 添加另一条 DNS 记录

创建一个新分支并为您的亚洲服务器添加一条 DNS 记录:

git checkout -b step4-configure-load-balancing

为位于亚洲的第二台 Web 服务器添加一条 DNS 记录。作为示例,此服务器的 IP 地址为 198.51.100.15。将第二条 DNS 记录添加到您的 main.tf 中:

# 亚洲源服务器
resource "cloudflare_dns_record" "www_asia" {
  zone_id = var.zone_id
  name    = "www"
  content = "198.51.100.15"
  type    = "A"
  ttl     = 300
  proxied = true
  comment = "Asia origin server"
}

应用此更改以查看基本的轮询行为:

terraform plan
terraform apply

测试基本的负载分配:

# 发出多个请求以查看两个源服务器
for i in {1..4}; do
  curl https://www.example.com
  sleep 1
done

预期输出:

Hello, this is 203.0.113.10!
Hello, this is 203.0.113.10!
Hello, this is 198.51.100.15!
Hello, this is 203.0.113.10!

您将看到在两个源服务器之间的随机分配。这种基于 DNS 的基本负载平衡有其局限性 - 没有运行状况检查、没有地理位置引导以及分配模式不可预测。对于诸如不同地理位置的源服务器或自动故障转移等更高级的场景,您需要使用 Cloudflare 的负载平衡 (Load Balancing)

2. 切换为使用 Cloudflare 的负载平衡产品

正如负载平衡教程中所述,您需要完成三项任务:

  1. 创建一个监视器以针对您的源服务器运行运行状况检查。
  2. 创建一个包含一个或多个源服务器的池,这些源服务器将接收负载平衡的流量。
  3. 创建一个具有外部主机名(例如 www.example.com)的负载平衡器以及一个或多个池。

我们可以通过创建一个基本的运行状况检查来监视源服务器,该检查对 URL 上的每个源服务器发出 GET 请求。如果源服务器在五秒内返回 200 状态代码 (OK),则认为它是健康的。如果连续三次未能做到,则认为它是不健康的。此运行状况检查将每分钟从几个区域运行一次,您可以配置在检测到任何故障时的电子邮件通知。

在此示例中,该池将被称为 www-origins,其中添加了两个源服务器:

  • www-us (203.0.113.10)
  • www-asia (198.51.100.15)

目前,跳过任何形式的地理路由 (geo routing)

当您创建一个负载平衡器 (LB) 时,它将替换具有相同名称的任何现有 DNS 记录。例如,如果您在下方创建 www.example.com 负载平衡器,它将取代您之前定义的两个 www DNS 记录。将 DNS 记录保留在原处的一个好处是,如果您暂时禁用负载平衡,仍然可以连接到此主机名。

为了实现上述目标,将负载平衡配置添加到 main.tf 中:

# 运行状况检查监视器
resource "cloudflare_load_balancer_monitor" "health_check" {
  account_id     = var.account_id
  expected_body = "alive"
  expected_codes = "2xx"
  method         = "GET"
  timeout        = 5
  path           = "/health"
  interval       = 60
  retries        = 2
  description    = "Health check for www origins"
  type           = "https"

  header = {
    Host = ["${var.domain}"]
  }
}

# 源服务器池
resource "cloudflare_load_balancer_pool" "www_pool" {
  account_id = var.account_id
  name       = "www-origins"
  monitor    = cloudflare_load_balancer_monitor.health_check.id

  origins = [{
    name    = "www-us"
    address = "203.0.113.10"
    enabled = true
  }, {
    name    = "www-asia"
    address = "198.51.100.15"
    enabled = true
  }]

  description     = "Primary www server pool"
  enabled         = true
  minimum_origins = 1
  notification_email = "<YOUR_EMAIL>"
  check_regions   = ["WEU", "EEU", "WNAM", "ENAM", "SEAS", "NEAS"]
}

# 负载平衡器
resource "cloudflare_load_balancer" "www_lb" {
  zone_id       = var.zone_id
  name          = "www.${var.domain}"
  default_pools = [cloudflare_load_balancer_pool.www_pool.id]
  fallback_pool = cloudflare_load_balancer_pool.www_pool.id
  description   = "Load balancer for www.${var.domain}"
  proxied       = true
}

预览并应用更改:

terraform plan
terraform apply

测试改进后的负载平衡:

# 测试具有运行状况监视的负载分配
for i in {1..6}; do
  echo "Request $i:"
  curl -s https://www.example.com
  sleep 2
done

预期输出:

Request 1:
Hello, this is 198.51.100.15!
Request 2:
Hello, this is 203.0.113.10!
Request 3:
Hello, this is 198.51.100.15!
Request 4:
Hello, this is 203.0.113.10!
Request 5:
Hello, this is 203.0.113.10!
Request 6:
Hello, this is 198.51.100.15!

您现在应该看到更可预测的负载分配,并且增加了运行状况监控和自动故障转移的好处。

合并并验证:

git add main.tf
git commit -m "Step 4 - Create load balancer (LB) monitor, LB pool, and LB"
git push

通过在 Cloudflare 仪表板下的 Traffic(流量) > Load Balancing 中进行检查,验证配置是否正在工作。您应该会看到带有健康状态指示器的监视器、池和负载平衡器。 您的负载平衡器现在将:

  • 在源服务器之间智能地分配流量
  • 自动绕过不健康的服务器
  • 提供实时的运行状况监控

这篇文档对您有帮助吗?