
Istio ã® EnvoyFilter ãçšããŠãããããŒã¹ã§ã®ã«ãŒãã£ã³ã°ã远å ããŠã¿ã REALITY Advent Calendar 2024 #23
ã¯ããã«
æ¬ãšã³ããªãŒã¯REALITY Advent Calendar 2024ã®23æ¥ç®ã®èšäºã§ãã
ãã®ä»ã®ãšã³ããªãŒã«ã€ããŠã¯ã以äžã®ããŒãžããã芧ãã ããã
ããã«ã¡ã¯ãDevOpsããŒã ãšã³ãžãã¢ãããŒãžã£ãŒã® kuyama ã§ãã
ããŠãæ¬æ¥ã¯ Istio ã® VirtualService ãå©çšããŠãããµãŒãã¹ã«ãããŠãVirtualService ã«ã«ãŒãã£ã³ã°ã远å ããã«ããããããŒã¹ã§ã®ã«ãŒãã£ã³ã°ã远å ãããã«ã¯ã©ããããã«ã€ããŠè§£èª¬ãå®è·µããŠãããŸãã
ä»åã¯ãåã« REALITY ã®æè¡ããã°ã§ãã Now In REALITY Tech ã§æžãããPRããšã«åäœç¢ºèªçšã®ç°å¢ãèªåçæãã Now In REALITY Tech #123ããäžæ·ãã«ããŠãããŸãããã¡ãã®èšäºãèªãã§ããªããŠã倧äžå€«ãªããã«è§£èª¬ããŠãããŸãããèªãã§ããã°ããã«çè§£ãæ·±ãŸãããšæããŸãã®ã§ããã²ãäžèªãã ããïŒ
ããŠããŸãã¯ãªããVirtualService ã«ã«ãŒãã£ã³ã°ã远å ããã«ãHTTPãããããŒã¹ã§ã®ã«ãŒãã£ã³ã°ã远å ããããªã©ãšããæ²èžãããããšããŠããã®ã§ããããã
åè¿°ã®èšäºã§èšåããããã«ãåäœç¢ºèªçšã®ç°å¢ãPRããšã«çæããã«ã¯ããåPRçšã®ãªãœãŒã¹ãžã®ã«ãŒãã£ã³ã°ã®èšå®ã Github Actions çµç±ã§ VirtualService ãªãœãŒã¹ã«è¿œå ããŠããããšããã¹ããããå¿ èŠã«ãªããŸããããã¯ãPRã®åé€åŸã«è©²åœã®ã«ãŒãã£ã³ã°èšå®ãåé€ããå¿ èŠãåºãŠããŠãããŸã奜ãŸãããããŸããã
ããã§ããIstio ã® VirtualService ãå©çšããŠããç¶æ ã§ããã«ãŒãã£ã³ã°ã«é¢ãããªãœãŒã¹ãåå¥ã«ãããã€ãã§ããªãããæš¡çŽ¢ããããšã«ããŸããã
çµè«ãšããŠããEnvoyFilter ãå©çšããå Pod ã«ãµã€ãã«ãŒãšããŠæ¿å ¥ãããŠãã Envoy ã«å¯Ÿã㊠VirtualHost ã® route æ å ±ã远å ããŠããããšããææ³ããšãããšã§è§£æ±ºããããšã«ããŸããã
Istio ã§ã¯ã©ã¹ã¿å ã®ã«ãŒãã£ã³ã°ãå®çŸãããŠããä»çµã¿
åæãšããŠãREALITYã§ã¯ãµãŒãã¹ã®æäŸã®ããã«GKEã¯ã©ã¹ã¿äžã«ã¢ããªã±ãŒã·ã§ã³ãµãŒããPodãšããŠç«ãŠãŠããããã®GKEã¯ã©ã¹ã¿äžã§ãµãŒãã¹ã¡ãã·ã¥ãå®çŸããããã«ãAnthos Service Mesh ïŒä»¥äž ASMïŒãå°å ¥ããŠããŸãã
ãã® ASM ã¯ãIstio ãããŒã¹ã«ããŠãããã¯ã©ã¹ã¿å ã® Pod ãã Pod ãžã®éä¿¡ã¯äžèšã®å³ã®ããã«ãå Pod ã« ãµã€ãã«ãŒã€ã³ãžã§ã¯ã·ã§ã³ ããã proxy ã³ã³ãããçµç±ããŠè¡ãããŸãã

ãã® Istio ã§å©çšããã Proxy ã³ã³ããã¯ãEnvoy ãããŒã¹ã«ããŠäœãããŠããŸãããããŠãIstio ã® Control Plane (istiod) ãçŸåšã®ã¯ã©ã¹ã¿å ã®ãªãœãŒã¹ã®ç¶æ ãæ€ç¥ããŠãå Pod ã«ã€ã³ãžã§ã¯ã·ã§ã³ããŠãã proxy ã«æ å ±ãäŒéããEnvoy ã®å®å æ å ±ãšããŠèšå®ããŸãã
äŸãšããŠã以äžã®ãã㪠Pod, Service, VirtualService ã deploy ããŸãã
apiVersion: v1
kind: Pod
metadata:
name: hello
labels:
app: hello
spec:
containers:
- name: hello
image: gcr.io/google-samples/hello-app:2.0
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: hello
spec:
type: NodePort
selector:
app: hello
ports:
- port: 8080
targetPort: 8080
---
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: hello
spec:
hosts:
- hello.default.svc.cluster.local
http:
- route:
- destination:
host: hello
port:
number: 8080ãã®æãäžèšã®ã³ãã³ãã§åãã¯ã©ã¹ã¿å ã« èžã¿å°Pod ãç«ãŠãŠãcurl ã§ã¯ã©ã¹ã¿å ãã¡ã€ã³ãå©ããšã以äžã®ããã«ã¬ã¹ãã³ã¹ãè¿ã£ãŠããŸãã
$ kubectl run -it --rm=true busybox --image=yauritux/busybox-curl
If you don't see a command prompt, try pressing enter.
/home # curl hello.default.svc.cluster.local.
Hello, world!
Version: 2.0.0
Hostname: helloãã® busybox Pod ã«ãµã€ãã«ãŒã€ã³ãžã§ã¯ã·ã§ã³ããã Proxy ã«èšå®ãããå®å æ å ±ã確èªããŠã¿ãŸããããïŒèšå®ã«ãããdefault ããŒã ã¹ããŒã¹äžã®å šãŠã® Pod ã«ã¯èªåçã« Proxy ã³ã³ããããµã€ãã«ãŒã€ã³ãžã§ã¯ã·ã§ã³ãããããã«ãªã£ãŠããŸãïŒ
å®å
æ
å ±ã¯ã以äžã®ã³ãã³ããå®è¡ã㊠API ãå©ãããšã§ååŸã§ããŸãã
ãã® API 㯠json ã§æ
å ±ãè¿ã£ãŠããã®ã§ãããèŠã«ããã®ã§ yaml 圢åŒã«å€æããŸãã
$ kubectl exec -it busybox -n default -c istio-proxy \
-- bash -c "curl http://localhost:15000/config_dump?resource={dynamic_route_configs}" | yq -Päžèšã®ããã«ãVirtualService ã§èšå®ããŠãã route æ å ±ã¯ãDynamicRouteConfig 以äžã® routes ã«èšå®ãããŸãã
configs:
- '@type': type.googleapis.com/envoy.admin.v3.RoutesConfigDump.DynamicRouteConfig
route_config:
'@type': type.googleapis.com/envoy.config.route.v3.RouteConfiguration
name: hello.default.svc.cluster.local:80
virtual_hosts:
- name: hello.default.svc.cluster.local:80
domains:
- '*'
routes:
# ãã¹ããããããŒã¹ã§ã®ã«ãŒãã£ã³ã°èšå®ã¯ããã«listããã
- match:
prefix: /
route:
cluster: outbound|8080||hello.default.svc.cluster.local
timeout: 0s
retry_policy:
retry_on: connect-failure,refused-stream,unavailable,cancelled,retriable-status-codes
num_retries: 2
retry_host_predicate:
- name: envoy.retry_host_predicates.previous_hosts
typed_config:
'@type': type.googleapis.com/envoy.extensions.retry.host.previous_hosts.v3.PreviousHostsPredicate
host_selection_retry_max_attempts: "5"
retriable_status_codes:
- 503
max_grpc_timeout: 0s
metadata:
filter_metadata:
istio:
config: /apis/networking.istio.io/v1alpha3/namespaces/default/virtual-service/hello
decorator:
operation: hello.default.svc.cluster.local:8080/*EnvoyFilter ã§ route config ã«èšå®ã远å ãã
ããã§ãIstio ã® EnvoyFilter ãšãããªãœãŒã¹ã«ã€ããŠç޹ä»ããŸãã
ãã® EnvoyFilter ãšãããªãœãŒã¹ã¯ãistiod ããå Envoy ãããã·ã«äŒéãããå®å æ å ±ã®èšå®ã倿Žããããšãã§ããŸããã€ãŸãããã®ãªãœãŒã¹ã䜿ãã°ã Virtual Service ãå©çšããªããŠã envoy ã«èšå®ããã route config ã«å¥ã®ã«ãŒãã£ã³ã°ã远å ããããšãã§ããŸãã
ã§ã¯ãä»åºŠã¯ä»¥äžã®ããã« Pod, Service, EnvoyFilter ã远å ããŠã¿ãŸãã
apiVersion: v1
kind: Pod
metadata:
name: hello-another
labels:
app: hello-another
spec:
containers:
- name: hello-another
image: gcr.io/google-samples/hello-app:2.0
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: hello-another
spec:
type: NodePort
selector:
app: hello-another
ports:
- port: 8080
targetPort: 8080
---
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
name: hello-another-envoy-filter
spec:
priority: 10
configPatches:
# HTTP_ROUTE ã®èšå®
- applyTo: HTTP_ROUTE
match:
context: SIDECAR_OUTBOUND
routeConfiguration:
vhost:
name: "hello.default.svc.cluster.local:80" # httpãªã¯ãšã¹ãã¯ããã©ã«ãã§80çªããŒãã«éä¿¡ãããã®ã§ãvhostã®nameã¯ãã®ããã«æå®ãã
patch:
operation: INSERT_FIRST
value:
# https://www.envoyproxy.io/docs/envoy/latest/api-v3/config/route/v3/route_components.proto#envoy-v3-api-msg-config-route-v3-routematch åç
§
match:
prefix: "/"
case_sensitive: true
headers:
- name: REQUEST_ROUTING
string_match:
prefix: "another"
route:
cluster: "outbound|8080||hello-another.default.svc.cluster.local"
timeout: 0s
retry_policy:
retry_on: connect-failure,refused-stream,unavailable,cancelled,retriable-status-codes
num_retries: 2
retry_host_predicate:
- name: envoy.retry_host_predicates.previous_hosts
typed_config:
'@type': type.googleapis.com/envoy.extensions.retry.host.previous_hosts.v3.PreviousHostsPredicate
host_selection_retry_max_attempts: "5"
retriable_status_codes:
- 503
max_grpc_timeout: 0s
decorator:
operation: hello-another.default.svc.cluster.local:8080/*ãããšã以äžã®ããã«è¿œå ãã Service ãžã®ã«ãŒãã£ã³ã°ã远å ãããŠããã®ãèªã¿åããŸãã
configs:
- '@type': type.googleapis.com/envoy.admin.v3.RoutesConfigDump.DynamicRouteConfig
route_config:
'@type': type.googleapis.com/envoy.config.route.v3.RouteConfiguration
name: "80"
virtual_hosts:
- name: hello.default.svc.cluster.local:80
domains:
- hello.default.svc.cluster.local
routes:
# 远å ãããã«ãŒãã£ã³ã°èšå®
- match:
prefix: /
case_sensitive: true
headers:
- name: REQUEST_ROUTING
string_match:
prefix: another
route:
cluster: outbound|8080||hello-another.default.svc.cluster.local
timeout: 0s
retry_policy:
retry_on: connect-failure,refused-stream,unavailable,cancelled,retriable-status-codes
num_retries: 2
retry_host_predicate:
- name: envoy.retry_host_predicates.previous_hosts
typed_config:
'@type': type.googleapis.com/envoy.extensions.retry.host.previous_hosts.v3.PreviousHostsPredicate
host_selection_retry_max_attempts: "5"
retriable_status_codes:
- 503
max_grpc_timeout: 0s
decorator:
operation: hello-another.default.svc.cluster.local:8080/*
# å
ã
ã®ã«ãŒãã£ã³ã°èšå® */
- match:
prefix: /
route:
cluster: outbound|8080||hello.default.svc.cluster.local
timeout: 0s
retry_policy:
retry_on: connect-failure,refused-stream,unavailable,cancelled,retriable-status-codes
num_retries: 2
retry_host_predicate:
- name: envoy.retry_host_predicates.previous_hosts
typed_config:
'@type': type.googleapis.com/envoy.extensions.retry.host.previous_hosts.v3.PreviousHostsPredicate
host_selection_retry_max_attempts: "5"
retriable_status_codes:
- 503
max_grpc_timeout: 0s
metadata:
filter_metadata:
istio:
config: /apis/networking.istio.io/v1alpha3/namespaces/default/virtual-service/hello
decorator:
operation: hello.default.svc.cluster.local:8080/*ããã§ãå ã»ã©ç«ãŠãèžã¿å° Pod ãã HTTP ãªã¯ãšã¹ããè¡ããå®éã«ãããããŒã¹ã§ã®ã«ãŒãã£ã³ã°ãè¡ãããŠããã確èªããŸãã
/home # curl hello.default.svc.cluster.local.
Hello, world!
Version: 2.0.0
Hostname: hello
/home # curl -H "REQUEST_ROUTING:another" hello.default.svc.cluster.local.
Hello, world!
Version: 2.0.0
Hostname: hello-another # æ°ããç«ãŠãPodåâŠâŠããããä»äžãããªã¯ãšã¹ãããæ°ããç«ãŠã Pod ã«ã«ãŒãã£ã³ã°ãããŠããã®ãèŠãŠåããŸãïŒ
ãŸãšã
äžèšã®ããã«ãEnvoyFilter ãå©çšããŠã«ãŒãã£ã³ã°ã远å ããããšãã§ããŸããïŒ
ãããå©çšããŠãåå¥ã®éçºç°å¢ãããã«ã¢ããããŒãããããšãã§ããŸãã
ãšããã§ãçŸåš REALITY ã§ã¯ ASM ãå©çšããŠããŸãããGoogle Cloud ããæ°ããæäŸãããŠãã Cloud Service MeshïŒä»¥äžCSMïŒ ã«ç§»è¡ããéã«ã¯ EnvoyFilter ãå©çšã§ããªããããã§ãã
ã€ãŸããCSM ã«ç§»è¡ããã¿ã€ãã³ã°ã§ä»åã®é 匵ãã¯ç¡é§ã«ãªãã®ã§ããâŠâŠããŸããCSM ã EnvoyFilter ã«å¯Ÿå¿ãããŸã§ç§»è¡ãåŸ ã€ããçŽ çŽã« Gateway API ã䜿ãããšã«ããŸãããïŒHAHAHAïŒ