k8s~envoy上添加wasm插件

先查看这篇文章k8s~envoy的部署

当在Kubernetes中使用Envoy的WASM过滤器时,WASM过滤器会与Envoy一起部署在同一个Pod中,并与后端服务进行通信。以下是一个简单的关系图示意:

  +----------------------+   |       Kubernetes     |   |        Cluster       |   +----------|-----------+              |              |   +----------v-----------+   |                      |   |        Pod           |   |                      |   | +------------------+ |   | |      Envoy       | |   | |   with WASM     | |   | |   Filter       | |   | +------------------+ |   | |   Backend App   | |   | +------------------+ |   |                      |   +----------------------+ 

在这个示意图中,我们有一个运行在Kubernetes中的Pod,其中包含了Envoy和后端服务两个容器。Envoy与WASM过滤器一起作为Sidecar代理与后端服务一起运行,负责处理流量转发、负载均衡、安全策略等功能。后端服务则是实际提供业务功能的应用程序。

wasm部署过程

WASM过滤器可以在Envoy中拦截请求并对其进行修改或增强,然后将请求发送到后端服务。这种部署模式允许WASM过滤器直接与Envoy共享相同的网络命名空间,并通过Envoy来与后端服务通信。

在Kubernetes中应用Envoy的Filter并实现WebAssembly(WASM)的过滤器涉及以下几个步骤:

  1. 准备WASM模块:

    • 编写你的WASM模块,其中包含Envoy的Filter逻辑。确保你的WASM模块包含了你期望的Filter行为,比如认证、日志记录等。
    • 编译WASM模块,以确保其与Envoy兼容。
  2. 配置Envoy Filter:

    • 在你的Envoy配置文件中,添加一个Filter配置,指定使用你的WASM模块。

    • 在配置文件中,你可能需要添加类似以下的部分:

      filters:   - name: envoy.filters.http.wasm     config:       name: "my_wasm_filter"       root_id: "my_root_id"       vm_config:         code:           local:              filename: "/etc/wasm/ip-rate-limit/main.wasm"         runtime: "envoy.wasm.runtime.v8"       configuration:           "@type": "type.googleapis.com/google.protobuf.StringValue"                value: |                  {                    "ttlSecond": 60,                    "burst": 3                  } 
  • 请根据实际情况替换 my_wasm_filtermy_root_id/etc/wasm/ip-rate-limit/main.wasm
  • 如果你的envoy部署在k8s里,那上面的文件应该是envoy pod里的路径,你可以通过pvc进行指定pv,(pv对应存储类或者持久类)
  1. 部署Envoy配置:

    • 将更新后的Envoy配置部署到Rancher or k8s中。
  2. 监控日志和错误:

    • 监控Envoy的日志以查看是否有任何关于WASM Filter的错误或警告。确保Envoy能够成功加载和运行你的WASM模块。
  3. 测试Filter行为:

    • 发送请求到Envoy并确保WASM Filter按照预期进行操作。可以通过查看Envoy的日志、观察返回的请求或使用其他调试工具来验证Filter的行为。

envoy版本的注意项

  • envoyproxy/envoy:v1.21-latest,对应10000端口
  • envoyproxy/envoy 对应80端口

完成的envoy.yaml配置

admin:   access_log_path: /tmp/admin_access.log   address:     socket_address: { address: 0.0.0.0, port_value: 9901 } #envoy后台系统的端口  static_resources:   listeners:   - name: listener_0     address:       socket_address: { address: 0.0.0.0, port_value: 10000 } #envoy路由的端口     filter_chains:     - filters:       - name: envoy.filters.network.http_connection_manager         typed_config:           "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager           scheme_header_transformation:             scheme_to_overwrite: https           stat_prefix: ingress_http           route_config:             name: local_route             virtual_hosts:             - name: local_service               domains: ["*"]               routes:               - match:                   prefix: "/"                 route:                   cluster: httpbin           http_filters:           - name: wasmdemo             typed_config:               "@type": type.googleapis.com/udpa.type.v1.TypedStruct               type_url: type.googleapis.com/envoy.extensions.filters.http.wasm.v3.Wasm               value:                 config:                   name: wasmdemo                   vm_config:                     runtime: envoy.wasm.runtime.v8                     code:                       local:                         filename: /etc/wasm/ip-rate-limit/main.wasm                   configuration:                     "@type": "type.googleapis.com/google.protobuf.StringValue"                     value: |                       {                         "ttlSecond": 60,                         "burst": 3                       }            - name: envoy.filters.http.router   clusters:   - name: httpbin     connect_timeout: 30s     type: LOGICAL_DNS     # Comment out the following line to test on v6 networks     dns_lookup_family: V4_ONLY     lb_policy: ROUND_ROBIN     load_assignment:       cluster_name: httpbin       endpoints:       - lb_endpoints:         - endpoint:             address:               socket_address:                 address: httpbin_app_service.app_namespace                 port_value: 8080 

最后,我们把envoy服务的10000端口公开出去,在集群外就可以访问它了,你的wasm就可以被启用了;当然将10000端口公开出现的方法有很多,比较通用的方式是将它通过ingress或者阿里higress进行代理,更灵活。

发表评论

评论已关闭。

相关文章