本页面介绍了如何在 Google Kubernetes Engine (GKE) 中使用容器原生负载均衡。利用容器原生负载均衡,负载均衡器能够直接定位 Kubernetes Pod,还能将流量均匀分发给 Pod。
如需详细了解容器原生负载均衡的优势、要求和限制,请参阅容器原生负载均衡。
准备工作
在开始之前,请确保您已执行以下任务:
- 启用 Google Kubernetes Engine API。 启用 Google Kubernetes Engine API
- 如果您要使用 Google Cloud CLI 执行此任务,请安装并初始化 gcloud CLI。 如果您之前安装了 gcloud CLI,请通过运行
gcloud components update命令来获取最新版本。较早版本的 gcloud CLI 可能不支持运行本文档中的命令。
- 确保您已有 VPC 原生集群。如有需要,请创建一个集群。GKE 集群默认为 VPC 原生集群。
使用容器原生负载均衡
以下几个部分将引导您完成 GKE 上的容器原生负载均衡配置。
创建 Deployment
以下示例 Deployment neg-demo-app 运行容器化 HTTP 服务器的单个实例。我们建议您使用利用 Pod 就绪性反馈的工作负载。
使用 Pod 就绪性反馈
apiVersion: apps/v1 kind: Deployment metadata: labels: run: neg-demo-app # Label for the Deployment name: neg-demo-app # Name of Deployment spec: selector: matchLabels: run: neg-demo-app template: # Pod template metadata: labels: run: neg-demo-app # Labels Pods from this Deployment spec: # Pod specification; each Pod created by this Deployment has this specification containers: - image: registry.k8s.io/serve_hostname:v1.4 # Application to run in Deployment's Pods name: hostname # Container name ports: - containerPort: 9376 protocol: TCP
使用硬编码延迟
apiVersion: apps/v1 kind: Deployment metadata: labels: run: neg-demo-app # Label for the Deployment name: neg-demo-app # Name of Deployment spec: minReadySeconds: 60 # Number of seconds to wait after a Pod is created and its status is Ready selector: matchLabels: run: neg-demo-app template: # Pod template metadata: labels: run: neg-demo-app # Labels Pods from this Deployment spec: # Pod specification; each Pod created by this Deployment has this specification containers: - image: registry.k8s.io/serve_hostname:v1.4 # Application to run in Deployment's Pods name: hostname # Container name # Note: The following line is necessary only on clusters running GKE v1.11 and lower. # For details, see https://cloud.google.com/kubernetes-engine/docs/how-to/container-native-load-balancing#align_rollouts ports: - containerPort: 9376 protocol: TCP terminationGracePeriodSeconds: 60 # Number of seconds to wait for connections to terminate before shutting down Pods
在此 Deployment 中,每个容器都运行一个 HTTP 服务器。该 HTTP 服务器返回应用服务器的主机名(运行服务器所在的 Pod 的名称)作为响应。
将此清单保存为 neg-demo-app.yaml,然后创建 Deployment:
kubectl apply -f neg-demo-app.yaml
为容器原生负载均衡器创建 Service
创建 Deployment 后,需要将其 Pod 分组到 Service 中。
以下示例 Service neg-demo-svc 会定位您在上一个部分中创建的示例 Deployment:
apiVersion: v1
kind: Service
metadata:
name: neg-demo-svc # Name of Service
spec: # Service's specification
type: ClusterIP
selector:
run: neg-demo-app # Selects Pods labelled run: neg-demo-app
ports:
- name: http
port: 80 # Service's port
protocol: TCP
targetPort: 9376
在为该 Service 创建 Ingress 之前,不会创建负载均衡器。
将此清单保存为 neg-demo-svc.yaml,然后创建 Service:
kubectl apply -f neg-demo-svc.yaml
为 Service 创建 Ingress
以下示例 Ingress neg-demo-ing 会定位您创建的 Service:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: neg-demo-ing
spec:
defaultBackend:
service:
name: neg-demo-svc # Name of the Service targeted by the Ingress
port:
number: 80 # Should match the port used by the Service
将此清单保存为 neg-demo-ing.yaml,然后创建 Ingress:
kubectl apply -f neg-demo-ing.yaml
创建 Ingress 后,系统会在项目中创建应用负载均衡器,并在集群运行的每个可用区中创建网络端点组 (NEG)。NEG 中的端点和 Service 的端点会保持同步。
验证 Ingress
部署了工作负载,将其 Pod 分组到 Service 中,并为 Service 创建了 Ingress 后,您应验证 Ingress 是否已成功预配容器原生负载均衡器。
检索 Ingress 的状态:
kubectl describe ingress neg-demo-ing
输出包括 ADD 和 CREATE 事件:
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ADD 16m loadbalancer-controller default/neg-demo-ing
Normal Service 4s loadbalancer-controller default backend set to neg-demo-svc:32524
Normal CREATE 2s loadbalancer-controller ip: 192.0.2.0
测试负载均衡器
以下几个部分说明如何测试容器原生负载均衡器的功能。
访问 Ingress 的 IP 地址
等待几分钟,让应用负载均衡器完成配置。
您可以通过访问 Ingress 的 IP 地址来验证容器原生负载均衡器是否正常运行。
如需获取 Ingress 的 IP 地址,请运行以下命令:
kubectl get ingress neg-demo-ing
在命令输出中,Ingress 的 IP 地址显示在 ADDRESS 列中。在网络浏览器中访问 IP 地址。
检查后端服务的运行状况
您还可以获取负载均衡器的后端服务的运行状况。
获取项目中运行的后端服务的列表:
gcloud compute backend-services list记录包含 Service 名称(例如
neg-demo-svc)的后端服务的名称。获取后端服务的运行状况:
gcloud compute backend-services get-health BACKEND_SERVICE_NAME --global将
BACKEND_SERVICE_NAME替换为后端服务的名称。
测试 Ingress
您可以采用另一种方法来测试负载均衡器是否按预期运行,也就是扩缩示例 Deployment,将测试请求发送到 Ingress,并验证响应的副本数量是否正确。
将
neg-demo-appDeployment 从一个实例扩容到两个实例:kubectl scale deployment neg-demo-app --replicas 2此命令可能需要几分钟才能完成。
验证发布是否已完成:
kubectl get deployment neg-demo-app输出内容应包含两个可用的副本:
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE neg-demo-app 2 2 2 2 26m获取 Ingress IP 地址:
kubectl describe ingress neg-demo-ing如果此命令返回 404 错误,请等待几分钟,以便负载均衡器启动,然后重试。
计算来自负载均衡器的不同响应的数量:
for i in `seq 1 100`; do \ curl --connect-timeout 1 -s IP_ADDRESS && echo; \ done | sort | uniq -c将
IP_ADDRESS替换为 Ingress IP 地址。输出类似于以下内容:
44 neg-demo-app-7f7dfd7bc6-dcn95 56 neg-demo-app-7f7dfd7bc6-jrmzf在此输出中,不同响应的数量与副本的数量相同,这表明所有后端 Pod 都在处理流量。
清理
完成本页面上的任务后,请按照以下步骤移除资源,以防止您的账号产生不必要的费用:
删除集群
gcloud
gcloud container clusters delete neg-demo-cluster
控制台
前往 Google Cloud 控制台中的 Google Kubernetes Engine 页面。
选择 neg-demo-cluster,然后点击 delete 删除。
当系统提示您确认时,点击删除。
后续步骤
- 了解如何通过独立可用区级 NEG 使用容器原生负载均衡。
- 详细了解 VPC 原生集群。
- 详细了解 Ingress 配置。
- 了解如何使用 Google 管理的 SSL 证书。