k8s operator

what is it

An Operator is software that encodes this domain knowledge and extends the Kubernetes API through the third party resources mechanism, enabling users to create, configure, and manage applications. Like Kubernetes’s built-in resources, an Operator doesn’t manage just a single instance of the application, but multiple instances across the cluster.

Operator 的设计旨在简化复杂有状态应用管理,其通过CRD扩展 Kubernetes API 来自动创建、管理和配置应用实例。其本质上是针对特定的场景去做有状态服务,或者说针对复杂应用场景,去简化其运维管理的工具。

Operator以deployment的形式部署到K8S中。部署完这个Operator之后,想要部署一个集群,其实很方便。因为不需要再去管理这个集群的配置信息了,只需要创建一个CRD,指定创建多少个节点,需要什么版本,Operator会监听该资源对象,创建出符合配置要求的集群,从而大大简化运维的难度和成本。

Operator 实际上作为kubernetes自定义扩展资源注册到controller-manager,通过list and watch的方式监听对应资源的变化,然后在周期内的各个环节做相应的协调处理。

Custom resources can appear and disappear in a running cluster through dynamic registration, and cluster admins can update custom resources independently of the cluster itself. Once a custom resource is installed, users can create and access its objects using kubectl, just as they do for built-in resources like Pods.

How is it built

Operators build upon two central Kubernetes concepts: Resources and Controllers.
As an example, the built-in ReplicaSet resource lets users set a desired number number of Pods to run, and controllers inside Kubernetes ensure the desired state set in the ReplicaSet resource remains true by creating or removing running Pods.

An Operator builds upon the basic Kubernetes resource and controller concepts and adds a set of knowledge or configuration that allows the Operator to execute common application tasks.

For example, when scaling an etcd cluster manually, a user has to perform a number of steps:

  1. create a DNS name for the new etcd member,
  2. launch the new etcd instance,
  3. and then use the etcd administrative tools (etcdctl member add) to tell the existing cluster about this new member.

Instead with the etcd Operator a user can simply increase the etcd cluster size field by 1.

k8s operator

ref: