【k8s资源调度-Deployment】

news2024/11/15 18:03:20

1、标签和选择器

1.1 标签Label

  • 配置文件:在各类资源的sepc.metadata.label 中进行配置
  • 通过kubectl 命令行创建修改标签,语法如下
    • 创建临时label:kubectl label po <资源名称> app=hello -n <命令空间(可不加)>
    • 修改已经存在的label: kubectl label po <资源名称> app=hello -n <命令空间(可不加)> --overwrite
    • 通过查看lable的时候修改label
      • selector 按照 label 单值查找节点: kubectl get po -A -l app=hello
      • 查看所有节点的labels :kubectl get po --show-labels
## 创建pod
[root@k8s-master ~]# kubectl create -f nginx-po.yml
pod/nginx-liveness-po created

# 查看pod状态
[root@k8s-master ~]# kubectl get po  nginx-liveness-po
NAME                READY   STATUS    RESTARTS   AGE
nginx-liveness-po   1/1     Running   0          51s

# 查看pod状态,显示label信息
[root@k8s-master ~]# kubectl get po  nginx-liveness-po --show-labels
NAME                READY   STATUS    RESTARTS   AGE   LABELS
nginx-liveness-po   1/1     Running   0          68s   test=l.0.0,type=app

# 临时给pod添加label
[root@k8s-master ~]# kubectl label po nginx-liveness-po  auth=xiaobai
pod/nginx-liveness-po labeled

[root@k8s-master ~]# kubectl get po  nginx-liveness-po --show-labels
NAME                READY   STATUS    RESTARTS   AGE    LABELS
nginx-liveness-po   1/1     Running   0          105s   auth=xiaobai,test=l.0.0,type=app

# 修改pod的label,但是不加 --overwrite 会提示失败,需要加 --overweite
[root@k8s-master ~]# kubectl label po nginx-liveness-po  auth=xiaohong
error: 'auth' already has a value (xiaobai), and --overwrite is false
[root@k8s-master ~]# kubectl label po nginx-liveness-po  auth=xiaohong   --overwrite
pod/nginx-liveness-po labeled

# 修改label属性已经更改
[root@k8s-master ~]# kubectl get po  nginx-liveness-po --show-labels
NAME                READY   STATUS    RESTARTS   AGE    LABELS
nginx-liveness-po   1/1     Running   0          2m3s   auth=xiaohong,test=l.0.0,type=app

# 通过单个labe信息查找pod
[root@k8s-master ~]# kubectl get po  -A  -l  type=app
NAMESPACE   NAME                READY   STATUS    RESTARTS   AGE
default     nginx-liveness-po   1/1     Running   0          8m27s

1.2 选择器Seletor

  • 通过yaml配置文件,在各对象的配置 spec.seletor 或其他可以写selector的属性中编写。
  • 通过命令行模式,语法如下
    • 匹配但个值,查找app=hello的pod:kubectl get po -A -I app=hello
    • 匹配多个值:kubectl get po -A -I ‘k8s-app in (metrics-server,kubernetes-dashboard)’
    • 查找version!=1 and app=nginx的pod信息:kubectl get po -I version!=1,app=nginx

# 单值匹配的时候
[root@k8s-master ~]# kubectl get po  -A  -l  type=app --show-labels
NAMESPACE   NAME                READY   STATUS    RESTARTS   AGE   LABELS
default     nginx-liveness-po   1/1     Running   0          18m   auth=xiaohong,test=l.0.0,type=app

# 显示所有的pod的label信息
[root@k8s-master ~]# kubectl get po  -A  --show-labels
NAMESPACE      NAME                                 READY   STATUS    RESTARTS      AGE     LABELS
default        nginx-liveness-po                    1/1     Running   0             18m     auth=xiaohong,test=l.0.0,type=app
kube-flannel   kube-flannel-ds-glkkb                1/1     Running   4 (24h ago)   3d19h   app=flannel,controller-revision-hash=7cfb6d964b,pod-template-generation=1,tier=node
kube-flannel   kube-flannel-ds-pdmtw                1/1     Running   1 (24h ago)   44h     app=flannel,controller-revision-hash=7cfb6d964b,pod-template-generation=1,tier=node
kube-flannel   kube-flannel-ds-tpm8x                1/1     Running   2 (24h ago)   3d19h   app=flannel,controller-revision-hash=7cfb6d964b,pod-template-generation=1,tier=node
kube-system    coredns-c676cc86f-pdsl6              1/1     Running   3 (24h ago)   2d7h    k8s-app=kube-dns,pod-template-hash=c676cc86f
kube-system    coredns-c676cc86f-q7hcw              1/1     Running   1 (24h ago)   2d7h    k8s-app=kube-dns,pod-template-hash=c676cc86f
kube-system    etcd-k8s-master                      1/1     Running   2 (24h ago)   3d20h   component=etcd,tier=control-plane
kube-system    kube-apiserver-k8s-master            1/1     Running   3 (24h ago)   3d20h   component=kube-apiserver,tier=control-plane
kube-system    kube-controller-manager-k8s-master   1/1     Running   4 (24h ago)   3d20h   component=kube-controller-manager,tier=control-plane
kube-system    kube-proxy-n2w92                     1/1     Running   4 (24h ago)   3d19h   controller-revision-hash=dd4c999cf,k8s-app=kube-proxy,pod-template-generation=1
kube-system    kube-proxy-p8fhs                     1/1     Running   1 (24h ago)   44h     controller-revision-hash=dd4c999cf,k8s-app=kube-proxy,pod-template-generation=1
kube-system    kube-proxy-xtllb                     1/1     Running   2 (24h ago)   3d20h   controller-revision-hash=dd4c999cf,k8s-app=kube-proxy,pod-template-generation=1
kube-system    kube-scheduler-k8s-master            1/1     Running   4 (24h ago)   3d20h   component=kube-scheduler,tier=control-plane

# 匹配单个值 app!=flannel
[root@k8s-master ~]# kubectl get po  -A   -l app!=flannel
NAMESPACE     NAME                                 READY   STATUS    RESTARTS      AGE
default       nginx-liveness-po                    1/1     Running   0             19m
kube-system   coredns-c676cc86f-pdsl6              1/1     Running   3 (24h ago)   2d7h
kube-system   coredns-c676cc86f-q7hcw              1/1     Running   1 (24h ago)   2d7h
kube-system   etcd-k8s-master                      1/1     Running   2 (24h ago)   3d20h
kube-system   kube-apiserver-k8s-master            1/1     Running   3 (24h ago)   3d20h
kube-system   kube-controller-manager-k8s-master   1/1     Running   4 (24h ago)   3d20h
kube-system   kube-proxy-n2w92                     1/1     Running   4 (24h ago)   3d19h
kube-system   kube-proxy-p8fhs                     1/1     Running   1 (24h ago)   44h
kube-system   kube-proxy-xtllb                     1/1     Running   2 (24h ago)   3d20h
kube-system   kube-scheduler-k8s-master            1/1     Running   4 (24h ago)   3d20h



[root@k8s-master ~]# kubectl get po  -A   -l app!=flannel --show-labels
NAMESPACE     NAME                                 READY   STATUS    RESTARTS      AGE     LABELS
default       nginx-liveness-po                    1/1     Running   0             19m     auth=xiaohong,test=l.0.0,type=app
kube-system   coredns-c676cc86f-pdsl6              1/1     Running   3 (24h ago)   2d7h    k8s-app=kube-dns,pod-template-hash=c676cc86f
kube-system   coredns-c676cc86f-q7hcw              1/1     Running   1 (24h ago)   2d7h    k8s-app=kube-dns,pod-template-hash=c676cc86f
kube-system   etcd-k8s-master                      1/1     Running   2 (24h ago)   3d20h   component=etcd,tier=control-plane
kube-system   kube-apiserver-k8s-master            1/1     Running   3 (24h ago)   3d20h   component=kube-apiserver,tier=control-plane
kube-system   kube-controller-manager-k8s-master   1/1     Running   4 (24h ago)   3d20h   component=kube-controller-manager,tier=control-plane
kube-system   kube-proxy-n2w92                     1/1     Running   4 (24h ago)   3d19h   controller-revision-hash=dd4c999cf,k8s-app=kube-proxy,pod-template-generation=1
kube-system   kube-proxy-p8fhs                     1/1     Running   1 (24h ago)   44h     controller-revision-hash=dd4c999cf,k8s-app=kube-proxy,pod-template-generation=1
kube-system   kube-proxy-xtllb                     1/1     Running   2 (24h ago)   3d20h   controller-revision-hash=dd4c999cf,k8s-app=kube-proxy,pod-template-generation=1
kube-system   kube-scheduler-k8s-master            1/1     Running   4 (24h ago)   3d20h   component=kube-scheduler,tier=control-plane

# 多值匹配
[root@k8s-master ~]# kubectl get po  -A   -l app!=flannel,test=1.0.0 --show-labels
No resources found


# 多值匹配,多值匹配是“与”的关系,不是“或”的关系
[root@k8s-master ~]# kubectl get po  -A   -l app!=flannel,type=app  --show-labels
NAMESPACE   NAME                READY   STATUS    RESTARTS   AGE   LABELS
default     nginx-liveness-po   1/1     Running   0          20m   auth=xiaohong,test=l.0.0,type=app

2、部署对象Deployment(无状态应用)

在这里插入图片描述

2.1 创建deploy

2.1.1 通过命令行创建一个deployment

# 创建一个deployment
[root@k8s-master ~]# kubectl create deploy nginx-deploy --image=nginx:1.20
deployment.apps/nginx-deploy created

创建一个deployment
使用命令行模式:kubectl create deploy nginx-deploy --image=nginx:1.20
或执行 kubectl create -f xxx.yaml --record
-record会在annotation中记录当前命令创建或升级了资源,后续可以查看做过哪变动操作。

2.1.2 查看deployment 信息

通过kubectl get 查看deployment,可以使用deployment,也可以用deploy

# 通过kubectl get 查看deployment,可以使用deployment,也可以用deploy
[root@k8s-master ~]# kubectl  get deployments
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deploy   1/1     1            1           10s

[root@k8s-master ~]# kubectl  get deploy
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deploy   1/1     1            1           14s

2.1.3 查看replicasets 信息

由于创建的deployment资源包含 replicasets信息(replicasets是可以自动扩容和缩容),可以查看到 replicasets的名字中包含deploy资源的名字信息

# 由于deployment包含 replicasets(replicasets是可以自动扩容和缩容),可以查看到 replicasets的名字中包含deploy的名字信息 
[root@k8s-master ~]# kubectl  get rs
NAME                      DESIRED   CURRENT   READY   AGE
nginx-deploy-86b7d8c46d   1         1         1       18s

[root@k8s-master ~]# kubectl  get replicasets.apps
NAME                      DESIRED   CURRENT   READY   AGE
nginx-deploy-86b7d8c46d   1         1         1       38s

2.1.4 查看pod 信息

可以查看到deployment创建的pod种包含 replicasets 资源的名字信息

# 下面的pod可以查看到pod的名字包含 replicasets 名字的信息
[root@k8s-master ~]# kubectl  get po
NAME                            READY   STATUS    RESTARTS   AGE
nginx-deploy-86b7d8c46d-78rj9   1/1     Running   0          46s

2.1.5 通过命令行模式创建的deployment 生成 yaml信息

# 通过创建好的deploy生成yaml文件 
[root@k8s-master ~]# kubectl get deploy nginx-deploy  -o yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  creationTimestamp: "2024-02-23T10:59:04Z"
  generation: 1
  labels:
    app: nginx-deploy
  name: nginx-deploy
  namespace: default
  resourceVersion: "235341"
  uid: def47aae-13f7-415a-a9d1-18ef72e5a925
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: nginx-deploy
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx-deploy
    spec:
      containers:
      - image: nginx:1.20
        imagePullPolicy: IfNotPresent
        name: nginx
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 1
  conditions:
  - lastTransitionTime: "2024-02-23T10:59:05Z"
    lastUpdateTime: "2024-02-23T10:59:05Z"
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  - lastTransitionTime: "2024-02-23T10:59:04Z"
    lastUpdateTime: "2024-02-23T10:59:05Z"
    message: ReplicaSet "nginx-deploy-86b7d8c46d" has successfully progressed.
    reason: NewReplicaSetAvailable
    status: "True"
    type: Progressing
  observedGeneration: 1
  readyReplicas: 1
  replicas: 1
  updatedReplicas: 1

2.1.6 查看po、rs、deploy中所有的label信息

通过以下信息,可以查看到:
deploy、rs、pod种都包含同一个label信息:app=nginx-deploy;
rs和pod中同时还有一个相同的pod标签信息:pod-template-hash=86b7d8c46d,这个标签信息就是为了我们能更方便的动态创建pod使用。

[root@k8s-master ~]# kubectl get po,rs,deploy  --show-labels
NAME                                READY   STATUS    RESTARTS   AGE   LABELS
pod/nginx-deploy-86b7d8c46d-78rj9   1/1     Running   0          30m   app=nginx-deploy,pod-template-hash=86b7d8c46d

NAME                                      DESIRED   CURRENT   READY   AGE   LABELS
replicaset.apps/nginx-deploy-86b7d8c46d   1         1         1       30m   app=nginx-deploy,pod-template-hash=86b7d8c46d

NAME                           READY   UP-TO-DATE   AVAILABLE   AGE   LABELS
deployment.apps/nginx-deploy   1/1     1            1           30m   app=nginx-deploy

2.1.7 通过(2.1.5)中生成的yaml信息创建yaml文件

通过命令行创建的deployment信息生成yaml信息,根据我们所需信息来创建yaml文件

apiVersion: apps/v1  # deployment api版本
kind: Deployment    # 资源类型为 Deployment
metadata:  # 原信息
  labels:  # 标签
    app: nginx-deploy  # 具体的标签信息:app=nginx-deploy   key: value 配置形式
  name: nginx-deploy   # deployment的名字
  namespace: default   # 所在的命名空间
spec:
  replicas: 1   # 期望副本数
  revisionHistoryLimit: 10   # 进行滚动更新后,保留的历史版本数量
  selector:  # 选择器,用于找到匹配的RS
    matchLabels:  # 按照标签匹配
      app: nginx-deploy   # 匹配的标签
  strategy:   #更新策略
    rollingUpdate:  # 滚动更新配置
      maxSurge: 25%  # 滚动更新时,更新的个数最多超过多少个期望副本数,或者比例
      maxUnavailable: 25%  # 进行滚动更新时,最大不可用比例更新比例,表示在所有副本数中,最多可以有多少个不更新成功
    type: RollingUpdate  # 更新类型,采用滚动更新
  template:   # pod 模板
    metadata:  # pod模板的元信息
      labels:   # pod模板的标签
        app: nginx-deploy   # pod模板的标签信息
    spec:      # pod 期望信息
      containers:    # pod 的容器信息
      - image: nginx:1.20    # 镜像信息
        imagePullPolicy: IfNotPresent   # 镜像拉取策略
        name: nginx        # 容器名字
      restartPolicy: Always   # pod的重启策略
      terminationGracePeriodSeconds: 30   # pod的过期时间

2.2 滚动更新

只有修改了deployment 配置文件文件中的template中的属性后,才会触发滚动更新操作

2.2.1 修改deploy中的其他属性查看pod是否会更新

[root@k8s-master ~]# kubectl get deployments --show-labels
NAME           READY   UP-TO-DATE   AVAILABLE   AGE   LABELS
nginx-deploy   1/1     1            1           44m   app=nginx-deploy


[root@k8s-master ~]# kubectl edit deployments nginx-deploy
deployment.apps/nginx-deploy edited

在这里插入图片描述

可以通过下面新的看到我们的deploy更新并没有导致pod更新,只是把新加的标签给更新到了deploy上。

[root@k8s-master ~]# kubectl get deployments --show-labels
NAME           READY   UP-TO-DATE   AVAILABLE   AGE   LABELS
nginx-deploy   1/1     1            1           49m   app=nginx-deploy,new-version=test

#  rs和pod并没有这个标签信息
[root@k8s-master ~]# kubectl get deployments,rs,pod  --show-labels
NAME                           READY   UP-TO-DATE   AVAILABLE   AGE   LABELS
deployment.apps/nginx-deploy   1/1     1            1           51m   app=nginx-deploy,new-version=test

NAME                                      DESIRED   CURRENT   READY   AGE   LABELS
replicaset.apps/nginx-deploy-86b7d8c46d   1         1         1       51m   app=nginx-deploy,pod-template-hash=86b7d8c46d

NAME                                READY   STATUS    RESTARTS   AGE   LABELS
pod/nginx-deploy-86b7d8c46d-78rj9   1/1     Running   0          51m   app=nginx-deploy,pod-template-hash=86b7d8c46d 

在这里插入图片描述

2.2.2 修改这个deploy的副本数

在这里插入图片描述

[root@k8s-master ~]# kubectl get deploy nginx-deploy --show-labels
NAME           READY   UP-TO-DATE   AVAILABLE   AGE   LABELS
nginx-deploy   3/3     3            3           55m   app=nginx-deploy,new-version=test
[root@k8s-master ~]# kubectl describe  deployments  nginx-deploy
Name:                   nginx-deploy
Namespace:              default
CreationTimestamp:      Fri, 23 Feb 2024 18:59:04 +0800
Labels:                 app=nginx-deploy
                        new-version=test
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=nginx-deploy
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-deploy
  Containers:
   nginx:
    Image:        nginx:1.20
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Progressing    True    NewReplicaSetAvailable
  Available      True    MinimumReplicasAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-deploy-86b7d8c46d (3/3 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  55m   deployment-controller  Scaled up replica set nginx-deploy-86b7d8c46d to 1
  Normal  ScalingReplicaSet  27s   deployment-controller  Scaled up replica set nginx-deploy-86b7d8c46d to 3 from 1
[root@k8s-master ~]#

通过增加副本数后,我们可以看到deploy变为了3,rs为1,pod为3,pod的模版使用的都是同一个模版。

[root@k8s-master ~]# kubectl get deployments,rs,po  --show-labels
NAME                           READY   UP-TO-DATE   AVAILABLE   AGE   LABELS
deployment.apps/nginx-deploy   3/3     3            3           57m   app=nginx-deploy,new-version=test

NAME                                      DESIRED   CURRENT   READY   AGE   LABELS
replicaset.apps/nginx-deploy-86b7d8c46d   3         3         3       57m   app=nginx-deploy,pod-template-hash=86b7d8c46d

NAME                                READY   STATUS    RESTARTS   AGE   LABELS
pod/nginx-deploy-86b7d8c46d-6cf95   1/1     Running   0          93s   app=nginx-deploy,pod-template-hash=86b7d8c46d
pod/nginx-deploy-86b7d8c46d-78rj9   1/1     Running   0          57m   app=nginx-deploy,pod-template-hash=86b7d8c46d
pod/nginx-deploy-86b7d8c46d-vjncw   1/1     Running   0          93s   app=nginx-deploy,pod-template-hash=86b7d8c46d

2.2.3 通过edit选项编辑template属性后我们看看会发生那些变化

在这里插入图片描述
通过实时监控deploy的时候可以看到deploy的变化

[root@k8s-master ~]# kubectl get deployments.apps  nginx-deploy  -w
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deploy   3/3     3            3           61m
nginx-deploy   3/3     3            3           61m
nginx-deploy   3/3     3            3           61m
nginx-deploy   3/3     0            3           61m
nginx-deploy   3/3     1            3           61m
nginx-deploy   4/3     1            4           62m
nginx-deploy   3/3     1            3           62m
nginx-deploy   3/3     2            3           62m
nginx-deploy   4/3     2            4           62m
nginx-deploy   3/3     2            3           62m
nginx-deploy   3/3     3            3           62m
nginx-deploy   4/3     3            4           62m
nginx-deploy   3/3     3            3           62m


# 通过这个命令可以看到更新的状态为成功了
[root@k8s-master ~]# kubectl rollout status deployment nginx-deploy
deployment "nginx-deploy" successfully rolled out

2.2.4 修改set选项编辑刚才的镜像信息看看更新如何

[root@k8s-master ~]# kubectl set  image  deployment/nginx-deploy   nginx=nginx:1.20
deployment.apps/nginx-deploy image updated


[root@k8s-master ~]# kubectl get deployments.apps  nginx-deploy  -w
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deploy   3/3     3            3           72m
nginx-deploy   3/3     3            3           72m
nginx-deploy   3/3     3            3           72m
nginx-deploy   3/3     0            3           72m
nginx-deploy   3/3     1            3           72m
nginx-deploy   4/3     1            4           72m
nginx-deploy   3/3     1            3           72m
nginx-deploy   3/3     2            3           72m
nginx-deploy   4/3     2            4           72m
nginx-deploy   3/3     2            3           72m
nginx-deploy   3/3     3            3           72m
nginx-deploy   4/3     3            4           72m
nginx-deploy   3/3     3            3           72m

在这里插入图片描述

通过如下信息,可以看到新的nginx pod信息可以看到他滚动更新到刚开始的rs信息

[root@k8s-master ~]# kubectl get rs
NAME                      DESIRED   CURRENT   READY   AGE
nginx-deploy-6bf65c4445   0         0         0       17m
nginx-deploy-86b7d8c46d   3         3         3       79m
[root@k8s-master ~]# kubectl get  pod
NAME                            READY   STATUS    RESTARTS   AGE
nginx-deploy-86b7d8c46d-4s98c   1/1     Running   0          7m17s
nginx-deploy-86b7d8c46d-7s9gz   1/1     Running   0          7m19s
nginx-deploy-86b7d8c46d-w8ljk   1/1     Running   0          7m15s

2.3 回滚操作

有时候你可能想回退一个Deployment,例如,当Deployment不稳定时,比如一直crash looping。
默认侍况下,kubernetes会在系统中保存前两次的Deployment的rollout历史记录,以便你可以随时会退(你可以修改revisionhistory limit来更改保存的revision数),

2.3.1 通过set选项修改一个错误的images信息

[root@k8s-master ~]# kubectl set  image  deployment/nginx-deploy   nginx=nginx:1.200
deployment.apps/nginx-deploy image updated



# 可以看到一个更新的这个nginx的状态是ImagePullBackOff
[root@k8s-master ~]# kubectl get pod  -w
NAME                            READY   STATUS             RESTARTS   AGE
nginx-deploy-85bc5c8fdb-h4rmn   0/1     ImagePullBackOff   0          83s
nginx-deploy-86b7d8c46d-4s98c   1/1     Running            0          25m
nginx-deploy-86b7d8c46d-7s9gz   1/1     Running            0          25m
nginx-deploy-86b7d8c46d-w8ljk   1/1     Running            0          25m

在这里插入图片描述

使用命令:kubectl describe pod nginx-deploy-85bc5c8fdb-h4rmn 可以查看到这个新的pod信息
在这里插入图片描述

2.3.2 通过edit选项查看这个deploy的信息

在这里插入图片描述

2.3.3 查看历史的版本信息

[root@k8s-master ~]# kubectl rollout history  deployment/nginx-deploy  --revision=0
deployment.apps/nginx-deploy
REVISION  CHANGE-CAUSE
2         <none>
3         <none>
4         <none>

2.3.4 查看历史更新的具体信息

[root@k8s-master ~]# kubectl rollout history  deployment/nginx-deploy  --revision=4
deployment.apps/nginx-deploy with revision #4
Pod Template:
  Labels:	app=nginx-deploy
	pod-template-hash=85bc5c8fdb
  Containers:
   nginx:
    Image:	nginx:1.200
    Port:	<none>
    Host Port:	<none>
    Environment:	<none>
    Mounts:	<none>
  Volumes:	<none>

[root@k8s-master ~]# kubectl rollout history  deployment/nginx-deploy  --revision=3
deployment.apps/nginx-deploy with revision #3
Pod Template:
  Labels:	app=nginx-deploy
	pod-template-hash=86b7d8c46d
  Containers:
   nginx:
    Image:	nginx:1.20
    Port:	<none>
    Host Port:	<none>
    Environment:	<none>
    Mounts:	<none>
  Volumes:	<none>

[root@k8s-master ~]# kubectl rollout history  deployment/nginx-deploy  --revision=2
deployment.apps/nginx-deploy with revision #2
Pod Template:
  Labels:	app=nginx-deploy
	pod-template-hash=6bf65c4445
  Containers:
   nginx:
    Image:	nginx:1.21
    Port:	<none>
    Host Port:	<none>
    Environment:	<none>
    Mounts:	<none>
  Volumes:	<none>

2.3.5 回滚为revision=3的版本

[root@k8s-master ~]# kubectl rollout undo  deployment/nginx-deploy   --to-revision=3
deployment.apps/nginx-deploy rolled back



[root@k8s-master ~]#   kubectl  rollout  status  deployments.apps  nginx-deploy
deployment "nginx-deploy" successfully rolled out

在这里插入图片描述
如下已经回滚为了revision=3的版本。

[root@k8s-master ~]# kubectl describe deployments nginx-deploy
Name:                   nginx-deploy
Namespace:              default
CreationTimestamp:      Fri, 23 Feb 2024 18:59:04 +0800
Labels:                 app=nginx-deploy
                        new-version=test
Annotations:            deployment.kubernetes.io/revision: 5
Selector:               app=nginx-deploy
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-deploy
  Containers:
   nginx:
    Image:        nginx:1.20
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-deploy-86b7d8c46d (3/3 replicas created)
Events:
  Type    Reason             Age                From                   Message
  ----    ------             ----               ----                   -------
  Normal  ScalingReplicaSet  57m                deployment-controller  Scaled up replica set nginx-deploy-86b7d8c46d to 3 from 1
  Normal  ScalingReplicaSet  51m                deployment-controller  Scaled up replica set nginx-deploy-6bf65c4445 to 1
  Normal  ScalingReplicaSet  50m                deployment-controller  Scaled down replica set nginx-deploy-86b7d8c46d to 2 from 3
  Normal  ScalingReplicaSet  50m                deployment-controller  Scaled up replica set nginx-deploy-6bf65c4445 to 2 from 1
  Normal  ScalingReplicaSet  49m                deployment-controller  Scaled down replica set nginx-deploy-86b7d8c46d to 1 from 2
  Normal  ScalingReplicaSet  49m                deployment-controller  Scaled up replica set nginx-deploy-6bf65c4445 to 3 from 2
  Normal  ScalingReplicaSet  49m                deployment-controller  Scaled down replica set nginx-deploy-86b7d8c46d to 0 from 1
  Normal  ScalingReplicaSet  40m                deployment-controller  Scaled up replica set nginx-deploy-86b7d8c46d to 1 from 0
  Normal  ScalingReplicaSet  40m                deployment-controller  Scaled down replica set nginx-deploy-6bf65c4445 to 2 from 3
  Normal  ScalingReplicaSet  40m (x4 over 40m)  deployment-controller  (combined from similar events): Scaled down replica set nginx-deploy-6bf65c4445 to 0 from 1
  Normal  ScalingReplicaSet  16m                deployment-controller  Scaled up replica set nginx-deploy-85bc5c8fdb to 1
  Normal  ScalingReplicaSet  116s               deployment-controller  Scaled down replica set nginx-deploy-85bc5c8fdb to 0 from 1

2.3.6 为啥可以回退为revision=3呢?

可以通过设置,spec.revisonHistoryLimit来指定deployment保留多少revision。如果revisonHistoryLimit设置为0,则不允许deployment回退了。

2.4 扩容和缩容

  • 扩缩容命令: kubectl scale --replicas=6 deployment nginx-deploy
  • 扩容和缩容一样,只需要通过–replicas 指定数量即可

2.4.1 扩容

# 目前有3个pod,扩容到6个
[root@k8s-master ~]# kubectl scale --replicas=6 deployment nginx-deploy
deployment.apps/nginx-deploy scaled
[root@k8s-master ~]# kubectl get deployments nginx-deploy
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deploy   6/6     6            6           5h43m
[root@k8s-master ~]# kubectl get rs
NAME                      DESIRED   CURRENT   READY   AGE
nginx-deploy-6bf65c4445   0         0         0       4h42m
nginx-deploy-85bc5c8fdb   0         0         0       4h7m
nginx-deploy-86b7d8c46d   6         6         6       5h43m
[root@k8s-master ~]# kubectl get pod
NAME                            READY   STATUS    RESTARTS   AGE
nginx-deploy-86b7d8c46d-4s98c   1/1     Running   0          4h31m
nginx-deploy-86b7d8c46d-7s9gz   1/1     Running   0          4h31m
nginx-deploy-86b7d8c46d-n2m6j   1/1     Running   0          33s
nginx-deploy-86b7d8c46d-w8ljk   1/1     Running   0          4h31m
nginx-deploy-86b7d8c46d-wgdnv   1/1     Running   0          33s
nginx-deploy-86b7d8c46d-z86rx   1/1     Running   0          33s

2.4.1 缩容

# 缩容,有6个pod,现在缩容到4个pod
[root@k8s-master ~]# kubectl scale --replicas=4  deployment nginx-deploy
deployment.apps/nginx-deploy scaled
[root@k8s-master ~]# kubectl get deployments nginx-deploy
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deploy   4/4     4            4           5h45m
[root@k8s-master ~]# kubectl get rs
NAME                      DESIRED   CURRENT   READY   AGE
nginx-deploy-6bf65c4445   0         0         0       4h44m
nginx-deploy-85bc5c8fdb   0         0         0       4h9m
nginx-deploy-86b7d8c46d   4         4         4       5h45m
[root@k8s-master ~]# kubectl get pod  -o wide
NAME                            READY   STATUS    RESTARTS   AGE     IP          NODE          NOMINATED NODE   READINESS GATES
nginx-deploy-86b7d8c46d-4s98c   1/1     Running   0          4h33m   10.2.2.12   k8s-node-01   <none>           <none>
nginx-deploy-86b7d8c46d-7s9gz   1/1     Running   0          4h33m   10.2.1.28   k8s-node-02   <none>           <none>
nginx-deploy-86b7d8c46d-w8ljk   1/1     Running   0          4h33m   10.2.1.29   k8s-node-02   <none>           <none>
nginx-deploy-86b7d8c46d-z86rx   1/1     Running   0          2m33s   10.2.2.14   k8s-node-01   <none>           <none>

2.5 暂停和恢复

由于每次对pod 的 template中的信息发生修改后,都会触发更新deployment操作,那么此时如果频繁修改信息,就会产生多次更新,而实际上只需要执行最后一次更新即可,当出现此类情况时我们就可以暂停deployment的 rollout。

2.5.1 给template添加一个最小cpu和内存的参数

在这里插入图片描述
在这里插入图片描述

2.5.2 暂停deploy更新

通过kubectl rollout pause deployment <name> 就可以实现暂停,直到你下次恢复后才会继续进行滚动更新。

# 暂时deploy更新
[root@k8s-master ~]# kubectl rollout pause deployment  nginx-deploy
deployment.apps/nginx-deploy paused

2.5.3 再次更新template,给cpu和内存添加一个最大参数

在这里插入图片描述

这次更新过之后,rs信息和pod信息都没有发生改变,是由于我们暂停了deploy的更新

[root@k8s-master ~]# kubectl edit  deploy  nginx-deploy
deployment.apps/nginx-deploy edited

[root@k8s-master ~]# kubectl get rs
NAME                      DESIRED   CURRENT   READY   AGE
nginx-deploy-6bf65c4445   0         0         0       5h7m
nginx-deploy-6dc7697cfb   3         3         3       8m33s
nginx-deploy-85bc5c8fdb   0         0         0       4h32m
nginx-deploy-86b7d8c46d   0         0         0       6h8m
[root@k8s-master ~]# kubectl get pod
NAME                            READY   STATUS    RESTARTS   AGE
nginx-deploy-6dc7697cfb-6p9mj   1/1     Running   0          8m38s
nginx-deploy-6dc7697cfb-lc472   1/1     Running   0          8m37s
nginx-deploy-6dc7697cfb-w295s   1/1     Running   0          8m35s

2.5.4 查看更新信息

通过下面的信息,可以看到刚才添加cpu和内存参数在更新历史列表里面没有显示,说明我们的deploy没有更新

[root@k8s-master ~]# kubectl rollout history  deployment nginx-deploy  --revision=0
deployment.apps/nginx-deploy
REVISION  CHANGE-CAUSE
2         <none>
4         <none>
5         <none>
6         <none>


[root@k8s-master ~]# kubectl rollout history  deployment nginx-deploy  --revision=6
deployment.apps/nginx-deploy with revision #6
Pod Template:
  Labels:	app=nginx-deploy
	pod-template-hash=6dc7697cfb
  Containers:
   nginx:
    Image:	nginx:1.20
    Port:	<none>
    Host Port:	<none>
    Requests:
      cpu:	100m
      memory:	128Mi
    Environment:	<none>
    Mounts:	<none>
  Volumes:	<none>

2.5.5 恢复deploy的更新

启动deploy的更新


[root@k8s-master ~]# kubectl rollout resume   deployment nginx-deploy
deployment.apps/nginx-deploy resumed

这里 rs 已经发生了改变

[root@k8s-master ~]# kubectl get rs
NAME                      DESIRED   CURRENT   READY   AGE
nginx-deploy-57bf686b9d   3         3         3       12s
nginx-deploy-6bf65c4445   0         0         0       5h15m
nginx-deploy-6dc7697cfb   0         0         0       17m
nginx-deploy-85bc5c8fdb   0         0         0       4h41m
nginx-deploy-86b7d8c46d   0         0         0       6h17m

刚才查询的历史更新版本只有4个,现在又多了一个

[root@k8s-master ~]# kubectl rollout history deployment nginx-deploy --revision=0
deployment.apps/nginx-deploy
REVISION  CHANGE-CAUSE
2         <none>
4         <none>
5         <none>
6         <none>
7         <none>

通过历史更新版本信息可以看到,我们添加的最大cpu和内存参数都发生了改变。

[root@k8s-master ~]# kubectl rollout history deployment nginx-deploy --revision=7
deployment.apps/nginx-deploy with revision #7
Pod Template:
  Labels:	app=nginx-deploy
	pod-template-hash=57bf686b9d
  Containers:
   nginx:
    Image:	nginx:1.20
    Port:	<none>
    Host Port:	<none>
    Limits:
      cpu:	500m
      memory:	512Mi
    Requests:
      cpu:	100m
      memory:	128Mi
    Environment:	<none>
    Mounts:	<none>
  Volumes:	<none>

2.6 配置文件(部署无状态服务nginx的yaml文件)

[root@k8s-master ~]# cat nginx-deploy.yaml
apiVersion: apps/v1  # deployment api版本
kind: Deployment    # 资源类型为 Deployment
metadata:  # 原信息
  labels:  # 标签
    app: nginx-deploy  # 具体的标签信息:app=nginx-deploy   key: value 配置形式
  name: nginx-deploy   # deployment的名字
  namespace: default   # 所在的命名空间
spec:
  replicas: 1   # 期望副本数
  revisionHistoryLimit: 10   # 进行滚动更新后,保留的历史版本数量
  selector:  # 选择器,用于找到匹配的RS
    matchLabels:  # 按照标签匹配
      app: nginx-deploy   # 匹配的标签
  strategy:   #更新策略
    rollingUpdate:  # 滚动更新配置
      maxSurge: 25%  # 滚动更新时,更新的个数最多超过多少个期望副本数,或者比例
      maxUnavailable: 25%  # 进行滚动更新时,最大不可用比例更新比例,表示在所有副本数中,最多可以有多少个不更新成功
    type: RollingUpdate  # 更新类型,采用滚动更新
  template:   # pod 模板
    metadata:  # pod模板的元信息
      labels:   # pod模板的标签
        app: nginx-deploy   # pod模板的标签信息
    spec:      # pod 期望信息
      containers:    # pod 的容器信息
      - image: nginx:1.20    # 镜像信息
        imagePullPolicy: IfNotPresent   # 镜像拉取策略
        name: nginx        # 容器名字
      restartPolicy: Always   # pod的重启策略
      terminationGracePeriodSeconds: 30   # pod的过期时间

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1469002.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【Ubuntu】通过网线连接两台电脑以实现局域网连接的方法

有时我们需要将多台计算机连接在一起&#xff0c;以便实现数据共享、资源访问等功能。本文将介绍如何通过网线连接两台运行Ubuntu操作系统的电脑&#xff0c;以便它们能够直接通信&#xff0c;从而实现局域网连接。 1. 准备工作 在开始之前&#xff0c;请准备好&#xff1a; …

Java毕业设计-基于ssm的共享型汽车租赁管理系统-第64期

获取源码资料&#xff0c;请移步从戎源码网&#xff1a;从戎源码网_专业的计算机毕业设计网站 项目介绍 基于ssm的共享型汽车租赁管理系统&#xff1a;有配套报告文档&#xff0c;前端jsp、jquery、bootstrap&#xff0c;后端 springmvc、spring、mybatis&#xff0c;集成汽车…

【JVM】StringTable 字符串常量池

目录 一、字符串常量池 二、String的不可变性 三、String的内存分配 四、intern() 方法与 new String() 一、字符串常量池 JVM的字符串常量池&#xff08;String Constant Pool&#xff09;是一块位于方法区&#xff08;Method Area&#xff09;的内存区域&#xff0c;用于…

四、深入学习TensorRT,Developer Guide篇(三)

上一篇文章我们一起看了下TensorRT有哪些特性或者支持哪些功能&#xff0c;这一节我们来详细的从API出发研究一下具体的实现&#xff0c;难度要上升了哦&#xff0c;请系好安全带&#xff0c;准备发车&#xff01; 文章目录 3. The C API3.1 The Build Phase3.1.1 Creating a …

10-pytorch-完整模型训练

b站小土堆pytorch教程学习笔记 一、从零开始构建自己的神经网络 1.模型构建 #准备数据集 import torch import torchvision from torch.utils.tensorboard import SummaryWriterfrom model import * from torch.utils.data import DataLoadertrain_datatorchvision.datasets.…

【深度学习】Pytorch 教程(十一):PyTorch数据结构:4、张量操作(2):索引和切片操作

文章目录 一、前言二、实验环境三、PyTorch数据结构1、Tensor&#xff08;张量&#xff09;1. 维度&#xff08;Dimensions&#xff09;2. 数据类型&#xff08;Data Types&#xff09;3. GPU加速&#xff08;GPU Acceleration&#xff09; 2、张量的数学运算1. 向量运算2. 矩阵…

企业如何定制化“可靠的”系统,实现数字化转型?

二十大提出高质量发展是首要任务&#xff0c;为顺应数字经济时代的发展&#xff0c;数字化转型正不断赋能各行各业。越来越多的企业管理者也意识到数字化转型是帮助企业提升内部运营效率&#xff0c;提升业务开展效率&#xff0c;减低企业成本的有效手段。 那么如何推动企业数字…

linux前端部署

安装jdk 配置环境变量 刷新配置文件 source profile source /etc/profile tomcat 解压文件 进去文件启动tomcat 开放tomcat的端口号 访问 curl localhsot:8080 改配置文件 改IP,改数据库名字&#xff0c;密码&#xff0c; 安装数据库 将war包拖进去 访问http:…

wpf 3d 后台加载模型和调整参数

下载了一个代码&#xff0c;加载obj模型&#xff1b;它的参数在xaml里&#xff0c;模型加载出来刚好&#xff1b; 然后加载另一个obj模型&#xff1b;加载出来之后大&#xff0c;偏到很高和左的位置&#xff1b; 它之前的摄像机位置&#xff0c; Position"9.94759830064…

橘子学es原理01之准备工作

es本身是具备很好的使用特性的&#xff0c;我指的是他的部署方面的&#xff0c;至于后期的使用和运维那还是很一眼难尽的。 我们从这一篇开始就着重于es的一些原理性的的一些探讨&#xff0c;当然我们也会有一些操作性的&#xff0c;业务性的会分为多个栏目来写。比如前面我写的…

AutoSAR(基础入门篇)10.8-实验:模式管理

目录 一、配置BswM 二、配置唤醒源 三、配置ComM Users 四、配置BswM的通信控制 五、Service Mapping 首先备份上一次的工程&#xff0c;养成好习惯&#xff08;最好还是用Git&#xff0c;这次最后再安利一下Git这个神器&#xff09;。今天的实验异常的简单&#xff0c;基…

SpringMVC 学习(三)之 @RequestMapping 注解

目录 1 RequestMapping 注解介绍 2 RequestMapping 注解的位置 3 RequestMapping 注解的 value 属性 4 RequestMapping 注解的 method 属性 5 RequestMapping 注解的 params 属性&#xff08;了解&#xff09; 6 RequestMapping 注解的 headers 属性&#xff08;了解&…

CSS三大定位方式(浮动、定位、弹性盒)详细解析

CSS三大定位方式 前言&#xff1a;作为一名前端开发&#xff0c;已经工作2年了。由于自己是半路出家&#xff0c;从嵌入式方向转到前端开发&#xff0c;都是边百度边开发&#xff0c;很多基础都不了解&#xff0c;只要解决问题就好&#xff0c;但是近来为了让自己知识体系化&a…

【数据结构(顺序表)】

一、什么是数据结构? 数据结构是由“数据”和“结构”两词组合而来。 什么是数据&#xff1f;常见的数值1、2、3、4.....、教务系统里保存的用户信息&#xff08;姓名、性别、年龄、学历等等&#xff09;、网页里肉眼可以看到的信息&#xff08;文字、图片、视频等等&#xff…

Yolo v9 “Silence”模块结构及作用!

论文链接&#xff1a;&#x1f47f; YOLOv9: Learning What You Want to Learn Using Programmable Gradient Information 代码链接&#xff1a;&#x1f47f; https://github.com/WongKinYiu/yolov9/tree/main Silence代码 class Silence(nn.Module):def __init__(self):supe…

Mysql运维篇(四) MHA

大佬博文 https://www.cnblogs.com/gomysql/p/3675429.html MySQL 高可用&#xff08;MHA&#xff09; - 知乎 一、MHA简介&#xff1a; MHA&#xff08;Master High Availability&#xff09;目前在MySQL高可用方面是一个相对成熟的解决方案&#xff0c;它由日本DeNA公司y…

【Linux进程】进程状态---进程僵尸与孤儿

&#x1f4d9; 作者简介 &#xff1a;RO-BERRY &#x1f4d7; 学习方向&#xff1a;致力于C、C、数据结构、TCP/IP、数据库等等一系列知识 &#x1f4d2; 日后方向 : 偏向于CPP开发以及大数据方向&#xff0c;欢迎各位关注&#xff0c;谢谢各位的支持 目录 1.进程排队2.进程状态…

uni-app nvue vue3 setup中实现加载webview,解决nvue中获取不到webview实例的问题

注意下面的方法只能在app端使用&#xff0c; let wv plus.webview.create("","custom-webview",{plusrequire:"none", uni-app: none, width: 300,height:400,top:uni.getSystemInfoSync().statusBarHeight44 }) wv.loadURL("https://ww…

Sentinel微服务流量治理组件实战上

目录 分布式系统遇到的问题 解决方案 Sentinel 是什么&#xff1f; Sentinel 工作原理 Sentinel 功能和设计理念 流量控制 熔断降级 Sentinel工作主流程 Sentinel快速开始 Sentinel资源保护的方式 基于API实现 SentinelResource注解实现 Spring Cloud Alibaba整合…

《隐私计算简易速速上手小册》第7章:隐私计算与云计算/边缘计算(2024 最新版)

文章目录 7.1 云计算中的隐私保护7.1.1 基础知识7.1.2 主要案例:使用 Python 实现云数据的安全上传和访问7.1.3 拓展案例 1:实现基于角色的访问控制7.1.4 拓展案例 2:使用 Python 保护 API 安全7.2 边缘计算的隐私问题7.2.1 基础知识7.2.2 主要案例:使用 Python 实现边缘设…