Istio 中 mTLS 设置方法
在默认情况下,istio中sidecar之间的相互访问,是采用automtls模式,即如果client和server端的pod都有sidecar的话,会采用mtls来通信,当client没有sidecar,而server中有sidecar,则也可以采用明文通信。
然而,有的客户为了提高通信性能,希望在mesh内部(部分)服务之间互访全部用明文,无论是否有sidecar。而也有客户处于安全性考虑,希望(部分)服务之间互访都通过mtls方式访问(前提是服务pod都注入了sidecar)。
istio主要提供了多种配置方式,来供用户使用:
- 修改全局配置
- 通过DestinationRule配置
- 通过PeerAuthentication配置
1 在全局开启自动mtls(自适应)
全局配置是通过全局配置 configmap istio
中的 enableAutoMtls
来配置,配置方法如下:
|
|
在istio中,默认情况下 enableAutoMtls
的值为 true
,上述配置中将其设置为 false
,将全局 mtls
关闭。下面是 enableAutoMtls
字段的官方介绍:
enabelAutoMtls:
This flag is used to enable mutual TLS automatically for service to service communication within the mesh, default true. If set to true, and a given service does not have a corresponding DestinationRule configured, or its DestinationRule does not have ClientTLSSettings specified, Istio configures client side TLS configuration appropriately. More specifically, If the upstream authentication policy is in STRICT mode, use Istio provisioned certificate for mutual TLS to connect to upstream. If upstream service is in plain text mode, use plain text. If the upstream authentication policy is in PERMISSIVE mode, Istio configures clients to use mutual TLS when server sides are capable of accepting mutual TLS traffic. If service DestinationRule exists and has ClientTLSSettings specified, that is always used instead.
官方文档见:Istio / Global Mesh Options
2 Destination Rule
主要设置workload作为client时的 mTLS
的配置:
|
|
mode
的取值:
Name | Description |
---|---|
DISABLE | Do not setup a TLS connection to the upstream endpoint. |
SIMPLE | Originate a TLS connection to the upstream endpoint. |
MUTUAL | Secure connections to the upstream using mutual TLS by presenting client certificates for authentication. |
ISTIO_MUTUAL | Secure connections to the upstream using mutual TLS by presenting client certificates for authentication. Compared to Mutual mode, this mode uses certificates generated automatically by Istio for mTLS authentication. When this mode is used, all other fields in ClientTLSSettings should be empty. |
同样以default 命名空间中 helloworld 服务为例,如果针对 helloworld 服务设置 trafficPolicy
后,会在访问helloworld的所有client中的 cds
配置中发生变化。

3 PeerAuthentication
设置workload中 mtls
的配置,主要设置workload作为server时的场景
|
|
mode的取值:
取值 | 功能描述 |
---|---|
UNSET | 如果父级配置中已经设置该的值,继承父级的值, 否则默认为 PERMISSIVE. |
DISABLE | 禁用 mTLS |
PERMISSIVE | 连接可以根据具体情况,选择plaintext 或 mTLS 默认通信. |
STRICT | 采用mTLS 隧道通信 (client必须有证书). |
下面分析一下通过 PeerAuthentication
设置禁用mTLS后,在envoy的config_dump中的变化。
以default 命名空间中 helloworld 服务为例,如果全局服务设置取消(DISABLE)mtls
后,会在访问helloworld的所有client中的 CDS
(客户端生效) 和 LDS
(服务端生效)的配置中发生变化。
|
|
CDS
中(客户端生效),会去掉tls相关的配置,如下图左侧所示:

LDS
中(服务端生效),默认情况下,在 VirtualInbound 中会配置 mTLS
相关的配置,识别请求数据是否为 mTLS
,并采用相应的方式解码数据。当 mode
设置为 DISABLE
时,LDS
中会去掉TLS相关的配置,如下图左侧所示:


通过 PeerAuthentication
配置 mTLS
时,在client端和server端的envoy中都会有相应的配置变更,主要变更点是 transportSocketMatches
配置项 ,在CDS和LDS中,都有该配置项,下面是两处配置的对比:

4 DestinationRule 和 PeerAuthentication 的设置 mTLS
的区别
DestinationRule, 主要针对有sidecar的client端,设置tls规则,只在client端生效,告诉client端,用何种方式来访问客户端。服务端可以是网格内的服务,也可以是网格外的服务。另外,DestinationRule 主要作用在 CDS
中生效,所以只在 client 端生效。
PeerAuthentication,则针对网格内所有sidecar的client端和server端都会生效。PeerAuthentication 在 CDS
和LDS中都会有相应的配置,所以在client端和server端都会有相应的配置。
5 其他问题
-
如果用PeerAuthentication设置全局严格 mtls后,访问外部网址如何设置?
client端,默认情况下,是既支持明文,也支持tls,默认tls,如果访问外部服务,还需要配置DestinationRule来配置。
-
DestinationRule 和 PeerAuthentication 同时设置,client端的设置以谁为准呢?
client端,以DestinationRule为准。