Open6

Istio Envoy learning notes

momotaro98momotaro98

Istio IngressGateway は K8sの概念のingress controllerの1つの実装と言えるのか?

Yes, Istio IngressGateway is a type of ingress controller in a Kubernetes cluster.

IstioにとってCustomResourceDefinitions (CRDs)とは何なのか?

Istio uses CustomResourceDefinitions (CRDs) to extend the Kubernetes API and allow users to define custom resources that can be used to configure Istio.
CRDs provide a way for users to define custom resources that can be managed and manipulated like built-in Kubernetes resources. This allows Istio to define its own resources, such as VirtualService, Gateway, and EnvoyFilter, which can be used to configure the behavior of the Istio service mesh.

→ VirtualService, Gateway, EnvoyFilter 等はIstioが作り出したカスタムリソースである。

Istio IngressGateway は Gateway, VirtualService, DestinationRule のリソースによってトラフィックの受付とルーティングとルーティング先が設定される。役割は以下に要約できる。

  • Gateway → 外部からのIncomingリクエストの受け付け方の設定 (port番号など)
    • → Envoy実態上では Listener の設定に対応する
  • VirtualService → サービスへのルーティング設定
    • → Envoy実態上では Listener.Filters.HTTP(L7)Connection.HttpFilters.envoyRoute.VirtualHost の設定に対応する
  • DestinationRule → サービス到達後のサービスバージョン分けごとのロードバランス設定
    • → Envoy実態上では Cluster.TrasportSocket の設定に対応する

Istio IngressGateway に EnvoyFilterも設定できる。IngressGatewayの実態は(結局)EnvoyでありHTTP通信のヘッダー更新やmTLSなどの設定はVirtualServiceではなくEnjoyFilterで設定することになる。

momotaro98momotaro98

Istio pilot (Istiod) と Envoyについて

EnvoyはxDS APIという独自仕様のAPIを持っており、それはEnvoy Proxyを柔軟に設定するために設計されている。

Istio pilot (Istiod)はそのxDS APIを使ってEnvoy Proxyに対してリクエストをして設定するための存在と見なして良い。Istioは独自のGateway, VirtualService, DestinationRuleというK8s CRDsを提供する形でEnvoy xDS APIをWrapしてEnvoyの設定を裏で行っている。

momotaro98momotaro98

EnvoyにはExtensionとして"Filters"という概念がある。Filtersの中で大きく分けてNetwork(TCPレベル)filtersとHTTP filtersがある。Network filterにはMySQL proxy filterやRedis filterなどが存在する。HTTP filtersの中にはAWS Labmda filter、Cors filter、gRPC filter、Lua filter、JWT filterなどがある。Rate limit filter、RBAC filterなどはNetwork filters、HTTP filtersの両方にそれぞれ別フィルターとして存在している。

各Filterには"名前"が存在しており、HTTP filtersのExternal Authorization filterでは、envoy.filters.http.ext_authz である。以下のように定義し、参照される。

with_request_bodygrpc_serviceExterna Authorization filter の APIの仕様のフィールドである。

定義側

# Istio Ingress Gateway OPA settings
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
  name: ext-authz-ingressgateway
  namespace: istio-system
spec:
  workloadSelector:
    labels:
      istio: ingressgateway
  configPatches:
    - applyTo: HTTP_FILTER
      match:
        context: GATEWAY
        listener:
          filterChain:
            filter:
              name: "envoy.http_connection_manager"
              subFilter:
                name: "envoy.router"
      patch:
        operation: INSERT_BEFORE
        value:
          name: envoy.filters.http.ext_authz
          typed_config:
            '@type': type.googleapis.com/envoy.extensions.filters.http.ext_authz.v3.ExtAuthz
            transport_api_version: V3
            status_on_error:
              code: ServiceUnavailable
            # for Channel serviceauth/gateway_policy config
            with_request_body:
              # see https://github.com/envoyproxy/envoy/issues/16217
              # max_request_bytes must be less than http_connection_manager#per_connection_buffer_limit_bytes (default 1MB)
              max_request_bytes: 972800 # 950KB
              allow_partial_message: true
              pack_as_bytes: true
            grpc_service:
              timeout: 10s
              envoy_grpc:
                cluster_name: opa_ingressgateway
              # fromat string(%~%) in initial_matadata cannot run at google_grpc, so that we must use envoy_grpc and add CLUSTER.
              # add origin-host metadata(as same as header)  from original request authority header value. this value use in opa service proxy route detection.
              initial_metadata:
                - key: "origin-host"
                  value: "%REQ(:AUTHORITY)%"

参照側

apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
  name: ratelimit-gateway-config
  namespace: istio-system
spec:
  workloadSelector:
    labels:
      istio: ingressgateway
  configPatches:
    # The Envoy config you want to modify
    - applyTo: HTTP_FILTER
      match:
        context: GATEWAY
        listener:
          filterChain:
            filter:
              name: "envoy.filters.network.http_connection_manager"
              subFilter:
                name: "envoy.filters.http.ext_authz"
momotaro98momotaro98

(前日譚) Envoy の公式ドキュメントの概要

  • 主人公1: Architecture overview
    • Listeners
    • HTTP
      • HTTP connection management
      • HTTP filters
      • HTTP routing
      • etc...
    • Upstream clusters
      • Cluster manager
      • Service discovery
      • DNS Resolution
      • Health checking
      • Connection pooling
      • Load Balancing
      • etc...
    • Obervability
      • Statistics
      • Access logging
      • Tracing
    • Security
      • TLS
      • JWT Authentication
      • External Authorization
      • Role Base Access Control
      • etc...
    • Operations
      • xDS configuration API overview
      • Draining
      • Hot restart
      • etc...
  • 主人公2: Configuration reference
    • Listeners
      • Architecture overview と対応
    • HTTP
      • HTTP connection manager
      • HTTP filters
        • HTTP filter と HTTP routing 両方ともこの中
      • etc...
    • Upstream clusters
      • Architecture overview と対応
    • 以降もArchitecture overview と対応
  • 主人公3: v3 API reference
    • Listeners
    • Clusters
    • HTTP route management
      • HTTP route components (proto)
      • HTTP route configuration (proto)
      • HTTP scoped routing configuration (proto)
momotaro98momotaro98

Envoy について

Envoy 用語

  • Host: An entity capable of network communication
  • Downstream: A downstream host connects to Envoy, sends requests, and receives responses.
  • Upstream: An upstream host receives connections and requests from Envoy and returns responses.
  • Listener: A listener is a named network location (e.g., port, unix domain socket, etc.) that can be connected to by downstream clients.
  • Cluster: A cluster is a group of logically similar upstream hosts that Envoy connects to.

Envoy の設定(Configuration)について

Envoyの設定のデータスキーマはProtocol Bufferで定義されている。xDS APIを通してEnvoyは設定されるがAPIは基本的にgRPCであるためである。

重要な概念

Listeners → TCPレイヤを扱う。TLS終端、コネクションThrottling、TCPレイヤロードバランシング 等 はListener filtersで扱う。

HTTP → HTTPレイヤを扱う。Header修正、セキュリティポリシー適用 等 はHTTP filtersで扱う。