(接上篇)
初始化porxy,具体如下所示。
KubeEdge/edgemesh/pkg/proxy/proxy.go |
// Init: init the proxy. create virtual device and assign ips, etc.. func Init() { go func() { unused = make([]string, 0) addrByService = &addrTable{} c := context.GetContext(context.MsgCtxTypeChannel) metaClient = client.New(c) //create virtual network device for { err := vdev.CreateDevice() if err == nil { break } klog.Warningf("[L4 Proxy] create Device is failed : %s", err) //there may have some exception need to be fixed on OS time.Sleep(2 * time.Second) } //configure vir ip ipPoolSize = 0 expandIpPool() //open epoll ep, err := poll.CreatePoll(pollCallback) if err != nil { vdev.DestroyDevice() klog.Errorf("[L4 Proxy] epoll is open failed : %s", err) return } epoll = ep go epoll.Loop() klog.Infof("[L4 Proxy] proxy is running now") }() } |
Init()函数主要做3件事情。
1)创建获得pod原数据的client;
2)创建和维护虚拟网络设备;
3)通过epoll管理代理进程。
CreateDevice()函数先判断虚拟网络设备edge0是否存在,如果存在就将其删除并重建;如果不存在就直接创建虚拟网络设备edge0。
启动服务
server.Start()定义具体内容如下所示。
KubeEdge/edgemesh/pkg/server/server.go |
func Start() { //Initialize the resolvers r := &resolver.MyResolver{"http"} resolver.RegisterResolver(r) //Initialize the handlers config.GlobalDefinition = &model.GlobalCfg{} config.GlobalDefinition.Panel.Infra = "fake" opts := control.Options{ Infra: config.GlobalDefinition.Panel.Infra, Address: config.GlobalDefinition.Panel.Settings["address"], } config.GlobalDefinition.Ssl = make(map[string]string) control.Init(opts) opt := registry.Options{} registry.DefaultServiceDiscoveryService = edgeregistry.NewServiceDiscovery(opt) myStrategy := mconfig.CONFIG.GetConfigurationByKey("mesh.loadbalance.strategy-name").(string) loadbalancer.InstallStrategy(myStrategy, func() loadbalancer.Strategy { switch myStrategy { case "RoundRobin": return &loadbalancer.RoundRobinStrategy{} case "Random": return &loadbalancer.RandomStrategy{} default: return &loadbalancer.RoundRobinStrategy{} } }) //Start dns server go DnsStart() //Start server StartTCP() } |
Start()函数做了3件事情:
1)初始化resolvers、handlers。初始化的resolvers、handlers在TCP服务处理连接的过程中被使用。
2)启动 DNS服务。该DNS服务主要为节点内的应用做域名解析。
3)启动TCP服务。该TCP服务在节点间做流量代理。
通过一个for循环接收通信管道中关于edgemesh的信息并处理,具体如下所示。
KubeEdge/edgemesh/pkg/module.go |
//Start sets context and starts the controller func (em *EdgeMesh) Start(c *context.Context) { ... // we need watch message to update the cache of instances for { if msg, ok := em.context.Receive(constant.ModuleNameEdgeMesh); ok == nil { proxy.MsgProcess(msg) klog.Infof("get message: %v", msg) continue } } } |
「未完待续……」