选主操作就是通过client去etcd获取锁信息,和本地一样,证明是主,开始启动各种controller,然后就更新主的锁的时间戳存入etcd,每过一段时间就更新时间戳等信息存入etcd. 如果发现有新主了,打个log,然后发送event到apiserver.停止master上的各种controller工作。

如果不一样就证明是有人是主了,就一直的过一段时间重试,不停的获取。

代码如下:

leaderelection.RunOrDie(leaderelection.LeaderElectionConfig{
   Lock:          &rl,
   LeaseDuration: s.LeaderElection.LeaseDuration.Duration,
   RenewDeadline: s.LeaderElection.RenewDeadline.Duration,
   RetryPeriod:   s.LeaderElection.RetryPeriod.Duration,
   Callbacks: leaderelection.LeaderCallbacks{
      OnStartedLeading: run,
      OnStoppedLeading: func() {
         glog.Fatalf("leaderelection lost")
      },
   },
})
// Run starts the leader election loop
func (le *LeaderElector) Run() {
   defer func() {
      runtime.HandleCrash()
      le.config.Callbacks.OnStoppedLeading()
   }()
   le.acquire()
   stop := make(chan struct{})
   go le.config.Callbacks.OnStartedLeading(stop)
   le.renew()
   close(stop)
}
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐