Deployments

Deployments

一个 Deployment 为 PodReplicaSet 提供声明式的更新能力。

你负责描述 Deployment 中的目标状态 ,而 Deployment 控制器(Controller) 以受控速率更改实际状态, 使其变为期望状态。你可以定义 Deployment 以创建新的 ReplicaSet,或删除现有 Deployment, 并通过新的 Deployment 收养其资源。

下面是一个 Deployment 示例。其中创建了一个 ReplicaSet,负责启动三个 nginx Pod:

yaml 复制代码
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

查看deployment的状态:

powershell 复制代码
root@k8s-master:~# kubectl get deploy nginx-deployment -o wide
NAME               READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
nginx-deployment   2/3     3            2           71s   nginx        nginx:1.14.2   app=nginx

要查看 Deployment 上线状态:

powershell 复制代码
root@k8s-master:~# kubectl rollout status deploy nginx-deployment
deployment "nginx-deployment" successfully rolled out

更新 Deployment

  • 先来更新 nginx Pod 以使用 nginx:1.16.1 镜像,而不是 nginx:1.14.2 镜像。

    powershell 复制代码
    root@k8s-master:~# kubectl get deploy nginx-deployment -o wide
    NAME               READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
    nginx-deployment   3/3     3            3           10m   nginx        nginx:1.14.2   app=nginx
    
    #更新
    root@k8s-master:~# kubectl set image deploy nginx-deployment nginx=nginx:1.16.1
    deployment.apps/nginx-deployment image updated
  • 查看上线状态:

    powershell 复制代码
    root@k8s-master:~# kubectl rollout status deployment nginx-deployment
    deployment "nginx-deployment" successfully rolled out
    root@k8s-master:~# kubectl get deployment nginx-deployment -o wide
    NAME               READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
    nginx-deployment   3/3     3            3           13m   nginx        nginx:1.16.1   app=nginx
    #看到版本已经变更为1.16.1

回滚 Deployment

有时,你可能想要回滚 Deployment;例如,当 Deployment 不稳定时(例如进入反复崩溃状态)。 默认情况下,Deployment 的所有上线记录都保留在系统中,以便可以随时回滚 (你可以通过修改修订历史记录限制来更改这一约束)。

  • 假设你在更新 Deployment 时犯了一个拼写错误,将镜像名称命名设置为 nginx:1.161 而不是 nginx:1.16.1

    powershell 复制代码
    root@k8s-master:~# kubectl set image deployment/nginx-deployment nginx=nginx:1.161
    deployment.apps/nginx-deployment image updated
  • 查看状态

    powershell 复制代码
    root@k8s-master:~# kubectl rollout status deployment nginx-deployment
    Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...

检查 Deployment 上线历史

首先,检查 Deployment 修订历史:

powershell 复制代码
root@k8s-master:~# kubectl rollout history deployment/nginx-deployment
deployment.apps/nginx-deployment
REVISION  CHANGE-CAUSE
1         <none>
2         <none>
3         <none>

CHANGE-CAUSE 的内容是从 Deployment 的 kubernetes.io/change-cause 注解复制过来的。 复制动作发生在修订版本创建时。你可以通过以下方式设置 CHANGE-CAUSE 消息:

  • 使用 kubectl annotate deployment/nginx-deployment kubernetes.io/change-cause="image updated to 1.16.1" 为 Deployment 添加注解。
  • 手动编辑资源的清单。

要查看修订历史的详细信息,运行:

powershell 复制代码
kubectl rollout history deployment/nginx-deployment --revision=2
powershell 复制代码
root@k8s-master:~# kubectl rollout history deployment/nginx-deployment --revision=2
deployment.apps/nginx-deployment with revision #2
Pod Template:
  Labels:       app=nginx
        pod-template-hash=559d658b74
  Containers:
   nginx:
    Image:      nginx:1.16.1
    Port:       80/TCP
    Host Port:  0/TCP
    Environment:        <none>
    Mounts:     <none>
  Volumes:      <none>

回滚到之前的修订版本

按照下面给出的步骤将 Deployment 从当前版本回滚到以前的版本(即版本 2)。

假定现在你已决定撤消当前上线并回滚到以前的修订版本:

powershell 复制代码
root@k8s-master:~# kubectl rollout undo deployment/nginx-deployment
deployment.apps/nginx-deployment rolled back

检查回滚是否成功以及 Deployment 是否正在运行,运行:

powershell 复制代码
root@k8s-master:~# kubectl get deployment nginx-deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3/3     3            3           13m

获取 Deployment 描述信息:

powershell 复制代码
root@k8s-master:~# kubectl describe deployment nginx-deployment
Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Mon, 23 Oct 2023 16:17:23 +0800
Labels:                 app=nginx
Annotations:            deployment.kubernetes.io/revision: 4
Selector:               app=nginx
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx:1.16.1
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-deployment-559d658b74 (3/3 replicas created)
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  17m    deployment-controller  Scaled up replica set nginx-deployment-66b6c48dd5 to 3
  Normal  ScalingReplicaSet  14m    deployment-controller  Scaled up replica set nginx-deployment-559d658b74 to 1
  Normal  ScalingReplicaSet  14m    deployment-controller  Scaled down replica set nginx-deployment-66b6c48dd5 to 2
  Normal  ScalingReplicaSet  14m    deployment-controller  Scaled up replica set nginx-deployment-559d658b74 to 2
  Normal  ScalingReplicaSet  14m    deployment-controller  Scaled down replica set nginx-deployment-66b6c48dd5 to 1
  Normal  ScalingReplicaSet  14m    deployment-controller  Scaled up replica set nginx-deployment-559d658b74 to 3
  Normal  ScalingReplicaSet  14m    deployment-controller  Scaled down replica set nginx-deployment-66b6c48dd5 to 0
  Normal  ScalingReplicaSet  12m    deployment-controller  Scaled up replica set nginx-deployment-66bc5d6c8 to 1
  Normal  ScalingReplicaSet  7m46s  deployment-controller  Scaled down replica set nginx-deployment-66bc5d6c8 to 0

版本已经回退到1.16.1

缩放 Deployment

你可以使用如下指令缩放 Deployment:

powershell 复制代码
root@k8s-master:~# kubectl scale deployment nginx-deployment --replicas=10
deployment.apps/nginx-deployment scaled

查看

powershell 复制代码
root@k8s-master:~# kubectl get deploy nginx-deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   10/10   10           10          38m

暂停、恢复 Deployment 的上线过程

在你更新一个 Deployment 的时候,或者计划更新它的时候, 你可以在触发一个或多个更新之前暂停 Deployment 的上线过程。 当你准备应用这些变更时,你可以重新恢复 Deployment 上线过程。 这样做使得你能够在暂停和恢复执行之间应用多个修补程序,而不会触发不必要的上线操作。

powershell 复制代码
#获取deploy信息
root@k8s-master:~# kubectl get deploy nginx-deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   10/10   10           10          38m

使用如下指令暂停上线:

powershell 复制代码
root@k8s-master:~# kubectl rollout pause deployment/nginx-deployment
deployment.apps/nginx-deployment paused

root@k8s-master:~# kubectl get deployment nginx-deployment -o wide
NAME               READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES         SELECTOR
nginx-deployment   10/10   10           10          78m   nginx        nginx:1.16.1   app=nginx

查看变更历史:

powershell 复制代码
root@k8s-master:~# kubectl rollout history deployment nginx-deployment
deployment.apps/nginx-deployment
REVISION  CHANGE-CAUSE
1         <none>
3         <none>
4         <none>

更新镜像:

powershell 复制代码
kubectl set image deployment/nginx-deployment nginx=nginx:1.16.2

查看历史(新上线没有被触发):

powershell 复制代码
root@k8s-master:~# kubectl rollout history deployment nginx-deployment
deployment.apps/nginx-deployment
REVISION  CHANGE-CAUSE
1         <none>
3         <none>
4         <none>

后续操作,如设置资源:

powershell 复制代码
kubectl set resources deployment/nginx-deployment -c=nginx --limits=cpu=200m,memory=512Mi

最终,恢复 Deployment 上线并观察新的 ReplicaSet 的创建过程,其中包含了所应用的所有更新:

powershell 复制代码
root@k8s-master:~# kubectl rollout resume deployment/nginx-deployment
deployment.apps/nginx-deployment resumed
powershell 复制代码
root@k8s-master:~# kubectl get deployments nginx-deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   8/10    5            8           91m
powershell 复制代码
root@k8s-master:~# kubectl rollout history deployment nginx-deployment
deployment.apps/nginx-deployment
REVISION  CHANGE-CAUSE
1         <none>
3         <none>
4         <none>
5         <none>
#发布历史又出现了新的一条
相关推荐
dessler12 分钟前
Docker-如何启动docker
运维·docker·云原生·容器·eureka
zhy2956312 分钟前
【DOCKER】基于DOCKER的服务之DUFS
运维·docker·容器·dufs
秋名山小桃子26 分钟前
Kunlun 2280服务器(ARM)Raid卡磁盘盘符漂移问题解决
运维·服务器
与君共勉1213827 分钟前
Nginx 负载均衡的实现
运维·服务器·nginx·负载均衡
MZWeiei38 分钟前
Zookeeper基本命令解析
大数据·linux·运维·服务器·zookeeper
Arenaschi1 小时前
在Tomcat中部署应用时,如何通过域名访问而不加端口号
运维·服务器
waicsdn_haha1 小时前
Java/JDK下载、安装及环境配置超详细教程【Windows10、macOS和Linux图文详解】
java·运维·服务器·开发语言·windows·后端·jdk
蜜獾云1 小时前
docker 安装雷池WAF防火墙 守护Web服务器
linux·运维·服务器·网络·网络安全·docker·容器
小屁不止是运维2 小时前
麒麟操作系统服务架构保姆级教程(五)NGINX中间件详解
linux·运维·服务器·nginx·中间件·架构
Hacker_Oldv2 小时前
WPS 认证机制
运维·服务器·wps