跳转到内容
搜索文档

快速入门

最后更新 查看 MarkdownAgent 设置

快速上手 Load Balancing。如需更深入的说明,请参阅 Learning path

本指南假设您熟悉 Cloudflare Load Balancing 组件



开始之前

请确保您:

  • 可以访问多个端点(源站服务器、私有或公有 IP 地址、虚拟 IP 地址(VIP)等),无论是物理还是基于云的。
  • 可以使用 Load Balancing,任何类型的账户均可作为附加功能购买。
  • 拥有受 SSL/TLS 证书覆盖的测试和生产主机名。

创建监视器

A monitor issues health monitor requests at regular intervals to evaluate the health of each endpoint within a pool.

When a pool becomes unhealthy, your load balancer takes that pool out of the endpoint rotation.

设置监视器

您可以在负载均衡器工作流中或 Monitors(监视器) 选项卡内创建 monitor:

  1. 前往 Load Balancing(负载均衡)

  2. 选择 Monitors(监视器) 选项卡。

  3. 选择 Create monitor(创建监视器)

  4. 添加以下信息:

    • Type(类型):健康 monitor 使用的协议
      • 非 Enterprise 客户:选择 HTTPHTTPSTCP
      • Enterprise 客户:选择 HTTPHTTPSTCPUDP ICMPICMP PingSMTP
    • Path(路径):运行健康 monitor 请求的 endpoint 路径
    • Port(端口):健康 monitor 的目标端口

  5. 对于其他设置,选择 Advanced health monitor settings(高级运行状况监视器设置)

    • Interval(时间间隔)
      • 增加默认值可以缩短故障转移时间,但也可能增加 endpoint 负载。
      • 最短间隔(秒)为 60(Pro)、15(Business)和 10(Enterprise)。
    • Timeout(超时)Retries(重试)
      • 如果健康 monitor 请求超过 Timeout(超时) 指定的时长(且超过 Retries(重试) 指定的次数),将返回不健康。
    • Expected Code(s)(预期状态码):预期的 HTTP 响应码,可单独列出(200302)或作为范围(例如,输入 2xx 将涵盖 200 范围内的所有响应码)。
    • Response Body(响应正文)
      • 在响应正文中查找不区分大小写的子字符串。
      • 确保该值相对静态,且位于 HTML 页面前 10 KB 内。
    • Simulate Zone(模拟区域)
    • Follow Redirects(跟随重定向)
      • 健康 monitor 请求会跟随重定向到最终 endpoint,而不是将 301302 码报告为不健康。
    • Configure Request Header(s)(配置请求标头)
      • 如果您的 endpoint 期望特定的传入标头,此选项很有用。
    • Header(标头)
      • 健康 monitor 中发送的 HTTP 请求标头。建议默认设置 Host 标头。User-Agent 标头无法覆盖。此参数仅对 HTTP 和 HTTPS monitor 有效。

  6. 选择 Save(保存)

准备您的服务器

确保防火墙或 Web 服务器不会阻止或限速您配置的健康 monitor,或与 Cloudflare IP 地址 关联的请求。

每个健康 monitor 的 HTTP user-agent 为 "Mozilla/5.0 (compatible; Cloudflare-Traffic-Manager/1.0; +https://www.cloudflare.com/traffic-manager/; pool-id: $poolid)",其中 $poolid关联 pool 的前 16 个字符。

设置监视器

有关 monitor 属性的完整列表,请参阅 Create Monitor。如需 API 身份验证帮助,请参阅 Cloudflare API 文档

Required API token permissions

At least one of the following token permissions is required:
  • Load Balancing: Monitors and Pools Write
Create Monitorbash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/load_balancers/monitors" \
	--request POST \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"type": "https",
		"description": "Login page monitor",
		"method": "GET",
		"path": "/health",
		"header": {
				"Host": [
						"example.com"
				],
				"X-App-ID": [
						"abc123"
				]
		},
		"port": 8080,
		"timeout": 3,
		"retries": 0,
		"interval": 90,
		"expected_body": "alive",
		"expected_codes": "2xx",
		"follow_redirects": true,
		"allow_insecure": true,
		"consecutive_up": 3,
		"consecutive_down": 2,
		"probe_zone": "example.com"
	}'

响应包含新 monitor 的完整定义。

Responsejson
{
  "success": true,
  "errors": [],
  "messages": [],
  "result": {
    "id": ":monitor-id",
    "created_on": "2021-01-01T05:20:00.12345Z",
    "modified_on": "2021-01-01T05:20:00.12345Z",
    "type": "https",
    "description": "Login page monitor",
    "method": "GET",
    "path": "/health",
    "header": {
      "Host": [
        "example.com"
      ],
      "X-App-ID": [
        "abc123"
      ]
    },
    "port": 8080,
    "timeout": 3,
    "retries": 0,
    "interval": 90,
    "expected_body": "alive",
    "expected_codes": "2xx",
    "follow_redirects": true,
    "allow_insecure": true,
    "consecutive_up": 3,
    "consecutive_down": 2,
    "probe_zone": "example.com"
  }
}
准备您的服务器

确保防火墙或 Web 服务器不会阻止或限速您配置的健康 monitor,或与 Cloudflare IP 地址 关联的请求。

每个健康 monitor 的 HTTP user-agent 为 "Mozilla/5.0 (compatible; Cloudflare-Traffic-Manager/1.0; +https://www.cloudflare.com/traffic-manager/; pool-id: $poolid)",其中 $poolid关联 pool 的前 16 个字符。

示例监控配置

字段
Type HTTP
Path /
Port 80
Interval 60
Method GET
Timeout 5 seconds
Retries 2
Expected Code(s) 200

创建池

Within Cloudflare, pools represent your endpoints and how they are organized. As such, a pool can be a group of several endpoints, or you could also have only one endpoint (an origin server, for example) per pool.

If you are familiar with DNS terminology, think of a pool as a “record set,” except Cloudflare only returns addresses that are considered healthy. You can attach health monitors to individual pools for customized monitoring. A pool can have either a single monitor or a monitor group attached — but not both.

您可以在负载均衡器工作流中或 Pools(池) 选项卡内创建 pool:

  1. 前往 Load Balancing(负载均衡)

  2. 选择 Pools(池) 选项卡,然后选择 Create pool(创建池)

  3. 为您的 pool 输入以下信息:

  4. 对于每个 endpoint,输入以下信息:

    • 名称(必须唯一)
    • endpoint 地址或关联主机名
    • (可选)Virtual Network。当 endpoint 具有私有 IP 地址时必填。
    • Weight
    • (可选)点击 Add host header(添加主机标头) 设置主机名
    • (可选)流量将送达的目标端口。
  1. 对 pool 中的其他 endpoint 重复此过程。

  2. (可选)在 pool 上设置 Proximity Steering 的坐标。

  3. 在 pool 上更新以下信息:

    • Health Threshold(健康阈值)

      Health Threshold 是 pool 整体被视为健康并根据负载均衡器中的 pool 顺序接收流量所需的健康 endpoint 数量。增加此数值会使 pool 更可靠,但也更容易变为不健康。

    • Monitor:附加 monitor
    • Health Monitor Regions(健康监视器区域):选择是否从多个位置检查 pool 健康状态,这提高准确性但可能导致对 endpoint 的探测流量
    • Pool Notifications(池通知):您可以设置新警报——并查看现有警报——以便在 pool 启用或禁用时,或 pool/endpoint 的健康状态发生变化时收到通知。
  4. 完成后,选择 Save(保存)

有关完整属性列表,请参阅 Create Pool。如需 API 身份验证帮助,请参阅 Cloudflare API 文档

Required API token permissions

At least one of the following token permissions is required:
  • Load Balancing: Monitors and Pools Write
Create Poolbash
curl "https://api.cloudflare.com/client/v4/accounts/$ACCOUNT_ID/load_balancers/pools" \
	--request POST \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"description": "Primary data center - Provider XYZ",
		"name": "primary-dc-1",
		"enabled": false,
		"load_shedding": {
				"default_percent": 0,
				"default_policy": "random",
				"session_percent": 0,
				"session_policy": "hash"
		},
		"minimum_origins": 2,
		"monitor": "f1aba936b94213e5b8dca0c0dbf1f9cc",
		"check_regions": [
				"WEU",
				"ENAM"
		],
		"origins": [
				{
						"name": "app-server-1",
						"address": "0.0.0.0",
						"enabled": true,
						"weight": 0.56,
						"header": {
								"Host": [
										"example.com"
								]
						}
				}
		],
		"origin_steering": {
				"policy": "random"
		},
		"notification_filter": {
				"origin": {
						"disable": false,
						"healthy": null
				},
				"pool": {
						"disable": false,
						"healthy": null
				}
		}
	}'

响应包含新 pool 的完整定义。

Responsejson
{
  "success": true,
  "errors": [],
  "messages": [],
  "result": {
    "id": "17b5962d775c646f3f9725cbc7a53df4",
    "created_on": "2021-01-01T05:20:00.12345Z",
    "modified_on": "2021-01-01T05:20:00.12345Z",
    "description": "Primary data center - Provider XYZ",
    "name": "primary-dc-1",
    "enabled": false,
    "load_shedding": {
      "default_percent": 0,
      "default_policy": "random",
      "session_percent": 0,
      "session_policy": "hash"
    },
    "minimum_origins": 2,
    "monitor": "f1aba936b94213e5b8dca0c0dbf1f9cc",
    "check_regions": [
      "WEU",
      "ENAM"
    ],
    "origins": [
      {
        "name": "app-server-1",
        "address": "0.0.0.0",
        "enabled": true,
        "weight": 0.56,
        "header": {
          "Host": [
            "example.com"
          ]
        }
      }
    ],
    "origin_steering": {
      "policy": "random"
    },
    "notification_filter": {
      "origin": {
        "disable": false,
        "healthy": null
      },
      "pool": {
        "disable": false,
        "healthy": null
      }
    }
  }
}

创建 pool 后,您还需要创建新通知,并指定以下参数:

"alert_type": "load_balancing_health_alert",
"filters": {
  "pool_id": <<ARRAY_OF_INCLUDED_POOL_IDS>>,
  "new_health": <<ARRAY_OF_STATUS_TRIGGERS>> ["Unhealthy", "Healthy"],
  "event_source": <<ARRAY_OF_OBJECTS_WATCHED>> ["pool", "origin"]
}

确认池健康状态

在将任何流量定向到 pool 之前,请确保 pool 和 monitor 设置正确。在首次检查结果可用之前,健康检查状态将为 unknown

要使用仪表板确认 pool 健康状态:

  1. 前往 Load Balancing(负载均衡)
  2. 选择 Pools(池) 选项卡。
  3. 对于 pool 和单个 endpoint,查看 HealthEndpoint Health 列中的值。

有关 pool 和 endpoint 健康状态的更多信息,请参阅 pool 如何变为不健康

要获取所有 pool 的最新健康状态,请使用 List Pools 命令,注意 pool 和 origins(endpoints)的 healthy 值。

要排查特定 pool 的健康问题,请使用 Pool Health Details 命令。

意外的健康状态

如果您发现健康的 pool 被标记为不健康:

在测试子域上创建负载均衡器

您可能不应直接在生产域名上开始,而应在测试或预发布域名上创建负载均衡器。根据基础设施设置,这可能需要对 monitor 和 pool 进行临时更改。

从测试域名开始可以让您在路由生产流量之前验证一切运行正常。

要在仪表板中创建 Public 或 Private 负载均衡器:

创建 Public 负载均衡器

  1. 前往 Load Balancing(负载均衡) 并选择 Create load balancer(创建负载均衡器)

  2. Load Balancer Setup(负载均衡器设置) 上,选择 Public load balancer(公共负载均衡器)

  3. 选择要添加此负载均衡器的网站。

  4. Hostname(主机名) 页面上:

    • 输入 Hostname(主机名),即负载均衡器可用的 DNS 名称。有关记录优先级的更多详情,请参阅负载均衡的 DNS 记录
    • Data Localization(数据本地化) 下拉菜单中选择要在域名上使用的区域
    • 切换橙色云图标以更新代理模式,这会影响流量路由方式和公布的 IP 地址。
    • 为负载均衡器添加描述。
    • 如果需要基于会话的负载均衡,切换 Session Affinity(会话亲和性) 开关。
    • 如果需要 Adaptive Routing,切换 Adaptive Routing(自适应路由) 开关。
  5. 选择 Next(下一步)

  6. Add a Pool 页面上:

    • 选择一个或多个现有 pool 或创建新 pool
    • 如果要将流量导向设置为 Off,请重新排序负载均衡器中的 pool 以调整故障转移顺序。
    • 如有需要,更新 Fallback Pool(备用池)
    • 如果选择将流量导向设置为 Random,可以通过 API 为 pool 设置 Weights 以确定发送到每个 pool 的流量百分比。
  7. 选择 Next(下一步)

  8. Monitors(监视器) 页面上:

    • 查看附加到 pool 的 monitor。
    • 如有需要,可以附加现有 monitor 或创建新 monitor
  9. 选择 Next(下一步)

  10. Traffic Steering(流量引导) 页面上,选择流量导向选项并选择 Next(下一步)

  11. Custom Rules 页面上,选择现有规则或创建新规则

  12. 选择 Next(下一步)

  13. Review 页面上:

    • 查看配置并进行任何更改。
    • 选择 Save as Draft(保存为草稿)Save and Deploy

创建 Private 负载均衡器

  1. 前往 Load Balancing(负载均衡) 并选择 Create load balancer(创建负载均衡器)

  2. Load Balancer Setup(负载均衡器设置) 上,选择 Private load balancer(私有负载均衡器)

  3. 将负载均衡器与 Cloudflare 私有 IP 或指定 IP 地址关联,并为负载均衡器创建描述。

  4. Add a Pool 页面上:

    • 选择一个或多个现有 pool 或创建新 pool
    • 如果要将流量导向设置为 Off,请重新排序负载均衡器中的 pool 以调整故障转移顺序。
    • 如有需要,更新 Fallback Pool(备用池)
    • 如果选择将流量导向设置为 Random,可以通过 API 为 pool 设置 Weights 以确定发送到每个 pool 的流量百分比。
  5. 选择 Next(下一步)

  6. Monitors(监视器) 页面上:

    • 查看附加到 pool 的 monitor。
    • 如有需要,可以附加现有 monitor 或创建新 monitor
  7. 选择 Next(下一步)

  8. Traffic Steering(流量引导) 页面上,选择流量导向选项并选择 Next(下一步)

  9. 选择 Next(下一步)

  10. Review 页面上:

    • 查看配置并进行任何更改。
    • 选择 Save as Draft(保存为草稿)Save and Deploy

有关完整属性列表,请参阅 Create Load Balancer。如需 API 身份验证帮助,请参阅 Cloudflare API 文档

Required API token permissions

At least one of the following token permissions is required:
  • Load Balancers Write
Create Load Balancerbash
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/load_balancers" \
	--request POST \
	--header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
	--json '{
		"description": "Load Balancer for lb.example.com",
		"name": "lb.example.com",
		"enabled": true,
		"ttl": 30,
		"fallback_pool": "17b5962d775c646f3f9725cbc7a53df4",
		"default_pools": [
				"17b5962d775c646f3f9725cbc7a53df4",
				"9290f38c5d07c2e2f4df57b1f61d4196",
				"00920f38ce07c2e2f4df50b1f61d4194"
		],
		"proxied": true,
		"steering_policy": "random_steering",
		"session_affinity": "cookie",
		"session_affinity_attributes": {
				"samesite": "Auto",
				"secure": "Auto",
				"drain_duration": 100,
				"zero_downtime_failover": "sticky"
		},
		"session_affinity_ttl": 5000,
		"adaptive_routing": {
				"failover_across_pools": true
		},
		"location_strategy": {
				"prefer_ecs": "always",
				"mode": "resolver_ip"
		},
		"random_steering": {
				"pool_weights": {
						"de90f38ced07c2e2f4df50b1f61d4194": 0.3,
						"9290f38c5d07c2e2f4df57b1f61d4196": 0.5
				},
				"default_weight": 0.2
		}
	}'

响应包含新负载均衡器的完整定义。

Responsejson
{
  "success": true,
  "errors": [],
  "messages": [],
  "result": {
    "id": "699d98642c564d2e855e9661899b7252",
    "created_on": "2021-01-01T05:20:00.12345Z",
    "modified_on": "2021-01-01T05:20:00.12345Z",
    "description": "Load Balancer for lb.example.com",
    "name": "lb.example.com",
    "enabled": true,
    "ttl": 30,
    "fallback_pool": "17b5962d775c646f3f9725cbc7a53df4",
    "default_pools": [
      "17b5962d775c646f3f9725cbc7a53df4",
      "9290f38c5d07c2e2f4df57b1f61d4196",
      "00920f38ce07c2e2f4df50b1f61d4194"
    ],
    "proxied": true,
    "steering_policy": "random_steering",
    "session_affinity": "cookie",
    "session_affinity_attributes": {
      "samesite": "Auto",
      "secure": "Auto",
      "drain_duration": 100,
      "zero_downtime_failover": "sticky"
    },
    "session_affinity_ttl": 5000,
    "random_steering": {
      "pool_weights": {
        "de90f38ced07c2e2f4df50b1f61d4194": 0.3,
        "9290f38c5d07c2e2f4df57b1f61d4196": 0.5
      },
      "default_weight": 0.2
    }
  }
}

可选 - 查看负载均衡分析

在向测试域发送示例请求时,查看负载均衡分析 页面,确认负载均衡器是否按预期分配请求。

路由生产流量

现在您已设置负载均衡器并验证一切运行正常,可以将其部署到 live 域名或子域名:

  1. 如果更新了 pool 和 monitor,请再次检查 pool 健康状态,确保一切按预期运行。
  2. 确认生产主机名具有正确的 DNS 记录优先级顺序,并受 SSL/TLS 证书覆盖。
  3. 配置负载均衡器以接收生产流量,可能包括:
    • 编辑现有负载均衡器的 Hostname(主机名)
    • 更新将流量发送到负载均衡器的 CNAME 记录。

可选 - 后续步骤

您的负载均衡器应该已在接收生产流量(您可以通过查看分析 来确认)。

虽然您的产品已正式设置完成,但您可能还想考虑以下建议。

基于用量的通知

由于这是一项按用量计费的服务,Cloudflare 建议您设置按用量计费通知,以避免意外账单。

要设置这些通知:

  1. 在 Cloudflare 仪表板中,前往 Notifications(通知) 页面。

    Go to Notifications ↗
  2. Alert Type(警报类型)Usage Based Billing(按用量计费) 下,点击 Select(选择)

  3. 填写以下信息:

    • Name(名称)
    • Product(产品)
    • Notification limit(具体指标因产品而异)
    • Notification email(通知邮箱)
  4. 选择 Save(保存)

其他配置选项

您可能希望进一步自定义负载均衡器如何路由流量,或将负载均衡器与其他 Cloudflare 产品集成:

这篇文档对您有帮助吗?