貔貅云原生

貔貅云原生

Deployment

56
0
0
2023-12-04
Deployment

Deployment

Deployment 资源类型:

# nginx-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  namespace: default
  labels:
    app: deployment  # 此处标签是标记这个 Deployment 的,并不会与下方的 Pod 关联
spec:
  replicas: 3  # 期望的 Pod 副本数量,默认值为1
  selector:  # Label Selector,必须匹配 Pod 模板中的标签,通过这个标签关联下方 Pod 的标签
    matchLabels:
      app: nginx
  template:  # Pod 模板
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:  # 端口类型没有实际作用,只是用作显示
        - containerPort: 80

创建这个资源对象:

[root@kube01 ~]# kubectl apply -f nginx-deployment.yaml
deployment.apps/nginx created

通过标签可以过滤查看 Pod

[root@kube01 ~]# kubectl get pod -l app=nginx
NAME                     READY   STATUS    RESTARTS   AGE
nginx-74d589986c-64rpq   1/1     Running   0          4m41s
nginx-74d589986c-h9w46   1/1     Running   0          4m41s
nginx-74d589986c-kz2mc   1/1     Running   0          4m41s

通过 describe 找一个 Pod 查看一下,发现 Pod 的 Controlled By 是 ReplicaSet

[root@kube01 ~]# kubectl  describe pod nginx-74d589986c-64rpq
Name:         nginx-74d589986c-64rpq
Namespace:    default
Priority:     0
Node:         kube02/192.168.17.6
Start Time:   Mon, 12 Jun 2023 14:56:35 +0800
Labels:       app=nginx
              pod-template-hash=74d589986c
Annotations:  <none>
Status:       Running
IP:           172.30.1.12
IPs:
  IP:           172.30.1.12
Controlled By:  ReplicaSet/nginx-74d589986c
Containers:
  nginx:
    Container ID:   docker://930f0e3d2f16d2a6060efa8a44ace81a1c81baf2999fef106e207588a759561e
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Mon, 12 Jun 2023 14:57:14 +0800
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-t8wsn (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  kube-api-access-t8wsn:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age    From               Message
  ----    ------     ----   ----               -------
  Normal  Scheduled  7m39s  default-scheduler  Successfully assigned default/nginx-74d589986c-64rpq to kube02
  Normal  Pulling    7m37s  kubelet            Pulling image "nginx"
  Normal  Pulled     6m59s  kubelet            Successfully pulled image "nginx" in 37.872905558s
  Normal  Created    6m59s  kubelet            Created container nginx
  Normal  Started    6m59s  kubelet            Started container nginx

再查看一下 ReplicaSet 发现 Controlled By 属于 Deployment,说明 Deployment 不直接参与 Pod 的控制。

[root@kube01 ~]# kubectl get rs
NAME               DESIRED   CURRENT   READY   AGE
nginx-74d589986c   3         3         3       9m46s
[root@kube01 ~]# kubectl describe rs nginx-74d589986c
Name:           nginx-74d589986c
Namespace:      default
Selector:       app=nginx,pod-template-hash=74d589986c
Labels:         app=nginx
                pod-template-hash=74d589986c
Annotations:    deployment.kubernetes.io/desired-replicas: 3
                deployment.kubernetes.io/max-replicas: 4
                deployment.kubernetes.io/revision: 1
Controlled By:  Deployment/nginx
Replicas:       3 current / 3 desired
Pods Status:    3 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=nginx
           pod-template-hash=74d589986c
  Containers:
   nginx:
    Image:        nginx
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age    From                   Message
  ----    ------            ----   ----                   -------
  Normal  SuccessfulCreate  9m57s  replicaset-controller  Created pod: nginx-74d589986c-64rpq
  Normal  SuccessfulCreate  9m57s  replicaset-controller  Created pod: nginx-74d589986c-kz2mc
  Normal  SuccessfulCreate  9m57s  replicaset-controller  Created pod: nginx-74d589986c-h9w46

通过观察发现,控制方式为 Deployment -> ReplicaSet -> Pod
image-ebqh.png

水平伸缩

通过 kubectl 的 scale 快捷命令扩容副本数量为 4 个,查看发现 deployment 的数据已经改为 4 个

[root@kube01 ~]# kubectl scale deployment nginx --replicas=4
deployment.apps/nginx scaled

[root@kube01 ~]# kubectl get deployment
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   4/4     4            4           25m

再次查看 ReplicaSet, 发现 ReplicaSet 也变成了 4 个

[root@kube01 ~]# kubectl get rs
NAME               DESIRED   CURRENT   READY   AGE
nginx-74d589986c   4         4         4       25m

再次查看 Pod, 发现 Pod 也变成了 4 个

[root@kube01 ~]# kubectl get pod -l app=nginx
NAME                     READY   STATUS    RESTARTS   AGE
nginx-74d589986c-64rpq   1/1     Running   0          28m
nginx-74d589986c-h9w46   1/1     Running   0          28m
nginx-74d589986c-kz2mc   1/1     Running   0          28m
nginx-74d589986c-rm759   1/1     Running   0          4m19s

滚动更新

Deployment 支持滚动更新RollingUpdate,就是所谓的新增一个,删除一个循环执行,保持 Pod 永远有可用状态的,这种适用于版本之间的切换。

apiVersion: apps/v1
kind: Deployment
metadata:
  name:  nginx
  namespace: default
  labels:
    app: deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  minReadySeconds: 5
  strategy:
    type: RollingUpdate  # 指定更新策略:RollingUpdate和Recreate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80
  • minReadySeconds:表示 Kubernetes 在等待设置的时间后才进行升级,如果没有设置该值,Kubernetes 会假设该容器启动起来后就提供服务了,如果没有设置该值,在某些极端情况下可能会造成服务不正常运行,默认值就是0。
  • type=RollingUpdate:表示设置更新策略为滚动更新,可以设置为Recreate和RollingUpdate两个值,Recreate表示全部重新创建,默认值就是RollingUpdate。
  • maxSurge:表示升级过程中最多可以比原先设置多出的 Pod 数量,例如:maxSurage=1,replicas=5,就表示Kubernetes 会先启动一个新的 Pod,然后才删掉一个旧的 Pod,整个升级过程中最多会有5+1个 Pod。
  • maxUnavaible:表示升级过程中最多有多少个 Pod 处于无法提供服务的状态,当maxSurge不为0时,该值也不能为0,例如:maxUnavaible=1,则表示 Kubernetes 整个升级过程中最多会有1个 Pod 处于无法服务的状态。

通过更新部署文件实现滚动更新

[root@kube01 ~]# kubectl apply -f nginx-deployment.yaml
deployment.apps/nginx configured

通过 watch 滚动更新的状态,会发现滚动更新的一个现状

[root@kube01 ~]# kubect rollout status deployment/nginx -w
Waiting for deployment "nginx" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx" rollout to finish: 2 of 3 updated replicas are available...
Waiting for deployment "nginx" rollout to finish: 2 of 3 updated replicas are available...
deployment "nginx" successfully rolled out

通过滚动更新可以通过 pause 与 resume,进行暂停、恢复的操作。

[root@kube01 ~]# kubectl rollout pause deployment/nginx
deployment.apps/nginx paused

[root@kube01 ~]# kubectl  rollout resume deployment/nginx
deployment.apps/nginx resumed

在滚动更新时通过 describe 查看 deployment 信息,发现存在OldReplicaSetsNewReplicaSet状态,判断出滚动更新是新建一个 ReplicaSet,删除OldReplicaSet 里的一个副本,再新建一个NewReplicaSet里的一个副本,知道更新完成为止。

[root@kube01 ~]# kubectl describe deployment nginx
Name:                   nginx
Namespace:              default
CreationTimestamp:      Mon, 12 Jun 2023 15:57:06 +0800
Labels:                 app=deployment
Annotations:            deployment.kubernetes.io/revision: 4
Selector:               app=nginx
Replicas:               3 desired | 3 updated | 4 total | 3 available | 1 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        5
RollingUpdateStrategy:  1 max unavailable, 1 max surge
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx:1.7.9
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status   Reason
  ----           ------   ------
  Available      True     MinimumReplicasAvailable
  Progressing    Unknown  DeploymentPaused
OldReplicaSets:  nginx-74d589986c (1/1 replicas created)
NewReplicaSet:   nginx-5fcc5d8c6d (3/3 replicas created)
Events:
  Type    Reason             Age                  From                   Message
  ----    ------             ----                 ----                   -------
  Normal  ScalingReplicaSet  8m14s                deployment-controller  Scaled up replica set nginx-74d589986c to 3
  Normal  ScalingReplicaSet  7m1s                 deployment-controller  Scaled up replica set nginx-5fcc5d8c6d to 1
  Normal  ScalingReplicaSet  6m39s                deployment-controller  Scaled up replica set nginx-5fcc5d8c6d to 3
  Normal  ScalingReplicaSet  6m22s                deployment-controller  Scaled down replica set nginx-74d589986c to 0
  Normal  ScalingReplicaSet  4m16s                deployment-controller  Scaled down replica set nginx-5fcc5d8c6d to 2
  Normal  ScalingReplicaSet  4m16s                deployment-controller  Scaled up replica set nginx-74d589986c to 1
  Normal  ScalingReplicaSet  26s (x2 over 7m1s)   deployment-controller  Scaled up replica set nginx-5fcc5d8c6d to 2
  Normal  ScalingReplicaSet  26s (x2 over 7m1s)   deployment-controller  Scaled down replica set nginx-74d589986c to 2
  Normal  ScalingReplicaSet  18s (x2 over 6m39s)  deployment-controller  Scaled down replica set nginx-74d589986c to 1
  Normal  ScalingReplicaSet  18s (x6 over 4m16s)  deployment-controller  (combined from similar events): Scaled up replica set nginx-5fcc5d8c6d to 3

image-wxtc.png

再查看 ReplicaSet 后发现,确实有两个 ReplicaSet

[root@kube01 ~]# kubectl get rs
NAME               DESIRED   CURRENT   READY   AGE
nginx-5fcc5d8c6d   3         3         3       8m42s
nginx-74d589986c   0         0         0       9m55s

滚动更新的 deployment 有历史记录,通过 history 查看历史记录,通过 revision 可以查看历史版本

[root@kube01 ~]# kubectl rollout history deployment nginx
deployment.apps/nginx
REVISION  CHANGE-CAUSE
3         <none>
4         <none>

可以指定 reversion 查看历史版本信息

[root@kube01 ~]# kubectl rollout history deployment nginx --revision=3
deployment.apps/nginx with revision #3
Pod Template:
  Labels:	app=nginx
	pod-template-hash=74d589986c
  Containers:
   nginx:
    Image:	nginx
    Port:	80/TCP
    Host Port:	0/TCP
    Environment:	<none>
    Mounts:	<none>
  Volumes:	<none>

假如现在要直接回退到当前版本的前一个版本,我们可以直接使用如下命令进行操作:

[root@kube01 ~]# kubectl rollout undo deployment nginx
deployment.apps/nginx rolled back

也可以指定版本回退

[root@kube01 ~]# kubectl rollout undo deployment nginx --to-revision=4
deployment.apps/nginx rolled back