容器与云原生 / 系统课时
治理入口 TLS 流量
治理入口 TLS 流量
入口 TLS 治理的关键不是“把 443 打开”,而是先决定证书在哪里终止。HTTPS 终止后可以做七层路由;TLS 透传则保持业务 TLS 密文,只能依据 SNI 等握手信息选择后端。下面分别配置两种模式,并用可复制的命令验证路由边界。
学习目标
- 能区分 HTTPS 终止与 TLS 透传的处理边界。
- 能使用
credentialName为入口网关引用证书。 - 能分别用 HTTP 路由和 SNI 路由验证两种模式。
先分清终止与透传
HTTPS 终止时,客户端到 Istio ingress gateway 的 TLS 会在网关解密,后续 VirtualService 可以按 Host、路径、Header 等 HTTP 属性路由。TLS 透传时,网关不持有业务证书,也看不到加密后的 HTTP 内容,只能在 TLS 握手阶段根据 SNI 等信息选择目标端点。

两种模式最容易混淆的是 VirtualService 的路由类型:终止 TLS 后使用 http;保持透传时使用 tls。如果在终止模式下仍写 tls 匹配,解密后的 HTTP 请求不会按预期命中该规则。
配置 HTTPS 终止
下面假设集群已安装 Istio,入口工作负载使用标准选择器 istio: ingressgateway,业务服务为 web.default.svc.cluster.local:8080,并且 /health 返回 ok。使用共享 ingress gateway 时,credentialName 引用的 TLS Secret 应放在该网关工作负载能够读取的命名空间;默认安装通常是 istio-system。
先创建 TLS Secret。将 <PATH> 替换为证书文件所在目录。
kubectl -n istio-system create secret tls web-example-cert \
--cert=<PATH>/tls.crt \
--key=<PATH>/tls.key预期输出
secret/web-example-cert created创建 gateway-https.yaml。Gateway 在 443 端口以 SIMPLE 模式终止 TLS,VirtualService 随后用 HTTP 规则把流量送到后端。
apiVersion: networking.istio.io/v1
kind: Gateway
metadata:
name: web-https-gateway
namespace: default
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 443
name: https
protocol: HTTPS
hosts:
- web.example.com
tls:
mode: SIMPLE
credentialName: web-example-cert
---
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
name: web-https-route
namespace: default
spec:
hosts:
- web.example.com
gateways:
- web-https-gateway
http:
- route:
- destination:
host: web.default.svc.cluster.local
port:
number: 8080应用配置。
kubectl apply -f gateway-https.yaml首次应用时预期输出
gateway.networking.istio.io/web-https-gateway created
virtualservice.networking.istio.io/web-https-route created使用入口地址验证。将 <IP> 替换为 ingress gateway 的外部地址,将 <PATH>/ca.crt 替换为签发测试证书的 CA 文件。--resolve 会把连接定向到指定入口地址,同时保留 web.example.com 的 Host 与 TLS SNI。
curl --cacert <PATH>/ca.crt \
--resolve web.example.com:443:<IP> \
https://web.example.com/health预期输出
ok如果证书的 SAN 不包含 web.example.com,或者 Secret 不在入口网关可读取的位置,TLS 握手会在到达 HTTP 路由之前失败。此时应先检查证书、Secret 与网关绑定关系,再排查 VirtualService。
配置 TLS 透传
透传模式不在入口网关终止 TLS,因此不配置 credentialName,后端必须自己监听 TLS。下面作为另一种独立模式,假设 secure-app.default.svc.cluster.local:8443 会为 passthrough.example.com 提供证书并完成 TLS 握手。
apiVersion: networking.istio.io/v1
kind: Gateway
metadata:
name: secure-passthrough-gateway
namespace: default
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 443
name: tls-passthrough
protocol: TLS
hosts:
- passthrough.example.com
tls:
mode: PASSTHROUGH
---
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
name: secure-passthrough-route
namespace: default
spec:
hosts:
- passthrough.example.com
gateways:
- secure-passthrough-gateway
tls:
- match:
- port: 443
sniHosts:
- passthrough.example.com
route:
- destination:
host: secure-app.default.svc.cluster.local
port:
number: 8443保存为 gateway-passthrough.yaml 后应用。
kubectl apply -f gateway-passthrough.yaml首次应用时预期输出
gateway.networking.istio.io/secure-passthrough-gateway created
virtualservice.networking.istio.io/secure-passthrough-route created用 SNI 发起握手,读取服务端返回证书的 Subject。透传模式下,入口网关不会替换业务证书,因此这里看到的应是后端 TLS 服务提供的证书;实际 Subject 会随证书变化。
openssl s_client -connect <IP>:443 -servername passthrough.example.com </dev/null 2>/dev/null \
| openssl x509 -noout -subject预期输出
subject=<VALUE>选择模式时守住路由边界
需要在网关做路径改写、JWT 校验、Header 治理或细粒度 HTTP 灰度时,应先终止 TLS,再进入七层路由;需要让业务 TLS 一直由后端终止、证书只能由后端持有时,选择透传,并把入口治理能力限制在 SNI 与连接级别。客户端证书认证可以使用 MUTUAL 等终止模式,但它仍属于“网关完成 TLS 处理后再做 HTTP 路由”的范畴。