「Kubernetes Objects」- Deployment

  CREATED BY JENKINSBOT

问题描述

Deployment(部署),提供管理 Pod 的方法

该笔记将记录:在 Kubernetes 中,与 Deployment 相关的内容,以及常见问题的解决方案。

部署更新策略

Rolling update strategy(默认更新策略)

牺牲更新速度,以减少停机时间。

apiVersion: apps/v1  #Older versions of k8s use apps/v1beta1
kind: Deployment
metadata:
  name: hello-deploy
spec:
  minReadySeconds: 10
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 1
      maxSurge: 1
  template:
...

Recreation Strategy

更新速度快,但是存在停机时间。

apiVersion: apps/v1  #Older versions of k8s use apps/v1beta1
kind: Deployment
metadata:
  name: hello-deploy
spec:
  minReadySeconds: 10
  strategy:
    type: Recreate
...

Canary Strategy

在 strategy.type 中,没有“Canary”类型,需要进行特殊配置:
1)定义两个不同名称的 Pod 资源,但是具有重复的标签;
2)这样 Service 在通过 Label 选择后端 Pod 时,会将流量转向“Canary Pod”资源;
3)通过设置副本数量,来控制比例;

下面是个简单的示例:

# Pod 1

     name: frontend
     replicas: 3
     ...
     labels:
        app: guestbook
        tier: frontend
        track: stable
     ...
     image: gb-frontend:v3
     

# Pod 2

     name: frontend-canary
     replicas: 1
     ...
     labels:
        app: guestbook
        tier: frontend
        track: canary
     ...
     image: gb-frontend:v4
     
# Service

  selector:
     app: guestbook
     tier: frontend

通过 kubectl 管理 Deployment 常用命令

导出命名空间中的 Deployment 定义:

kubectl get -n "namespace" deployment "deployment-name" -o yaml --export

查看 Deployment 信息:

kubectl get deployment kubernet-dashboard -n kube-system

修改 Deployment 镜像:

kubectl set -n "namespace" image "deployment/deploy-foo"  "deploy-foo"="image-name"

打开编辑器修改 Deployment 信息:

kubectl edit deployment/myapp-deployment --image=172.20.34.206:5000/myapp:img:3.0
kubectl edit deployment kubernet-dashboard -n kube-system
kubectl patch deployment "deployment-name" -p "{......}"

相关链接

『中级篇』k8s重要的Deployment(64)
How can I edit a Deployment without modify the file manually?

参考文献

A deep dive into Kubernetes Deployment strategies
Managing Resources | Kubernetes