原文连接:https://blog.csdn.net/weixin_33918114/article/details/92211707
概览
该项目是 Operator Framework的组成部分, 是一个开源的工具用于管理 Kubernetes 原生的应用, 称为 Operators, 以一种更为有效、自动化、可伸缩的方式。更多信息参考 introduction blog post。
Operators 使在Kubernetes中管理复杂的有状态的应用更为简单。但是,目前编写 operator 还是比较困难,因为需要与底层的APIs打交道,编写模版和模块化资源缺乏也导致重复的工作。
该 Operator SDK 是一个框架,使用 controller-runtime 库来使得编写 operators 更为容易,提供了:
- 高阶APIs 和抽象,使编写运维逻辑更为直观。
- Tools和启动模块,使创建新的工程时更快。
- 覆盖常用的operator的扩展。
工作流
该 SDK 提供了一个工作流程,用于使用 Go、 Ansible 或 Helm来开发operators。
下面的工作流用于创建新的 Go operator:
- 创建新的 operator project,使用 SDK Command Line Interface(CLI)。
- 定义新的resource APIs,通过添加Custom Resource Definitions(CRD)。
- 定义 Controllers 观察和协调资源。
- 编写协调逻辑,使用 SDK 和 controller-runtime APIs。
- 使用 SDK CLI 构建和生成 operator deployment manifests。
下面的工作流用于创建新的Ansible operator:
- 创建新的 operator project,使用SDK Command Line Interface(CLI)。
- 编写协调逻辑,为自己的对象,使用ansible playbooks 和 roles。
- 使用 SDK CLI 构建和生成 operator deployment manifests。
- 可选添加额外的 CRD's,使用 SDK CLI,重复步骤2、3。
下面的工作流用于创建新的Helm operator:
- 创建新的 operator project,使用 SDK Command Line Interface(CLI)。
- 创建新的 (或添加已有的) Helm chart,用于 operator's 协调逻辑使用。
- 使用SDK CLI 构建和生成operator deployment manifests。
- 可选添加额外的CRD's,使用SDK CLI,重复步骤 2 和 3。
预先要求
- dep version v0.5.0+.
- git
- go version v1.10+.
- docker version 17.03+.
- kubectl version v1.11.0+.
- Access to a kubernetes v.1.11.0+ cluster.
快速开始
首先, 检出和安装 operator-sdk CLI,如下:
$ mkdir -p $GOPATH/src/github.com/operator-framework $ cd $GOPATH/src/github.com/operator-framework $ git clone https://github.com/operator-framework/operator-sdk $ cd operator-sdk $ git checkout master $ make dep $ make install
创建和部署一个 app-operator,使用SDK CLI来完成,如下:
# Create an app-operator project that defines the App CR. $ mkdir -p $GOPATH/src/github.com/example-inc/ # Create a new app-operator project $ cd $GOPATH/src/github.com/example-inc/ $ operator-sdk new app-operator $ cd app-operator
Add a new API for the custom resource AppService
$ operator-sdk add api --api-version=app.example.com/v1alpha1 --kind=AppService
Add a new controller that watches for AppService
$ operator-sdk add controller --api-version=app.example.com/v1alpha1 --kind=AppService
Build and push the app-operator image to a public registry such as quay.io
$ operator-sdk build quay.io/example/app-operator
$ docker push quay.io/example/app-operator
Update the operator manifest to use the built image name (if you are performing these steps on OSX, see note below)
$ sed -i ‘s|REPLACE_IMAGE|quay.io/example/app-operator|g’ deploy/operator.yaml
On OSX use:
$ sed -i “” ‘s|REPLACE_IMAGE|quay.io/example/app-operator|g’ deploy/operator.yaml
Setup Service Account
$ kubectl create -f deploy/service_account.yaml
Setup RBAC
$ kubectl create -f deploy/role.yaml
$ kubectl create -f deploy/role_binding.yaml
Setup the CRD
$ kubectl create -f deploy/crds/app_v1alpha1_appservice_crd.yaml
Deploy the app-operator
$ kubectl create -f deploy/operator.yaml
Create an AppService CR
The default controller will watch for AppService objects and create a pod for each CR
$ kubectl create -f deploy/crds/app_v1alpha1_appservice_cr.yaml
Verify that a pod is created
$ kubectl get pod -l app=example-appservice
NAME READY STATUS RESTARTS AGE
example-appservice-pod 1/1 Running 0 1m
Test the new Resource Type
$ kubectl describe appservice example-appservice
Name: example-appservice
Namespace: myproject
Labels: <none>
Annotations: <none>
API Version: app.example.com/v1alpha1
Kind: AppService
Metadata:
Cluster Name:
Creation Timestamp: 2018-12-17T21:18:43Z
Generation: 1
Resource Version: 248412
Self Link: /apis/app.example.com/v1alpha1/namespaces/myproject/appservices/example-appservice
UID: 554f301f-0241-11e9-b551-080027c7d133
Spec:
Size: 3
Cleanup
$ kubectl delete -f deploy/crds/app_v1alpha1_appservice_cr.yaml
$ kubectl delete -f deploy/operator.yaml
$ kubectl delete -f deploy/role.yaml
$ kubectl delete -f deploy/role_binding.yaml
$ kubectl delete -f deploy/service_account.yaml
$ kubectl delete -f deploy/crds/app_v1alpha1_appservice_crd.yaml
命令行界面-CLI
了解更多 SDK CLI, 查看 SDK CLI Reference, 或者运行 operator-sdk [command] -h
。
使用指南
了解更多使用 Go 语言编写 operator的方法,查看 user guide。
该 SDK 同时支持使用 Ansible 和 Helm开发 operator。查看 Ansible 和 Helm 的operator 用户指南。
例子
探索operator-sdk的例子,查看 operator-sdk-samples。
贡献
查看 CONTRIBUTING 了解提交布丁和贡献的方法和流程。
查看 proposal docs 提交需求和规划。
报告 bugs
查看 reporting bugs 报告Bugs。
许可
Operator SDK的许可为 Apache 2.0 license. 查看许可文件 LICENSE 了解细节。