2

我正在尝试运行一个容器的 3 个 pod/副本,我希望其中一个作为领导者运行,如果该特定 pod 停止或失败,其他非领导者运行的 pod 应立即领导继续执行这是领导者吊舱所必需的。只是为了清楚其他 pod 会执行,但是领导 pod 一旦获得领导权,就会执行一些其他 pod 不会执行的额外代码。这是我关注的文档的链接。

这是我运行的一个测试示例,我在运行容器应该运行的实际代码时遇到问题。

package main
import (
    "fmt"
    "github.com/operator-framework/operator-lib/leader"
    "sigs.k8s.io/controller-runtime/pkg/manager"
    "sigs.k8s.io/controller-runtime/pkg/client/config"
    "sigs.k8s.io/controller-runtime/pkg/manager/signals"
    "context"
    "time"
    "os"
)

func main() {
    fmt.Println("Program has started successfully")

    cfg, err := config.GetConfig()
    if err != nil {
        os.Exit(1)
    }

    opts := manager.Options{
        LeaderElection:     true,
        LeaderElectionID:   "memcached-operator-lock",
    }
    fmt.Println("Creating a manager")
    mgr, err := manager.New(cfg, opts)
    if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
        fmt.Println(err, "Manager exited non-zero")
        os.Exit(1)
    }
    fmt.Println("Competing to become the leader")
    err = leader.Become(context.TODO(), "memcached-operator-lock")
    if err != nil {
        fmt.Println("Failed to retry for leader lock because : %v ", err)
    }
    fmt.Println("I am the leader, i will pause for 4 minutes")
    time.Sleep(4 * time.Minute)
}

leader pod的日志如下:

[diamanti@appserv41 leader_test]$ kubectl logs test-rs-qbm7k
Program has started successfully
Creating a manager
I1007 00:14:10.785904       1 leaderelection.go:242] attempting to acquire leader lease  default/memcached-operator-lock...
I1007 00:14:28.184916       1 leaderelection.go:252] successfully acquired lease default/memcached-operator-lock

我希望 pod 能够运行到“我是领导者”打印语句并执行我希望领导者 pod 执行的任务。即使我可以在某个地方传递一个函数,一旦它获得领导权就可以执行它,这对我来说没问题。目前,我在某处犯了错误,在 3 个 pod 之一获得领导权后无法做任何事情。

4

0 回答 0