K8S系统组件高可用选主逻辑

2020-10-12

我们知道,在K8S的高可用实现中,kube-apiserver是通过启动多副本实例,前面挂负载均衡将请求分发到后端的实例来实现高可用,即多个实例同时提供服务,而kube-scheduler和kube-controller-manager是通过主备的方式实现高可用

先来看一个命令:

MacBook-Pro-WRJ:~ wrj$ kubectl get endpoints  kube-scheduler -n kube-system -o yaml
apiVersion: v1
kind: Endpoints
metadata:
  annotations:
    control-plane.alpha.kubernetes.io/leader: '{"holderIdentity":"iZbp1ip0k218fyn4474oapZ_b0ad5354-cbcd-11ea-9a2d-00163e09b061","leaseDurationSeconds":15,"acquireTime":"2020-07-22T03:46:32Z","renewTime":"2020-10-12T08:32:11Z","leaderTransitions":13}'
  creationTimestamp: "2019-07-08T03:22:07Z"
  name: kube-scheduler
  namespace: kube-system
  resourceVersion: "163436659"
  selfLink: /api/v1/namespaces/kube-system/endpoints/kube-scheduler
  uid: 90e7dad1-a12f-11e9-b020-00163e121eee

分布式系统中,一般需要借助外部系统进行选主,如:redis、zookeeper、etcd等, k8s的存储使用了etcd,但并没有使用etcd来实现选主,而是对 endpoint 这个资源做抢占,谁想抢到并将自己的信息写入 endpoint的 annotation 中,谁就获得了主

获得锁的状态信息存放在key为control-plane.alpha.kubernetes.io/leader的注解中,holderIdentity:iZbp1ip0k218fyn4474oapZ_b0ad5354-cbcd-11ea-9a2d-00163e09b061即为当前锁的获得者。可以看到租约是15s,获得锁成功的时刻是2020-07-22T03:46:32Z,
最新一次更新锁的时刻是:2020-10-12T08:32:11Z,然后锁发生交换的次数是13次

参考自:http://www.xuyasong.com/?p=2037