GitHub – wwwil/glb-demo:使用 Terraform 在 Google Cloud Platform 上演示容器原生多集群全局负载均衡器
| Name | ||
|---|---|---|
|
|
6 years ago
|
|
|
|
6 years ago
|
|
|
|
6 years ago
|
|
|
|
6 years ago
|
|
|
|
6 years ago
|
|
|
|
5 years ago
|
|
存储库文件导航
这是使用 Terraform 的 Google Cloud Platform (GCP) 的容器原生多集群全局负载均衡器演示,其中包含 Cloud Armor 策略。
它旨在伴随 Jetstack 博客上的帖子(即将推出)。
这只是一个演示。它不应在生产环境或任何其他共享的长期环境中“按原样”使用。 它只是为了快速展示和测试全局负载均衡器的功能而设计的。 使用的许多配置和过程并不安全或不可靠。
- Google Cloud SDK(命令)
gcloud kubectl命令terraform命令、版本0.12.x
确保该命令是最新的,并使用正确的 Google 帐户和项目登录。 然后为 Terraform 生成凭据。 此方法将设置 Terraform 以使用您的帐户。 这通常是不好的做法,应将 Terraform 设置为使用服务帐户。gcloud
gcloud auth application-default login
在浏览器中完成网络登录,命令输出应告诉您凭据文件的位置,并显示消息。 然后将环境变量设置为指向创建的凭据文件,例如:Credentials saved to file:GOOGLE_CLOUD_KEYFILE_JSON
export GOOGLE_CLOUD_KEYFILE_JSON="/Users/wwwil/.config/gcloud/application_default_credentials.json"
进入目录,初始化 Terraform,应用项目文件。01-clusters/
terraform init
terraform apply
确保计划看起来正确,然后输入 。yes
进入目录。02-apps/
获取第一个集群的凭据,并应用 和 的清单。zone-printerhello-app
gcloud container clusters get-credentials glb-demo-eu --region europe-west2
kubectl apply -f zone-printer.yaml
kubectl apply -f hello-app.yaml
对第二个集群重复此作。
gcloud container clusters get-credentials glb-demo-us --region us-central1
kubectl apply -f zone-printer.yaml
kubectl apply -f hello-app.yaml
确保 Pod 在两个集群中运行。
kubectl get pods
NAME READY STATUS RESTARTS AGE
hello-app-858d49df47-88cd7 1/1 Running 0 21s
hello-app-858d49df47-gcnnz 1/1 Running 0 21s
hello-app-858d49df47-gcz4c 1/1 Running 0 21s
hello-app-858d49df47-gfgqr 1/1 Running 0 21s
hello-app-858d49df47-j64w2 1/1 Running 0 21s
hello-app-858d49df47-qrg69 1/1 Running 0 21s
hello-app-858d49df47-smrk6 1/1 Running 0 21s
hello-app-858d49df47-srt24 1/1 Running 0 21s
hello-app-858d49df47-xdp2w 1/1 Running 0 21s
zone-printer-7c9568c559-6klrj 1/1 Running 0 23s
zone-printer-7c9568c559-8mf4w 1/1 Running 0 23s
zone-printer-7c9568c559-8pw4p 1/1 Running 0 23s
zone-printer-7c9568c559-csbvk 1/1 Running 0 23s
zone-printer-7c9568c559-drktf 1/1 Running 0 23s
zone-printer-7c9568c559-fmzmq 1/1 Running 0 23s
zone-printer-7c9568c559-rxsth 1/1 Running 0 23s
zone-printer-7c9568c559-vp7dr 1/1 Running 0 23s
zone-printer-7c9568c559-zdpkk 1/1 Running 0 23s
zoneprinter-546c64f489-lm5vd 1/1 Running 0 21h
进入目录。03-glb/
对于每个集群,获取为部署的服务创建的网络终结点组 (NEG) 的名称。 这些 NEG 的名称和区域将作为注释添加到服务中。 需要将 NEG 名称提供给 Terraform 才能在负载均衡器中使用。 这是使用输入变量和模板文件实现的。.tfvars
gcloud container clusters get-credentials glb-demo-eu --region europe-west2
ZONE_PRINTER_NEG_EU=$(kubectl get service zone-printer -o json | jq '.metadata.annotations["cloud.google.com/neg-status"] | fromjson | .network_endpoint_groups["80"]')
HELLO_APP_NEG_EU=$(kubectl get service hello-app -o json | jq '.metadata.annotations["cloud.google.com/neg-status"] | fromjson | .network_endpoint_groups["80"]')
gcloud container clusters get-credentials glb-demo-us --region us-central1
ZONE_PRINTER_NEG_US=$(kubectl get service zone-printer -o json | jq '.metadata.annotations["cloud.google.com/neg-status"] | fromjson | .network_endpoint_groups["80"]')
HELLO_APP_NEG_US=$(kubectl get service hello-app -o json | jq '.metadata.annotations["cloud.google.com/neg-status"] | fromjson | .network_endpoint_groups["80"]')
cp terraform.tfvars.template terraform.tfvars
sed -i.bak "s|ZONE_PRINTER_NEG_EU|$ZONE_PRINTER_NEG_EU|g" terraform.tfvars
sed -i.bak "s|ZONE_PRINTER_NEG_US|$ZONE_PRINTER_NEG_US|g" terraform.tfvars
sed -i.bak "s|HELLO_APP_NEG_EU|$HELLO_APP_NEG_EU|g" terraform.tfvars
sed -i.bak "s|HELLO_APP_NEG_US|$HELLO_APP_NEG_US|g" terraform.tfvars
rm -f terraform.tfvars.bak
要支持 HTTPS,负载均衡器需要 SSL 证书。 这是使用 Terraform 从证书和密钥文件创建的 GCP SSL 证书资源提供给负载均衡器 HTTPS 代理的。 生成要使用的密钥和自签名证书。
openssl genrsa -out example.key 2048
openssl req -new -key example.key -out example.csr
-subj "/CN=example.com"
openssl x509 -req -days 365 -in example.csr -signkey example.key
-out example.crt
现在初始化 Terraform,并应用项目文件。
terraform init
terraform apply
确保计划看起来正确,然后输入 。yes
Terraform 完成后,它将输出它保留的全局 IP 地址的值。 在浏览器中输入此 IP,您应该会看到该应用程序,其中将显示您所连接的实例的 GCP 区域。 如果这不起作用,您可能需要等待更长的时间,直到负载均衡器配置由 Google 网络传播。zone-printer
负载均衡器中的最大连接速率设置得非常低。 这应该意味着,通过在浏览器中积极刷新与 IP 的连接,您应该会看到连接到的区域发生了变化。 这演示了有效的负载平衡。
验证 HTTPS 是否正常工作,请在 IP 地址前加上 。 这可能会显示一条警告,指出证书未被识别,因为我们使用的是自签名证书。 忽略警告并继续进入该页面,它应该显示该应用程序。https://zone-printer
显示的区域不应更改,并且应始终是最接近您连接位置的区域。 为了验证全局负载均衡是否正确引导流量,我们可以从其他区域的远程计算机运行。zone-printercurl
连接到当前未从中获得服务的区域中的群集。 例如,如果你位于欧洲,请连接到美国群集。
gcloud container clusters get-credentials glb-demo-us --region us-central1
或者,如果你在美国,请连接到欧洲群集。
gcloud container clusters get-credentials glb-demo-us --region us-central1
然后通过连接运行到其中一个节点上的全局 IP 地址。curlssh
ADDRESS=$(terraform output glb_demo_address)
INSTANCE=$(kubectl get nodes -o json | jq -r '.items[0].metadata.name')
ZONE=$(kubectl get nodes -o json | jq -r '.items[0].metadata.labels["failure-domain.beta.kubernetes.io/zone"]')
gcloud compute ssh $INSTANCE --zone $ZONE --command "curl $ADDRESS"
这应该显示另一个区域中的一个区域。 反复使用卷曲应该会导致区域发生变化。
直接访问全局 IP 地址时显示 ,因为它被设置为默认后端。 由于 URL Map,我们还可以通过在浏览器中附加来连接到应用程序。 这应该显示消息。zone-printerhello-web/hello-appHello, world!
完成负载均衡器的测试后,可以清理资源。
进入目录,运行销毁负载均衡资源。03-glb
terraform destroy
验证计划是否正确,然后输入“是”以继续。
然后进入目录,用Terraform销毁集群。01-clusters
terraform destroy
再次检查计划并输入“是”继续。
此演示中部署的应用程序是 Zone Printer 和 Hello App。
2. 分享目的仅供大家学习和交流,请不要用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员!
8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载
声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性
GLB下载网 - GLB/GLTF模型与格式资源免费下载,支持在线浏览与转换 » 使用 Terraform 在 Google Cloud Platform 上演示容器原生多集群全局负载均衡器
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载要提取码
- 分享过期和重复下载怎么办
- 模型和平台不兼容怎么办