你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
使用适用于容器的应用程序网关,可以利用 WebSocket 协议连接到后端目标。
使用情况详细信息
WebSocket 协议没有使用网关 API 进行配置或启用的特定实现。 但是,你可以使用本操作指南来了解端到端配置和适当的运行状况探测配置。 这可确保 WebSocket 请求由后端服务正确处理。
先决条件
如果遵循 BYO 部署策略,请确保设置适用于容器的应用程序网关资源和 ALB 控制器。
如果遵循 ALB 托管部署策略,请确保预配 ALB 控制器,并通过 ApplicationLoadBalancer 自定义资源预配适用于容器的应用程序网关资源。
部署示例 HTTP 和 WebSocket 应用程序:
请在群集上应用以下 deployment.yaml 文件,以创建示例 Web 应用程序来演示 WebSocket 支持。
kubectl apply -f https://raw.githubusercontent.com/MicrosoftDocs/azure-docs/refs/heads/main/articles/application-gateway/for-containers/examples/websocket-scenario/deployment.yaml
此命令在群集上创建以下内容:
- 名为
test-infra
的命名空间 -
test-infra
命名空间中一个名为websocket-backend
的服务 -
test-infra
命名空间中一个名为websocket-backend
的部署
- 名为
部署所需的网关 API 资源
创建网关
kubectl apply -f - <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: Gateway metadata: name: gateway-01 namespace: test-infra annotations: alb.networking.azure.io/alb-namespace: alb-test-infra alb.networking.azure.io/alb-name: alb-test spec: gatewayClassName: azure-alb-external listeners: - name: http-listener port: 80 protocol: HTTP allowedRoutes: namespaces: from: Same EOF
注意
当 ALB 控制器在 ARM 中创建适用于容器的应用程序网关时,它将对前端资源使用以下命名约定:fe-<8 个随机生成的字符>
如果要更改在 Azure 中创建的前端的名称,请考虑按照创建自己的部署策略中的说明进行操作。
创建网关资源后,请确保状态有效,侦听器已编程,并地址已分配给网关。
kubectl get gateway gateway-01 -n test-infra -o yaml
成功创建了网关时的示例输出。
status:
addresses:
- type: Hostname
value: xxxx.yyyy.alb.azure.com
conditions:
- lastTransitionTime: "2023-06-19T21:04:55Z"
message: Valid Gateway
observedGeneration: 1
reason: Accepted
status: "True"
type: Accepted
- lastTransitionTime: "2023-06-19T21:04:55Z"
message: Application Gateway For Containers resource has been successfully updated.
observedGeneration: 1
reason: Programmed
status: "True"
type: Programmed
listeners:
- attachedRoutes: 0
conditions:
- lastTransitionTime: "2023-06-19T21:04:55Z"
message: ""
observedGeneration: 1
reason: ResolvedRefs
status: "True"
type: ResolvedRefs
- lastTransitionTime: "2023-06-19T21:04:55Z"
message: Listener is accepted
observedGeneration: 1
reason: Accepted
status: "True"
type: Accepted
- lastTransitionTime: "2023-06-19T21:04:55Z"
message: Application Gateway For Containers resource has been successfully updated.
observedGeneration: 1
reason: Programmed
status: "True"
type: Programmed
name: https-listener
supportedKinds:
- group: gateway.networking.k8s.io
kind: HTTPRoute
创建网关后,为 contoso.com
创建 HTTPRoute 资源。
kubectl apply -f - <<EOF
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: websocket-example
namespace: test-infra
spec:
parentRefs:
- name: gateway-01
hostnames:
- "contoso.com"
rules:
- backendRefs:
- name: websocket-backend
port: 8080
EOF
创建 HTTPRoute 资源后,请确保 HTTPRoute 资源显示为“已接受”,并且适用于容器的应用程序网关为“已编程”。
kubectl get httproute websocket-example -n test-infra -o yaml
请验证是否已为 HTTPRoute 成功更新适用于容器的应用程序网关资源。
status:
parents:
- conditions:
- lastTransitionTime: "2023-06-19T22:18:23Z"
message: ""
observedGeneration: 1
reason: ResolvedRefs
status: "True"
type: ResolvedRefs
- lastTransitionTime: "2023-06-19T22:18:23Z"
message: Route is Accepted
observedGeneration: 1
reason: Accepted
status: "True"
type: Accepted
- lastTransitionTime: "2023-06-19T22:18:23Z"
message: Application Gateway For Containers resource has been successfully updated.
observedGeneration: 1
reason: Programmed
status: "True"
type: Programmed
controllerName: alb.networking.azure.io/alb-controller
parentRef:
group: gateway.networking.k8s.io
kind: Gateway
name: gateway-01
namespace: test-infra
在此示例中,运行状况检查在路径 /health
上公开。 若要确保适用于容器的应用程序网关可以验证此应用程序的运行状况,请为 HTTP 路径 /health
定义 HealthCheckPolicy 资源。
kubectl apply -f - <<EOF
apiVersion: alb.networking.azure.io/v1
kind: HealthCheckPolicy
metadata:
name: websockets-backend-health-check-policy
namespace: test-infra
spec:
targetRef:
group: ""
kind: Service
name: websocket-backend
namespace: test-infra
default:
interval: 5s
timeout: 3s
healthyThreshold: 1
unhealthyThreshold: 1
http:
path: /health
EOF
创建 HealthCheckPolicy 资源后,请确保 HealthCheckPolicy 资源显示为“已接受”,并且适用于容器的应用程序网关资源显示为“已编程”。
kubectl get httproute websocket-example -n test-infra -o yaml
请验证是否已成功更新适用于容器的应用程序网关资源 HealthCheckPolicy。
status:
conditions:
- lastTransitionTime: "2024-10-29T16:40:34Z"
message: Valid HealthCheckPolicy
observedGeneration: 1
reason: Accepted
status: "True"
type: Accepted
测试对应用程序的访问
现在,我们已准备好通过分配给前端的 FQDN 将一些流量发送到示例应用程序。 请使用以下命令获取 FQDN 并解析其 IP 地址。
fqdn=$(kubectl get gateway gateway-01 -n test-infra -o jsonpath='{.status.addresses[0].value}')
fqdnIp=$(dig +short $fqdn)
请获取 websocket 实用工具以发出 websocket 请求。 例如,将下载最新版本的实用工具,并将其标记为 Linux 主目录中的可执行文件,而不是在安装在计算机本地。 这样,便可以通过 Cloud Shell 使用该实用工具。
wget -O ~/websocat https://github.com/vi/websocat/releases/latest/download/websocat.x86_64-unknown-linux-musl
chmod a+x ~/websocat
请调用 websocket 实用工具以发出 websocket 请求。 在此示例中,聊天应用程序在路径 /chat
上公开,并在该路径上以发送的文本进行响应。
./websocat -t - --ws-c-uri=ws://contoso.com/chat ws-c:tcp:$fqdnIp:80
连接后,请键入 Hello world!!!
此时,我们应该会看到相同的响应:
Hello world!!!
请使用击键组合 Control + C 来退出 websocket 实用工具。
恭喜,你安装了 ALB 控制器,部署了后端应用程序,并使用 WebSocket 协议在适用于容器的应用程序网关上建立了与后端目标的连接。