envoy源代码分析(一)--从零开始

 

什么是envoy

 

是七层代理,是通信中间层,是服务网关的sidecar。

更多看这里:https://www.envoyproxy.io/docs/envoy/latest/intro/what_is_envoy

官方安装方法:https://www.envoyproxy.io/docs/envoy/latest/start/install

这个介绍讲挺好:https://blog.csdn.net/z69183787/article/details/110733370

这个文档也不错:https://www.lijiaocn.com/soft/envoy/index.html

 

为什么是envoy

https://mp.weixin.qq.com/s/r5j9Bp-NYhkFI1ZdsJt3Pw

https://mp.weixin.qq.com/s/vUkA4SeMVtG-genfP2b0ww

 

源码编译

源码编译的文档有点不好找,在这:https://www.envoyproxy.io/docs/envoy/latest/start/building

使用bazel编译系统,

编译步骤:https://github.com/envoyproxy/envoy/blob/7e9a0e7bfda6bf563f148521000ceb41e6d53f48/bazel/README.md

 

sudo wget -O /usr/local/bin/bazel https://github.com/bazelbuild/bazelisk/releases/latest/download/bazelisk-linux-$([ $(uname -m) = "aarch64" ] && echo "arm64" || echo "amd64")
sudo chmod +x /usr/local/bin/bazel
python3 tools/github/write_current_source_version.py

 

安装clang 14

wget https://github.com/llvm/llvm-project/releases/download/llvmorg-14.0.6/clang+llvm-14.0.6-x86_64-linux-gnu-rhel-8.4.tar.xz
tar Jxvf clang+llvm-14.0.6-x86_64-linux-gnu-rhel-8.4.tar.xz
bash bazel/setup_clang.sh  /home/tong/Source/clang+llvm-14.0.6-x86_64-linux-gnu-rhel-8.4/
echo "build --config=clang" >> user.bazelrc

 

bazel会下载所有的依赖代码,然后对依赖进行编译,所以编译起来非常慢。

编译命令:

bazel build //:envoy --copt=-Wno-error=missing-requires --copt=-Wno-changes-meaning --copt=-Wno-error=dangling-reference --copt=-Wno-dangling-pointer --copt=-Wno-error=dangling-pointer --sandbox_debug --verbose_failures

 

 

运行

编出来可以直接download二进制也一样:

https://github.com/envoyproxy/envoy/releases/download/v1.24.0/envoy-1.24.0-linux-x86_64

配置文件:

https://www.envoyproxy.io/docs/envoy/latest/_downloads/92dcb9714fb6bc288d042029b34c0de4/envoy-demo.yaml

运行命令

./envoy -c envoy-demo.yaml
curl -v 127.0.0.1:10000

 

修改启动日志级别的方法

sudo ./bazel-bin/source/exe/envoy-static -c ~/envoy/test.yaml --concurrency 1 --component-log-level upstream:debug,config:debug

 

 

查看监听

 

override配置 

envoy -c envoy-demo.yaml --config-yaml "$(cat envoy-override.yaml)"

 

竟然还有个web页

 

 

 

动态配置

Listener、Cluster 等除了在配置文件中静态配置,还可以从控制平面动态获取。下发动态配置的服务叫做 控制平面 ,它的地址以 cluster 的形式在配置文件中静态配置,
然后在 dynamic_resources 中引用这个 cluster。 可以动态下发的配置见 Dynamic configuration,主要有: CDS:cluster 配置动态下发 LDS:listener 配置动态下发 EDS:cluster 中的 endpoint 的动态下发 RDS:listener 中的 route 的动态下发 SDS:证书的动态下发 其中只有 CDS 和 LDS 的发现地址是在 dynamic_resources 中指定的,后面有详细说明。

 

好好读:https://www.envoyproxy.io/docs/envoy/v1.11.0/intro/arch_overview/operations/dynamic_configuration

 

文件系统动态加载

https://www.envoyproxy.io/docs/envoy/latest/_downloads/9a41bc513e17e885884b3deebf435d2a/envoy-dynamic-filesystem-demo.yaml

https://www.envoyproxy.io/docs/envoy/latest/_downloads/5cf56125ff834c0e2f21f71e1e8916f2/envoy-dynamic-lds-demo.yaml

https://www.envoyproxy.io/docs/envoy/latest/_downloads/92bba5b0c48a649b4bc8663000cd097a/envoy-dynamic-cds-demo.yaml

 

最小配置

node:
  cluster: test-cluster
  id: test-id

dynamic_resources:
  cds_config:
    path: /home/tong/envoy/dynamic/cds.yaml
  lds_config:
    path: /home/tong/envoy/dynamic/lds.yaml


admin:
  address:
    socket_address:
      address: 0.0.0.0
      port_value: 19000

 

命令

./envoy -c envoy-dynamic-filesystem-demo.yaml --config-yaml "$(cat envoy-override.yaml)"

监听

 

修改配置

改监听

 

 改upstream

 

 修改:

 

 

如截图所示,要mv一下触发一个inotify事件出来。

 

 运行效果

 

添加一个listener

 

修改内容:

原封不动的复制了一段,只修改了listener的名字,以及追加了一个新的端口号10002

 

可以看见系统中新增了监听:

 

 

控制面动态加载

使用xds API:

https://www.envoyproxy.io/docs/envoy/latest/api-docs/xds_protocol#xds-protocol

 最小配置

node:
  cluster: test-cluster
  id: test-id

dynamic_resources:
  ads_config:
    api_type: GRPC
    grpc_services:
    - envoy_grpc:
        cluster_name: xds_cluster
  cds_config:
    ads: {}
  lds_config:
    ads: {}

static_resources:
  clusters:
  - type: STRICT_DNS
    typed_extension_protocol_options:
      envoy.extensions.upstreams.http.v3.HttpProtocolOptions:
        "@type": type.googleapis.com/envoy.extensions.upstreams.http.v3.HttpProtocolOptions
        explicit_http_config:
          http2_protocol_options: {}
    name: xds_cluster
    load_assignment:
      cluster_name: xds_cluster
      endpoints:
      - lb_endpoints:
        - endpoint:
            address:
              socket_address:
                address: my-control-plane
                port_value: 18000

admin:
  address:
    socket_address:
      address: 0.0.0.0
      port_value: 19000

 

所有k8s ingress兼容的控制面都适用,包括istol,hingress,等。

envoy提供了一个go的控制器:https://github.com/envoyproxy/go-control-plane

 

健康检查

https://www.envoyproxy.io/docs/envoy/latest/intro/arch_overview/upstream/health_checking#health-checking

https://www.envoyproxy.io/docs/envoy/latest/configuration/upstream/cluster_manager/cluster_hc#config-cluster-manager-cluster-hc

健康检查日志

https://www.envoyproxy.io/docs/envoy/latest/intro/arch_overview/upstream/health_checking#health-check-event-logging

 

限速

https://www.envoyproxy.io/docs/envoy/latest/intro/arch_overview/other_features/global_rate_limiting#arch-overview-global-rate-limit

连接数限制

https://www.envoyproxy.io/docs/envoy/latest/configuration/listeners/network_filters/connection_limit_filter#config-network-filters-connection-limit

https://www.envoyproxy.io/docs/envoy/latest/configuration/listeners/runtime#config-listeners-runtime

Hot restart

https://www.envoyproxy.io/docs/envoy/v1.11.0/intro/arch_overview/operations/hot_restart#arch-overview-hot-restart

 

证书管理

https://www.envoyproxy.io/docs/envoy/latest/operations/certificates

 

运维状态查询

https://www.envoyproxy.io/docs/envoy/latest/operations/admin

 

统计与查询

https://www.envoyproxy.io/docs/envoy/latest/operations/stats_overview

https://www.envoyproxy.io/docs/envoy/latest/configuration/http/http_conn_man/stats#config-http-conn-man-stats

 

重点读物

https://www.envoyproxy.io/docs/envoy/latest/configuration/configuration

https://www.envoyproxy.io/docs/envoy/latest/operations/operations

 

其他

https://jimmysong.io/book/envoy-made-simple/architecture/data-flow-request-processing/

 

posted on 2025-03-27 21:08  toong  阅读(11)  评论(0)    收藏  举报