首页 > 其他分享 >k8s pod版本升级与回滚

k8s pod版本升级与回滚

时间:2023-06-19 15:44:33浏览次数:39  
标签:kubectl 回滚 set Deployment demoapp 版本升级 deployment pod demoappv10

创建应用

depoly-demoapp-v10.yaml

apiVersion: v1
kind: Namespace
metadata:
    name: demoapp
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: demoappv10
  name: demoappv10
  namespace: demoapp
spec:
  ports:
  - name: http-8080
    port: 8080
    protocol: TCP
    targetPort: 8080
  selector:
    app: demoapp
    version: v1.0
  type: ClusterIP

---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: demoappv10
    version: v1.0
  name: demoappv10
  namespace: demoapp
spec:
  replicas: 2
  selector:
    matchLabels:
      app: demoapp
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: demoapp
        version: v1.0
    spec:
      containers:
      - image: ikubernetes/demoapp:v1.0
        name: demoapp
        env:
        - name: PORT
          value: "8080"
        resources: {}

创建deployment资源

# kubectl apply -f depoly-demoapp-v10.yaml
namespace/demoapp created
deployment.apps/demoappv10 created

访问服务

# curl `kubectl get svc/demoappv10 -n demoapp -o jsonpath="{.spec.clusterIP}"`:8080
iKubernetes demoapp v1.0 !! ClientIP: 172.20.151.128, ServerName: demoappv10-69f6cf9477-v9m6g, ServerIP: 172.20.154.216!

查看deployment资源

# kubectl get deploy -n demoapp
NAME         READY   UP-TO-DATE   AVAILABLE   AGE
demoappv10   2/2     2            2           104s

查看 Deployment 滚动更新状态 

# kubectl rollout status deploy/demoappv10 -n demoapp
deployment "demoappv10" successfully rolled out

查看ReplicaSet 

# kubectl get rs -n demoapp
NAME                    DESIRED   CURRENT   READY   AGE
demoappv10-78b6586d58   2         2         2       4m59s

查看pod 标签

# kubectl get pods -n demoapp --show-labels
NAME                          READY   STATUS    RESTARTS   AGE     LABELS
demoappv10-78b6586d58-64hq4   1/1     Running   0          6m48s   app=demoapp,pod-template-hash=78b6586d58,version=v1.0
demoappv10-78b6586d58-f72jw   1/1     Running   0          6m48s   app=demoapp,pod-template-hash=78b6586d58,version=v1.0

查看Deployment 修订历史

# kubectl rollout history deployment/demoappv10 -n demoapp
deployment.apps/demoappv10 
REVISION  CHANGE-CAUSE
1         <none>

Deployment 添加注解

# kubectl annotate deployment/demoappv10 kubernetes.io/change-cause="kubectl apply -f depoly-demoapp-v10.yaml" -n demoapp
deployment.apps/demoappv10 annotated

确认Deployment 添加注解

# kubectl rollout history deployment/demoappv10 -n demoapp
deployment.apps/demoappv10 
REVISION  CHANGE-CAUSE
1         kubectl apply -f depoly-demoapp-v10.yaml

应用更新

仅当 Deployment Pod 模板(即 .spec.template)发生改变时,例如模板的标签或容器镜像被更新, 才会触发 Deployment 更新。其他更新(如对 Deployment 执行扩缩容的操作)不会触发更新动作。

更新操作

# kubectl set image deployment/demoappv10 demoapp=ikubernetes/demoapp:v1.1 -n demoapp
deployment.apps/demoappv10 image updated
deployment/demoappv10表明 Deployment 的名称,demoapp表明需要进行更新的容器,ikubernetes/demoapp:v1.1则表示镜像的新版本以及它的标签。

查看滚动更新过程

# kubectl rollout status deploy demoappv10 -n demoapp
deployment "demoappv10" successfully rolled out

查看rs控制器

# kubectl get rs -n demoapp
NAME                    DESIRED   CURRENT   READY   AGE
demoappv10-6d99f44649   2         2         2       62s
demoappv10-78b6586d58   0         0         0       85s

查看deployment日志

# kubectl describe deployments -n demoapp
Name:                   demoappv10
Namespace:              demoapp
CreationTimestamp:      Fri, 16 Jun 2023 15:34:50 +0800
Labels:                 app=demoappv10
                        version=v1.0
Annotations:            deployment.kubernetes.io/revision: 2
Selector:               app=demoapp
Replicas:               2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=demoapp
           version=v1.0
  Containers:
   demoapp:
    Image:      ikubernetes/demoapp:v1.1
    Port:       <none>
    Host Port:  <none>
    Environment:
      PORT:  8080
    Mounts:  <none>
  Volumes:   <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   demoappv10-6d99f44649 (2/2 replicas created)
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  3m15s  deployment-controller  Scaled up replica set demoappv10-78b6586d58 to 2
  Normal  ScalingReplicaSet  2m52s  deployment-controller  Scaled up replica set demoappv10-6d99f44649 to 1
  Normal  ScalingReplicaSet  2m50s  deployment-controller  Scaled down replica set demoappv10-78b6586d58 to 1 from 2
  Normal  ScalingReplicaSet  2m50s  deployment-controller  Scaled up replica set demoappv10-6d99f44649 to 2 from 1
  Normal  ScalingReplicaSet  2m49s  deployment-controller  Scaled down replica set demoappv10-78b6586d58 to 0 from 1

访问服务

# curl `kubectl get svc/demoappv10 -n demoapp -o jsonpath="{.spec.clusterIP}"`:8080
iKubernetes demoapp v1.1 !! ClientIP: 172.20.151.128, ServerName: demoappv10-6d99f44649-q8cbc, ServerIP: 172.20.44.209!

Deployment 添加注解

# kubectl annotate deployment/demoappv10 kubernetes.io/change-cause="kubectl set image deployment/demoappv10 demoapp=ikubernetes/demoapp:v1.1 -n demoapp" -n demoapp
deployment.apps/demoappv10 annotated

查看 Deployment 修订历史

# kubectl rollout history deployment/demoappv10 -n demoapp
deployment.apps/demoappv10 
REVISION  CHANGE-CAUSE
1         kubectl apply -f depoly-demoapp-v10.yaml
2         kubectl set image deployment/demoappv10 demoapp=ikubernetes/demoapp:v1.1 -n demoapp

应用回滚

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

检查 Deployment 上线历史

# kubectl rollout history deployment/demoappv10 -n demoapp
deployment.apps/demoappv10 
REVISION  CHANGE-CAUSE
1         kubectl apply -f depoly-demoapp-v10.yaml
2         kubectl set image deployment/demoappv10 demoapp=ikubernetes/demoapp:v1.1 -n demoapp

要查看修订历史的详细信息

# kubectl rollout history deployment/demoappv10 --revision=2 -n demoapp
deployment.apps/demoappv10 with revision #2
Pod Template:
  Labels:	app=demoapp
	pod-template-hash=6d99f44649
	version=v1.0
  Annotations:	kubernetes.io/change-cause: kubectl set image deployment/demoappv10 demoapp=ikubernetes/demoapp:v1.1 -n demoapp
  Containers:
   demoapp:
    Image:	ikubernetes/demoapp:v1.1
    Port:	<none>
    Host Port:	<none>
    Environment:
      PORT:	8080
    Mounts:	<none>
  Volumes:	<none>

回滚到上一个版本

执行操作

# kubectl rollout undo deployment/demoappv10 -n demoapp
deployment.apps/demoappv10 rolled back

访问服务

# curl `kubectl get svc/demoappv10 -n demoapp -o jsonpath="{.spec.clusterIP}"`:8080
iKubernetes demoapp v1.0 !! ClientIP: 172.20.151.128, ServerName: demoappv10-78b6586d58-c5n96, ServerIP: 172.20.89.151!

获取 Deployment 描述信息

# kubectl describe deployments -n demoapp
Name:                   demoappv10
Namespace:              demoapp
CreationTimestamp:      Fri, 16 Jun 2023 15:52:30 +0800
Labels:                 app=demoappv10
                        version=v1.0
Annotations:            deployment.kubernetes.io/revision: 3
                        kubernetes.io/change-cause: kubectl apply -f depoly-demoapp-v10.yaml
Selector:               app=demoapp
Replicas:               2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=demoapp
           version=v1.0
  Containers:
   demoapp:
    Image:      ikubernetes/demoapp:v1.0
    Port:       <none>
    Host Port:  <none>
    Environment:
      PORT:  8080
    Mounts:  <none>
  Volumes:   <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   demoappv10-78b6586d58 (2/2 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  108s  deployment-controller  Scaled up replica set demoappv10-78b6586d58 to 1 from 0
  Normal  ScalingReplicaSet  106s  deployment-controller  Scaled down replica set demoappv10-6d99f44649 to 1 from 2
  Normal  ScalingReplicaSet  106s  deployment-controller  Scaled up replica set demoappv10-78b6586d58 to 2 from 1
  Normal  ScalingReplicaSet  105s  deployment-controller  Scaled down replica set demoappv10-6d99f44649 to 0 from 1

Deployment 添加注解

# kubectl annotate deployment/demoappv10 kubernetes.io/change-cause="kubectl rollout undo deployment/demoappv10 -n demoapp" -n demoapp
deployment.apps/demoappv10 annotated

查看 Deployment 修订历史

# kubectl rollout history deployment/demoappv10 -n demoapp
deployment.apps/demoappv10 
REVISION  CHANGE-CAUSE
2         kubectl set image deployment/demoappv10 demoapp=ikubernetes/demoapp:v1.1 -n demoapp
3         kubectl rollout undo deployment/demoappv10 -n demoapp

回滚到指定版本

# kubectl rollout undo deployment/demoappv10 --revision=2 -n demoapp

参考文档

https://kubernetes.io/zh-cn/docs/tutorials/kubernetes-basics/update/update-intro/

标签:kubectl,回滚,set,Deployment,demoapp,版本升级,deployment,pod,demoappv10
From: https://www.cnblogs.com/wangguishe/p/17483012.html

相关文章

  • git回滚!!!线上遇到问题紧急救援
    一、打开GitBashHere输入gitreflog//输入命令gitreflog//得到以下历史提交记录4d3221f(HEAD->dev,origin/dev)HEAD@{13}:commit:拓扑图配色优化fa646feHEAD@{14}:commit:角色管理站点权限不可编辑bug修改60b35d4HEAD@{15}:commit:拓扑图相关修改93173e7a......
  • 数据面节点kubelet一直报错找不到静态Pod路径
    问题现象问题分析pkg/kubelet/config/file_linux.gostartWatch方法pkg/kubelet/kubelet.gomakePodSourceConfig函数cmd/kubelet/app/options/options.goAddKubeletConfigFlags函数kubelet启动参数中--pod-manifest-path已是""说明有别的地方设置了默认值为/etc/kuberne......
  • GreenPlum版本升级
    文章目录前言......
  • 因磁盘压力而驱逐Pod
    k8s1.15.0问题现象node2上产生磁盘压力前状态node2上产生磁盘压力后guaranteedPod和dsPod被驱逐nginx-deploymentPod被驱逐后容忍污点调度到node2,kubelet二次确认否决,继续驱逐pkg/kubelet/eviction/eviction_manager.goAdmit方法1.接受关键Pod2.节点存在内存压力,接......
  • rabbit MQ —— ha-sync-mode. message 同步/ 丢失 in new pods
    经典队列镜像—兔子MQ(rabbitmq.com) why?message信息同步=》queue一段时间不可用(可用性降低) ConfiguringSynchronisationLet'sstartwiththemostimportantaspectofqueuesynchronisation: whileaqueueisbeingsynchronised,allotherqueueoperati......
  • CKS 考试题整理 (05)-容器安全,删除特权pod
    context检查在namespaceproduction中运行的Pod,并删除任何非无状态或非不可变的Pod。task使用以下对无状态和不可变的严格解释:能够在容器内存储数据的Pod的容器必须被视为非无状态的。注意:你不必担心数据是否实际上已经存储在容器中。被配置为任何形式的特权Po......
  • K8S中pod状态详解
    Pod有以下几个状态:Pending等待中Running运行中Succeeded正常终止Failed异常停止Unkonwn未知状态Pod状态详解PendingPod已经被创建,但还没有完成调度,可能处在:写数据到etcd,调度,pull镜像,启动容器这四个阶段中的任何一个阶段,pending伴随的事件通常会有:ADDED,Modified这两个事件的......
  • Kubernetes Pod 驱逐详解
    参考网址1参考网址2QoS等级为Guaranteed的Pod会在QoS等级为Burstable的Pod之前被驱逐吗?在Kubernetes中,Pod使用的资源最重要的是CPU、内存和磁盘IO,这些资源可以被分为可压缩资源(CPU)和不可压缩资源(内存,磁盘IO)。可压缩资源不可能导致Pod被驱逐,因为当Pod的......
  • k8s集群根据进程PID获取Pod名称
    简单说明在实际的应用场景中,我们如果看到某个进程资源或服务异常,需要根据这个进程排查到底是哪个服务的Pod,这里我们介绍一种根据PID快速寻找Pod名称的方法。实际操作查看进程PID这里我们以GPU任务为例说明,可以看到占用显卡的任务PID为8241[root@centos~]#nvidia-smiThuJu......
  • Kubernetes 中的 Pod 内存请求(request)和限制(limit)设置多大合适
    Kubernetes中的Pod内存请求(request)和限制(limit)是为容器编排和资源管理提供支持的重要概念。一般来说,合适的内存request和limit需要基于应用程序的内存需求大小、应用程序的容器镜像大小以及在Kubernetes集群中Pod的数量等因素进行考虑。以下是一些关于如何设置Pod的......