【ServiceMesh学习之路】pilot-agent 源码分析[01]

Service Mesh

本章主要学习 istio 中的 pilot-agent 的源码;
使用的 istio 1.16.1 版本:commitID: f5212a6f7df61fd8156f3585154bed2f003c4117

【个人建议】
如果有条件可以在集群中安装 istio 组件,这样学习可能更直观;


1、pilot-agent 的主要功能

  • 启动 envoy 代理程序
  • 生成 envoy 的 BootStrap 配置文件
  • 健康检查
  • 监视证书变化,通知 envoy 程序热重启,实现证书热加载
  • 提供 envoy 的守护功能,当 envoy 程序意外退出时重新启动
  • 通知 envoy 优雅退出

上面的功能会在源码中进行一一分析;

2、pilot-agent 在 k8s 中的部署方式

sidecar 在注入的时候,会在业务 Pod 中注入 istio-init 和 istio-proxy 两个容器:

  • istio-init 容器

    • initContainers: 初始化容器,
    • args: istio-iptables
    • image: docker.io/istio/proxyv2:1.16.1 写入 iptables 规则,将发送给 Pod 的流量转发到 proxy 上,实现对流量的拦截;
  • istio-proxy 容器

    • containers: sidecar 容器
    • args: proxy sidecar
    • image: docker.io/istio/proxyv2:1.16.1

核心功能实现容器

3、代码功能分析

  1. 代码入口
pilot/cmd/pilot-agent/main.go

通过 cobra 的方式实现命令行参数;

  1. Pilot proxy 配置信息
# pilot/cmd/pilot-agent/options/agent_proxy.go
// ProxyArgs provides all of the configuration parameters for the Pilot proxy.
type ProxyArgs struct {
   DNSDomain          string
   StsPort            int
   TokenManagerPlugin string

   MeshConfigFile string

   // proxy config flags (named identically)
   ServiceCluster         string
   ProxyLogLevel          string
   ProxyComponentLogLevel string
   Concurrency            int
   TemplateFile           string
   OutlierLogPath         string

   PodName      string
   PodNamespace string
}
  1. Pilot agent 总体流程
proxyCmd := newProxyCommand()
rootCmd.AddCommand(proxyCmd)

生成对应的 proxy cmd, 再加入到根命令中;

  1. NewRootCommand() 分析
# pilot/cmd/pilot-agent/app/cmd.go
func NewRootCommand() *cobra.Command {}

在这个函数里,主要调用 newProxyCommand() 生成 proxy 命令:

proxyCmd := newProxyCommand()

根据对应传入的参数,生成 model.Proxy{} 结构:

proxy, err := initProxy(args)

这是生成 proxy 配置信息 meshconfig.ProxyConfig{} 结构:

proxyConfig, err := config.ConstructProxyConfig()

生成安全相关 security.Options{} 结构:

options.NewSecurityOptions()

【学习仓库】https://github.com/librant/learn

84
0
1
0
关于作者
相关产品
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论