衆所周知,在Kubernetes中管理有狀態的服務是困難的。操做器(Operator)模式就是爲了處理這個問題而建立的,可是現有的框架要求用戶熟悉Go和Kubernetes控制器模式。mysql
大約一年前,咱們開啓了新的開源項目KUDO,即Kubernetes通用聲明性框架Operator(Kubernetes Universal Declarative Operator)。KUDO是Kubernetes Operator的開發工具和運行時,經過幾行YAML的聲明性方式,取代了數萬行的複雜代碼庫,使編寫操做器變得高效和簡單,容許Operator開發者和最終用戶使用他們已知的工具來管理有狀態服務。KUDO提供了基於DC/OS Commons SDK的一致性生命週期運維方式,而且基於大規模集羣的分佈式有狀態應用生產運行經驗,尤爲適用於企業生產運營。git
雖然KUDO的目標是解決在Kubernetes運行有狀態應用時所出現的問題,但它同時適用於你想在集羣上面運行的任何應用——從一個簡單的收尾工做直至服務的生產就緒安裝,例如Apache Kafka。github
自開發KUDO到如今已有一年時間,本文着重對KUDO的實際操做進行介紹,尤爲對Operator開發者以及服務部署和管理負責人有所助益。web
Part 1sql
基於Kubernetes的有狀態應用shell
Part 2json
StatefulSetsapi
Part 3微信
Operator網絡
爲了解決上述全部的問題,KUDO應運而生
Instance CRD
使用KUDO部署和管理服務
將KUDO部署到集羣
brew tap kudobuilder/tap
brew install kudo-cli
$ kubectl kudo --version
kubectl-kudo version 0.6.0
$ kubectl kudo init
$KUDO_HOME has been configured at /Users/nick/.kudo
$ kubectl api-resources --api-group kudo.dev
NAME SHORTNAMES APIGROUP NAMESPACED KIND
instances kudo.dev true Instance
operators kudo.dev true Operator
operatorversions kudo.dev true OperatorVersion
planexecutions kudo.dev true PlanExecution
$ kubectl get pods -n kudo-system
NAME READY STATUS RESTARTS AGE
kudo-controller-manager-0 1/1 Running 0 23m
$ kubectl get operators
No resources found.
$ kubectl get instances
No resources found.
$ kubectl kudo install zookeeper --instance zk
operator.kudo.dev/v1alpha1/zookeeper created
operatorversion.kudo.dev/v1alpha1/zookeeper-0.1.0 created
instance.kudo.dev/v1alpha1/zk created
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
zk-zookeeper-0 0/1 ContainerCreating 0 3s
zk-zookeeper-1 0/1 ContainerCreating 0 3s
zk-zookeeper-2 0/1 ContainerCreating 0 3s
# And then after a minute or so, depending>$ kubectl get pods
NAME READY STATUS RESTARTS AGE
zk-zookeeper-0 1/1 Running 0 54s
zk-zookeeper-1 1/1 Running 0 54s
zk-zookeeper-2 1/1 Running 0 54s
-
從KUDO Operator社區資源庫中安裝處理ZooKeeper Operator (若是尚未的話); -
默認狀況下,安裝觸發部署Plan,從而建立咱們的ZooKeeper Instance。 注意,能夠經過——skip-instance選項跳過這一步; -
KUDO會爲咱們建立一個3節點的ZooKeeper集羣。 默認狀況下是3節點集羣,所以這裏不須要指定任何其餘內容; -
instance zk option選項容許咱們命名Instance。 全部Instance必須有一個惟一名稱; 若是咱們不指定一個,那麼KUDO會自動爲咱們建立一個,但爲了方便,我在這個演示中選擇本身命名。
$ kubectl get instances
NAME AGE
zk 9m18s
$ kubectl kudo plan status --instance zk
Plan(s) for "zk" in namespace "default":
.
└── zk (Operator-Version: "zookeeper-0.1.0" Active-Plan: "zk-deploy-554891141")
├── Plan deploy (serial strategy) [COMPLETE]
│ ├── Phase zookeeper (parallel strategy) [COMPLETE]
│ │ └── Step everything (COMPLETE)
│ └── Phase validation (parallel strategy) [COMPLETE]
│ └── Step validation (COMPLETE)
└── Plan validation (serial strategy) [NOT ACTIVE]
└── Phase connection (parallel strategy) [NOT ACTIVE]
└── Step connection (parallel strategy) [NOT ACTIVE]
└── connection [NOT ACTIVE]
$ kubectl get events --field-selector involvedObject.name=zk
LAST SEEN TYPE REASON OBJECT MESSAGE
37m Normal CreatePlanExecution instance/zk Creating "deploy" planExecution execution
37m Normal PlanCreated instance/zk PlanExecution "zk-deploy-554891141" created
36m Normal PlanComplete instance/zk PlanExecution zk-deploy-554891141 completed
$ kubectl kudo install kafka --instance=kafka -p BROKER_MEM=1024m
operator.kudo.dev/v1alpha1/kafka created
operatorversion.kudo.dev/v1alpha1/kafka-0.2.0 created
instance.kudo.dev/v1alpha1/kafka created
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kafka-kafka-0 0/1 ContainerCreating 0 4s
# [..]
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kafka-kafka-0 1/1 Running 0 39s
kafka-kafka-1 1/1 ContainerCreating 0 18s
# [..]
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kafka-kafka-0 1/1 Running 0 96s
kafka-kafka-1 1/1 Running 0 75s
kafka-kafka-2 1/1 Running 0 58s
# [..]
$ kubectl apply -f - << EOF
heredoc> apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
labels:
app: prometheus-operator
release: prometheus-kubeaddons
name: kafka-cluster-monitor
spec:
endpoints:
- interval: 30s
port: metrics
selector:
matchLabels:
kudo.dev/servicemonitor: "true"
heredoc> EOF
servicemonitor.monitoring.coreos.com/kafka-cluster-monitor created
$ kubectl kudo update --instance kafka -p BROKER_MEM=4096m -p BROKER_COUNT=5
Instance kafka was updated.
$ kubectl describe instance kafka | grep -A5 Spec
Spec:
Operator Version:
Name: kafka-0.2.0
Parameters:
BROKER_COUNT: 5
BROKER_MEM: 4096m
$ kubectl get pods | grep kafka
kafka-kafka-0 1/1 Running 0 13m
kafka-kafka-1 1/1 Running 0 13m
kafka-kafka-2 1/1 Running 0 12m
kafka-kafka-3 0/1 Pending 0 64s
# [..]
kafka-kafka-0 1/1 Running 0 30m
kafka-kafka-1 1/1 Running 0 30m
kafka-kafka-2 0/1 ContainerCreating 0 10s
kafka-kafka-3 1/1 Running 0 18m
kafka-kafka-4 1/1 Running 0 55s
$ pwd
/Users/nick/src/kudo-mysql-operator/operator
$ tree
.
├── operator.yaml
├── params.yaml
└── templates
├── backup-pv.yaml
├── backup.yaml
├── init.yaml
├── mysql.yaml
├── param.yaml
└── restore.yam
Plan foo
├─ Phase bar
│ ├─ Step qux
│ └─ Step quux
└─ Phase baz
├─ Step quuz
├─ Step corge
└─ Step grault
plans:
deploy:
strategy: serial
phases:
- name: deploy
strategy: serial
steps:
- name: deploy
tasks:
- deploy
- name: init
tasks:
- init
- name: cleanup
tasks:
- init
delete: true
tasks:
deploy:
resources:
- mysql.yaml
init:
resources:
- init.yaml
BACKUP_FILE:
description: "Filename to save the backups to"
default: "backup.sql"
displayName: "BackupFile"
trigger: backup
PASSWORD:
default: "password"
apiVersion: batch/v1
kind: Job
metadata:
namespace: default
name: {{ .PlanName }}-job
spec:
template:
metadata:
name: {{ .PlanName }}-job
spec:
restartPolicy: OnFailure
containers:
- name: bb
image: mysql:5.7
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- "mysqldump -u root -h {{ .Name }}-mysql -p{{ .Params.PASSWORD }} kudo > /backups/{{ .Params.BACKUP_FILE }}"
volumeMounts:
- name: backup-pv
mountPath: /backups
volumes:
- name: backup-pv
persistentVolumeClaim:
claimName: {{ .Name }}-backup-pv
KUDO的社區及其將來發展
-
使用KUDO提供的編排功能擴展Helm charts和Cloud Native Application Bundles(CNAB) 詳見: https://github.com/kudobuilder/kudo/blob/master/keps/0013-external-specs.md -
動態CRD: 爲基礎設施和應用程序管理員提供一種擴展方式,或是添加現有KUDO Operator的操做功能,而無需安裝新版本 -
Operator擴展: 擴展機制將爲管理員提供一種不須要上一級的Operator幫助,便可添加或修改現有功能的簡單方法,不用承受維護的負擔 詳見: https://github.com/kudobuilder/kudo/blob/master/keps/0012-operator-extensions.md -
Operator相關性——這包括現有實例之間的相關性,能夠基於前一階段的輸出來構建綜合管道。 (所以在上面的Kafka和ZooKeeper示例中,前者將默認須要並做爲依賴項安裝後者) 詳見: https://github.com/kudobuilder/kudo/blob/master/keps/0017-pipe-tasks.md -
Operator工具包: 使KUDO Operator的開發更加容易,包括「skeleton」模板生成、linting等功能。 詳見: https://github.com/kudobuilder/kudo/blob/master/keps/0009-operator-toolkit.md
在明天的D2iQ北京雲原生會議中,咱們將爲您現場演示和講解KUDO,最後報名機會,精彩不容錯過!
D2iQ首期雲原生主題論壇(北京站)
時間:10月25日(星期五)
往期精彩文章
本文分享自微信公衆號 - D2iQ(d2iq_apac)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。