云原生|Operator本质

时间:2022-07-25
本文章向大家介绍云原生|Operator本质,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

什么是 Operator

CoreOS 在 2016 年底提出了 Operator 的概念,当时的一段官方定义如下:

“An Operator represents human operational knowledge in software, to reliably manage an application.

什么是Controller Loop?

简单来说,Controller Loop是Controller动作的基础。想象一下,有一个非终止的进程(在Kubernetes中称为和解循环)在不断地发生,如下图所示:

这个过程至少观察一个Kubernetes对象,该对象包含有关所需状态的信息。比如:

  • Deployment
  • Services
  • Secrets
  • Ingress
  • Config Maps

这些对象由JSON或YAML中的manifest组成的配置文件定义。然后controller根据内置逻辑,通过Kubernetes API进行持续调整,模仿所需状态,直到当前状态变成所需状态。

通过这种方式,Kubernetes通过处理不断的更改来处理Cloud Native系统的动态性质。为达到预期状态而执行的修改实例包括:

  • 注意到节点宕机时,要求更换新的节点。
  • 检查是否需要复制pods。
  • 如果需要,创建一个新的负载均衡器。

Operator是一个特定应用程序的controller,它扩展了一个Kubernetes API,替代运维工程师或SRE工程师来创建、配置和管理复杂的应用程序。在Kubernetes官方文档中对此有以下描述:

Operator是Kubernetes的软件拓展,它利用自定义资源来管理应用程序及其组件。Operator遵循Kubernetes的原则,尤其遵循control loop。

到目前为止,你已经了解Operator会利用观察Kubernetes对象的controller。这些controller有点不同,因为它们正在追踪自定义对象,通常称为自定义资源(CR)。CR是Kubernetes API的扩展,它提供了一个可以存储和检索结构化数据的地方——你的应用程序的期望状态。整个操作原理如下图所示:

Operator会持续跟踪与特定类型的自定义资源相关的集群事件。可以跟踪的关于这些自定义资源的事件类型有:

  • Add
  • Update
  • Delete

当Operator接收任何信息时,它将采取行动将Kubernetes集群或外部系统调整到所需的状态,作为其在自定义controller中的和解循环(reconciliation loop)的一部分。

Why are Operators different than other tools out there?
Operators are purpose-built to run a Kubernetes application, with operational knowledge baked in. They will be smarter and more tailored than generic tools.

The cloud-like capabilities that are encoded into the Operator code can provide an advanced user experience, automating such features as updates, backups and scaling. All of this is accomplished using standard Kubernetes tools, CLI and API.

What are benefits of using the Operator Framework?
If you are a community member, builder, consumer of applications, or a user of Kubernetes overall, the Operator Framework offers a number of benefits. Operators are built on top of a common set of Kubernetes APIs. They act like cloud services, make it more simple to install and update Kubernetes applications, without having to worry about the underlying platform.

Operators helps your teams to build a great automation experience. They allow teams to build in expertise of automated operations, instead of building manually each time.

How does the Operator Framework make hybrid cloud easier?
For consumers of applications across the hybrid cloud, keeping those applications up to date as new versions become available is of supreme importance, both for security reasons and for managing the applications’ lifecycles and other needs. The Operator Framework helps address these user requirements, aiding in the creation of cloud-native applications that are easier to consume, to keep updated, and to secure.

Who builds an Operator?
Operators are best built by those that are experts in the “business logic” of installing, running and upgrading an application.

Experience has shown that the creation of an Operator typically starts by automating an application’s installation and self-service provisioning capabilities, and then evolves to take on more complex automation.

Who deploys an Operator?
Operators are deployed by end-users through the Lifecycle Manager. Common patterns are for centralized infrastructure teams to grant access to a team’s Namespaces to run specific Operators. Afterwards, each team can manage, upgrade and scale their Operators in a self-service manner.

Operators can package internal applications at an enterprise, software that is deployed by commercial customers, or popular open source projects. Operators can even power a SaaS environment with a large amount of individual instances of an application.

Does an Operator require Kubernetes?
Yes, Kubernetes is required, but range of versions/distros are supported. The goal is to provide tooling to build Kubernetes applications, Operators, that are independent to a specific vendor or cloud platform.

Do I always need to write my own Operator to get value out of the Operator Framework?
You do not need to write your own Operator in order to get value out of the Operator Framework. Operators can be written such that they can be reused for applications. This means that you can, for example, create a generic Helm Operator that can be specialized for individual Charts. Even applications that do not require more than the built-in Kubernetes Workloads APIs can benefit from the lifecycle management and unified user-experience provided by the Operator Framework.

How do I join the community or help out?
All core components are part of a dedicated GitHub organization called “Operator Framework”. Please join the Operator Framework mailing list for discussion, questions and comments. Please also review and assist community operators as listed here and here.

https://developers.redhat.com/blog/2018/12/18/introduction-to-the-kubernetes-operator-framework/

https://coreos.com/operators/

https://www.openshift.com/blog/kubernetes-operators-best-practices?extIdCarryOver=true&sc_cid=701f2000001OH7EAAW

https://sdk.operatorframework.io/docs/building-operators/golang/tutorial/

https://book.kubebuilder.io/cronjob-tutorial/controller-overview.html

https://www.okd.io/

https://github.com/kubernetes-sigs/controller-tools