在Kubernetes中创建控制器的最佳方法是什么?

huangapple go评论79阅读模式
英文:

What is the best approach for creating a controller in Kubernetes?

问题

我不太确定以下哪种方法是在Kubernetes中创建控制器的更好方法,但我知道:

  • 我绝不想以任何方式创建自定义资源。
  • 我只想获取有关Kubernetes本机资源(Pod等)的信息,考虑到每个命名空间中可能有很多Pod。

我看到了一些模式,例如:

ctrl, err := controller.New("name-here", mgr, controller.Options{
	Reconciler: &ReconcilePod{Client: mgr.GetClient(), Logger: log},
})

其中ReconcilePod是一个结构体,它有一个名为Reconcile的函数,负责整个业务逻辑。

另一种方法是这样定义一个控制器:

type Controller struct {
	indexer  cache.Indexer
	queue    workqueue.RateLimitingInterface
	informer cache.Controller
}

然后定义shared informerwatcher等。

第三种模式是使用operators

也许我不明白的是上述提到的方法之间的主要区别,以及哪种方法适合在大规模情况下满足我的需求。

英文:

I'm not quite sure if which of the following approaches is the better approach to create a controller in kubernetes however I know that:

  • I don't want to create a custom resource by any means.
  • I do only want to fetch information about k8s native resources (pods, ...) given that there might be a lot of pods in each namespace

I have seens some patterns like:

ctrl, err := controller.New("name-here", mgr, controller.Options{
	Reconciler: &ReconcilePod{Client: mgr.GetClient(), Logger: log},
})

which ReconcilePod is a struct that has a function Reconcile that keep whole business logic.

Another approach I have seens is like following:

type Controller struct {
    indexer  cache.Indexer
    queue    workqueue.RateLimitingInterface
    informer cache.Controller
}

and then defining shared informer and watcher etc.
And the third pattern that I have seen is using operators

what I don't get perhaps is what is the main differences between mentioned approaches above and which one fits my need at scale.

答案1

得分: 1

如果您不想“控制”任何内容,就没有必要创建一个控制器。

如果您只想“读取”和“观察”资源,可以使用client-go,并参考通过共享informer扩展Kubernetes以获取关于如何“读取”和“观察”资源的灵感。

要及时了解这些事件何时触发,您可以使用Kubernetes和client-go提供的一个名为SharedInformer的原始工具,它位于缓存包中。让我们看看它在实践中是如何工作的。

控制器更复杂,包含一个“协调循环”,因为它们应该实现/管理一个“期望状态”。

一个“操作员”也是一个控制器。

英文:

If you don't want to "control" anything, there is no need to create a controller.

If you just want to "read" and "watch" resources, you can use client-go and see e.g. Extend Kubernetes via a shared informer for inspiration about how to read and watch resources.

>To stay informed about when these events get triggered you can use a primitive exposed by Kubernetes and the client-go called SharedInformer, inside the cache package. Let’s see how it works in practice.

Controllers are more complex and contains a reconciliation loop since they should realize/manage a desired state.

An "operator" is a controller as well.

答案2

得分: 0

你可以简单地使用operator-sdk来创建一个控制器,它会生成代码,然后你只需要为你的对象添加业务逻辑。你可以从这里开始:https://sdk.operatorframework.io/docs/building-operators/golang/quickstart/

mkdir memcached-operator
cd memcached-operator
operator-sdk init --domain example.com --repo github.com/example/memcached-operator

然后,你可以为现有的kind或者新的kind创建控制器:

operator-sdk create api --group cache --version v1alpha1 --kind Memcached --resource --controller

这是使用operator-sdk管理kind和资源的最简单方式,不需要担心复杂的客户端代码。

英文:

You can simply start with operator-sdk provided to create a controller easily in which the code will be generated and you need to add business logic to your objects. https://sdk.operatorframework.io/docs/building-operators/golang/quickstart/

mkdir memcached-operator
cd memcached-operator
operator-sdk init --domain example.com --repo github.com/example/memcached-operator

You can simply then create controller eith for existing kind or a new kind

operator-sdk create api --group cache --version v1alpha1 --kind Memcached --resource --controller

It is the simplest way to manage kinds and resources using operator sdk and no need to worry about complex code of client set

huangapple
  • 本文由 发表于 2022年4月23日 04:12:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/71974119.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定