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>
#发布历史又出现了新的一条
相关推荐
传而习乎38 分钟前
Linux:CentOS 7 解压 7zip 压缩的文件
linux·运维·centos
soulteary39 分钟前
突破内存限制:Mac Mini M2 服务器化实践指南
运维·服务器·redis·macos·arm·pika
Python私教1 小时前
ubuntu搭建k8s环境详细教程
linux·ubuntu·kubernetes
运维&陈同学2 小时前
【zookeeper01】消息队列与微服务之zookeeper工作原理
运维·分布式·微服务·zookeeper·云原生·架构·消息队列
是阿建吖!2 小时前
【Linux】进程状态
linux·运维
明明跟你说过2 小时前
Linux中的【tcpdump】:深入介绍与实战使用
linux·运维·测试工具·tcpdump
O&REO3 小时前
单机部署kubernetes环境下Overleaf-基于MicroK8s的Overleaf应用部署指南
云原生·容器·kubernetes
politeboy3 小时前
k8s启动springboot容器的时候,显示找不到application.yml文件
java·spring boot·kubernetes
运维小文4 小时前
K8S资源限制之LimitRange
云原生·容器·kubernetes·k8s资源限制
登云时刻4 小时前
Kubernetes集群外连接redis集群和使用redis-shake工具迁移数据(二)
redis·容器·kubernetes