配置客户端和 APISIX 之间的 HTTPS
TLS (传输层安全性协议) 是一种旨在保护两个实体之间通信的加密协议。在客户端和 APISIX 之间强制执行 HTTPS 可提高数据传输过程中的安全性和真实性。
本指南将向你展示如何在客户端应用程序和 APISIX 之间配置 HTTPS。

前置条件
创建路由
创建一个将所有请求 /ip 转发到上游 httpbin.org 的路由:
- Admin API
- ADC
- Ingress Controller
curl -i "http://127.0.0.1:9180/apisix/admin/routes" -X PUT -d '
{
"id": "quickstart-client-ip",
"uri": "/ip",
"upstream": {
"nodes": {
"httpbin.org:80":1
},
"type": "roundrobin"
}
}'
HTTP/1.1 200 OK 响应验证了路由已成功创建。
services:
- name: httpbin Service
routes:
- uris:
- /ip
name: quickstart-client-ip
upstream:
type: roundrobin
nodes:
- host: httpbin.org
port: 80
weight: 1
将配置同步到 APISIX:
adc sync -f adc-service.yaml
- 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: quickstart-client-ip
spec:
parentRefs:
- name: apisix
rules:
- matches:
- path:
type: Exact
value: /ip
backendRefs:
- name: httpbin-external-domain
port: 80
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
namespace: ingress-apisix
name: httpbin-external-domain
spec:
ingressClassName: apisix
externalNodes:
- type: Domain
name: httpbin.org
---
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
namespace: ingress-apisix
name: quickstart-client-ip
spec:
ingressClassName: apisix
http:
- name: quickstart-client-ip
match:
paths:
- /ip
upstreams:
- name: httpbin-external-domain
将配置应用到你的集群:
kubectl apply -f httpbin-route.yaml
生成证书和密钥
生成证书颁发机构 (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
生成具有通用名称 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
为 APISIX 配置 HTTPS
你可以选择将 server.crt 和 server.key 的内容加载到环境变量中:
export server_cert=$(cat server.crt)
export server_key=$(cat server.key)
- Admin API
- ADC
- Ingress Controller
创建一个 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:test.com,与服务器证书 CN 值相同
❷ cert:服务器证书
❸ key:服务器证书的私钥
ssls:
- snis:
// Annotate 1
- test.com
certificates:
// Annotate 2
- certificate: ${server_cert}
// Annotate 3
key: ${server_key}
❶ sni:test.com,与服务器证书 CN 值相同
❷ cert:服务器证书
❸ key:服务器证书的私钥
将配置同步到 APISIX:
adc sync -f adc-ssl.yaml -f adc-service.yaml
如果你尚未启用网关的 SSL,请启用它:
helm upgrade apisix apisix/apisix \
--namespace ingress-apisix \
--set apisix.deployment.role=traditional \
--set apisix.deployment.role_traditional.config_provider=yaml \
--set etcd.enabled=false \
--set ingress-controller.enabled=true \
--set ingress-controller.config.provider.type=apisix-standalone \
--set ingress-controller.apisix.adminService.namespace=ingress-apisix \
--set ingress-controller.gatewayProxy.createDefault=true \
--set apisix.ssl.enabled=true
将 SSL 端口转发到你的主机:
kubectl port-forward svc/apisix-gateway 9443:443 &
创建一个 Kubernetes TLS secret:
kubectl create secret tls test-tls-secret \
--cert=server.crt \
--key=server.key \
--namespace=ingress-apisix
- 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 之间的 HTTPS
由于证书仅对 CN test.com 有效,你应该使用 test.com 作为托管 APISIX 的域名。
向 https://test.com:9443/ip 发送请求并将 test.com 解析为 127.0.0.1:
curl -ikv --resolve "test.com:9443:127.0.0.1" "https://test.com:9443/ip"
类似于以下的 TLS 握手过程验证了客户端和 APISIX 之间的 TLS 已启用:
* 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...
* Connected to test.com (127.0.0.1) port 9443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
* CAfile: /etc/ssl/certs/ca-certificates.crt
* CApath: /etc/ssl/certs
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use h2
* Server certificate:
* subject: CN=test.com
* start date: Apr 21 07:47:54 2023 GMT
* expire date: Mar 28 07:47:54 2123 GMT
* issuer: CN=ROOTCA
* SSL certificate verify result: unable to get local issuer certificate (20), continuing anyway.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x556274d632e0)
> GET /ip HTTP/2
> Host: test.com:9443
> user-agent: curl/7.74.0
> accept: */*
>
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* old SSL session ID is stale, removing
* Connection state changed (MAX_CONCURRENT_STREAMS == 128)!
< HTTP/2 200
HTTP/2 200
...
下一步
你可以在 SSL 证书 中了解更多关于 TLS 和 APISIX SSL 对象的信息。
APISIX 还支持客户端和 APISIX 之间的 mTLS 连接。有关更多详细信息,请参阅 配置客户端和 APISIX 之间的 mTLS。