控制器之一

ReplicaSet 的介绍

ReplicaSet 的作用是维持在任何给定时间运行的一组稳定的副本 Pod。 通常,你会定义一个 Deployment,并用这个 Deployment 自动管理 ReplicaSet。

ReplicaSet 的目的是维护一组在任何时候都处于运行状态的 Pod 副本的稳定集合。 因此,它通常用来保证给定数量的、完全相同的 Pod 的可用性。

ReplicaSet 是通过一组字段来定义的,包括一个用来识别可获得的 Pod 的集合的选择算符、一个用来标明应该维护的副本个数的数值、一个用来指定应该创建新 Pod 以满足副本个数条件时要使用的 Pod 模板等等。 每个 ReplicaSet 都通过根据需要创建和删除 Pod 以使得副本个数达到期望值, 进而实现其存在价值。当 ReplicaSet 需要创建新的 Pod 时,会使用所提供的 Pod 模板。

ReplicaSet 通过 Pod 上的 metadata.ownerReferences 字段连接到附属 Pod,该字段给出当前对象的属主资源。 ReplicaSet 所获得的 Pod 都在其 ownerReferences 字段中包含了属主 ReplicaSet 的标识信息。正是通过这一连接,ReplicaSet 知道它所维护的 Pod 集合的状态, 并据此计划其操作行为。

ReplicaSet 使用其选择算符来辨识要获得的 Pod 集合。如果某个 Pod 没有 OwnerReference 或者其 OwnerReference 不是一个控制器, 且其匹配到某 ReplicaSet 的选择算符,则该 Pod 立即被此 ReplicaSet 获得。

ReplicaSet 确保任何时间都有指定数量的 Pod 副本在运行。 然而,Deployment 是一个更高级的概念,它管理 ReplicaSet,并向 Pod 提供声明式的更新以及许多其他有用的功能。 因此,我们建议使用 Deployment 而不是直接使用 ReplicaSet, 除非你需要自定义更新业务流程或根本不需要更新。

这实际上意味着,你可能永远不需要操作 ReplicaSet 对象:而是使用 Deployment,并在 spec 部分定义你的应用。

[root@master replicaset]# cat rs-1.yaml 
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: rs1
  namespace: default
spec:
  minReadySeconds: 5 #创建pod 环境准备的时间
  replicas: 4 # pod 个数
  selector: # 标签选择
    matchLabels:
      app: rs-1
  template: 
    metadata: # pod 元数据
      name: cxrs
      labels: # 设置标签
        app: rs-1
    spec: 
      containers:
      - name: cx
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - name: web
          containerPort: 80
        livenessProbe:
          exec:
            command:
            - /bin/sh
            - "-c"
            - "ls /usr/share/nginx/html/index.html"
          initialDelaySeconds: 3
          periodSeconds: 5
          successThreshold: 1
          failureThreshold: 2
          
[root@master replicaset]# kubectl delete -f rs-1.yaml 
replicaset.apps "rs1" deleted
[root@master replicaset]# kubectl apply -f rs-1.yaml | kubectl get rs ; kubectl get po -w
No resources found in default namespace.
NAME              READY   STATUS              RESTARTS      AGE
liveness-1        1/1     Running             0             22h
liveness-3        1/1     Running             0             21h
lrso              1/1     Running             0             112m
periodseconds-1   1/1     Running             0             21h
periodseconds-2   1/1     Running             0             17h
pod-test-1        1/1     Running             0             24h
pod-test-2        1/1     Running             0             24h
pod-test-cx       1/1     Running             1 (37h ago)   4d23h
rs1-4fhk7         0/1     ContainerCreating   0             0s
rs1-5qz66         0/1     ContainerCreating   0             0s
rs1-p66m8         0/1     ContainerCreating   0             0s
rs1-x4qjm         0/1     Pending             0             0s
stat-1            1/1     Running             0             14h
stat-2            1/1     Running             0             147m
stat-3            1/1     Running             0             142m
rs1-x4qjm         0/1     ContainerCreating   0             0s
rs1-4fhk7         0/1     ContainerCreating   0             1s
rs1-p66m8         0/1     ContainerCreating   0             1s
rs1-5qz66         0/1     ContainerCreating   0             1s
rs1-x4qjm         0/1     ContainerCreating   0             1s
rs1-4fhk7         1/1     Running             0             2s
rs1-5qz66         1/1     Running             0             2s
rs1-p66m8         1/1     Running             0             2s
rs1-x4qjm         1/1     Running             0             3s

  更新

[root@master replicaset]# cat rs-1.yaml 
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: rs1
  namespace: default
spec:
  minReadySeconds: 5 #创建pod 环境准备的时间
  replicas: 5 # pod 个数
  selector: # 标签选择
    matchLabels:
      app: rs-1
  template: 
    metadata: # pod 元数据
      name: cxrs
      labels: # 设置标签
        app: rs-1
    spec: 
      containers:
      - name: cx
        #image: nginx
        image: docker.io/ikubernetes/myapp:v1
        imagePullPolicy: IfNotPresent
        ports:
        - name: web
          containerPort: 80
        livenessProbe:
          exec:
            command:
            - /bin/sh
            - "-c"
            - "ls /"
          initialDelaySeconds: 3
          periodSeconds: 5
          successThreshold: 1
          failureThreshold: 2


[root@master replicaset]# kubectl apply -f rs-1.yaml 
replicaset.apps/rs1 configured

查看
[root@master replicaset]# kubectl get po -w
NAME              READY   STATUS    RESTARTS      AGE
liveness-1        1/1     Running   0             22h
liveness-3        1/1     Running   0             21h
lrso              1/1     Running   0             150m
periodseconds-1   1/1     Running   0             21h
periodseconds-2   1/1     Running   0             18h
pod-test-1        1/1     Running   0             25h
pod-test-2        1/1     Running   0             25h
pod-test-cx       1/1     Running   1 (38h ago)   4d23h
rs1-4fhk7         1/1     Running   0             38m
rs1-5qz66         1/1     Running   0             38m
rs1-fbfxj         1/1     Running   0             4m4s
rs1-p66m8         1/1     Running   0             38m
rs1-x4qjm         1/1     Running   0             38m
stat-1            1/1     Running   0             15h
stat-2            1/1     Running   0             3h6m
stat-3            1/1     Running   0             3h1m
手动删除pod,完成更新
[root@master replicaset]# kubectl delete po rs1-4fhk7 
pod "rs1-4fhk7" deleted
rs1-4fhk7         1/1     Terminating   0             41m
rs1-49p8w         0/1     Pending       0             0s
rs1-49p8w         0/1     Pending       0             0s
rs1-49p8w         0/1     ContainerCreating   0             0s
rs1-4fhk7         1/1     Terminating         0             41m
rs1-4fhk7         0/1     Terminating         0             41m
rs1-4fhk7         0/1     Terminating         0             41m
rs1-49p8w         0/1     ContainerCreating   0             1s
rs1-4fhk7         0/1     Terminating         0             41m
rs1-4fhk7         0/1     Terminating         0             41m
rs1-49p8w         1/1     Running             0             11s
    
[root@master replicaset]# kubectl delete po rs1-5qz66 rs1-fbfxj rs1-p66m8 rs1-x4qjm 
pod "rs1-5qz66" deleted
pod "rs1-fbfxj" deleted
pod "rs1-p66m8" deleted
pod "rs1-x4qjm" deleted
rs1-5qz66         1/1     Terminating         0             43m
rs1-fbfxj         1/1     Terminating         0             8m49s
rs1-zwb7p         0/1     Pending             0             0s
rs1-p66m8         1/1     Terminating         0             43m
rs1-x4qjm         1/1     Terminating         0             43m
rs1-zwb7p         0/1     Pending             0             0s
rs1-xhnz2         0/1     Pending             0             0s
rs1-xhnz2         0/1     Pending             0             0s
rs1-zwb7p         0/1     ContainerCreating   0             0s
rs1-g9hlt         0/1     Pending             0             0s
rs1-wg5rt         0/1     Pending             0             0s
rs1-g9hlt         0/1     Pending             0             0s
rs1-wg5rt         0/1     Pending             0             0s
rs1-xhnz2         0/1     ContainerCreating   0             0s
rs1-g9hlt         0/1     ContainerCreating   0             0s
rs1-wg5rt         0/1     ContainerCreating   0             0s
rs1-5qz66         1/1     Terminating         0             43m
rs1-p66m8         1/1     Terminating         0             43m
rs1-fbfxj         1/1     Terminating         0             8m49s
rs1-x4qjm         1/1     Terminating         0             43m
rs1-x4qjm         0/1     Terminating         0             43m
rs1-xhnz2         0/1     ContainerCreating   0             1s
rs1-5qz66         0/1     Terminating         0             43m
rs1-wg5rt         0/1     ContainerCreating   0             1s
rs1-fbfxj         0/1     Terminating         0             8m50s
rs1-p66m8         0/1     Terminating         0             43m
rs1-g9hlt         0/1     ContainerCreating   0             1s
rs1-zwb7p         0/1     ContainerCreating   0             2s
rs1-x4qjm         0/1     Terminating         0             43m
rs1-xhnz2         1/1     Running             0             2s
rs1-x4qjm         0/1     Terminating         0             43m
rs1-x4qjm         0/1     Terminating         0             43m
rs1-p66m8         0/1     Terminating         0             43m
rs1-fbfxj         0/1     Terminating         0             8m51s
rs1-5qz66         0/1     Terminating         0             43m
rs1-fbfxj         0/1     Terminating         0             8m51s
rs1-fbfxj         0/1     Terminating         0             8m51s
rs1-p66m8         0/1     Terminating         0             43m
rs1-p66m8         0/1     Terminating         0             43m
rs1-5qz66         0/1     Terminating         0             43m
rs1-5qz66         0/1     Terminating         0             43m
rs1-wg5rt         1/1     Running             0             3s
rs1-g9hlt         1/1     Running             0             11s
rs1-zwb7p         1/1     Running             0             14s

  deployment 控制器https://kubernetes.io/zh-cn/docs/concepts/workloads/controllers/deployment/

Deployment 用于管理运行一个应用负载的一组 Pod,通常适用于不保持状态的负载。

一个 Deployment 为 Pod 和 ReplicaSet 提供声明式的更新能力。

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

更新

[root@master dpment]# vim dpment.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
[root@master dpment]# kubectl apply -f dpment.yaml 
deployment.apps/nginx-deployment created
[root@master dpment]# kubectl get deployments.apps  -w
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3/3     3            3           40s
更新镜像
[root@master dpment]# kubectl set image -n default deployment.apps/nginx-deployment nginx=nginx:1.16.1
deployment.apps/nginx-deployment image updated
[root@master dpment]# kubectl get po -w
NAME                                READY   STATUS              RESTARTS      AGE
liveness-1                          1/1     Running             0             22h
liveness-3                          1/1     Running             0             22h
lrso                                1/1     Running             0             166m
nginx-deployment-848dd6cfb5-5ncfc   0/1     ContainerCreating   0             9s
nginx-deployment-86dcfdf4c6-bxdtm   1/1     Running             0             2m35s
nginx-deployment-86dcfdf4c6-h9l4j   1/1     Running             0             2m35s
nginx-deployment-86dcfdf4c6-swvxg   1/1     Running             0             2m35s
periodseconds-1                     1/1     Running             0             22h
periodseconds-2                     1/1     Running             0             18h
pod-test-1                          1/1     Running             0             25h
pod-test-2                          1/1     Running             0             25h
pod-test-cx                         1/1     Running             1 (38h ago)   4d23h
rs1-49p8w                           1/1     Running             0             12m
rs1-g9hlt                           1/1     Running             0             11m
rs1-wg5rt                           1/1     Running             0             11m
rs1-xhnz2                           1/1     Running             0             11m
rs1-zwb7p                           1/1     Running             0             11m
stat-1                              1/1     Running             0             15h
stat-2                              1/1     Running             0             3h22m
stat-3                              1/1     Running             0             3h17m
nginx-deployment-848dd6cfb5-5ncfc   1/1     Running             0             25s
nginx-deployment-86dcfdf4c6-h9l4j   1/1     Terminating         0             2m51s
nginx-deployment-848dd6cfb5-48dkv   0/1     Pending             0             0s
nginx-deployment-848dd6cfb5-48dkv   0/1     Pending             0             0s
nginx-deployment-848dd6cfb5-48dkv   0/1     ContainerCreating   0             0s
nginx-deployment-848dd6cfb5-48dkv   0/1     ContainerCreating   0             0s
nginx-deployment-86dcfdf4c6-h9l4j   1/1     Terminating         0             2m52s
nginx-deployment-86dcfdf4c6-h9l4j   0/1     Terminating         0             2m52s
nginx-deployment-86dcfdf4c6-h9l4j   0/1     Terminating         0             2m53s
nginx-deployment-86dcfdf4c6-h9l4j   0/1     Terminating         0             2m53s
nginx-deployment-86dcfdf4c6-h9l4j   0/1     Terminating         0             2m53s
nginx-deployment-848dd6cfb5-48dkv   1/1     Running             0             25s
nginx-deployment-86dcfdf4c6-swvxg   1/1     Terminating         0             3m16s
nginx-deployment-848dd6cfb5-c242w   0/1     Pending             0             0s
nginx-deployment-848dd6cfb5-c242w   0/1     Pending             0             0s
nginx-deployment-848dd6cfb5-c242w   0/1     ContainerCreating   0             0s
nginx-deployment-848dd6cfb5-c242w   0/1     ContainerCreating   0             1s
nginx-deployment-86dcfdf4c6-swvxg   1/1     Terminating         0             3m17s
nginx-deployment-86dcfdf4c6-swvxg   0/1     Terminating         0             3m17s
nginx-deployment-86dcfdf4c6-swvxg   0/1     Terminating         0             3m18s
nginx-deployment-86dcfdf4c6-swvxg   0/1     Terminating         0             3m18s
nginx-deployment-86dcfdf4c6-swvxg   0/1     Terminating         0             3m18s
nginx-deployment-848dd6cfb5-c242w   1/1     Running             0             2s
nginx-deployment-86dcfdf4c6-bxdtm   1/1     Terminating         0             3m18s
nginx-deployment-86dcfdf4c6-bxdtm   1/1     Terminating         0             3m18s
nginx-deployment-86dcfdf4c6-bxdtm   0/1     Terminating         0             3m18s
nginx-deployment-86dcfdf4c6-bxdtm   0/1     Terminating         0             3m19s
nginx-deployment-86dcfdf4c6-bxdtm   0/1     Terminating         0             3m19s
nginx-deployment-86dcfdf4c6-bxdtm   0/1     Terminating         0             3m19s

[root@master dpment]# kubectl get rs
NAME                          DESIRED   CURRENT   READY   AGE
nginx-deployment-848dd6cfb5   3         3         3       6m47s
nginx-deployment-86dcfdf4c6   0         0         0       9m13s
rs1                           5         5         5       60m

  更新策略

翻转(多 Deployment 动态更新)

Deployment 控制器每次注意到新的 Deployment 时,都会创建一个 ReplicaSet 以启动所需的 Pod。 如果更新了 Deployment,则控制标签匹配 .spec.selector 但模板不匹配 .spec.template 的 Pod 的现有 ReplicaSet 被缩容。 最终,新的 ReplicaSet 缩放为 .spec.replicas 个副本, 所有旧 ReplicaSet 缩放为 0 个副本。

当 Deployment 正在上线时被更新,Deployment 会针对更新创建一个新的 ReplicaSet 并开始对其扩容,之前正在被扩容的 ReplicaSet 会被翻转,添加到旧 ReplicaSet 列表 并开始缩容。

回滚 Deployment

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

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

kubectl set image deployment/nginx-deployment nginx=nginx:1.161
输出类似于:

deployment.apps/nginx-deployment image updated
此上线进程会出现停滞。你可以通过检查上线状态来验证:

kubectl rollout status deployment/nginx-deployment
输出类似于:

Waiting for rollout to finish: 1 out of 3 new replicas have been updated...
按 Ctrl-C 停止上述上线状态观测。有关上线停滞的详细信息,参考这里。
你可以看到旧的副本(算上来自 nginx-deployment-1564180365 和 nginx-deployment-2035384211 的副本)有 3 个, 新的副本(来自 nginx-deployment-3066724191)有 1 个:

kubectl get rs
输出类似于:

NAME                          DESIRED   CURRENT   READY   AGE
nginx-deployment-1564180365   3         3         3       25s
nginx-deployment-2035384211   0         0         0       36s
nginx-deployment-3066724191   1         1         0       6s
查看所创建的 Pod,你会注意到新 ReplicaSet 所创建的 1 个 Pod 卡顿在镜像拉取循环中。

kubectl get pods
输出类似于:

NAME                                READY     STATUS             RESTARTS   AGE
nginx-deployment-1564180365-70iae   1/1       Running            0          25s
nginx-deployment-1564180365-jbqqo   1/1       Running            0          25s
nginx-deployment-1564180365-hysrc   1/1       Running            0          25s
nginx-deployment-3066724191-08mng   0/1       ImagePullBackOff   0 

  检查历史

按照如下步骤检查回滚历史:

首先,检查 Deployment 修订历史:

kubectl rollout history deployment/nginx-deployment
输出类似于:

deployments "nginx-deployment"
REVISION    CHANGE-CAUSE
1           kubectl apply --filename=https://k8s.io/examples/controllers/nginx-deployment.yaml
2           kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1
3           kubectl set image deployment/nginx-deployment nginx=nginx:1.161
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 添加注解。
手动编辑资源的清单。
要查看修订历史的详细信息,运行:

kubectl rollout history deployment/nginx-deployment --revision=2
输出类似于:

deployments "nginx-deployment" revision 2
  Labels:       app=nginx
          pod-template-hash=1159050644
  Annotations:  kubernetes.io/change-cause=kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1
  Containers:
   nginx:
    Image:      nginx:1.16.1
    Port:       80/TCP
     QoS Tier:
        cpu:      BestEffort
        memory:   BestEffort
    Environment Variables:      <none>
  No volumes.
回滚到之前的修订版本

  回退

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

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

kubectl rollout undo deployment/nginx-deployment
输出类似于:

deployment.apps/nginx-deployment rolled back
或者,你也可以通过使用 --to-revision 来回滚到特定修订版本:

kubectl rollout undo deployment/nginx-deployment --to-revision=2
输出类似于:

deployment.apps/nginx-deployment rolled back
与回滚相关的指令的更详细信息,请参考 kubectl rollout。

现在,Deployment 正在回滚到以前的稳定版本。正如你所看到的,Deployment 控制器生成了回滚到修订版本 2 的 DeploymentRollback 事件。

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

kubectl get deployment nginx-deployment
输出类似于:

NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3/3     3            3           30m

  扩缩容pod

kubectl scale deployment/nginx-deployment --replicas=10
输出类似于:

deployment.apps/nginx-deployment scaled

  

滚动更新 Deployment

Deployment 会在 .spec.strategy.type==RollingUpdate时,采取 滚动更新的方式更新 Pod。你、可以指定 maxUnavailable 和 maxSurge 来控制滚动更新过程。

最大不可用

.spec.strategy.rollingUpdate.maxUnavailable 是一个可选字段, 用来指定更新过程中不可用的 Pod 的个数上限。该值可以是绝对数字(例如,5),也可以是所需 Pod 的百分比(例如,10%)。百分比值会转换成绝对数并去除小数部分。 如果 .spec.strategy.rollingUpdate.maxSurge 为 0,则此值不能为 0。 默认值为 25%。

例如,当此值设置为 30% 时,滚动更新开始时会立即将旧 ReplicaSet 缩容到期望 Pod 个数的70%。 新 Pod 准备就绪后,可以继续缩容旧有的 ReplicaSet,然后对新的 ReplicaSet 扩容, 确保在更新期间可用的 Pod 总数在任何时候都至少为所需的 Pod 个数的 70%。

最大峰值(可用)

.spec.strategy.rollingUpdate.maxSurge 是一个可选字段,用来指定可以创建的超出期望 Pod 个数的 Pod 数量。此值可以是绝对数(例如,5)或所需 Pod 的百分比(例如,10%)。 如果 MaxUnavailable 为 0,则此值不能为 0。百分比值会通过向上取整转换为绝对数。 此字段的默认值为 25%。

例如,当此值为 30% 时,启动滚动更新后,会立即对新的 ReplicaSet 扩容,同时保证新旧 Pod 的总数不超过所需 Pod 总数的 130%。一旦旧 Pod 被杀死,新的 ReplicaSet 可以进一步扩容, 同时确保更新期间的任何时候运行中的 Pod 总数最多为所需 Pod 总数的 130%。

进度期限秒数

.spec.progressDeadlineSeconds 是一个可选字段,用于指定系统在报告 Deployment 进展失败之前等待 Deployment 取得进展的秒数。 这类报告会在资源状态中体现为 type: Progressingstatus: False、 reason: ProgressDeadlineExceeded。Deployment 控制器将在默认 600 毫秒内持续重试 Deployment。 将来,一旦实现了自动回滚,Deployment 控制器将在探测到这样的条件时立即回滚 Deployment。

如果指定,则此字段值需要大于 .spec.minReadySeconds 取值。

[root@master dpment]# cat dpment-1.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment-ro
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-ro
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  template:
    metadata:
      labels:
        app: nginx-ro
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
[root@master dpment]# kubectl apply -f dpment-1.yaml 
deployment.apps/nginx-deployment-ro created
[root@master dpment]# kubectl get po
NAME                                   READY   STATUS    RESTARTS   AGE
nginx-deployment-ro-56d78cc78d-9l5nh   1/1     Running   0          2s
nginx-deployment-ro-56d78cc78d-b2vj8   1/1     Running   0          2s
nginx-deployment-ro-56d78cc78d-p9wlj   1/1     Running   0          2s
[root@master dpment]# kubectl set image deployment.apps/nginx-deployment-ro nginx=nginx:1.16.1 | kubectl get po -w 
NAME                                   READY   STATUS    RESTARTS   AGE
nginx-deployment-ro-56d78cc78d-9l5nh   1/1     Running   0          108s
nginx-deployment-ro-56d78cc78d-b2vj8   1/1     Running   0          108s
nginx-deployment-ro-56d78cc78d-p9wlj   1/1     Running   0          108s
nginx-deployment-ro-56d78cc78d-p9wlj   1/1     Terminating   0          108s
nginx-deployment-ro-f56978bf-jn8sv     0/1     Pending       0          0s
nginx-deployment-ro-f56978bf-jn8sv     0/1     Pending       0          0s
nginx-deployment-ro-f56978bf-jn8sv     0/1     ContainerCreating   0          0s
nginx-deployment-ro-f56978bf-qknvh     0/1     Pending             0          0s
nginx-deployment-ro-f56978bf-qknvh     0/1     Pending             0          0s
nginx-deployment-ro-f56978bf-qknvh     0/1     ContainerCreating   0          0s
nginx-deployment-ro-56d78cc78d-p9wlj   1/1     Terminating         0          108s
nginx-deployment-ro-56d78cc78d-p9wlj   0/1     Terminating         0          108s
nginx-deployment-ro-f56978bf-jn8sv     0/1     ContainerCreating   0          0s
nginx-deployment-ro-f56978bf-qknvh     0/1     ContainerCreating   0          0s
nginx-deployment-ro-56d78cc78d-p9wlj   0/1     Terminating         0          108s
nginx-deployment-ro-56d78cc78d-p9wlj   0/1     Terminating         0          109s
nginx-deployment-ro-56d78cc78d-p9wlj   0/1     Terminating         0          109s
nginx-deployment-ro-f56978bf-jn8sv     1/1     Running             0          1s
nginx-deployment-ro-56d78cc78d-9l5nh   1/1     Terminating         0          109s
nginx-deployment-ro-f56978bf-5p4f8     0/1     Pending             0          0s
nginx-deployment-ro-f56978bf-qknvh     1/1     Running             0          2s
nginx-deployment-ro-f56978bf-5p4f8     0/1     Pending             0          0s
nginx-deployment-ro-f56978bf-5p4f8     0/1     ContainerCreating   0          0s
nginx-deployment-ro-56d78cc78d-b2vj8   1/1     Terminating         0          110s
nginx-deployment-ro-56d78cc78d-9l5nh   1/1     Terminating         0          110s
nginx-deployment-ro-56d78cc78d-9l5nh   0/1     Terminating         0          110s
nginx-deployment-ro-56d78cc78d-b2vj8   1/1     Terminating         0          110s
nginx-deployment-ro-f56978bf-5p4f8     0/1     ContainerCreating   0          0s
nginx-deployment-ro-56d78cc78d-b2vj8   0/1     Terminating         0          110s
nginx-deployment-ro-56d78cc78d-b2vj8   0/1     Terminating         0          110s
nginx-deployment-ro-56d78cc78d-b2vj8   0/1     Terminating         0          110s
nginx-deployment-ro-56d78cc78d-b2vj8   0/1     Terminating         0          110s
nginx-deployment-ro-56d78cc78d-9l5nh   0/1     Terminating         0          111s
nginx-deployment-ro-56d78cc78d-9l5nh   0/1     Terminating         0          111s
nginx-deployment-ro-56d78cc78d-9l5nh   0/1     Terminating         0          111s
nginx-deployment-ro-f56978bf-5p4f8     1/1     Running             0          1s
[root@master dpment]# kubectl get rs -w
NAME                             DESIRED   CURRENT   READY   AGE
nginx-deployment-848dd6cfb5      0         0         0       45m
nginx-deployment-86dcfdf4c6      0         0         0       47m
nginx-deployment-ro-56d78cc78d   3         3         3       97s
rs1                              0         0         0       99m
nginx-deployment-ro-f56978bf     1         0         0       0s
nginx-deployment-ro-56d78cc78d   2         3         3       109s
nginx-deployment-ro-f56978bf     2         0         0       0s
nginx-deployment-ro-56d78cc78d   2         3         3       109s
nginx-deployment-ro-f56978bf     2         0         0       0s
nginx-deployment-ro-56d78cc78d   2         2         2       109s
nginx-deployment-ro-f56978bf     2         1         0       0s
nginx-deployment-ro-f56978bf     2         2         0       0s
nginx-deployment-ro-f56978bf     2         2         1       1s
nginx-deployment-ro-56d78cc78d   1         2         2       110s
nginx-deployment-ro-f56978bf     3         2         1       2s
nginx-deployment-ro-56d78cc78d   1         2         2       111s
nginx-deployment-ro-56d78cc78d   1         1         1       111s
nginx-deployment-ro-f56978bf     3         2         1       2s
nginx-deployment-ro-f56978bf     3         3         2       2s
nginx-deployment-ro-56d78cc78d   0         1         1       111s
nginx-deployment-ro-56d78cc78d   0         1         1       111s
nginx-deployment-ro-56d78cc78d   0         0         0       111s
nginx-deployment-ro-f56978bf     3         3         3       3s

  查看历史版本

[root@master dpment]# kubectl rollout history deployment nginx-deployment-ro 
deployment.apps/nginx-deployment-ro 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>

  命令行回滚

[root@master dpment]# kubectl rollout undo deployment nginx-deployment-ro --to-revision=1
deployment.apps/nginx-deployment-ro rolled back

  查看回滚rs 控制器过程

[root@master dpment]# kubectl get rs -w
NAME                             DESIRED   CURRENT   READY   AGE
nginx-deployment-848dd6cfb5      0         0         0       168m
nginx-deployment-86dcfdf4c6      0         0         0       170m
nginx-deployment-ro-56d78cc78d   0         0         0       124m
nginx-deployment-ro-f56978bf     3         3         3       122m
rs1                              0         0         0       3h42m
nginx-deployment-ro-56d78cc78d   0         0         0       125m
nginx-deployment-ro-56d78cc78d   1         0         0       125m
nginx-deployment-ro-f56978bf     2         3         3       123m
nginx-deployment-ro-56d78cc78d   2         0         0       125m
nginx-deployment-ro-f56978bf     2         3         3       123m
nginx-deployment-ro-f56978bf     2         2         2       123m
nginx-deployment-ro-56d78cc78d   2         0         0       125m
nginx-deployment-ro-56d78cc78d   2         1         0       125m
nginx-deployment-ro-56d78cc78d   2         2         0       125m
nginx-deployment-ro-56d78cc78d   2         2         1       125m
nginx-deployment-ro-f56978bf     1         2         2       123m
nginx-deployment-ro-f56978bf     1         2         2       123m
nginx-deployment-ro-56d78cc78d   3         2         1       125m
nginx-deployment-ro-f56978bf     1         1         1       123m
nginx-deployment-ro-56d78cc78d   3         2         1       125m
nginx-deployment-ro-56d78cc78d   3         3         1       125m
nginx-deployment-ro-56d78cc78d   3         3         2       125m
nginx-deployment-ro-f56978bf     0         1         1       123m
nginx-deployment-ro-f56978bf     0         1         1       123m
nginx-deployment-ro-f56978bf     0         0         0       123m
nginx-deployment-ro-56d78cc78d   3         3         3       125m

  deployment  基于反亲和性创建po

[root@master dpment]# cat dpment-2.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-cx1
  labels:
    app: nginx
spec:
  replicas: 3
  revisionHistoryLimit: 4
 # paused: true
  selector:
    matchLabels:
      app: nginx-cx
  strategy:
 #   type: rollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  template:
    metadata:
      labels:
        app: nginx-cx
    spec:
      affinity:
        podAntiAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values:
                - nginx-cx
            topologyKey: kubernetes.io/hostname
      tolerations: # 污点容忍度
      - key: node-role.kubernetes.io/control-plane # 污点KEY
        operator: Exists
        effect: NoSchedule
      containers:
      - name: nginx
        image: nginx:1.10
        ports:
        - containerPort: 80
[root@master dpment]# kubectl apply -f dpment-2.yaml ; kubectl get po -w
deployment.apps/nginx-cx1 created
NAME                                   READY   STATUS              RESTARTS   AGE
nginx-cx1-5c86fddfc5-4wdvg             0/1     ContainerCreating   0          0s
nginx-cx1-5c86fddfc5-8qgqb             0/1     Pending             0          0s
nginx-cx1-5c86fddfc5-jgtt5             0/1     Pending             0          0s
nginx-deployment-ro-56d78cc78d-5sskn   1/1     Running             0          44m
nginx-deployment-ro-56d78cc78d-77m8k   1/1     Running             0          44m
nginx-deployment-ro-56d78cc78d-kvhsw   1/1     Running             0          44m
nginx-cx1-5c86fddfc5-jgtt5             0/1     ContainerCreating   0          0s
nginx-cx1-5c86fddfc5-8qgqb             0/1     ContainerCreating   0          0s
nginx-cx1-5c86fddfc5-4wdvg             0/1     ContainerCreating   0          1s
nginx-cx1-5c86fddfc5-jgtt5             0/1     ContainerCreating   0          1s
nginx-cx1-5c86fddfc5-8qgqb             0/1     ContainerCreating   0          2s
nginx-cx1-5c86fddfc5-4wdvg             1/1     Running             0          2s
nginx-cx1-5c86fddfc5-jgtt5             1/1     Running             0          2s
nginx-cx1-5c86fddfc5-8qgqb             1/1     Running             0          33s

[root@master dpment]# kubectl get rs -w
NAME                             DESIRED   CURRENT   READY   AGE
nginx-deployment-848dd6cfb5      0         0         0       3h33m
nginx-deployment-86dcfdf4c6      0         0         0       3h36m
nginx-deployment-ro-56d78cc78d   3         3         3       169m
nginx-deployment-ro-f56978bf     0         0         0       168m
rs1                              0         0         0       4h27m
nginx-cx1-5c86fddfc5             3         0         0       0s
nginx-cx1-5c86fddfc5             3         0         0       0s
nginx-cx1-5c86fddfc5             3         3         0       0s
nginx-cx1-5c86fddfc5             3         3         1       2s
nginx-cx1-5c86fddfc5             3         3         2       2s
nginx-cx1-5c86fddfc5             3         3         3       33s
[root@master dpment]# kubectl get deployments.apps 
NAME                  READY   UP-TO-DATE   AVAILABLE   AGE
nginx-cx1             3/3     3            3           3m13s
nginx-deployment      0/0     0            0           3h39m
nginx-deployment-ro   3/3     3            3           173m

  

posted @ 2024-10-17 13:04  烟雨楼台,行云流水  阅读(7)  评论(0编辑  收藏  举报