我想配置 Google 可以负载平衡,以便:
我有:
全局转发规则
目标代理
网址映射
这不起作用。
通过此设置,如果我将 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.
初始设置非常重要,因此以下是引用。
基于我的配置中的示例:
防火墙
确保您有防火墙规则允许 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
, REGION
和 named-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
错误检查云控制台中的负载均衡器报告健康实例 其他注意事项
AWS
上所做的那样使用网络负载均衡器来终止 SSL 并提供 HTTP 服务。 关于google-cloud-platform - Google Cloud 混合 HTTP/HTTPS 应用程序负载平衡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41181496/