【问题标题】:GCE: How do you create a forwarding rule from port 80 external to port 5555 internalGCE:如何创建从外部 80 端口到内部 5555 端口的转发规则
【发布时间】:2015-04-14 05:41:57
【问题描述】:

我是第一次使用谷歌计算引擎。我想设置一个侦听端口 80 的网络负载均衡器(使用静态 IP),但转发到侦听端口 5555 的后端服务器。我发现的所有示例都显示将 80 转发到 80,这对我的情况。

参考:https://cloud.google.com/compute/docs/load-balancing/network/forwarding-rules

谢谢

【问题讨论】:

  • 顺便说一句我转发 HTTP
  • 仅供参考:因为我创建了这个问题/答案,GCE 将 http/https 代理从 alpha 移动到 beta 到发布状态。我需要检查他们是否从那时起清理了设置过程......

标签: google-compute-engine


【解决方案1】:

经过大量阅读和测试,我找到了一个解决方案,它允许 GCE 将请求代理到不同端口上的内部端口。要转发到不同的端口,我必须设置 Proxies、ServerPools、UrlMaps 等,因此设置比简单的网络转发要复杂得多。

##############################
# Setting up API port forwarding from external 80 to internal 5555
export INTERNAL_PORT=5555    #The port number that api is running on. 
export EXTERNAL_PORT=80      #The port number that will be exposed externally by the proxy

export ZONE=us-central1-b
export NETWORK=mynetwork

export INSTANCE_GRP="api-us"
export HEALTH_CHECK="api-basic-check"
export HEALTH_CHECK_CHECKPATH="/isok"
export BK_SRV_SERVICE="api-srv"
export PROXY_NAME="api-proxy"
export URLMAP_NAME="api-urlmap"
export HTTP_FW_NAME="api-http-fw-rule"
export ADDRESS_NAME="api-external-ip"

export BACKEND_SRV01="apiserver01"

gcloud preview instance-groups --zone $ZONE create $INSTANCE_GRP  --network $NETWORK
gcloud preview instance-groups --zone $ZONE instances \
    --group $INSTANCE_GRP add $BACKEND_SRV01
#The load balancing service by default looks for a service with a key of http. 
gcloud preview instance-groups --zone $ZONE add-service $INSTANCE_GRP \
    --port $INTERNAL_PORT --service http

gcloud compute http-health-checks create $HEALTH_CHECK \
    --check-interval 5s --healthy-threshold 2 \
    --port $INTERNAL_PORT --timeout 3s --unhealthy-threshold 4 \
    --request-path $HEALTH_CHECK_CHECKPATH

gcloud compute backend-services create $BK_SRV_SERVICE \
        --http-health-check $HEALTH_CHECK
gcloud compute backend-services add-backend $BK_SRV_SERVICE \
    --group $INSTANCE_GRP --zone $ZONE

gcloud compute url-maps create $URLMAP_NAME --default-service $BK_SRV_SERVICE
gcloud compute target-http-proxies create $PROXY_NAME --url-map $URLMAP_NAME

#create a static address to expose externally so that we can keep it if we remove the proxy.
gcloud compute addresses create $ADDRESS_NAME --global
export IP=`gcloud compute addresses describe $ADDRESS_NAME --global --format json | jq --raw-output '.address'`

gcloud compute forwarding-rules create $HTTP_FW_NAME --global \
    --target-http-proxy $PROXY_NAME --port-range $EXTERNAL_PORT --address $IP 

echo $IP # This is the IP to use for DNS etc...

【讨论】:

  • 按天保存,无法想象转发规则(前端服务)需要 target-http-proxy 和 url-map。我原以为前端服务可以直接指向后端服务
【解决方案2】:

目前,端口转发不是 GCE 负载均衡器 (LB) 功能:LB 将新传入请求转发到目标池 (TP),目标池在其实例之间分布。不执行 IP 或端口映射,因为只转发传入的请求。 LB 按原样公开端口。因此,对于多个端口,您可以定义一个端口范围,或者为每个端口定义一个不同的 LB。

要实现这样的目的,您可以使用 HAProxy 的端口转发设置、IPTables 在实例级别的 NAT,或在软件级别将客户端从端口 80 重定向到端口 5555。

使用 Kubernetes,您可以使用服务轻松实现端口转发。服务定义了一个代理,它将自动为端口转发执行所有必要的 iptables 魔术。希望这会有所帮助。

【讨论】:

  • 我做端口转发是因为我在后端服务器上运行多个服务。我们还有一个 kubernetes 主机集群,可以运行许多 docker 容器,每个容器都在不同的端口上。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-17
  • 1970-01-01
  • 2016-11-27
相关资源
最近更新 更多