【k8s资源调度-Deployment】

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: 1labels:app: nginx-deployname: nginx-deploynamespace: defaultresourceVersion: "235341"uid: def47aae-13f7-415a-a9d1-18ef72e5a925
spec:progressDeadlineSeconds: 600replicas: 1revisionHistoryLimit: 10selector:matchLabels:app: nginx-deploystrategy:rollingUpdate:maxSurge: 25%maxUnavailable: 25%type: RollingUpdatetemplate:metadata:creationTimestamp: nulllabels:app: nginx-deployspec:containers:- image: nginx:1.20imagePullPolicy: IfNotPresentname: nginxresources: {}terminationMessagePath: /dev/termination-logterminationMessagePolicy: FilednsPolicy: ClusterFirstrestartPolicy: AlwaysschedulerName: default-schedulersecurityContext: {}terminationGracePeriodSeconds: 30
status:availableReplicas: 1conditions:- lastTransitionTime: "2024-02-23T10:59:05Z"lastUpdateTime: "2024-02-23T10:59:05Z"message: Deployment has minimum availability.reason: MinimumReplicasAvailablestatus: "True"type: Available- lastTransitionTime: "2024-02-23T10:59:04Z"lastUpdateTime: "2024-02-23T10:59:05Z"message: ReplicaSet "nginx-deploy-86b7d8c46d" has successfully progressed.reason: NewReplicaSetAvailablestatus: "True"type: ProgressingobservedGeneration: 1readyReplicas: 1replicas: 1updatedReplicas: 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=86b7d8c46dNAME                                      DESIRED   CURRENT   READY   AGE   LABELS
replicaset.apps/nginx-deploy-86b7d8c46d   1         1         1       30m   app=nginx-deploy,pod-template-hash=86b7d8c46dNAME                           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:  # 选择器,用于找到匹配的RSmatchLabels:  # 按照标签匹配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=testNAME                                      DESIRED   CURRENT   READY   AGE   LABELS
replicaset.apps/nginx-deploy-86b7d8c46d   1         1         1       51m   app=nginx-deploy,pod-template-hash=86b7d8c46dNAME                                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-deploynew-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-deployContainers:nginx:Image:        nginx:1.20Port:         <none>Host Port:    <none>Environment:  <none>Mounts:       <none>Volumes:        <none>
Conditions:Type           Status  Reason----           ------  ------Progressing    True    NewReplicaSetAvailableAvailable      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 1Normal  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=testNAME                                      DESIRED   CURRENT   READY   AGE   LABELS
replicaset.apps/nginx-deploy-86b7d8c46d   3         3         3       57m   app=nginx-deploy,pod-template-hash=86b7d8c46dNAME                                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-deploypod-template-hash=85bc5c8fdbContainers:nginx:Image:	nginx:1.200Port:	<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-deploypod-template-hash=86b7d8c46dContainers:nginx:Image:	nginx:1.20Port:	<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-deploypod-template-hash=6bf65c4445Containers:nginx:Image:	nginx:1.21Port:	<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-deploynew-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-deployContainers:nginx:Image:        nginx:1.20Port:         <none>Host Port:    <none>Environment:  <none>Mounts:       <none>Volumes:        <none>
Conditions:Type           Status  Reason----           ------  ------Available      True    MinimumReplicasAvailableProgressing    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 1Normal  ScalingReplicaSet  51m                deployment-controller  Scaled up replica set nginx-deploy-6bf65c4445 to 1Normal  ScalingReplicaSet  50m                deployment-controller  Scaled down replica set nginx-deploy-86b7d8c46d to 2 from 3Normal  ScalingReplicaSet  50m                deployment-controller  Scaled up replica set nginx-deploy-6bf65c4445 to 2 from 1Normal  ScalingReplicaSet  49m                deployment-controller  Scaled down replica set nginx-deploy-86b7d8c46d to 1 from 2Normal  ScalingReplicaSet  49m                deployment-controller  Scaled up replica set nginx-deploy-6bf65c4445 to 3 from 2Normal  ScalingReplicaSet  49m                deployment-controller  Scaled down replica set nginx-deploy-86b7d8c46d to 0 from 1Normal  ScalingReplicaSet  40m                deployment-controller  Scaled up replica set nginx-deploy-86b7d8c46d to 1 from 0Normal  ScalingReplicaSet  40m                deployment-controller  Scaled down replica set nginx-deploy-6bf65c4445 to 2 from 3Normal  ScalingReplicaSet  40m (x4 over 40m)  deployment-controller  (combined from similar events): Scaled down replica set nginx-deploy-6bf65c4445 to 0 from 1Normal  ScalingReplicaSet  16m                deployment-controller  Scaled up replica set nginx-deploy-85bc5c8fdb to 1Normal  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-deploypod-template-hash=6dc7697cfbContainers:nginx:Image:	nginx:1.20Port:	<none>Host Port:	<none>Requests:cpu:	100mmemory:	128MiEnvironment:	<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-deploypod-template-hash=57bf686b9dContainers:nginx:Image:	nginx:1.20Port:	<none>Host Port:	<none>Limits:cpu:	500mmemory:	512MiRequests:cpu:	100mmemory:	128MiEnvironment:	<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:  # 选择器,用于找到匹配的RSmatchLabels:  # 按照标签匹配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.rhkb.cn/news/264520.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

IO进程:信号灯集

程序代码&#xff1a; sem.h: 1 #ifndef __SEM_H__2 #define __SEM_H__3 4 //创建信号灯集并初始化&#xff0c;semcount表示灯个数5 int open_sem(int semcount);6 7 //申请资源操作&#xff0c;semno表示灯的编号8 int P(int semid,int semno);9 10 //释放资源操作&#xff…

uniapp播放mp4省流方案

背景&#xff1a; 因为项目要播放一个宣传和讲解视频&#xff0c;视频文件过大&#xff0c;同时还为了节省存储流量&#xff0c;想到了一个方案&#xff0c;用m3u8切片替代mp4。 m3u8&#xff1a;切片播放&#xff0c;可以理解为一个1G的视频文件&#xff0c;自行设置文…

【前端素材】推荐优质后台管理系统Be admin平台模板(附源码)

一、需求分析 后台管理系统&#xff08;或称作管理后台、管理系统、后台管理平台&#xff09;是一种专门用于管理网站、应用程序或系统后台运营的软件系统。它通常由一系列功能模块组成&#xff0c;为管理员提供了管理、监控和控制网站或应用程序的各个方面的工具和界面。以下…

Spring-IoC

文章目录 Spring IoC一、IoC概述1、概述2、IoC原理3、IoC作用4、Spring Bean 二、Spring容器 相关接口1、BeanFactory2、ApplicationContext1&#xff09;HierarchicalBeanFactory2&#xff09;ListableBeanFactory3&#xff09;EnvironmentCapable4&#xff09;ApplicationEve…

Web3 基金会推出去中心化之声计划:投入高额 DOT 和 KSM ,助力去中心化治理

作者&#xff1a;Web3 Foundation Team 编译&#xff1a;OneBlock 原文&#xff1a;https://medium.com/web3foundation/decentralized-voices-program-93623c27ae43 Web3 基金会为 Polkadot 和 Kusama 创建了去中心化之声计划&#xff08;Decentralized Voices Program&…

软考39-上午题-【数据库】-关系代数运算1-传统的集合运算

一、笛卡尔积 二、关系代数 关系代数是施加于关系之上的集合代数运算。 关系代数包含&#xff1a; 传统的集合运算专门的关系运算 2-1、传统的集合运算 1、关系的并 示例&#xff1a; 2、关系的差 示例&#xff1a; 3、关系的交 示例&#xff1a; 关系的并、差、交&#xf…

LeetCode 448.找到所有数组中消失的数字

目录 1.题目 2.代码及思路 3.进阶 3.1题目 3.2代码及思路 1.题目 给你一个含 n 个整数的数组 nums &#xff0c;其中 nums[i] 在区间 [1, n] 内。请你找出所有在 [1, n] 范围内但没有出现在 nums 中的数字&#xff0c;并以数组的形式返回结果。 示例 1&#xff1a; 输入&am…

【数学建模入门】

数学建模入门 数学建模需要的学科知识怎么学习数学模型如何读好一篇优秀论文数学建模赛题常见类别数学建模常见问题数学建模组队和分工数学建模准备工作 数学建模需要的学科知识 怎么学习数学模型 &#x1f4a6;推荐阅读书籍&#xff1a; 《数学建模算法与应用》&#xff0c;…

车规级MCU的行业走向

1 主要厂家 车规级MCU&#xff08;车用微控制器单元&#xff09;的主要厂家包括&#xff1a; NXP半导体&#xff1a;NXP是全球领先的车规级MCU提供商之一&#xff0c;提供广泛的产品用于汽车控制和管理系统。英飞凌科技&#xff1a;作为汽车半导体的领导者之一&#xff0c;英飞…

fly-barrage 前端弹幕库(1):项目介绍

fly-barrage 是我写的一个前端弹幕库&#xff0c;由于经常在 Bilibili 上看视频&#xff0c;所以对网页的弹幕功能一直蛮感兴趣的&#xff0c;所以做了这个库&#xff0c;可以帮助前端快速的实现弹幕功能。 项目官网地址&#xff1a;https://fly-barrage.netlify.app/&#xff…

旅游分享系列之:福建旅游攻略

旅游分享系列之&#xff1a;福建旅游攻略 一、漳州1.福建土楼2.云水谣3.四菜一汤景点 二、厦门1.园林博览苑2.海上自行车道3.山海步道4.海滩5.闽南菜6.落日 三、泉州1.衙口沙滩2.海上日出3.珞珈寺4.海滩烟花 一、漳州 游玩2个景点&#xff1a;云水谣&#xff0c;四菜一汤可以住…

《图解HTTP》笔记2:http的构成

目录 1&#xff0c;查看浏览器上面一个具体的http请求 2&#xff0c;HTTP报文的具体构成 2.1&#xff0c;http的报文结构 2.2&#xff0c;http的请求报文例子 2.3&#xff0c;http的响应报文例子 1&#xff0c;查看浏览器上面一个具体的http请求 浏览器地址栏输入网址&…

ES6内置对象 - Set

Set&#xff08;es6提供的一种数据结构&#xff0c;类似数组&#xff0c;是一个集合&#xff0c;可以存储任何类型的元素且唯一、不重复&#xff0c;so,多用于元素去重&#xff09; 如上图&#xff0c;Set数据结构自带一些方法 1.Set对象创建 let a new Set([1,2,3,3,1,2,4,…

spring boot3登录开发-2(1图形验证码接口实现)

⛰️个人主页: 蒾酒 &#x1f525;系列专栏&#xff1a;《spring boot实战》 &#x1f30a;山高路远&#xff0c;行路漫漫&#xff0c;终有归途。 目录 前置条件 内容简介 图形验证码接口实现 导入糊涂工具依赖 接口分析 编写验证码接口 测试验证码接口 前置条件 …

浅浅的画一个STDP的图像吧

stdp最重要的是两个窗口函数 根据这个方程我们刻画出他的轨迹&#xff0c;代码如下 import numpy as np import matplotlib.pyplot as plt# 定义STDP参数 tau_pos 30 # 正向突触权重变化的时间常数 tau_neg 30 # 负向突触权重变化的时间常数 A_pos 0.1 # 正向突触权重变…

都说了别用BeanUtils.copyProperties,这不翻车了吧

分享是最有效的学习方式。 博客&#xff1a;https://blog.ktdaddy.com/ 故事 新年新气象&#xff0c;小猫也是踏上了新年新征程&#xff0c;自从小猫按照老猫给的建议【系统梳理大法】完完整整地梳理完毕系统之后&#xff0c;小猫对整个系统的把控可谓又是上到可一个新的高度。…

[计算机网络]--IP协议

前言 作者&#xff1a;小蜗牛向前冲 名言&#xff1a;我可以接受失败&#xff0c;但我不能接受放弃 如果觉的博主的文章还不错的话&#xff0c;还请点赞&#xff0c;收藏&#xff0c;关注&#x1f440;支持博主。如果发现有问题的地方欢迎❀大家在评论区指正 目录 一、IP协议…

大数据构建知识图谱:从技术到实战的完整指南

文章目录 大数据构建知识图谱&#xff1a;从技术到实战的完整指南一、概述二、知识图谱的基础理论定义与分类核心组成历史与发展 三、知识获取与预处理数据源选择数据清洗实体识别 四、知识表示方法知识表示模型RDFOWL属性图模型 本体构建关系提取与表示 五、知识图谱构建技术图…

低功耗设计——门控时钟

1. 前言 芯片功耗组成中&#xff0c;有高达40%甚至更多是由时钟树消耗掉的。这个结果的原因也很直观&#xff0c;因为这些时钟树在系统中具有最高的切换频率&#xff0c;而且有很多时钟buffer&#xff0c;而且为了最小化时钟延时&#xff0c;它们通常具有很高的驱动强度。此外&…

2024Node.js零基础教程(小白友好型),nodejs新手到高手,(九)NodeJS入门——http模块

060_http模块_网页URL之绝对路径 hello&#xff0c;大家好&#xff0c;这一个小题的话我们来补充一个之前学习过的内容&#xff0c;就是网页当中的URL&#xff0c;咱们这个小题的话主要是来说一下绝对路径&#xff0c;有同学可能会说&#xff0c;这这这&#xff0c;不对劲&…