Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize kubeproxy source code analysis

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article is to share with you about how to achieve kubeproxy source code analysis, the editor feels very practical, so share with you to learn, I hope you can get something after reading this article, do not say much, follow the editor to have a look.

Kubernetes offline installation package

Kube-proxy source code parsing

Ipvs has higher performance and stability than iptables mode. This paper focuses on the source code parsing of this mode. If you want to understand the principle of iptables mode, you can refer to its implementation, and there is no difference in architecture.

The main function of kube-proxy is to listen for events of service and endpoint, and then delegate the agent policy to the machine. The bottom layer calls docker/libnetwork, and libnetwork finally calls netlink and netns to implement actions such as the creation of ipvs.

Initialize configuration

Code entry: cmd/kube-proxy/app/server.go Run () function

Initialize the configuration of proxyServer with command line arguments

ProxyServer, err: = NewProxyServer (o) type ProxyServer struct {/ / k8s client Client clientset.Interface EventClient v1core.EventsGetter / / ipvs related interface IptInterface utiliptables.Interface IpvsInterface utilipvs.Interface IpsetInterface utilipset.Interface / / processor Proxier proxy.ProxyProvider / / proxy mode for processing synchronization Ipvs iptables userspace kernelspace (windows) four ProxyMode string / / configuration synchronization cycles ConfigSyncPeriod time.Duration / / service and endpoint event handler ServiceEventHandler config.ServiceHandler EndpointsEventHandler config.EndpointsHandler}

Proxier is the main entry, abstracting two functions:

Type ProxyProvider interface {/ / Sync immediately synchronizes the ProxyProvider's current state to iptables. Sync () / / execute SyncLoop () on a regular basis

Ipvs's interface is very important:

Type Interface interface {/ / Delete all rules Flush () error / / add a virtual server AddVirtualServer (* VirtualServer) error UpdateVirtualServer (* VirtualServer) error DeleteVirtualServer (* VirtualServer) error GetVirtualServer (* VirtualServer) (* VirtualServer, error) GetVirtualServers () ([] * VirtualServer, error) / / add a realserver to virtual server For example, VirtualServer is a clusterip realServer: pod (or custom endpoint) AddRealServer (* VirtualServer, * RealServer) error GetRealServers (* VirtualServer) ([] * RealServer, error) DeleteRealServer (* VirtualServer, * RealServer) error}

Let's take a closer look at how ipvs_linux implements the above interface below.

Virtual server and realserver, the most important is ip:port, and then there are some agent modes such as sessionAffinity and so on:

Type VirtualServer struct {Address net.IP Protocol string Port uint16 Scheduler string Flags ServiceFlags Timeout uint32} type RealServer struct {Address net.IP Port uint16 Weight int}

Create apiserver client

Client, eventClient, err: = createClients (config.ClientConnection, master)

Create a Proxier that is a proxier that only focuses on the ipvs schema

Else if proxyMode = = proxyModeIPVS {glog.V (0) .Info ("Using ipvs Proxier.") ProxierIPVS, err: = ipvs.NewProxier (iptInterface, ipvsInterface, ipsetInterface, utilsysctl.New (), execer, config.IPVS.SyncPeriod.Duration, config.IPVS.MinSyncPeriod.Duration Config.IPTables.MasqueradeAll, int (* config.IPTables.MasqueradeBit), config.ClusterCIDR, hostname, getNodeIP (client, hostname), recorder, healthzServer Config.IPVS.Scheduler,). Proxier = proxierIPVS serviceEventHandler = proxierIPVS endpointsEventHandler = proxierIPVS

This Proxier has the following methods:

+ OnEndpointsAdd (endpoints * api.Endpoints) + OnEndpointsDelete (endpoints * api.Endpoints) + OnEndpointsSynced () + OnEndpointsUpdate (oldEndpoints, endpoints * api.Endpoints) + OnServiceAdd (service * api.Service) + OnServiceDelete (service * api.Service) + OnServiceSynced () + OnServiceUpdate (oldService, service * api.Service) + Sync () + SyncLoop ()

So this Proxier of ipvs implements most of the interfaces we need.

A brief summary:

+-> endpointHandler | +-> serviceHandler | ^ | | +-> sync periodic synchronization, etc. | ProxyServer- > Proxier-> service event callback | +-> endpoint event callback | | trigger +-> ipvs interface ipvs handler |

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report