llhuii / dive-into-k8s

Apache License 2.0
0 stars 0 forks source link

endpoint controller #13

Open llhuii opened 3 years ago

llhuii commented 3 years ago

endpoint controller

给设置了selector的service 管理endpoint

list/watch service:更新serviceSelectorCache,将对应service加到队列 list/watch endpoint: 只监听删除事件,将对应service(endpoint与service同名)加到队列

list/watch pod:

worker处理service对象逻辑:

  1. 如果service not found: 删除相同名字的endpoint
  2. 如果service没有selector,则不处理
  3. 根据service的selector,获取pods,并遍历pods,更新subnets a) PodIP没有值(cni没有分配地址)或者DeletionTimestamp有值(删除中),不处理 b) 创建pod对应的EndpointAddress c) 如果service无port且为headless service,添加到subsets d) 遍历service.Ports, 根据对应pod的容器port号和EndpointAddress,添加到subsets
  4. 获取service对应的endpoint,如果没有,则fake一个ep
  5. 当以下条件满足,不更新endpoint: 1) subnets和ep.subnets一致 2) 且service.labels与ep.labels一致 3) 且ep.subnets没有超过容量(为1000) 4) 且没有被truncated过(打注解)
  6. 否则更新ep: 1) 设置ep.subnets = subnets 2) ep.labels = service.labels 3) 需要truncate subnet, 则设置endpoints.kubernetes.io/over-capacity的注解为truncated; 否则删除该注解 4) 设置headless label 5) 调用client-go 更新或者创建ep
llhuii commented 2 years ago

endpoint slice KEP https://github.com/kubernetes/enhancements/tree/master/keps/sig-network/0752-endpointslices#motivation

解决单个endpoint 过大的情况,导致: 1)etcd可能拒绝单个endpoint 对象的更新 2)endpoint更新广播到kubeproxy的流量过大引起性能问题

llhuii commented 2 years ago

拓扑感知提示 KEP https://github.com/kubernetes/enhancements/blob/master/keps/sig-network/2433-topology-aware-hints/README.md

流量均衡:将一个service下的endpoint根据zone的能力(即总核数)打散到各个zone。 流量优先:集群pod优先访问在同一个zone内的endpoint。

大致原理:

  1. node 打了label:topology.kubernetes.io/zone
  2. service标有注解:key为service.kubernetes.io/topology-aware-routing 值为Auto
  3. endpoint slice controller:根据zone的能力均衡地更新service下endpoint 的hints
  4. kubeproxy:过滤跟该node的zone 匹配的hint的endpoint的规则(注如果存在endpoint 无hint,则不进行过滤)