Kubernetes CRD开发工具Operator-SDK简介
原文連接:https://blog.csdn.net/weixin_33918114/article/details/92211707
概覽
- 原文來自:https://github.com/operator-framework/operator-sdk
該項目是 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:
下面的工作流用于創建新的Ansible operator:
下面的工作流用于創建新的Helm operator:
預先要求
- 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-operatorAdd 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 了解細節。
總結
以上是生活随笔為你收集整理的Kubernetes CRD开发工具Operator-SDK简介的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用kuberbuilder创建工程示例
- 下一篇: 深入解析 Kubebuilder:让编写