配置客户端和 APISIX 之间的 HTTP/3 QUIC
HTTP/3 是超文本传输协议 (HTTP) 的第三个主要版本。与依赖 TCP 的前身不同,HTTP/3 基于 QUIC (Quick UDP Internet Connections) 协议。它带来了多项好处,共同导致延迟降低和性能提高:
- 实现不同网络连接之间的无缝转换,例如从 Wi-Fi 切换到移动数据。
- 消除队头阻塞,使丢失的数据包不会阻塞所有流。
- 在 TLS 握手的同时协商 TLS 版本,从而实现更快的连接。
- 默认提供加密,确保通过 HTTP/3 连接传输的所有数 据都受到保护和保密。
- 在与客户端已建立连接的服务器通信时提供零往返时间 (0-RTT)。
APISIX 目前支持下游客户端和 APISIX 之间的 HTTP/3 连接。尚不支持与上游服务的 HTTP/3 连接。
此功能目前处于实验阶段,不建议用于生产环境。
本指南将向你展示如何配置 APISIX 以启用客户端和 APISIX 之间的 HTTP/3 连接。
前置条件
- 安装 Docker。
- 安装 static-curl 或其他支持 HTTP/3 的 curl。
- 按照 快速入门教程 在 Docker 或 Kubernetes 中启动一个新的 APISIX 实例。
在 APISIX 中启用 HTTP/3
- Docker
- Kubernetes
通过将以下配置添加到 APISIX 的 config.yaml 配置文件中,在端口 9443 上启用 HTTP/3:
docker exec apisix-quickstart /bin/sh -c "echo '
apisix:
enable_control: true
control:
ip: 0.0.0.0
port: 9092
ssl:
listen:
- port: 9443
enable_http3: true
deployment:
role: traditional
role_traditional:
config_provider: etcd
admin:
admin_key_required: false
allow_admin:
- 0.0.0.0/0
plugin_attr:
prometheus:
export_addr:
ip: 0.0.0.0
port: 9091
' > /usr/local/apisix/conf/config.yaml"
重新加载 APISIX 以使配置更改生效:
docker exec apisix-quickstart apisix reload
要更新网关的 SSL 和 HTTP3 配置,首先导出所有值(包括默认值):
helm get values -n ingress-apisix apisix --all > values.yaml
在 values 文件中,更新以下部分值如下:
apisix:
ssl:
enabled: true
containerPort: 9443
enableHTTP3: true
升级发布:
helm upgrade -n ingress-apisix apisix apisix/apisix -f values.yaml
生成证书和密钥
HTTP/3 需要 TLS。你可以购买证书或自行生成证书,视情况而定。
要自行生成,首先生成证书颁发机构 (CA) 密钥和证书:
openssl genrsa -out ca.key 2048 && \
openssl req -new -sha256 -key ca.key -out ca.csr -subj "/CN=ROOTCA" && \
openssl x509 -req -days 36500 -sha256 -extensions v3_ca -signkey ca.key -in ca.csr -out ca.crt
接下来,为 APISIX 生成通用名称为 test.com 的密钥和证书,并使用 CA 证书进行签名:
openssl genrsa -out server.key 2048 && \
openssl req -new -sha256 -key server.key -out server.csr -subj "/CN=test.com" && \
openssl x509 -req -days 36500 -sha256 -extensions v3_req \
-CA ca.crt -CAkey ca.key -CAserial ca.srl -CAcreateserial \
-in server.csr -out server.crt
配置 HTTPS
- Admin API
- Ingress Controller
你可以选择将 server.crt 和 server.key 的内容加载到 shell 变量中:
server_cert=$(cat server.crt)
server_key=$(cat server.key)
创建一个 SSL 证书对象以保存服务器证书及其密钥:
curl -i "http://127.0.0.1:9180/apisix/admin/ssls" -X PUT -d '
{
"id": "quickstart-tls-client-ssl",
// Annotate 1
"sni": "test.com",
// Annotate 2
"cert": "'"${server_cert}"'",
// Annotate 3
"key": "'"${server_key}"'"
}'
❶ 配置 SNI 以匹配服务器证书 CN。
❷ 配置服务器证书。
❸ 配置服务器证书的私钥。
使用提供的证书和私钥文件生成名为 test-tls-secret 的 Kubernetes TLS secret,并将清单输出到 ingress-apisix 命名空间中的 secret.yaml:
kubectl create secret tls test-tls-secret \
--cert=server.crt \
--key=server.key \
--namespace=ingress-apisix \
--dry-run=client -o yaml > secret.yaml
将 secret 应用到你的集群:
kubectl apply -f secret.yaml
- Gateway API
- APISIX CRD
按如下方式更新你的 Gateway 清单文件:
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
namespace: ingress-apisix
name: apisix
spec:
gatewayClassName: apisix
listeners:
- name: http
protocol: HTTP
port: 80
- name: https
protocol: HTTPS
port: 443
hostname: test.com
tls:
certificateRefs:
- kind: Secret
group: ""
name: test-tls-secret
infrastructure:
parametersRef:
group: apisix.apache.org
kind: GatewayProxy
name: apisix-config
请注意,Gateway 监听器中的 port 是必需的,但被忽略。这是由于数据平面的限制:它无法动态打开新端口。由于 Ingress Controller 不管理数据平面部署,它无法自动更新配置或重新启动数据平面以应用端口更改。
将配置应用到你的集群:
kubectl apply -f gateway.yaml
为 TLS 配置创建一个 Kubernetes 清单文件:
apiVersion: apisix.apache.org/v2
kind: ApisixTls
metadata:
namespace: ingress-apisix
name: test-tls
spec:
ingressClassName: apisix
hosts:
- test.com
secret:
name: test-tls-secret
namespace: ingress-apisix
将配置应用到你的集群:
kubectl apply -f tls.yaml
在 APISIX 中创建路由
创建一个指向 httpbin.org 的示例路由:
- Admin API
- Ingress Controller
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT -d '
{
"id":"httpbin-route",
"uri":"/get",
"upstream": {
"type":"roundrobin",
"nodes": {
"httpbin.org:80": 1
}
}
}'
- Gateway API
- APISIX CRD
apiVersion: v1
kind: Service
metadata:
namespace: ingress-apisix
name: httpbin-external-domain
spec:
type: ExternalName
externalName: httpbin.org
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
namespace: ingress-apisix
name: httpbin-route
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /get
backendRefs:
- name: httpbin-external-domain
port: 80
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: ingress-apisix
name: httpbin-external-domain
spec:
externalNodes:
- type: Domain
name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: ingress-apisix
name: httpbin-route
spec:
ingressClassName: apisix
http:
- name: httpbin-route
match:
paths:
- /get
upstreams:
- name: httpbin-external-domain
将配置应用到你的集群:
kubectl apply -f httpbin-route.yaml
验证客户端和 APISIX 之间的 HTTP/3 连接
- Docker
- Kubernetes
向路由发送请求:
curl -kv --http3-only \
-H "Host: test.com" \
--resolve "test.com:9443:127.0.0.1" "https://test.com:9443/get"
kubectl port-forward 仅支持 TCP,不支持 UDP,而 HTTP/3 (QUIC) 需要 UDP。要测试 HTTP/3 连接,你可以通过 NodePort 公开服务:
kubectl expose deployment apisix \
--name=apisix-gateway-quic \
--port=9443 \
--target-port=9443 \
--protocol=UDP \
--type=NodePort
查找你的节点 IP:
kubectl get svc apisix-gateway-quic
向路由发送请求:
curl -kv --http3-only \
-H "Host: test.com" \
--resolve "test.com:9443:${YOUR_NODE_IP}" "https://test.com:9443/get"
你应该收到类似以下的 HTTP/3 200 响应:
* Added test.com:9443:127.0.0.1 to DNS cache
* Hostname test.com was found in DNS cache
* Trying 127.0.0.1:9443...
* QUIC cipher selection: TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_CCM_SHA256
* Skipped certificate verification
* Connected to test.com (127.0.0.1) port 9443
* using HTTP/3
* [HTTP/3] [0] OPENED stream for https://test.com:9443/get
* [HTTP/3] [0] [:method: GET]
* [HTTP/3] [0] [:scheme: https]
* [HTTP/3] [0] [:authority: test.com]
* [HTTP/3] [0] [:path: /get]
* [HTTP/3] [0] [user-agent: curl/8.7.1]
* [HTTP/3] [0] [accept: */*]
> GET /get HTTP/3
> Host: test.com
> User-Agent: curl/8.7.1
> Accept: */*
>
* Request completely sent off
< HTTP/3 200
...
{
"args": {},
"headers": {
"Accept": "*/*",
"Content-Length": "0",
"Host": "test.com",
"User-Agent": "curl/8.7.1",
"X-Amzn-Trace-Id": "Root=1-6656013a-27da6b6a34d98e3e79baaf5b",
"X-Forwarded-Host": "test.com"
},
"origin": "172.19.0.1, 123.40.79.456",
"url": "http://test.com/get"
}
* Connection #0 to host test.com left intact
下一步
你已了解如何配置客户端和 APISIX 之间的 HTTP/3。与上游服务的 HTTP/3 连接尚不支持,欢迎任何贡献。