【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.mzph.cn/news/703374.shtml

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

相关文章

2024生物科学、医学技术与化学国际会议(ICBSMTC2024)

2024生物科学、医学技术与化学国际会议(ICBSMTC2024) 会议简介 ICBSMTC2024是一个聚焦于生物科学、医学技术与化学领域的学术交流活动&#xff0c;会议将在中国桂林举行&#xff0c;会议旨在促进相关领域的学术交流与发展。会议将汇集来自世界各地的顶级学者和专家&#xf…

人工智能_CPU微调ChatGLM大模型_使用P-Tuning v2进行大模型微调_007_微调_002---人工智能工作笔记0102

这里我们先试着训练一下,我们用官方提供的训练数据进行训练. 也没有说使用CPU可以进行微调,但是我们先执行一下试试: https://www.heywhale.com/mw/project/6436d82948f7da1fee2be59e 可以看到说INT4量化级别最低需要7GB显存可以启动微调,但是 并没有说CPU可以进行微调.我们…

【b站咸虾米】chapter5_uniapp-API_新课uniapp零基础入门到项目打包(微信小程序/H5/vue/安卓apk)全掌握

课程地址&#xff1a;【新课uniapp零基础入门到项目打包&#xff08;微信小程序/H5/vue/安卓apk&#xff09;全掌握】 https://www.bilibili.com/video/BV1mT411K7nW/?p12&share_sourcecopy_web&vd_sourceb1cb921b73fe3808550eaf2224d1c155 目录 5 API 5.1 页面和路…

Linux 文件操作

目录 C语言下的文件操作 Linux下的文件操作 文件描述符的前因后果 文件描述符的概念 文件描述符的分配规则 理解C语言的FILE结构体 Linux重定向 文件缓冲区 文件系统 文件系统的概念 ext2文件系统 对ext2的补充 虚拟文件系统的概念 软硬链接 C语言下的文件操作 …

备战蓝桥杯---基础算法刷题2

题目有一点水&#xff0c;不过还是有几个好题的&#xff0c;我在这分享一下&#xff1a; 很容易想到先往最高处跳再往最低处跳&#xff0c;依次类推&#xff0c;那怎么保证其正确性呢&#xff1f; 证法1. 在此&#xff0c;我们从0开始&#xff0c;假设可以跳到a,b,c(a<b<…

UE学习笔记-- bUseUnity 加速编译 及 踩坑记录

前言 在写 UE 项目时&#xff0c;使用 VS 2022 进行编译&#xff0c;发现编译不过。 原因是少了头文件&#xff0c;导致某些了类型缺失。 问题发现 但是很奇怪的是&#xff0c;以前编译没问题&#xff0c;在原本应该出问题的 cpp 文件里面加了一行注释之后&#xff0c;编译就…

嵌入式学习day25 Linux

进程基本概念: 1.进程: 程序&#xff1a;存放在外存中的一段数据组成的文件 进程&#xff1a;是一个程序动态执行的过程,包括进程的创建、进程的调度、进程的消亡 2.进程相关命令: 1.top 动态查看当前系统中的所有进程信息&#xff08;根据CPU占用率排序&a…

查看仓库版本记录

打开命令行窗口 输入git log即可。 若发现分支不对&#xff0c;方法如下 查看项目目录&#xff0c;命令行输入dir可以查看 多个moudel&#xff0c;进入到需要查版本记录的moudel下 命令行输入cd .\文件名如wowo-win-server\ 切换到wowo-win-server文件夹下后&#xff0c;再输入…

C语言内存管理-C进程内存布局

C进程内存布局 任何一个程序&#xff0c;正常运行都需要内存资源&#xff0c;用来存放诸如变量、常量、函数代码等等。这些不同的内容&#xff0c;所存储的内存区域是不同的&#xff0c;且不同的区域有不同的特性。因此我们需要研究C语言进程的内存布局&#xff0c;逐个了解不…

sonar-java 手写一个规则-单元测试分析

前言 最近做项目&#xff0c;定制sonar规则&#xff0c;提高Java代码质量&#xff0c;在编写的sonar规则&#xff0c;做验证时&#xff0c;使用单元测试有一些简单的心得感悟&#xff0c;分享出来。 自定义规则模式 sonar的自定义规则很简单&#xff0c;一般而言有2种模式可…

MySQL多实例与Mycat分布式读写分离的架构实践

文章目录 1.Mycat读写分离分布式架构规划2.在两台服务器中搭建八个MySQL实例2.1.安装MySQL软件2.2.创建每个MySQL实例的数据目录并初始化2.3.准备每个实例的配置文件2.4.准备每个实例的启动脚本2.6启动每台机器的MySQL多实例2.7.为每个MySQL实例设置密码2.8.查看每个MySQL实例的…

使用GPT生成python图表

首先&#xff0c;生成一脚本&#xff0c;读取到所需的excel表格 import xlrddata xlrd.open_workbook(xxxx.xls) # 打开xls文件 table data.sheet_by_index(0) # 通过索引获取表格# 初始化奖项字典 awards_dict {"一等奖": 0,"二等奖": 0,"三等…

three中界面交互gui.js库的使用

gui.js库(可视化改变三维场景) dat.gui.js说白了就是一个前端js库&#xff0c;对HTML、CSS和JavaScript进行了封装&#xff0c;学习开发的时候&#xff0c;借助dat.gui.js可以快速创建控制三维场景的UI交互界面&#xff0c;你打开课件中案例源码体验一下就能感受到。 学习dat…

一元函数微分学——刷题(22

目录 1.题目&#xff1a;2.解题思路和步骤&#xff1a;3.总结&#xff1a;小结&#xff1a; 1.题目&#xff1a; 2.解题思路和步骤&#xff1a; 由于是极坐标方程&#xff0c;所以这个式子一定成立&#xff1a; 然后代入r即可变为参数方程的求导&#xff1a; 3.总结&#xff…

【Linux运维系列】vim操作

&#x1f49d;&#x1f49d;&#x1f49d;欢迎来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅所欲言、分享您的想法和见解。 推荐:kwan 的首页,持续学…

linux系统消息中间件rabbitmq普通集群的部署

rabbitmq普通集群的部署 普通集群准备环境查询版本对应安装rabbitmq软件启动创建登录用户开启用户远程登录查看端口 部署集群创建数据存放目录和日志存放目录:拷⻉erlang.cookie将其他两台服务器作为节点加⼊节点集群中查看集群状态创建新的队列 普通集群准备环境 配置hosts⽂件…

Matlab自学笔记二十七:详解格式化文本sprintf各参数设置方法

1.一个程序引例 上篇文章已经介绍了格式化文本的初步应用&#xff0c;程序示例如下&#xff1a; sprintf(|%f\n|%.2f\n|%8.2f,pi*ones(1,3)) 2.格式化操作符各字段的含义解析 格式化操作符可以有六个字段&#xff0c;只有主字符%和转换格式是必需的&#xff0c;其他都是可选…

数据库安全性与完整性设计

文章标签集合[数据库安全,数据敏感,通信安全,MD5,盐加密] 1 系统设计 1.1设计目标 &#xff08;1&#xff09;确定系统中需要保护的敏感数据和通信内容&#xff1b; &#xff08;2&#xff09;设计合适的签名、加密和解密算法&#xff1b; &#xff08;3&#xff09;实现…

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

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

DAY33--learning English

一、积累 1.testify 2.sanctuary 3.assauslt 4.grocery 5.peg 6.figure 7.carton 8.bruise 9.dangle 10.script 11.lad 12.manipulate 13.molecule 14.shuttle 15.gutter 16.drastic 17.wag 18.rear 19.nail clipper 20.cereal 二、练习 1.牛津原译 Testify v. /ˈtestɪfaɪ…