您好,登錄后才能下訂單哦!
這篇文章給大家介紹Kubernetes集群中怎么利用CRD 實現分批發布,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
新集群默認安裝了alicloud-application-controller,老集群請先手動安裝,安裝方式,kubectl create -f alicloud-application-controller.yml
apiVersion: extensions/v1beta1 kind: Deploymentmetadata: name: alicloud-application-controller labels: owner: aliyun app: alicloud-application-controller namespace: kube-systemspec: replicas: 1 selector: matchLabels: owner: aliyun app: alicloud-application-controller template: metadata: labels: owner: aliyun app: alicloud-application-controller annotations: scheduler.alpha.kubernetes.io/critical-pod: '' spec: tolerations: - effect: NoSchedule operator: Exists key: node-role.kubernetes.io/master - effect: NoSchedule operator: Exists key: node.cloudprovider.kubernetes.io/uninitialized containers: - name: alicloud-application-controller image: registry.cn-hangzhou.aliyuncs.com/acs/aliyun-app-lifecycle-manager:0.1-93095ba imagePullPolicy: IfNotPresent serviceAccount: admin
我們這里以下面這個StatefulSet
為例子演示分批發布的流程,
apiVersion: apps/v1 kind: StatefulSetmetadata: name: webspec: selector: matchLabels: app: nginx # has to match .spec.template.metadata.labels serviceName: "nginx" replicas: 3 # by default is 1 template: metadata: labels: app: nginx # has to match .spec.selector.matchLabels spec: terminationGracePeriodSeconds: 10 containers: - name: nginx image: registry.cn-hangzhou.aliyuncs.com/xianlu/old-nginx ports: - containerPort: 80 name: web
這是一個含有三個實例的nginx 實例,為了暴露此容器,我們使用Service來暴露,Service的Yaml如下
apiVersion: v1 kind: Servicemetadata: name: nginx labels: app: nginxspec: ports: - port: 80 name: web selector: app: nginx type: LoadBalancer
這里通過SLB 暴露nginx 服務。
為了保證用戶的Yaml安全性,分批發布需要使用Secret來存儲最終的yaml,這里需要將yaml執行一下base64操作,再存儲。
這個是StatefulSet
的Secret Yaml
apiVersion: v1 kind: Secretmetadata: name: mysts type: Opaquedata: yaml: YXBpVmVyc2lvbjogYXBwcy92MQpraW5kOiBTdGF0ZWZ1bFNldAptZXRhZGF0YToKICBuYW1lOiB3ZWIKc3BlYzoKICBzZWxlY3RvcjoKICAgIG1hdGNoTGFiZWxzOgogICAgICBhcHA6IG5naW54ICMgaGFzIHRvIG1hdGNoIC5zcGVjLnRlbXBsYXRlLm1ldGFkYXRhLmxhYmVscwogIHNlcnZpY2VOYW1lOiAibmdpbngiCiAgcmVwbGljYXM6IDMgIyBieSBkZWZhdWx0IGlzIDEKICB0ZW1wbGF0ZToKICAgIG1ldGFkYXRhOgogICAgICBsYWJlbHM6CiAgICAgICAgYXBwOiBuZ2lueCAjIGhhcyB0byBtYXRjaCAuc3BlYy5zZWxlY3Rvci5tYXRjaExhYmVscwogICAgc3BlYzoKICAgICAgdGVybWluYXRpb25HcmFjZVBlcmlvZFNlY29uZHM6IDEwCiAgICAgIGNvbnRhaW5lcnM6CiAgICAgIC0gbmFtZTogbmdpbngKICAgICAgICBpbWFnZTogcmVnaXN0cnkuY24taGFuZ3pob3UuYWxpeXVuY3MuY29tL3hpYW5sdS9vbGQtbmdpbngKICAgICAgICBwb3J0czoKICAgICAgICAtIGNvbnRhaW5lclBvcnQ6IDgwCiAgICAgICAgICBuYW1lOiB3ZWI=
下面的為Service的Secret Yaml
apiVersion: v1 kind: Secretmetadata: name: mysvc type: Opaquedata: yaml: YXBpVmVyc2lvbjogdjEKa2luZDogU2VydmljZQptZXRhZGF0YToKICBuYW1lOiBuZ2lueAogIGxhYmVsczoKICAgIGFwcDogbmdpbngKc3BlYzoKICBwb3J0czoKICAtIHBvcnQ6IDgwCiAgICBuYW1lOiB3ZWIKICBzZWxlY3RvcjoKICAgIGFwcDogbmdpbngKICB0eXBlOiBMb2FkQmFsYW5jZXI=
分別將上面的兩個Secret 創建出來,kubectl create -f xxxxx
cdn.com/a20640c6e505c48a39cec1b05f30d748796d2221.png">
可以看到兩個Secret都已經創建完畢,下面就可以來創建分批發布的 CRD。分批發布的 CRD 格式如下,
apiVersion: alicloud.com/v1beta1 kind: BatchReleasemetadata: name: example-batch-release annotations: aliyun.batchnum: "2"spec: statefulSetSecretName: mysts serviceSecretName: mysvc
?aliyun.batchnum
: 代表分幾批發布,目前默認是2批statefulSetSecretName
: 表示存儲StatefulSet的Secret 名稱serviceSecretName
: 表示存儲Service的Secret 名稱
使用kubectl create -f xxx
創建此CRD分批發布
可以看到,描述資源都已經創建出來了。
[root@iZbp11x2k7by5gfy6pkrl1Z ~]# kubectl get stsNAME DESIRED CURRENT AGE web 3 3 6m [root@iZbp11x2k7by5gfy6pkrl1Z ~]# kubectl get pods -o=wideNAME READY STATUS RESTARTS AGE IP NODE web-0 1/1 Running 0 7m 172.16.2.2 cn-hangzhou.i-bp199b7a244chaux4ozh web-1 1/1 Running 0 6m 172.16.2.131 cn-hangzhou.i-bp199b7a244chaux4ozi web-2 1/1 Running 0 6m 172.16.1.136 cn-hangzhou.i-bp199b7a244chaux4ozg [root@iZbp11x2k7by5gfy6pkrl1Z ~]# kubectl get svcNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 172.19.0.1 <none> 443/TCP 4h nginx LoadBalancer 172.19.12.163 120.55.148.238 80:32065/TCP 7m [root@iZbp11x2k7by5gfy6pkrl1Z ~]# curl 120.55.148.238old
可以看到,nginx 的StatefulSet和對應的Service 都已經創建出來,并且curl 是可以直接訪問。
下面我們開始分批發布一個新版本的StatefulSet,這里我們新的StatefulSet Yaml 模板如下
apiVersion: apps/v1 kind: StatefulSet metadata: name: web spec: selector: matchLabels: app: nginx # has to match .spec.template.metadata.labels serviceName: "nginx" replicas: 3 # by default is 1 template: metadata: labels: app: nginx # has to match .spec.selector.matchLabels spec: terminationGracePeriodSeconds: 10 containers: - name: nginx image: registry.cn-hangzhou.aliyuncs.com/xianlu/new-nginx ports: - containerPort: 80 name: web
可以看到,新老StatefulSet的區別在于,更換了新版本的鏡像。
同樣,我們需要創建一個Secret 來存儲這個新的StatefulSet
apiVersion: v1 kind: Secretmetadata: name: my-new-sts type: Opaquedata: yaml: YXBpVmVyc2lvbjogYXBwcy92MQpraW5kOiBTdGF0ZWZ1bFNldAptZXRhZGF0YToKICBuYW1lOiB3ZWIKc3BlYzoKICBzZWxlY3RvcjoKICAgIG1hdGNoTGFiZWxzOgogICAgICBhcHA6IG5naW54ICMgaGFzIHRvIG1hdGNoIC5zcGVjLnRlbXBsYXRlLm1ldGFkYXRhLmxhYmVscwogIHNlcnZpY2VOYW1lOiAibmdpbngiCiAgcmVwbGljYXM6IDMgIyBieSBkZWZhdWx0IGlzIDEKICB0ZW1wbGF0ZToKICAgIG1ldGFkYXRhOgogICAgICBsYWJlbHM6CiAgICAgICAgYXBwOiBuZ2lueCAjIGhhcyB0byBtYXRjaCAuc3BlYy5zZWxlY3Rvci5tYXRjaExhYmVscwogICAgc3BlYzoKICAgICAgdGVybWluYXRpb25HcmFjZVBlcmlvZFNlY29uZHM6IDEwCiAgICAgIGNvbnRhaW5lcnM6CiAgICAgIC0gbmFtZTogbmdpbngKICAgICAgICBpbWFnZTogcmVnaXN0cnkuY24taGFuZ3pob3UuYWxpeXVuY3MuY29tL3hpYW5sdS9uZXctbmdpbngKICAgICAgICBwb3J0czoKICAgICAgICAtIGNvbnRhaW5lclBvcnQ6IDgwCiAgICAgICAgICBuYW1lOiB3ZWI=
然后我們更新剛才創建的 CRD,將statefulSetSecretName改成新的StatefulSet名稱。
修改后的CRD Yaml 如下:
[root@iZbp11x2k7by5gfy6pkrl1Z ~]# kubectl get BatchRelease example-batch-release -o=yaml apiVersion: alicloud.com/v1beta1 kind: BatchRelease metadata: annotations: aliyun.batchnum: "2" clusterName: "" creationTimestamp: 2018-07-31T08:17:17Z generation: 1 name: example-batch-release namespace: default resourceVersion: "43484" selfLink: /apis/alicloud.com/v1beta1/namespaces/default/batchreleases/example-batch-release uid: 2386ddfd-949a-11e8-a3c2-00163e086528spec: serviceSecretName: mysvc statefulSetSecretName: my-new-sts
[root@iZbp11x2k7by5gfy6pkrl1Z ~]# kubectl describe BatchRelease example-batch-releaseName: example-batch-release Namespace: default Labels: <none> Annotations: aliyun.batchnum=2API Version: alicloud.com/v1beta1 Kind: BatchRelease Metadata: Cluster Name: Creation Timestamp: 2018-07-31T08:17:17Z Generation: 1 Resource Version: 43484 Self Link: /apis/alicloud.com/v1beta1/namespaces/default/batchreleases/example-batch-release UID: 2386ddfd-949a-11e8-a3c2-00163e086528 Spec: Service Secret Name: mysvc Stateful Set Secret Name: my-new-sts Status: Control: Release: Batch _ Order: 1 Progress: finished Status: WaitingForConfirm Resources: Service: Name: nginx Namespace: default Status: Updating Stateful Set: Name: web Namespace: default Status: Updating Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal CreateSubResource 2m aliyun-controller Create StatefulSet resource sucessfully Normal CreateSubResource 2m aliyun-controller Create service resource sucessfully Normal Synced 2m aliyun-controller Batch CRD synced successfully Normal UpdateResource 1m aliyun-controller Begin to update StatefulSet web with partition 2 Normal UpdateResource 1m aliyun-controller Waiting StatefulSet default:web ready, readyReplicas 2 replicas 3 times 1 Normal UpdateResource 1m aliyun-controller Waiting StatefulSet default:web ready, readyReplicas 2 replicas 3 times 2 Normal UpdateResource 1m aliyun-controller Begin to update service with new selector map[app:nginx aliyun.version:2] Normal UpdateResource 1m aliyun-controller Update StatefulSet and Service sucessfully, waiting confirm [root@iZbp11x2k7by5gfy6pkrl1Z ~]# kubectl get pods -o=wideNAME READY STATUS RESTARTS AGE IP NODE web-0 1/1 Running 0 2m 172.16.2.132 cn-hangzhou.i-bp199b7a244chaux4ozi web-1 1/1 Running 0 2m 172.16.2.3 cn-hangzhou.i-bp199b7a244chaux4ozh web-2 1/1 Running 0 1m 172.16.1.138 cn-hangzhou.i-bp199b7a244chaux4ozg [root@iZbp11x2k7by5gfy6pkrl1Z ~]# kubectl get svcNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 172.19.0.1 <none> 443/TCP 5h nginx LoadBalancer 172.19.2.184 101.37.107.187 80:32173/TCP 2m[root@iZbp11x2k7by5gfy6pkrl1Z ~]# curl 101.37.107.187new [root@iZbp11x2k7by5gfy6pkrl1Z ~]#
我們可以通過event 看到 CRD 的所有事件。同時可以看到,nginx的3個pod,已經有一個 pod發生了改變,變成了最新鏡像,同時 curl對應的service,會發現流量已經指向了新的pod。這樣用戶就可以快速試錯了。
下面我們來演示如何快速回滾,如果流量打到新的 pod后發現有問題,需要回滾,那么我們需要編輯CRD,
將Status 的action 置為rollback 就可以快速回滾。
[root@iZbp11x2k7by5gfy6pkrl1Z ~]# kubectl edit BatchRelease example-batch-releasebatchrelease.alicloud.com "example-batch-release" edited [root@iZbp11x2k7by5gfy6pkrl1Z ~]# kubectl get pods -o=wideNAME READY STATUS RESTARTS AGE IP NODE web-0 1/1 Running 0 7m 172.16.2.132 cn-hangzhou.i-bp199b7a244chaux4ozi web-1 1/1 Running 0 7m 172.16.2.3 cn-hangzhou.i-bp199b7a244chaux4ozh web-2 1/1 Running 0 3s 172.16.1.139 cn-hangzhou.i-bp199b7a244chaux4ozg [root@iZbp11x2k7by5gfy6pkrl1Z ~]# kubectl get svcNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 172.19.0.1 <none> 443/TCP 5h nginx LoadBalancer 172.19.2.184 101.37.107.187 80:32173/TCP 7m [root@iZbp11x2k7by5gfy6pkrl1Z ~]# curl 101.37.107.187old
可以看到原來web-2所在的pod已經被回滾到了老鏡像,現在curl service 也都是老的服務了。
發布第二批需要在action 置為continue 就可以
[root@iZbp11x2k7by5gfy6pkrl1Z ~]# kubectl get pods -o=wide NAME READY STATUS RESTARTS AGE IP NODE web-0 1/1 Running 0 3s 172.16.2.138 cn-hangzhou.i-bp199b7a244chaux4ozi web-1 1/1 Running 0 13s 172.16.2.12 cn-hangzhou.i-bp199b7a244chaux4ozh web-2 1/1 Running 0 2m 172.16.1.153 cn-hangzhou.i-bp199b7a244chaux4ozg [root@iZbp11x2k7by5gfy6pkrl1Z ~]# kubectl get BatchRelease example-batch-release -o=yaml apiVersion: alicloud.com/v1beta1 kind: BatchRelease metadata: annotations: aliyun.batchnum: "2" clusterName: "" creationTimestamp: 2018-07-31T09:44:58Z generation: 1 name: example-batch-release namespace: default resourceVersion: "58066" selfLink: /apis/alicloud.com/v1beta1/namespaces/default/batchreleases/example-batch-release uid: 6321dcaf-94a6-11e8-a3c2-00163e086528spec: serviceSecretName: mysvc statefulSetSecretName: my-new-sts status: control: {} release: batch_order: "2" progress: finished status: WaitingForConfirm resources: Service: name: nginx namespace: default status: Updating StatefulSet: name: web namespace: default status: Updating
可以看到StatefulSet的三個 pod都被更新了。
這個時候就可以確認發布了
置為confirm 就確定了這次發布。
[root@iZbp11x2k7by5gfy6pkrl1Z ~]# kubectl get BatchRelease example-batch-release -o=yaml apiVersion: alicloud.com/v1beta1 kind: BatchRelease metadata: annotations: aliyun.batchnum: "2" clusterName: "" creationTimestamp: 2018-07-31T09:44:58Z generation: 1 name: example-batch-release namespace: default resourceVersion: "58542" selfLink: /apis/alicloud.com/v1beta1/namespaces/default/batchreleases/example-batch-release uid: 6321dcaf-94a6-11e8-a3c2-00163e086528spec: serviceSecretName: mysvc statefulSetSecretName: my-new-sts status: control: {} release: progress: finished status: Success resources: Service: name: nginx namespace: default status: Success StatefulSet: name: web namespace: default status: Success [root@iZbp11x2k7by5gfy6pkrl1Z ~]# kubectl get secret -l owner=aliyun -n=kube-system NAME TYPE DATA AGE example-batch-release.v1 Opaque 4 18m example-batch-release.v2 Opaque 4 56s
關于Kubernetes集群中怎么利用CRD 實現分批發布就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。