在Kubernetes中有几种不同的方式发布应用,所以为了让应用在升级期间依然平稳提供服务,选择一个正确的发布策略就非常重要了。
选择正确的部署策略是要依赖于我们的业务需求的,下面我们列出了一些可能会使用到的策略:
A/B测实际上是一种基于数据统计做出业务决策的技术。在 Kubernetes 中并不原生支持,需要额外的一些高级组件来完成改设置(比如 Istio、Linkerd、Traefik、或者自定义 Nginx/Haproxy 等)。你可以在Kubernetes集群上来对上面的这些策略进行测试,下面的仓库中有需要使用到的资源清单:https://github.com/ContainerSolutions/k8s-deployment-strategies
接下来我们来介绍下每种策略,看看在什么场景下面适合哪种策略。
策略定义为Recreate的Deployment,会终止所有正在运行的实例,然后用较新的版本来重新创建它们。
spec: replicas: 3 strategy: type: Recreate

重新创建策略是一个虚拟部署,包括关闭版本 A,然后在关闭版本 A 后部署版本 B. 此技术意味着服务的停机时间取决于应用程序的关闭和启动持续时间。
我们这里创建两个相关的资源清单文件,app-v1.yaml:
`apiVersion: v1 kind: Service metadata: name: my-app labels: app: my-app spec: type: NodePort ports:
apiVersion: apps/v1 kind: Deployment metadata: name: my-app labels: app: my-app spec: replicas: 3 selector: matchLabels: app: my-app strategy: type: Recreate selector: matchLabels: app: my-app template: metadata: labels: app: my-app version: v1.0.0 annotations: prometheus.io/scrape: "true" prometheus.io/port: "9101" spec: containers: - name: my-app image: containersol/k8s-deployment-strategies ports: - name: http containerPort: 8080 - name: probe containerPort: 8086 env: - name: VERSION value: v1.0.0 livenessProbe: httpGet: path: /live port: probe initialDelaySeconds: 5 periodSeconds: 5 readinessProbe: httpGet: path: /ready port: probe periodSeconds: 5`
app-v2.yaml 文件内容如下:
apiVersion: apps/v1 kind: Deployment metadata: name: my-app labels: app: my-app spec: replicas: 3 strategy: type: Recreate selector: matchLabels: app: my-app template: metadata: labels: app: my-app version: v2.0.0 annotations: prometheus.io/scrape: "true" prometheus.io/port: "9101" spec: containers: - name: my-app image: containersol/k8s-deployment-strategies ports: - name: http containerPort: 8080 - name: probe containerPort: 8086 env: - name: VERSION value: v2.0.0 livenessProbe: httpGet: path: /live port: probe initialDelaySeconds: 5 periodSeconds: 5 readinessProbe: httpGet: path: /ready port: probe periodSeconds: 5
上面两个资源清单文件中的 Deployment 定义几乎是一直的,唯一不同的是定义的环境变量VERSION值不同,接下来按照下面的步骤来验证Recreate策略: