You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用gcloud CLI创建GCP新一代全球L7负载均衡器?

使用gcloud CLI创建GCP新一代全球L7负载均衡器的方案

新一代全球L7负载均衡器(正式名称为全球外部HTTP(S)负载均衡器(第2代))可通过gcloud CLI完成创建,核心围绕gcloud compute下的资源命令构建,以下是分步操作方案:

1. 准备基础资源(前提步骤)

先确保已创建必要的基础组件:

  • 后端服务(Backend Service):关联后端实例组、云函数等资源
  • 健康检查(Health Check):检测后端服务可用性
  • URL映射(URL Map):定义请求路由规则
  • 目标HTTP(S)代理(Target HTTP(S) Proxy):关联URL映射与SSL证书(HTTPS场景)
  • 转发规则(Forwarding Rule):对外暴露负载均衡的IP和端口

2. 具体创建命令示例

步骤1:创建健康检查

gcloud compute health-checks create http my-http-health-check \
    --port 80 \
    --request-path "/healthz"

步骤2:创建后端服务并关联实例组

gcloud compute backend-services create my-backend-service \
    --global \
    --health-checks my-http-health-check \
    --enable-cdn  # 可选,开启CDN功能

添加后端实例组到服务:

gcloud compute backend-services add-backend my-backend-service \
    --global \
    --instance-group my-instance-group \
    --instance-group-zone us-central1-a

步骤3:创建URL映射

默认路由规则:

gcloud compute url-maps create my-url-map \
    --default-service my-backend-service

添加自定义路径路由(可选):

gcloud compute url-maps add-path-matcher my-url-map \
    --path-matcher-name my-path-matcher \
    --default-service my-backend-service \
    --path-rules "/api/*=my-api-backend-service"

步骤4:创建目标代理

HTTP场景:

gcloud compute target-http-proxies create my-target-proxy \
    --url-map my-url-map

HTTPS场景(需先准备SSL证书):

gcloud compute target-https-proxies create my-target-https-proxy \
    --url-map my-url-map \
    --ssl-certificates my-ssl-certificate

步骤5:创建全局转发规则

HTTP协议:

gcloud compute forwarding-rules create my-http-forwarding-rule \
    --global \
    --target-http-proxy my-target-proxy \
    --ports 80

HTTPS协议:

gcloud compute forwarding-rules create my-https-forwarding-rule \
    --global \
    --target-https-proxy my-target-https-proxy \
    --ports 443

3. 验证负载均衡器状态

查看转发规则详情:

gcloud compute forwarding-rules describe my-http-forwarding-rule --global

检查后端服务健康状态:

gcloud compute backend-services get-health my-backend-service --global

注意事项

  • 新一代L7负载均衡器的资源均为**全局(--global)**属性,不要使用区域参数
  • 若用自定义域名,需将域名解析到转发规则分配的静态IP(创建时可通过--address指定已有静态IP)
  • 如需Cloud Armor安全策略、自定义请求头等高级功能,可在对应资源创建时添加--security-policy等参数

内容的提问来源于stack exchange,提问作者Randy L

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 01:57:42