侧车
Sidecar
描述了与之关联的工作负载实例的入站和出站通信进行中介的侧车代理的配置。默认情况下,Istio 将使用到达网格中每个工作负载实例所需的必要配置以及在与工作负载关联的所有端口上接受流量来对网格中的所有侧车代理进行编程。Sidecar
配置提供了一种方法来微调代理在将流量转发到工作负载和从工作负载转发流量时将接受的端口和协议集。此外,可以限制代理在转发工作负载实例的出站流量时可以到达的服务集。
网格中的服务和配置组织到一个或多个命名空间中(例如,Kubernetes 命名空间或 CF org/space)。命名空间中的 Sidecar
配置将应用于同一命名空间中的一个或多个工作负载实例,这些实例使用 workloadSelector
字段进行选择。在没有 workloadSelector
的情况下,它将应用于同一命名空间中的所有工作负载实例。在确定要应用于工作负载实例的 Sidecar
配置时,将优先考虑选择此工作负载实例的具有 workloadSelector
的资源,而不是任何 workloadSelector
都没有的 Sidecar
配置。
注意 1:每个命名空间只能有一个没有 workloadSelector
的 Sidecar
配置,该配置指定该命名空间中所有 Pod 的默认值。建议为命名空间范围的侧车使用名称 default
。如果在给定命名空间中存在多个无选择器的 Sidecar
配置,则系统的行为未定义。如果两个或多个具有 workloadSelector
的 Sidecar
配置选择同一个工作负载实例,则系统的行为未定义。
注意 2:在MeshConfig
根命名空间 中的Sidecar
配置将默认应用于所有没有Sidecar
配置的命名空间。此全局默认Sidecar
配置不应有任何workloadSelector
。
注意 3:即使网关是istio-代理,Sidecar
也不适用于网关。
以下示例在名为istio-config
的根命名空间中声明了一个全局默认Sidecar
配置,该配置将所有命名空间中的sidecar配置为仅允许出站流量到同一命名空间中的其他工作负载以及istio-system
命名空间中的服务。
apiVersion: networking.istio.io/v1
kind: Sidecar
metadata:
name: default
namespace: istio-config
spec:
egress:
- hosts:
- "./*"
- "istio-system/*"
以下示例在prod-us1
命名空间中声明了一个Sidecar
配置,该配置覆盖了上面定义的全局默认值,并将该命名空间中的sidecar配置为允许出站流量到prod-us1
、prod-apis
和istio-system
命名空间中的公共服务。
apiVersion: networking.istio.io/v1
kind: Sidecar
metadata:
name: default
namespace: prod-us1
spec:
egress:
- hosts:
- "prod-us1/*"
- "prod-apis/*"
- "istio-system/*"
以下示例在prod-us1
命名空间中声明了一个Sidecar
配置,用于所有具有标签app: ratings
且属于ratings.prod-us1
服务的Pod。工作负载在端口9080上接受入站HTTP流量。然后,流量被转发到在Unix域套接字上侦听的附加工作负载实例。在出站方向,除了istio-system
命名空间之外,sidecar代理仅代理绑定到端口9080的服务在prod-us1
命名空间中的HTTP流量。
apiVersion: networking.istio.io/v1
kind: Sidecar
metadata:
name: ratings
namespace: prod-us1
spec:
workloadSelector:
labels:
app: ratings
ingress:
- port:
number: 9080
protocol: HTTP
name: somename
defaultEndpoint: unix:///var/run/someuds.sock
egress:
- port:
number: 9080
protocol: HTTP
name: egresshttp
hosts:
- "prod-us1/*"
- hosts:
- "istio-system/*"
如果工作负载在没有基于IPTables的流量捕获的情况下部署,则Sidecar
配置是配置附加到工作负载实例的代理上的端口的唯一方法。以下示例在prod-us1
命名空间中声明了一个Sidecar
配置,用于所有具有标签app: productpage
且属于productpage.prod-us1
服务的Pod。假设这些Pod是在没有IPtable规则(即istio-init
容器)的情况下部署的,并且代理元数据ISTIO_META_INTERCEPTION_MODE
设置为NONE
,则以下规范允许此类Pod在端口9080(包装在Istio双向TLS中)接收HTTP流量,并将其转发到侦听127.0.0.1:8080
的应用程序。它还允许应用程序与127.0.0.1:3306
上的后端MySQL数据库通信,然后将其代理到mysql.foo.com:3306
处的外部托管MySQL服务。
apiVersion: networking.istio.io/v1
kind: Sidecar
metadata:
name: no-ip-tables
namespace: prod-us1
spec:
workloadSelector:
labels:
app: productpage
ingress:
- port:
number: 9080 # binds to proxy_instance_ip:9080 (0.0.0.0:9080, if no unicast IP is available for the instance)
protocol: HTTP
name: somename
defaultEndpoint: 127.0.0.1:8080
captureMode: NONE # not needed if metadata is set for entire proxy
egress:
- port:
number: 3306
protocol: MYSQL
name: egressmysql
captureMode: NONE # not needed if metadata is set for entire proxy
bind: 127.0.0.1
hosts:
- "*/mysql.foo.com"
以及与路由到mysql.foo.com:3306
关联的服务条目。
apiVersion: networking.istio.io/v1
kind: ServiceEntry
metadata:
name: external-svc-mysql
namespace: ns1
spec:
hosts:
- mysql.foo.com
ports:
- number: 3306
name: mysql
protocol: MYSQL
location: MESH_EXTERNAL
resolution: DNS
还可以在一个代理中混合和匹配流量捕获模式。例如,考虑一个内部服务位于192.168.0.0/16
子网上的设置。因此,在VM上设置IP表以捕获192.168.0.0/16
子网上的所有出站流量。假设VM在172.16.0.0/16
子网上有一个额外的网络接口用于入站流量。以下Sidecar
配置允许VM在172.16.1.32:80
(VM的IP)上公开一个侦听器,用于来自172.16.0.0/16
子网的流量。
注意:VM中代理上的ISTIO_META_INTERCEPTION_MODE
元数据应包含REDIRECT
或TPROXY
作为其值,这意味着基于IP表的流量捕获处于活动状态。
apiVersion: networking.istio.io/v1
kind: Sidecar
metadata:
name: partial-ip-tables
namespace: prod-us1
spec:
workloadSelector:
labels:
app: productpage
ingress:
- bind: 172.16.1.32
port:
number: 80 # binds to 172.16.1.32:80
protocol: HTTP
name: somename
defaultEndpoint: 127.0.0.1:8080
captureMode: NONE
egress:
# use the system detected defaults
# sets up configuration to handle outbound traffic to services
# in 192.168.0.0/16 subnet, based on information provided by the
# service registry
- captureMode: IPTABLES
hosts:
- "*/*"
以下示例在prod-us1
命名空间中声明了一个Sidecar
配置,用于所有具有标签app: ratings
且属于ratings.prod-us1
服务的Pod。该服务在端口8443上接受入站HTTPS流量,并且sidecar代理使用给定的服务器证书终止单向TLS。然后,流量被转发到在Unix域套接字上侦听的附加工作负载实例。预计将配置PeerAuthentication策略,以便在特定端口上将mTLS模式设置为“DISABLE”。在此示例中,端口80上的mTLS模式已禁用。此功能目前处于实验阶段。
apiVersion: networking.istio.io/v1
kind: Sidecar
metadata:
name: ratings
namespace: prod-us1
spec:
workloadSelector:
labels:
app: ratings
ingress:
- port:
number: 80
protocol: HTTPS
name: somename
defaultEndpoint: unix:///var/run/someuds.sock
tls:
mode: SIMPLE
privateKey: "/etc/certs/privatekey.pem"
serverCertificate: "/etc/certs/servercert.pem"
---
apiVersion: v1
kind: Service
metadata:
name: ratings
labels:
app: ratings
service: ratings
spec:
ports:
- port: 8443
name: https
targetPort: 80
selector:
app: ratings
---
apiVersion: security.istio.io/v1
kind: PeerAuthentication
metadata:
name: ratings-peer-auth
namespace: prod-us1
spec:
selector:
matchLabels:
app: ratings
mtls:
mode: STRICT
portLevelMtls:
80:
mode: DISABLE
除了配置流量捕获以及如何将流量转发到应用程序外,还可以控制入站连接池设置。默认情况下,Istio会将连接池设置从DestinationRules
推送到客户端(用于到服务的出站连接)以及服务器(用于到服务实例的入站连接)。在Sidecar
中使用InboundConnectionPool
和每个端口的ConnectionPool
设置,您可以单独控制服务器的这些连接池,而与推送到所有客户端的设置无关。
apiVersion: networking.istio.io/v1
kind: Sidecar
metadata:
name: connection-pool-settings
namespace: prod-us1
spec:
workloadSelector:
labels:
app: productpage
inboundConnectionPool:
http:
http1MaxPendingRequests: 1024
http2MaxRequests: 1024
maxRequestsPerConnection: 1024
maxRetries: 100
ingress:
- port:
number: 80
protocol: HTTP
name: somename
connectionPool:
http:
http1MaxPendingRequests: 1024
http2MaxRequests: 1024
maxRequestsPerConnection: 1024
maxRetries: 100
tcp:
maxConnections: 100
侧车
Sidecar
描述了代理的配置,该代理会协调附加到的工作负载实例的入站和出站通信。
IstioIngressListener
IstioIngressListener
指定附加到工作负载实例的sidecar代理上入站流量侦听器的属性。
IstioEgressListener
IstioEgressListener
指定附加到工作负载实例的sidecar代理上出站流量侦听器的属性。
WorkloadSelector
WorkloadSelector
指定用于确定 Gateway
、Sidecar
、EnvoyFilter
、ServiceEntry
或 DestinationRule
配置是否可以应用于代理的标准。匹配标准包括与代理关联的元数据、工作负载实例信息(例如附加到 Pod/VM 的标签)或代理在初始握手期间提供给 Istio 的任何其他信息。如果指定了多个条件,则所有条件都必须匹配才能选择工作负载实例。目前,仅支持基于标签的选择机制。
OutboundTrafficPolicy
OutboundTrafficPolicy
设置 sidecar 处理应用程序发出的未知出站流量的默认行为。
SidecarPort
Port 描述服务的特定端口的属性。
OutboundTrafficPolicy.Mode
名称 | 描述 |
---|---|
REGISTRY_ONLY | 在 注意:Istio 不提供出站流量安全策略。此选项不会充当安全策略或任何形式的出站防火墙。相反,此选项主要用于为用户提供一种方法,通过显式失败来检测缺少的 |
ALLOW_ANY | 在 |
CaptureMode
CaptureMode
描述如何预期捕获到侦听器的流量。仅当侦听器绑定到 IP 时才适用。
名称 | 描述 |
---|---|
默认 | 环境定义的默认捕获模式。 |
IPTABLES | 使用 IPtables 重定向捕获流量。 |
无 | 不捕获流量。在出口侦听器中使用时,应用程序预计会显式地与侦听器端口或 Unix 域套接字通信。在入口侦听器中使用时,需要小心确保主机上的其他进程未使用侦听器端口。 |