Cilium 源码解读之 Agent 启动
|
|
Overview
cilium-agent 通过命令行启动,输入配置文件可以通过 ConfigMap 传入:
|
|
函数的入口是 runDaemon():
|
|
This method performs following tasks sequentially:
- Prapare to create daemon
- Enable IP forwarding in kernel
- Init k8s package
- Create new daemon, restore endpoints
- Enable conntrack/nat GC
- Init kvstore (cilium-etcd)
- Regenerate BPF for endpoints
- Init health checks, status checker, metrics
- Send agent started messsage to monitor
- Serve Cilium HTTP API
- Mark node as ready in k8s
- Start hubble if enabled
Prepare to create daemon
Enable IP forwarding: enableIPForwarding()
对于 Linux,将会通过 sysctl 在节点上打开以下内核参数:
net.ipv4.ip_forward=1net.ipv6.ip_forward=1net.ipv4.conf.all.forwarding=1
Init k8s package: k8s.Init()
可以看到创建了 k8s REST client:
|
|
Create daemon: NewDaemon()
接下来创建的 Daemon 对象,做了
|
|
Read custom CNI file if configured
See Trip.com: First Step towards Cloud-native Networking for how custom CNI can be used.
Create daemon instance
It then initiates a daemon instance, and its fields:
|
|
initMaps(): open or create all BPF maps
|
|
RestoreServices(): restore service <-> backend mappings from BPF maps
|
|
RunK8sServiceHandler(): listen to k8s Service changes
|
|
RegisterCRDs
|
|
bootstrapIPAM
|
|
restoreOldEndpoints(): extract endpoints info from local files
|
|
AnnotateNode
|
|
See Cilium Code Walk Through: Agent CIDR Init.
d.init(): clean state dir, setup sockops, init datapath loader
|
|
RestoreTemplates
loader.RestoreTemplates(Config.StateDir) // restore previous BPF templates
// pkg/datapath/loader/cache.go
// RestoreTemplates populates the object cache from templates on the filesystem at the specified path.
func RestoreTemplates(stateDir string) error {
path := filepath.Join(stateDir, defaults.TemplatesDir)
RemoveAll(path)
}
Start identity watcher
|
|
This will listen to the ip -> identity mapping changes in kvstore, to be specific, it will listen to cilium/state/ip/v1/ resource in kvstore, an example:
- key:
cilium/state/ip/v1/default/192.168.1.2. - value:
{"IP":"192.168.1.2","Mask":null,"HostIP":"xx","ID":44827,"Key":0,"Metadata":"cilium-global:default:node1:2191","K8sNamespace":"default","K8sPodName":"pod-1"}, note that theIDfield is just the identity.
|
|
gc.Enable(): enable conntrack/nat GC
|
|
initKVStore(): setup some GC jobs, connect to kvstore
|
|
initRestore(): regenerate BPF for restored endpoints
|
|
Re-regenerate restored endpoints
This will regenerate BPF for all restored endpoints.
See Cilium Code Walk Through: Agent Restore Endpoints And Identities.
Init ClusterMesh if enabled
See Cilium Code Walk Through: ClusterMesh.
Init health checks, metrics, Cilium API server
Init health checks
See Cilium Code Walk Through: Node & Endpoint Health Probe.
Init status collector
|
|
Send notification to monitor
Sends a "Cilium agent started" (monitorAPI.AgentNotifyStart) message to the monitor.
|
|
Serve Cilium HTTP API
At this point, cilium commands can be correctly get handled, e.g. cilium status --brief.
Mark node ready
Launch hubble if configured
Misc
IPAM States Restoration
IPAM manages IP address allocation, it tracks two states:
|
|
IPAM stores its states in memory. How could this survive agent restart or host reboot?
The secret lies in the files on local node:
- For each allocated IP, Cilium creates an endpoint for it, and write the endpoint info into a local file (C header file).
- When agent restarts or host reboots, IPAM states in memory will be reset. The agent will loop over all endpoint files, parsing the IP inside it, and reserve them in IPAM.
In this way, IPAM restores its states.
-
No backlinks found.