如何将自定义客户端证书用于 istio 的外部服务?

How can I use custom client certificate for external service with istio?

我需要设置从 kubernetes pod 到外部服务的双向 tls 通信。我的系统是运行istio系统。

我找到了关于这个的参考资料。

https://istio.io/docs/reference/config/networking/v1alpha3/destination-rule/#TLSSettings

apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: external-mtls
spec:
  host: *.external.com
  trafficPolicy:
    tls:
      mode: MUTUAL
      clientCertificate: /etc/certs/myclientcert.pem
      privateKey: /etc/certs/client_private_key.pem
      caCertificates: /etc/certs/rootcacerts.pem

根据此文档,我需要做的就是设置 MUTUAL 模式(而不是 ISTIO_MUTUAL)并设置证书文件。可以看到,clientCertificateprivateKeycaCertificates是本地文件路径。

我认为它们应该在 envoy proxy 的磁盘中。但是我找不到将我的证书文件放入 envoy 代理卷的方法。

我该怎么做?

你可以运行 istioctl kube-inject -f your-deployment.yaml > your-deployment-with-istio-sidecar.yaml.

然后编辑 your-deployment-with-istio-sidecar.yaml 并添加一些秘密证书的安装。然后根据您的证书创建机密。

或者,创建您的 sidecar 注入模板,请参阅 https://istio.io/blog/2019/data-plane-setup/#manual-injection

为证书创建秘密的示例:https://istio.io/docs/tasks/traffic-management/egress/egress-gateway-tls-origination/#redeploy-the-egress-gateway-with-the-client-certificates

从此处描述的秘密安装卷 https://kubernetes.io/docs/concepts/configuration/secret/#using-secrets-as-files-from-a-pod

我找到了解决方案。

  1. 创建秘密或配置映射
kubectl create secret generic my-cert --from-file=cert1.crt --from-file=cert2.crt
  1. 使用 sidecar.istio.io/userVolumeMount, sidecar.istio.io/userVolume[= 注释 Pod 或部署27=]
annotations:                                                                                       
  sidecar.istio.io/userVolumeMount: '[{"name":"my-cert", "mountPath":"/etc/my-cert", "readonly":true}]'
  sidecar.istio.io/userVolume: '[{"name":"my-cert", "secret":{"secretName":"my-cert"}}]'

关于这些注释和其他注释的文档:https://preliminary.istio.io/docs/reference/config/annotations/

完成。它已安装到 envoy 代理 pod。