Istio mTLS 加密:启用服务间通信加密与证书自动轮换的配置步骤

Istio 的服务间 mTLS(mutual Transport Layer Security)加密能确保 Kubernetes 集群中的微服务通信安全,防止窃听和篡改。同时,Istio 内置的证书管理组件(如 Istiod)默认支持证书自动轮换,无需手动干预。以下配置步骤基于 Istio 1.10+ 版本,确保结构清晰、可靠。请确保已安装 Istio 和 kubectl 工具。

步骤 1: 前提条件
  • 确认 Istio 安装:确保 Istio 已部署在 Kubernetes 集群中。如果未安装,使用 istioctl 快速安装:
    istioctl install --set profile=demo -y
    

    验证安装:
    kubectl get pods -n istio-system
    

  • 启用证书自动管理:Istio 默认使用 Istiod 自动颁发和轮换证书(轮换周期通常为 90 天)。无需额外配置,但需确认:
    kubectl get deployment istiod -n istio-system -o yaml | grep "cert-manager"
    

    输出应包含 cert-manager 相关组件。
步骤 2: 启用服务间 mTLS 加密

通过创建 PeerAuthentication 策略启用 mTLS,确保所有服务间通信强制加密。以下示例在默认命名空间启用严格 mTLS。

  • 创建 PeerAuthentication 策略

    1. 编写 YAML 文件(例如 mtls-strict.yaml):
      apiVersion: security.istio.io/v1beta1
      kind: PeerAuthentication
      metadata:
        name: default
        namespace: istio-system
      spec:
        mtls:
          mode: STRICT
      

      此策略将全局启用严格 mTLS(对所有命名空间生效)。
    2. 应用策略:
      kubectl apply -f mtls-strict.yaml
      

  • 可选:按命名空间启用
    如果仅需在特定命名空间启用,修改 metadata.namespace 为您的命名空间(如 default)。

步骤 3: 配置证书自动轮换(默认启用)

Istio 自动处理证书轮换,但可自定义轮换参数(如周期)。通常无需修改,但以下是调整方法:

  • 查看默认证书设置

    kubectl get secret istio-ca-secret -n istio-system -o jsonpath='{.data.ca-cert\.pem}' | base64 --decode | openssl x509 -text -noout
    

    输出中检查 Validity 字段(默认有效期 90 天)。

  • 自定义轮换周期(可选)
    通过 IstioOperator 配置(例如,将轮换周期改为 60 天):

    1. 创建配置文件 custom-operator.yaml
      apiVersion: install.istio.io/v1alpha1
      kind: IstioOperator
      spec:
        components:
          pilot:
            k8s:
              env:
              - name: CERT_ROTATION_INTERVAL
                value: "1440h"  # 60 天(单位:小时)
      

    2. 应用配置:
      istioctl install -f custom-operator.yaml
      

步骤 4: 验证配置

确保 mTLS 和证书轮换工作正常:

  • 检查 mTLS 状态

    istioctl authn tls-check <your-service-pod>.<namespace>  # 替换为实际服务 Pod 名称和命名空间
    

    输出应显示 mTLS is enabledCONFIGURED 状态。

  • 验证证书轮换

    1. 模拟证书过期(可选):
      kubectl delete secret istio-ca-secret -n istio-system
      

      Istiod 会自动重新生成证书。
    2. 监控轮换日志:
      kubectl logs -l app=istiod -n istio-system | grep "certificate rotation"
      

      输出应包含 Certificate rotated successfully
注意事项
  • 兼容性:确保所有服务注入 Istio Sidecar(使用 istio-injection=enabled 标签)。
  • 性能影响:mTLS 可能轻微增加延迟,但 Istio 优化后影响可忽略。
  • 故障排查
    • 如果 mTLS 失败,检查策略是否冲突:kubectl get peerauthentication --all-namespaces
    • 证书问题:重启 Istiod Pod 以触发轮换:kubectl rollout restart deployment/istiod -n istio-system
  • 备份:定期备份集群状态,以防配置错误。

通过以上步骤,您已成功启用 Istio 服务间 mTLS 加密并确保证书自动轮换。Istio 的自动化机制减少了维护负担,同时提升了安全性。如需更多细节,参考 Istio 官方文档

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐