服务网格Istio mTLS配置实战

服务网格Istio mTLS配置实战 服务网格Istio mTLS配置实战一、mTLS概述mTLSMutual TLS是一种双向认证机制服务之间相互验证身份确保通信安全。1.1 mTLS工作原理┌─────────────────┐ ┌─────────────────┐ │ Service A │ │ Service B │ │ (客户端) │ │ (服务端) │ └────────┬────────┘ └────────┬────────┘ │ │ │ 1. ClientHello (支持的加密套件) │ │─────────────────────────────────────│ │ │ │ 2. ServerHello 服务端证书 │ │─────────────────────────────────────│ │ │ │ 3. 客户端证书 ClientKeyExchange │ │─────────────────────────────────────│ │ │ │ 4. 服务端验证客户端证书 │ │ │ │ 5. 双方计算会话密钥 │ │ │ │ 6. 加密通信开始 │ │─────────────────────────────────────│ │ │ └───────────────────────────────────────┘1.2 Istio mTLS架构┌─────────────────────────────────────────────────────────────┐ │ Istio Control Plane │ │ ┌─────────────────┐ ┌─────────────────┐ │ │ │ Citadel │ │ Pilot │ │ │ │ (证书管理) │ │ (配置下发) │ │ │ └────────┬────────┘ └────────┬────────┘ │ └───────────┼─────────────────────┼─────────────────────────┘ │ │ ▼ ▼ ┌─────────────────────────────────────────────────────────────┐ │ Data Plane │ │ ┌─────────────────┐ ┌─────────────────┐ │ │ │ Envoy Proxy │────────────│ Envoy Proxy │ │ │ │ (sidecar) │ mTLS通信 │ (sidecar) │ │ │ │ Service A │ │ Service B │ │ │ └─────────────────┘ └─────────────────┘ │ └─────────────────────────────────────────────────────────────┘二、启用mTLS2.1 全局启用mTLSapiVersion: security.istio.io/v1beta1 kind: PeerAuthentication metadata: name: default namespace: istio-system spec: mtls: mode: STRICT2.2 命名空间级别配置apiVersion: security.istio.io/v1beta1 kind: PeerAuthentication metadata: name: default namespace: my-namespace spec: mtls: mode: PERMISSIVE2.3 工作负载级别配置apiVersion: security.istio.io/v1beta1 kind: PeerAuthentication metadata: name: my-service-policy namespace: my-namespace spec: selector: matchLabels: app: my-service mtls: mode: STRICT三、认证策略配置3.1 目标规则配置apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: my-service-dr namespace: my-namespace spec: host: my-service.my-namespace.svc.cluster.local trafficPolicy: tls: mode: ISTIO_MUTUAL3.2 虚拟服务配置apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: my-service-vs namespace: my-namespace spec: hosts: - my-service.my-namespace.svc.cluster.local http: - route: - destination: host: my-service.my-namespace.svc.cluster.local subset: v13.3 完整示例apiVersion: security.istio.io/v1beta1 kind: PeerAuthentication metadata: name: default namespace: istio-system spec: mtls: mode: STRICT --- apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: default namespace: istio-system spec: host: *.local trafficPolicy: tls: mode: ISTIO_MUTUAL四、证书管理4.1 查看证书# 查看Pod的证书 kubectl exec -it my-pod -c istio-proxy -- cat /etc/certs/cert-chain.pem # 查看证书有效期 kubectl exec -it my-pod -c istio-proxy -- openssl x509 -in /etc/certs/cert-chain.pem -text -noout4.2 证书轮换apiVersion: security.istio.io/v1beta1 kind: MeshPolicy metadata: name: default spec: peers: - mtls: credentialName: my-cert mode: STRICT4.3 自定义CAapiVersion: v1 kind: Secret metadata: name: custom-ca namespace: istio-system data: root-cert.pem: base64-encoded-root-cert cert-chain.pem: base64-encoded-cert-chain key.pem: base64-encoded-key五、mTLS策略优先级5.1 优先级顺序工作负载级别PeerAuthentication with selector命名空间级别PeerAuthentication without selector全局级别MeshPolicy5.2 策略组合示例# 全局策略 - 允许mTLS apiVersion: security.istio.io/v1beta1 kind: PeerAuthentication metadata: name: default namespace: istio-system spec: mtls: mode: PERMISSIVE # 命名空间策略 - 强制mTLS apiVersion: security.istio.io/v1beta1 kind: PeerAuthentication metadata: name: default namespace: sensitive spec: mtls: mode: STRICT # 工作负载策略 - 禁用mTLS apiVersion: security.istio.io/v1beta1 kind: PeerAuthentication metadata: name: legacy-service namespace: sensitive spec: selector: matchLabels: app: legacy mtls: mode: DISABLE六、验证mTLS6.1 使用istioctl验证# 检查mTLS配置 istioctl experimental analyze # 检查特定服务 istioctl pc secret my-pod -n my-namespace # 检查认证策略 istioctl get peerauthentication6.2 使用curl验证# 在Pod中测试mTLS连接 kubectl exec -it my-pod -c istio-proxy -- curl -v https://my-service.my-namespace.svc.cluster.local:80806.3 查看Envoy配置# 查看Envoy配置 istioctl pc routes my-pod -n my-namespace # 查看Envoy TLS配置 kubectl exec -it my-pod -c istio-proxy -- curl localhost:15000/config_dump | jq .configs[].dynamic_active_secrets七、mTLS性能优化7.1 连接复用apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: my-service namespace: my-namespace spec: host: my-service.my-namespace.svc.cluster.local trafficPolicy: connectionPool: http: maxRequestsPerConnection: 100 tls: mode: ISTIO_MUTUAL7.2 会话缓存apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: my-service namespace: my-namespace spec: host: my-service.my-namespace.svc.cluster.local trafficPolicy: tls: mode: ISTIO_MUTUAL maxSessionAge: 300s maxSessionAgeGrace: 50s7.3 证书缓存大小apiVersion: v1 kind: ConfigMap metadata: name: istio namespace: istio-system data: mesh: | accessLogFile: /dev/stdout defaultConfig: proxyMetadata: ISTIO_META_CERT_SDS_CACHE_SIZE: 1024八、故障排除8.1 常见问题问题原因解决方案连接拒绝mTLS模式不匹配检查PeerAuthentication和DestinationRule配置证书过期证书轮换失败检查Citadel日志手动轮换证书性能下降TLS握手开销启用连接复用和会话缓存证书验证失败CA配置错误检查Secret中的证书链8.2 日志分析# 查看Envoy日志 kubectl logs my-pod -c istio-proxy | grep -i tls # 查看Citadel日志 kubectl logs -n istio-system -l appcitadel # 查看Pilot日志 kubectl logs -n istio-system -l apppilot | grep -i mTLS8.3 调试工具# 使用istioctl诊断 istioctl diagnose # 检查Pod的Envoy配置 istioctl proxy-config secret my-pod -n my-namespace # 测试mTLS连接 kubectl exec -it my-pod -- istioctl experimental mTLS-check my-service.my-namespace.svc.cluster.local九、最佳实践9.1 配置建议逐步启用先使用PERMISSIVE模式再切换到STRICT分层策略根据安全要求为不同命名空间配置不同策略监控证书设置证书过期告警性能测试在生产环境启用前进行性能测试9.2 安全建议定期轮换证书建议90天轮换一次使用强加密套件禁用弱加密算法限制证书权限最小权限原则9.3 监控指标apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: name: istio-mtls namespace: istio-system spec: selector: matchLabels: app: istio-pilot endpoints: - port: http-monitoring path: /metrics interval: 30s通过合理配置Istio mTLS可以为服务间通信提供强大的安全保障同时保持良好的性能表现。