使用 Istio 设置自定义调用源 header

Setting a custom call source header with Istio

我有一个使用 Kubernetes 和 Istio 的设置,其中我们 运行 一组服务。我们的每项服务都有一个 istio-sidecar 和一个 REST-api。我们想要的是,每当我们的设置中的服务调用另一个服务时,被调用的服务知道调用者是什么服务(最好通过 header)。

查看来自 bookinfo 的示例图像: bookinfo-image(Link 由于 <10 声望) 这意味着在评级服务的源代码中,我希望能够,例如,读取 header 告诉我请求来自例如评论-v2.

我的直觉告诉我,我应该能够在 istio sidecars 中处理这个问题,但我没有意识到具体如何。

到目前为止,我一直在特别关注envoy filters,希望他们能帮助我。我看到对于 envoy 过滤器我可以设置 header,但我看不到的是我将如何获取有关发出调用的服务的信息以便将其设置在header.

Envoy 自动设置 X-Forwarded-Client-Cert header, which contains the SPIFFE ID of the caller. SPIFFE ID in Istio is a URI in the form spiffe://cluster.local/ns/<namespace>/sa/<service account>. Practically, it designates the Kubernetes Service Account of the caller. You may want to test it by using the Istio httpbin 示例并向 httpbin:8000/headers

发送请求

我最终使用 "rule" 找到了另一个解决方案。如果我们确保策略执行已启用,然后添加规则:

apiVersion: config.istio.io/v1alpha2
kind: rule
metadata:
  name: header-rule
  namespace: istio-system
spec:
  actions: []
  requestHeaderOperations:
    - name: serviceid
      values:
      - source.labels["app"]
      operation: REPLACE

我们实现了我们的目标。