One minute
傳輸全程加密:從 ALB 到 Pod、再到 Redis 與 MongoDB 的 TLS 設定
客戶要求 encryption in transit 不只到 Load Balancer,而是一路到 Pod 與資料庫。記錄 ALB SSL policy、cert-manager、TLS sidecar 與資料庫 TLS 的做法
一般的 Kubernetes 部署,TLS 通常在 Load Balancer 就結束了:
使用者 ──HTTPS──> ALB ──HTTP──> Pod ──明文──> Redis / MongoDB
八月客戶的資安要求寫得很清楚:傳輸中的資料要全程加密,上圖每一段都要走 TLS。
使用者到 ALB
這段本來就是 HTTPS,要看的是 SSL policy。預設的 policy 可能還允許 TLS 1.0、1.1。AWS Load Balancer Controller 可以用 annotation 指定:
metadata:
annotations:
alb.ingress.kubernetes.io/ssl-policy: ELBSecurityPolicy-TLS13-1-2-2021-06
後來 AWS 出了支援後量子(PQ)演算法的 policy(ELBSecurityPolicy-TLS13-1-3-PQ-2025-09),十月我們也換上去了。
ALB 到 Pod
ALB 的 backend protocol 改成 HTTPS,Pod 自己就要有憑證。我分幾步做:
- 裝 cert-manager
- 建一條內部 CA(自簽 root 加 issuer),包成可以替換的 chart,客戶想用自己的 CA 時換掉這層就好
- 本身不處理 TLS 的服務,前面加一個 nginx TLS sidecar
- 前端本來就有 nginx,直接在 nginx 終止 TLS
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: gateway-tls
spec:
secretName: gateway-tls
issuerRef:
name: internal-ca
kind: ClusterIssuer
dnsNames:
- gateway.app.svc.cluster.local
# Ingress
alb.ingress.kubernetes.io/backend-protocol: HTTPS
加了 sidecar 之後,Socket.IO 一直連不上。查了一下是 nginx 沒把 Upgrade header 轉過去:
location / {
proxy_pass http://127.0.0.1:8000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
Pod 到資料庫
ElastiCache(Redis)開了 in-transit encryption 之後,client 要用 rediss://(兩個 s)或設 ssl=True:
redis.Redis(host=host, port=6379, ssl=True, ssl_cert_reqs="required")
要不要開 TLS 我是依 provider 判斷,託管服務一律開,本機開發用的 Redis 就不用。
DocumentDB 要下載 AWS 的 CA bundle,在連線字串裡指定:
mongodb://user:pass@host:27017/?tls=true&tlsCAFile=/certs/global-bundle.pem&retryWrites=false
retryWrites=false 是 DocumentDB 才需要的,下一篇會再提。
Amazon MQ(RabbitMQ)用 amqps:// 和 5671 port。
確認有沒有生效
# 確認 Pod 真的在講 TLS
kubectl exec -it deploy/gateway -- openssl s_client -connect localhost:8443 </dev/null
整件事我是一段一段做的,每做完一段就驗一次,出問題比較好定位。
comments powered by Disqus