gpt4 book ai didi

routes - 在负载均衡器中向 HTTP 请求添加自定义 header

转载 作者:行者123 更新时间:2023-12-04 13:30:13 25 4
gpt4 key购买 nike

我在带有 istio 服务网格的 openshift 容器平台中部署了一个容器化应用程序/服务。在 istio 虚拟服务 yaml 中,我想验证 http 请求是否具有 header (例如:版本)并且值为 v1。我在验证 header 的虚拟服务 yaml 中添加了以下配置。
但是我正在寻找可用的选项来使用 loadbalancer/ingress/openshif 路由等在 HTTP 请求中注入(inject)这个 header 。
因为我的 istio-ingressgateway 服务是使用 ClusterIp 部署的。我已经使用 openshift 路由将外部流量发送到 ingressgateway。
请分享向http请求添加 header 的可能方法

  http:
- match:
- headers: # Match header
version: # header that we decided for dark release
exact: v1 # exact match



最佳答案

您可以使用 envoy filter要做到这一点。
在特使过滤器下方添加名为 customer-id 的请求 header 与 alice通过 istio 入口网关的所有请求的值。如果有人想使用它,我还评论了响应 header 的代码。

apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:
name: lua-filter
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.lua
typed_config:
"@type": "type.googleapis.com/envoy.config.filter.http.lua.v2.Lua"
inlineCode: |
function envoy_on_request(request_handle)
request_handle:headers():add("customer-id", "alice")
end
# function envoy_on_response(response_handle)
# response_handle:headers():add("customer-id", "alice")
# end

我曾经用过 yaml 来测试它,它可能对测试上面的过滤器很有用。
  • 如果您使用带有 alice header 的上述过滤器,则所有请求都会发送到 nginx-v1
  • 如果您将上述过滤器与 bob header 一起使用,则所有请求都将发送至 nginx-v2
  • 如果你删除这个过滤器,那么它在 nginx-v1 和 nginx-v2 之间是 50/50
  • apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: nginx-v1
    spec:
    selector:
    matchLabels:
    run: nginx1
    replicas: 1
    template:
    metadata:
    labels:
    run: nginx1
    app: frontend
    spec:
    containers:
    - name: nginx1
    image: nginx
    ports:
    - containerPort: 80
    lifecycle:
    postStart:
    exec:
    command: ["/bin/sh", "-c", "echo Hello nginx1 > /usr/share/nginx/html/index.html"]

    ---

    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: nginx-v2
    spec:
    selector:
    matchLabels:
    run: nginx2
    replicas: 1
    template:
    metadata:
    labels:
    run: nginx2
    app: frontend
    spec:
    containers:
    - name: nginx2
    image: nginx
    ports:
    - containerPort: 80
    lifecycle:
    postStart:
    exec:
    command: ["/bin/sh", "-c", "echo Hello nginx2 > /usr/share/nginx/html/index.html"]



    ---

    apiVersion: v1
    kind: Service
    metadata:
    name: nginx
    labels:
    app: frontend
    spec:
    ports:
    - name: http-front
    port: 80
    protocol: TCP
    selector:
    app: frontend

    ---

    apiVersion: networking.istio.io/v1alpha3
    kind: Gateway
    metadata:
    name: gatewayx
    spec:
    selector:
    istio: ingressgateway # use istio default controller
    servers:
    - port:
    number: 80
    name: http
    protocol: HTTP
    hosts:
    - "*"

    ---

    apiVersion: networking.istio.io/v1alpha3
    kind: VirtualService
    metadata:
    name: nginxvirt
    spec:
    hosts:
    - '*'
    gateways:
    - gatewayx
    http:
    - name: "route-1"
    match:
    - headers:
    customer-id:
    exact: alice
    route:
    - destination:
    host: nginx
    subset: v1
    - name: "route-2"
    match:
    - headers:
    customer-id:
    exact: bob
    route:
    - destination:
    host: nginx
    subset: v2
    - route:
    - destination:
    host: nginx

    ---

    apiVersion: networking.istio.io/v1alpha3
    kind: DestinationRule
    metadata:
    name: nginxdest
    spec:
    host: nginx
    subsets:
    - name: v1
    labels:
    run: nginx1
    - name: v2
    labels:
    run: nginx2

    关于routes - 在负载均衡器中向 HTTP 请求添加自定义 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65527456/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com