关于运维:MoE-系列三|使用-Istio-动态更新-Go-扩展配置

4次阅读

共计 2770 个字符,预计需要花费 7 分钟才能阅读完成。

上一篇咱们用 Go 扩大实现了 Basic Auth,体验了 Go 扩大从 Envoy 承受配置。

之所以这么设计,是想复用 Envoy 原有的 xDS 配置推送通道,明天咱们就来体验一番, 云原生的配置变更

前提筹备

这次咱们须要一套 K8s 环境,如果你手头没有,举荐应用 Kind 装置一套。具体装置形式,这里就不开展了。

装置 Istio

咱们间接装置最新版的 Istio:

# 下载最新版的 istioctl$ export ISTIO_VERSION=1.18.0-alpha.0$ curl -L https://istio.io/downloadIstio | sh -
# 将 istioctl 退出 PATH$ cd istio-$ISTIO_VERSION/$ export PATH=$PATH:$(pwd)/bin
# 装置,包含 istiod 和 ingressgateway$ istioctl install

是的,因为 Go 扩大曾经奉献给了上游官网,Istiod(Pilot)和 Ingress Gateway 都曾经默认开启了 Go 扩大,并不需要从新编译。

Istio 配置 Ingress

咱们先用 Istio 实现规范的 Ingress 场景配置,具体能够看 Istio 的官网文档 [1]。

配置好了之后,简略测试一下:

$ curl -s -I -HHost:httpbin.example.com "http://$INGRESS_HOST:$INGRESS_PORT/status/200"HTTP/1.1 200 OKserver: istio-envoydate: Fri, 10 Mar 2023 15:49:37 GMT

根本的 Ingress 曾经跑起来了。

挂载 Golang so

之前咱们介绍过,Go 扩大是独自编译为 so 文件的,所以,咱们须要把 so 文件,挂载到 Ingress Gateway 中。

这里咱们把上次 Basic Auth 编译进去的 libgolang.so,通过本地文件挂载进来。简略点搞,间接 edit deployment 加了这些配置:

# 申明一个 hostPath 的 volumevolumes:- name: golang-so-basic-auth  hostPath:    path: /data/golang-so/example-basic-auth/libgolang.so    type: File
# 挂载进来 volumeMounts:- mountPath: /etc/golang/basic-auth.so  name: golang-so-basic-auth  readOnly: true

开启 Basic Auth 认证

Istio 提供了 EnvoyFilter CRD,所以,用 Istio 来配置 Go 扩大也比拟不便,apply 这段配置,Basic Auth 就开启了。

apiVersion: networking.istio.io/v1alpha3kind: EnvoyFiltermetadata:  name: golang-filter  namespace: istio-systemspec:  configPatches:    # The first patch adds the lua filter to the listener/http connection manager  - applyTo: HTTP_FILTER    match:      context: GATEWAY      listener:        filterChain:          filter:            name: "envoy.filters.network.http_connection_manager"            subFilter:              name: "envoy.filters.http.router"    patch:      operation: INSERT_BEFORE      value: # golang filter specification       name: envoy.filters.http.golang       typed_config:          "@type": "type.googleapis.com/envoy.extensions.filters.http.golang.v3alpha.Config"          library_id: example          library_path: /etc/golang/basic-auth.so          plugin_name: basic-auth          plugin_config:            "@type": "type.googleapis.com/xds.type.v3.TypedStruct"            type_url: typexx            value:              username: foo              password: bar

尽管有点长,然而,也很显著,配置的用户名明码还是:foo:bar

测试

咱们测试一下:

$ curl -s -I -HHost:httpbin.example.com "http://$INGRESS_HOST:$INGRESS_PORT/status/200"HTTP/1.1 401 Unauthorized
# valid foo:bar$ curl -s -I -HHost:httpbin.example.com "http://$INGRESS_HOST:$INGRESS_PORT/status/200" -H 'Authorization: basic Zm9vOmJhcg=='HTTP/1.1 200 OK

合乎预期。

接下来,咱们改一下 EnvoyFilter 中的明码,从新 apply,再测试一下:

# foo:bar not match the new password$ curl -s -I -HHost:httpbin.example.com "http://$INGRESS_HOST:$INGRESS_PORT/status/200" -H 'Authorization: basic Zm9vOmJhcg=='HTTP/1.1 401 Unauthorized

此时的 Envoy 并不需要重启,新的配置就立刻失效了,云原生的体验就是这么溜~

总结

因为 Go 扩大能够利用 Envoy 原有的 xDS 来承受配置,所以,从 Istio 推送配置也变得很顺利。

不过,Istio 提供的 EnvoyFilter CRD 在应用上,其实并不是那么不便和天然,前面咱们找机会试试 Envoy Gateway,看看 K8s Gateway API 的体验如何。

至此,咱们曾经体验了整个 Envoy Go 的开发 & 应用流程,在云原生时代,人均 Golang 的背景下,置信能够很好的实现网关场景的各种定制需要。

下一篇,咱们将介绍,如何在 Go 扩大中应用异步协程。这意味着,咱们能够应用的是一个全功能的 Go 语言,而不是像 Go Wasm 那样,只能用阉割版的。

敬请期待:MoE 系列(四)|Go 扩大的异步模式

[1]Istio 的官网文档:

https://istio.io/latest/docs/tasks/traffic-management/ingress/ingress-control/

正文完
 0