Envoy 過濾器
EnvoyFilter
提供一種機制來自訂 Istio Pilot 產生的 Envoy 設定。使用 EnvoyFilter 修改特定欄位的值、新增特定篩選器,甚至新增全新的監聽器、叢集等。必須謹慎使用此功能,因為不正確的設定可能會導致整個網格不穩定。與其他 Istio 網路物件不同,EnvoyFilter 是以累加方式套用。在特定命名空間中,一個給定的工作負載可以存在任意數量的 EnvoyFilter。這些 EnvoyFilter 的套用順序如下:設定 根命名空間 中的所有 EnvoyFilter,然後是工作負載命名空間中的所有符合的 EnvoyFilter。
注意 1:此 API 的某些方面與 Istio 網路子系統和 Envoy 的 XDS API 的內部實作緊密相關。雖然 EnvoyFilter API 本身會保持向後相容性,但應在 Istio Proxy 版本升級時仔細監控透過此機制提供的任何 Envoy 設定,以確保已移除已棄用的欄位並適當取代。
注意 2:當多個 EnvoyFilter 在給定命名空間中繫結到相同的工作負載時,所有修補程式將依建立時間順序依序處理。如果多個 EnvoyFilter 設定彼此衝突,則行為未定義。
注意 3:若要將 EnvoyFilter 資源套用至系統中的所有工作負載(Sidecar 和閘道),請在設定 根命名空間 中定義資源,而不要使用 workloadSelector。
以下範例在根命名空間中宣告一個名為 istio-config
的全域預設 EnvoyFilter 資源,該資源會在系統中所有 sidecar 上針對出站連接埠 9307 新增自訂通訊協定篩選器。此篩選器應在終止的 tcp_proxy 篩選器之前新增,才會生效。此外,它會為閘道和 sidecar 中的所有 HTTP 連線設定 30 秒的閒置逾時。
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
name: custom-protocol
namespace: istio-config # as defined in meshConfig resource.
spec:
configPatches:
- applyTo: NETWORK_FILTER
match:
context: SIDECAR_OUTBOUND # will match outbound listeners in all sidecars
listener:
portNumber: 9307
filterChain:
filter:
name: "envoy.filters.network.tcp_proxy"
patch:
operation: INSERT_BEFORE
value:
# This is the full filter config including the name and typed_config section.
name: "envoy.extensions.filters.network.mongo_proxy"
typed_config:
"@type": "type.googleapis.com/envoy.extensions.filters.network.mongo_proxy.v3.MongoProxy"
...
- applyTo: NETWORK_FILTER # http connection manager is a filter in Envoy
match:
# context omitted so that this applies to both sidecars and gateways
listener:
filterChain:
filter:
name: "envoy.filters.network.http_connection_manager"
patch:
operation: MERGE
value:
name: "envoy.filters.network.http_connection_manager"
typed_config:
"@type": "type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager"
common_http_protocol_options:
idle_timeout: 30s
以下範例針對 bookinfo 命名空間中標籤為「app: reviews」的 reviews 服務 Pod 的服務連接埠 8080 上收到的所有入站 HTTP 呼叫啟用 Envoy 的 Lua 篩選器。Lua 篩選器會呼叫外部服務 internal.org.net:8888,該服務需要在 Envoy 中進行特殊的叢集定義。此叢集也會做為此設定的一部分新增至 sidecar。
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
name: reviews-lua
namespace: bookinfo
spec:
workloadSelector:
labels:
app: reviews
configPatches:
# The first patch adds the lua filter to the listener/http connection manager
- applyTo: HTTP_FILTER
match:
context: SIDECAR_INBOUND
listener:
portNumber: 8080
filterChain:
filter:
name: "envoy.filters.network.http_connection_manager"
subFilter:
name: "envoy.filters.http.router"
patch:
operation: INSERT_BEFORE
value: # lua filter specification
name: envoy.filters.http.lua
typed_config:
"@type": "type.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua"
defaultSourceCode:
inlineString: |
function envoy_on_request(request_handle)
-- Make an HTTP call to an upstream host with the following headers, body, and timeout.
local headers, body = request_handle:httpCall(
"lua_cluster",
{
[":method"] = "POST",
[":path"] = "/acl",
[":authority"] = "internal.org.net"
},
"authorize call",
5000)
end
# The second patch adds the cluster that is referenced by the lua code
# cds match is omitted as a new cluster is being added
- applyTo: CLUSTER
match:
context: SIDECAR_OUTBOUND
patch:
operation: ADD
value: # cluster specification
name: "lua_cluster"
type: STRICT_DNS
connect_timeout: 0.5s
lb_policy: ROUND_ROBIN
load_assignment:
cluster_name: lua_cluster
endpoints:
- lb_endpoints:
- endpoint:
address:
socket_address:
protocol: TCP
address: "internal.org.net"
port_value: 8888
以下範例會針對 istio-system 命名空間中 SNI 主機 app.example.com 的輸入閘道上的接聽程式,覆寫 HTTP 連線管理員中的特定欄位 (HTTP 閒置逾時和 X-Forward-For 信任躍點)。
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
name: hcm-tweaks
namespace: istio-system
spec:
workloadSelector:
labels:
istio: ingressgateway
configPatches:
- applyTo: NETWORK_FILTER # http connection manager is a filter in Envoy
match:
context: GATEWAY
listener:
filterChain:
sni: app.example.com
filter:
name: "envoy.filters.network.http_connection_manager"
patch:
operation: MERGE
value:
typed_config:
"@type": "type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager"
xff_num_trusted_hops: 5
common_http_protocol_options:
idle_timeout: 30s
以下範例會插入一個 attributegen 篩選器,該篩選器會產生 istio_operationId
屬性,供 istio.stats 篩選器取用。filterClass: STATS
會對此相依性進行編碼。
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
name: reviews-request-operation
namespace: myns
spec:
workloadSelector:
labels:
app: reviews
configPatches:
- applyTo: HTTP_FILTER
match:
context: SIDECAR_INBOUND
patch:
operation: ADD
filterClass: STATS # This filter will run *before* the Istio stats filter.
value:
name: istio.request_operation
typed_config:
"@type": type.googleapis.com/udpa.type.v1.TypedStruct
type_url: type.googleapis.com/envoy.extensions.filters.http.wasm.v3.Wasm
value:
config:
configuration: |
{
"attributes": [
{
"output_attribute": "istio_operationId",
"match": [
{
"value": "ListReviews",
"condition": "request.url_path == '/reviews' && request.method == 'GET'"
}]
}]
}
vm_config:
runtime: envoy.wasm.runtime.null
code:
local: { inline_string: "envoy.wasm.attributegen" }
以下範例會在 myns
命名空間中插入一個 http ext_authz 篩選器。
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
name: myns-ext-authz
namespace: myns
spec:
configPatches:
- applyTo: HTTP_FILTER
match:
context: SIDECAR_INBOUND
patch:
operation: ADD
filterClass: AUTHZ # This filter will run *after* the Istio authz filter.
value:
name: envoy.filters.http.ext_authz
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.ext_authz.v3.ExtAuthz
grpc_service:
envoy_grpc:
cluster_name: acme-ext-authz
initial_metadata:
- key: foo
value: myauth.acme # required by local ext auth server.
myns
命名空間中的工作負載需要存取不接受初始中繼資料的不同 ext_auth 伺服器。由於 proto 合併無法移除欄位,因此下列設定會使用 REPLACE
作業。如果您不需要繼承欄位,則建議使用 REPLACE 而非 MERGE。
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
name: mysvc-ext-authz
namespace: myns
spec:
workloadSelector:
labels:
app: mysvc
configPatches:
- applyTo: HTTP_FILTER
match:
context: SIDECAR_INBOUND
patch:
operation: REPLACE
value:
name: envoy.filters.http.ext_authz
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.ext_authz.v3.ExtAuthz
grpc_service:
envoy_grpc:
cluster_name: acme-ext-authz-alt
以下範例會針對所有入站 sidecar HTTP 要求部署 Wasm 擴充功能。
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
name: wasm-example
namespace: myns
spec:
configPatches:
# The first patch defines a named Wasm extension and provides a URL to fetch Wasm binary from,
# and the binary configuration. It should come before the next patch that applies it.
# This resource is visible to all proxies in the namespace "myns". It is possible to provide
# multiple definitions for the same name "my-wasm-extension" in multiple namespaces. We recommend that:
# - if overriding is desired, then the root level definition can be overridden per namespace with REPLACE.
# - if overriding is not desired, then the name should be qualified with the namespace "myns/my-wasm-extension",
# to avoid accidental name collisions.
- applyTo: EXTENSION_CONFIG
patch:
operation: ADD
value:
name: my-wasm-extension
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.wasm.v3.Wasm
config:
root_id: my-wasm-root-id
vm_config:
vm_id: my-wasm-vm-id
runtime: envoy.wasm.runtime.v8
code:
remote:
http_uri:
uri: http://my-wasm-binary-uri
configuration:
"@type": "type.googleapis.com/google.protobuf.StringValue"
value: |
{}
# The second patch instructs to apply the above Wasm filter to the listener/http connection manager.
- applyTo: HTTP_FILTER
match:
listener:
filterChain:
filter:
name: envoy.filters.network.http_connection_manager
subFilter:
name: envoy.filters.http.router
patch:
operation: INSERT_BEFORE
value:
name: my-wasm-extension # This must match the name above
config_discovery:
config_source:
ads: {}
type_urls: ["type.googleapis.com/envoy.extensions.filters.http.wasm.v3.Wasm"]
以下範例會在 envoy.filters.listener.tls_inspector
之前插入 envoy.filters.listener.proxy_protocol
接聽程式篩選器。
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
name: listener-filter-example
namespace: myns
spec:
configPatches:
- applyTo: LISTENER_FILTER
match:
context: SIDECAR_INBOUND # will match inbound listeners in all sidecars
listener:
portNumber: 15006
listenerFilter: "envoy.filters.listener.tls_inspector"
patch:
operation: INSERT_BEFORE
value:
# This is the full filter config including the name and typed_config section.
name: "envoy.filters.listener.proxy_protocol"
typed_config:
"@type": "type.googleapis.com/envoy.extensions.filters.listener.proxy_protocol.v3.ProxyProtocol"
EnvoyFilter
EnvoyFilter 提供一種機制來自訂 Istio Pilot 產生的 Envoy 設定。
EnvoyFilter.ProxyMatch
要比對的 Proxy 的一個或多個屬性。
EnvoyFilter.ClusterMatch
必須符合 ClusterMatch
中指定的條件,修補程式才能套用至叢集。
EnvoyFilter.RouteConfigurationMatch
必須符合 RouteConfigurationMatch 中指定的條件,修補程式才能套用至路由組態物件或路由組態中的特定虛擬主機。
EnvoyFilter.ListenerMatch
必須符合接聽程式比對中指定的條件,修補程式才能套用至所有篩選器鏈中的特定接聽程式,或是接聽程式內的特定篩選器鏈。
EnvoyFilter.Patch
修補程式指定應該如何修改選取的物件。
EnvoyFilter.EnvoyConfigObjectMatch
在將修補程式套用至指定 Proxy 的產生組態之前,必須符合的一個或多個比對條件。
EnvoyFilter.EnvoyConfigObjectPatch
要對各種 Envoy 組態物件進行的變更。
EnvoyFilter.RouteConfigurationMatch.RouteMatch
比對路由組態中虛擬主機內的特定路由。
EnvoyFilter.RouteConfigurationMatch.VirtualHostMatch
比對路由組態內的特定虛擬主機。
EnvoyFilter.ListenerMatch.FilterChainMatch
對於具有多個篩選器鏈的接聽程式 (例如,具有允許 mTLS 的 sidecar 上的入站接聽程式、具有多個 SNI 比對的閘道接聽程式),篩選器鏈比對可用於選取要修補的特定篩選器鏈。
EnvoyFilter.ListenerMatch.FilterMatch
在篩選器鏈中匹配特定篩選器的條件。
EnvoyFilter.ListenerMatch.SubFilterMatch
在另一個篩選器內匹配特定篩選器的條件。此欄位通常用於匹配 envoy.filters.network.http_connection_manager
網路篩選器內的 HTTP 篩選器。這也適用於 Thrift 篩選器。
EnvoyFilter.RouteConfigurationMatch.RouteMatch.Action
Action 指的是當 HTTP 路由匹配時,Envoy 採取的路由動作。
名稱 | 描述 |
---|---|
ANY | 所有三個路由動作 |
ROUTE | 將流量路由到叢集 / 加權叢集。 |
REDIRECT | 重新導向請求。 |
DIRECT_RESPONSE | 使用特定酬載直接回應請求。 |
EnvoyFilter.Patch.Operation
Operation 表示如何將修補程式套用至選定的組態。
名稱 | 描述 |
---|---|
INVALID | |
MERGE | 使用 proto 合併語意,將提供的組態與產生的組態合併。如果您要完整指定組態,請改用 |
ADD | 將提供的組態新增至現有清單(監聽器、叢集、虛擬主機、網路篩選器或 HTTP 篩選器)。當 |
REMOVE | 從清單中移除選取的物件(監聽器、叢集、虛擬主機、網路篩選器、路由或 HTTP 篩選器)。不需要指定值。當 |
INSERT_BEFORE | 對具名物件陣列執行插入操作。此操作通常僅在篩選器或路由的上下文中有用,其中元素的順序很重要。路由應根據最特定到最不特定的匹配條件排序,因為會選取第一個匹配的元素。對於叢集和虛擬主機,陣列中元素的順序並不重要。在選取的篩選器或子篩選器之前插入。如果未選取篩選器,則指定的篩選器將插入清單的最前面。 |
INSERT_AFTER | 對具名物件陣列執行插入操作。此操作通常僅在篩選器或路由的上下文中有用,其中元素的順序很重要。路由應根據最特定到最不特定的匹配條件排序,因為會選取第一個匹配的元素。對於叢集和虛擬主機,陣列中元素的順序並不重要。在選取的篩選器或子篩選器之後插入。如果未選取篩選器,則指定的篩選器將插入清單的最後面。 |
INSERT_FIRST | 對具名物件陣列執行插入操作。此操作通常僅在篩選器或路由的上下文中有用,其中元素的順序很重要。路由應根據最特定到最不特定的匹配條件排序,因為會選取第一個匹配的元素。對於叢集和虛擬主機,陣列中元素的順序並不重要。根據選取篩選器是否存在,插入清單中的第一個位置。當您希望篩選器根據「匹配子句」中指定的匹配條件位於清單中的第一個位置時,這特別有用。 |
REPLACE | 以新內容取代具名篩選器的內容。 |
EnvoyFilter.Patch.FilterClass
FilterClass 決定篩選器在篩選器鏈中的插入點,相對於控制平面隱式插入的篩選器。它與 ADD
操作結合使用。這是新增篩選器的慣用插入機制,而不是 INSERT_*
操作,因為這些操作依賴於可能不穩定的篩選器名稱。如果您的篩選器依賴或影響篩選器鏈中另一個篩選器的運作,則篩選器排序很重要。在篩選器類別內,篩選器會按處理順序插入。
名稱 | 描述 |
---|---|
UNSPECIFIED | 控制平面決定在哪裡插入篩選器。如果篩選器與其他篩選器無關,請勿指定 |
AUTHN | 在 Istio 驗證篩選器之後插入篩選器。 |
AUTHZ | 在 Istio 授權篩選器之後插入篩選器。 |
STATS | 在 Istio 統計篩選器之前插入篩選器。 |
EnvoyFilter.ApplyTo
ApplyTo
指定應將給定修補程式套用在 Envoy 組態中的哪個位置。
名稱 | 描述 |
---|---|
INVALID | |
LISTENER | 將修補程式套用至監聽器。 |
FILTER_CHAIN | 將修補程式套用至篩選器鏈。 |
NETWORK_FILTER | 將修補程式套用至網路篩選器鏈,以修改現有篩選器或新增篩選器。 |
HTTP_FILTER | 將修補程式套用至 HTTP 連線管理員中的 HTTP 篩選器鏈,以修改現有篩選器或新增篩選器。 |
ROUTE_CONFIGURATION | 將修補程式套用至 HTTP 連線管理員內的路由組態(rds 輸出)。這不適用於虛擬主機。目前,僅允許對路由組態物件執行 |
VIRTUAL_HOST | 將修補程式套用至路由組態內的虛擬主機。 |
HTTP_ROUTE | 將修補程式套用至路由組態中匹配的虛擬主機內的路由物件。 |
CLUSTER | 將修補程式套用至 CDS 輸出中的叢集。也用於新增叢集。 |
EXTENSION_CONFIG | 將修補程式套用至 ECDS 輸出中的擴充組態或新增擴充組態。請注意,ECDS 僅由 HTTP 篩選器支援。 |
BOOTSTRAP | 已棄用。將修補程式套用至啟動組態。 |
LISTENER_FILTER | 將修補程式套用至監聽器篩選器。 |
EnvoyFilter.PatchContext
PatchContext 根據流量方向和工作負載類型選取一組組態。
名稱 | 描述 |
---|---|
ANY | 側車和閘道中的所有監聽器/路由/叢集。 |
SIDECAR_INBOUND | 側車中的入站監聽器/路由/叢集。 |
SIDECAR_OUTBOUND | 側車中的出站監聽器/路由/叢集。 |
GATEWAY | 閘道監聽器/路由/叢集。 |