Kubernetes集群中基于 CRD 实现分批发布

分批发布是一种通用的发布方式,但是在Kubernetes集群中,要实现分批发布,需要控制各种状态,维护service流量,以及各种label配置,十分麻烦。阿里云容器服务提供一种基于 CRD 的分批发布方式,大大方便发布流程。

使用kubectl进行分批发布

新集群默认安装了alicloud-application-controller,老集群请先手动安装,安装方式,
kubectl create -f alicloud-application-controller.yml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: alicloud-application-controller
  labels:
    owner: aliyun
    app: alicloud-application-controller
  namespace: kube-system
spec:
  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-c8d5da8
          imagePullPolicy: IfNotPresent
      serviceAccount: admin

我们这里以下面这个StatefulSet为例子演示分批发布的流程,

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/old-nginx
        ports:
        - containerPort: 80
          name: web

这是一个含有三个实例的nginx 实例,为了暴露此容器,我们使用Service来暴露,Service的Yaml如下

apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    name: web
  selector:
    app: nginx
  type: LoadBalancer

这里通过SLB 暴露nginx 服务。
为了保证用户的Yaml安全性,分批发布需要使用Secret来存储最终的yaml,这里需要将yaml执行一下base64操作,再存储。
这个是StatefulSet的Secret Yaml

apiVersion: v1
kind: Secret
metadata:
  name: mysts
type: Opaque
data:
  yaml: YXBpVmVyc2lvbjogYXBwcy92MQpraW5kOiBTdGF0ZWZ1bFNldAptZXRhZGF0YToKICBuYW1lOiB3ZWIKc3BlYzoKICBzZWxlY3RvcjoKICAgIG1hdGNoTGFiZWxzOgogICAgICBhcHA6IG5naW54ICMgaGFzIHRvIG1hdGNoIC5zcGVjLnRlbXBsYXRlLm1ldGFkYXRhLmxhYmVscwogIHNlcnZpY2VOYW1lOiAibmdpbngiCiAgcmVwbGljYXM6IDMgIyBieSBkZWZhdWx0IGlzIDEKICB0ZW1wbGF0ZToKICAgIG1ldGFkYXRhOgogICAgICBsYWJlbHM6CiAgICAgICAgYXBwOiBuZ2lueCAjIGhhcyB0byBtYXRjaCAuc3BlYy5zZWxlY3Rvci5tYXRjaExhYmVscwogICAgc3BlYzoKICAgICAgdGVybWluYXRpb25HcmFjZVBlcmlvZFNlY29uZHM6IDEwCiAgICAgIGNvbnRhaW5lcnM6CiAgICAgIC0gbmFtZTogbmdpbngKICAgICAgICBpbWFnZTogcmVnaXN0cnkuY24taGFuZ3pob3UuYWxpeXVuY3MuY29tL3hpYW5sdS9vbGQtbmdpbngKICAgICAgICBwb3J0czoKICAgICAgICAtIGNvbnRhaW5lclBvcnQ6IDgwCiAgICAgICAgICBuYW1lOiB3ZWI=

下面的为Service的Secret Yaml

apiVersion: v1
kind: Secret
metadata:
  name: mysvc
type: Opaque
data:
  yaml: YXBpVmVyc2lvbjogdjEKa2luZDogU2VydmljZQptZXRhZGF0YToKICBuYW1lOiBuZ2lueAogIGxhYmVsczoKICAgIGFwcDogbmdpbngKc3BlYzoKICBwb3J0czoKICAtIHBvcnQ6IDgwCiAgICBuYW1lOiB3ZWIKICBzZWxlY3RvcjoKICAgIGFwcDogbmdpbngKICB0eXBlOiBMb2FkQmFsYW5jZXI=

分别将上面的两个Secret 创建出来,kubectl create -f xxxxx

1

可以看到两个Secret都已经创建完毕,下面就可以来创建分批发布的 CRD。分批发布的 CRD 格式如下,

apiVersion: alicloud.com/v1beta1
kind: BatchRelease
metadata:
  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分批发布
2
可以看到,描述资源都已经创建出来了。

[root@iZbp11x2k7by5gfy6pkrl1Z ~]# kubectl get sts
NAME      DESIRED   CURRENT   AGE
web       3         3         6m
[root@iZbp11x2k7by5gfy6pkrl1Z ~]# kubectl get pods -o=wide
NAME      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 svc
NAME         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.238
old

可以看到,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: Secret
metadata:
  name: my-new-sts
type: Opaque
data:
  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-00163e086528
spec:
  serviceSecretName: mysvc
  statefulSetSecretName: my-new-sts
[root@iZbp11x2k7by5gfy6pkrl1Z ~]# kubectl describe BatchRelease example-batch-release
Name:         example-batch-release
Namespace:    default
Labels:       <none>
Annotations:  aliyun.batchnum=2
API 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=wide
NAME      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 svc
NAME         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.187
new
[root@iZbp11x2k7by5gfy6pkrl1Z ~]#

我们可以通过event 看到 CRD 的所有事件。同时可以看到,nginx的3个pod,已经有一个 pod发生了改变,变成了最新镜像,同时 curl对应的service,会发现流量已经指向了新的pod。这样用户就可以快速试错了。

下面我们来演示如何快速回滚,如果流量打到新的 pod后发现有问题,需要回滚,那么我们需要编辑CRD,

3

将Status 的action 置为rollback 就可以快速回滚。

[root@iZbp11x2k7by5gfy6pkrl1Z ~]# kubectl edit BatchRelease example-batch-release
batchrelease.alicloud.com "example-batch-release" edited
[root@iZbp11x2k7by5gfy6pkrl1Z ~]# kubectl get pods -o=wide
NAME      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 svc
NAME         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.187
old

可以看到原来web-2所在的pod已经被回滚到了老镜像,现在curl service 也都是老的服务了。

发布第二批

4

发布第二批需要在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-00163e086528
spec:
  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都被更新了。

这个时候就可以确认发布了

6

置为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-00163e086528
spec:
  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

可以看到,所有的确认版本历史都会被记录到secret里面,方便到时候跨版本回滚。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值