google-cloud-platform - Google Cloud 混合 HTTP/HTTPS 应用程序负载平衡

标签 google-cloud-platform

我想配置 Google 可以负载平衡,以便:

  • 对端口 443 的所有边缘请求在负载均衡器处终止 SSL 并路由到托管实例组中的端口 8080
  • 对端口 80 的所有边缘请求路由到托管实例组中的端口 8081,然后将向 HTTPS 服务发送 307 响应以强制使用 SSL。

  • 我有:

    全局转发规则
  • 全局转发规则 STATIC_IP:80 -> httpsreditect-target-proxy
  • 全局转发规则 STATIC_IP:443 -> webapp-target-proxy

  • 目标代理
  • httpsreditect-target-proxy -> httpredirect_urlmap
  • webapp-target-proxy -> webapp_urlmap

  • 网址映射
  • httpredirect_urlmap -> redirect_backend(实例池中的8​​081)
  • webapp_urlmap -> webapp_backend(实例池中的8​​080)

  • 这不起作用。

    通过此设置,如果我将 redirect_backend 端口设置为 8081,则 webapp_backend 端口也将更改为 8081。同样,如果我将 webapp_backend 端口设置为 8080,则 redirect_backend 端口将设置为 8080。

    是否可以将基于端口的流量路由到不同的后端? 选项是它们在 GUI 中,没有验证错误,感觉应该是可能的,但是当设置后端端口时,所有后端都设置为相同的端口?

    我知道将 HAProxy 放在节点上是一种解决方案,并在那里反向代理微服务,但我宁愿让 Google Cloud Application Loadbalancer 终止 SSL 作为使用 f1-micro 实例。

    最佳答案

    做到这一点的关键是 https://cloud.google.com/compute/docs/load-balancing/http/backend-service#restrictions_and_guidance 上的一个很容易错过的片段。 .

    Your configuration will be simpler if you do not add the same instance group to two different backends. If you do add the same instance group to two backends:

    ...

    If your instance group serves two or more ports for several backends respectively, you have to specify different port names in the instance group.


    初始设置非常重要,因此以下是引用。
    基于我的配置中的示例:
  • 托管实例组
  • 在端口 80 上运行的主 web 应用程序
  • 在端口 8081 上运行的 HTTP 重定向服务


  • 防火墙
    确保您有防火墙规则允许 Google 对您的服务进行健康检查访问:
    gcloud compute firewall-rules create allow-http-from-lb \
      --description "Incoming http allowed from cloud loadbalancer." \
      --allow tcp:80
      --source-ranges "130.211.0.0/22"
    
    gcloud compute firewall-rules create allow-http-redirect-from-lb \
      --description "Incoming http redirect service allowed from cloud loadbalancer." \
      --allow tcp:8081
      --source-ranges "130.211.0.0/22"
    
    健康检查
    确保您为两个服务设置了健康检查,检查正确的内部端口。
    gcloud compute http-health-checks create webapp-healthcheck \
      --description "Main webapp healthcheck" \
      --port 80 \
      --request-path "/healthcheck"
    
    gcloud compute http-health-checks create httpsredirect-service-healthcheck \
      --description "HTTP redirect service healthcheck" \
      --port 8081 \
      --request-path "/healthcheck"
    
    配置命名端口
    如果您的实例组有多个在不同端口上运行的微服务,并且您希望在公共(public)负载均衡器下公开这些微服务,那么看起来是关键。
    替换 INSTANCE_GROUP_NAME , REGIONnamed-ports为您的服务提供正确的值(value)。
    gcloud compute instance-groups set-named-ports INSTANCE_GROUP_NAME \
      --region=REGION \
      --named-ports "webapp:80,httpsredirectservice:8081"
    
    创建负载均衡器后端
    确保 --port-name匹配上一步中正确命名的端口。
    gcloud compute backend-services create webapp-lb-backend \
      --http-health-check webapp-healthcheck \
      --protocol http \
      --description "Webapp load balancer backend" \
      --port-name webapp
    
    
    gcloud compute backend-services create httpsredirect-lb-backend \
      --http-health-check webapp-healthcheck \
      --protocol http \
      --description "HTTP -> HTTPS redirect service load balancer backend" \
      --port-name httpsredirectservice
    
    为两个服务创建 URL Map
    确保 --default-service使用上一步中配置的值。
    gcloud compute url-maps create webapp-urlmap \
      --default-service webapp-lb-backend
    
    gcloud compute url-maps create httpsredirect-urlmap \
      --default-service httpsredirect-lb-backend
    
    创建目标代理

    Target proxies are referenced by one or more global forwarding rules and route the incoming HTTP or HTTPS requests to a URL map.


    我们为 webapp 创建一个 https 目标代理,以在负载均衡器上终止 SSL。
    gcloud compute target-https-proxies create webapp-target-proxy \
      --url-map webapp-urlmap \
      --ssl-certificate [SSL_CERTIFICATES]
    
    重定向服务:
    gcloud compute target-http-proxies create httpsredirect-target-proxy \
      --url-map httpsredirect-urlmap 
    
    全局转发规则
    最后一步是创建全局转发规则
    gcloud compute forwarding-rules create webapp-forwarding-rule
      --global \
      --address LB_STATIC_IP \
      --port-range 443 \
      --target-https-proxy webapp-target-proxy
    
    gcloud compute forwarding-rules create httpsredirect-forwarding-rule
      --global \
      --address LB_STATIC_IP \
      --port-range 80 \
      --target-http-proxy httpsredirect-target-proxy
    
    我遇到的问题
  • 确保正确配置防火墙以允许进行健康检查并设置健康检查。
  • 如果出现间歇性502错误检查云控制台中的负载均衡器报告健康实例

  • 其他注意事项
  • 因为需要两个 url 映射,所以您需要为两个负载均衡器付费查看我的帐单信息 .端口 80 和端口 443 都使用自己的负载均衡器
  • 看起来不可能像在 AWS 上所做的那样使用网络负载均衡器来终止 SSL 并提供 HTTP 服务。
  • 关于google-cloud-platform - Google Cloud 混合 HTTP/HTTPS 应用程序负载平衡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41181496/

    相关文章:

    google-app-engine - 如何删除谷歌云数据存储中的命名空间

    spring-boot - GCP 和 Spring logback。严重性始终是信息

    python-3.x - 语法错误 : invalid syntax while running Job

    google-cloud-platform - GCP - 如何使用 gcloud 过滤启动磁盘?

    python - Dataflow SDK 2.7.0,同时附加安装程序 2.10.0

    kubernetes - 谷歌云平台的 "Managed Infrastructure Mixer Client"是什么?

    Python BigQuery 存储。并行读取多个流

    google-cloud-platform - 如何获取 Cloud Build 的最新更新?

    java - 无法从 Genymotion 模拟器连接到本地 gae 端点

    google-cloud-platform - 想要通过 terraform 将多个 Google Cloud IAM 角色分配给服务帐户