4. kubernetes资源——deployment无状态负载

一、deployment无状态负载

1、deployment无状态负载

一种特殊的pod

优势:
1、支持副本自动维护
2、支持滚动更新

应用场景: 频繁更新的业务

deployment创建流程:
deployment -----> RS(副本集) -----> POD

标签选择器:
标签,就是个key:value对的数据
通过标签在RS和POD间建立对应关系

二、创建deployment

1、创建deployment

apiVersion: apps/v1
kind: Deployment
metadata:
    name: test1-nginx
spec:
    replicas: 4
    selector:
        matchLabels:
            app: nginx								// rs标签 
    template:
        metadata:
            labels:
                app: nginx						// pod标签 
        spec:
            containers:
            - name: test1-nginx
              image: nginx:1.16
              imagePullPolicy: IfNotPresent
// 查看无状态负载
[root@k8s-master deployTest]# kubectl get deploy
NAME          READY   UP-TO-DATE   AVAILABLE   AGE
test1-nginx   4/4     4            4           52s

// 查看rs副本集
[root@k8s-master deployTest]# kubectl get rs 
NAME                     DESIRED   CURRENT   READY   AGE
test1-nginx-5d858b7fc5   4         4         4       101s


[root@k8s-master deployTest]# kubectl get pod 
NAME                           READY   STATUS    RESTARTS   AGE
test1-nginx-5d858b7fc5-74mps   1/1     Running   0          2m43s
test1-nginx-5d858b7fc5-kg8f7   1/1     Running   0          2m43s
test1-nginx-5d858b7fc5-ldx8s   1/1     Running   0          2m43s
test1-nginx-5d858b7fc5-x76z2   1/1     Running   0          2m43s

// 查看pod的标签 
[root@k8s-master deployTest]# kubectl get pod --show-labels 
NAME                           READY   STATUS    RESTARTS   AGE     LABELS
test1-nginx-5d858b7fc5-74mps   1/1     Running   0          5m28s   app=nginx,pod-template-hash=5d858b7fc5
test1-nginx-5d858b7fc5-kg8f7   1/1     Running   0          5m28s   app=nginx,pod-template-hash=5d858b7fc5
test1-nginx-5d858b7fc5-ldx8s   1/1     Running   0          5m28s   app=nginx,pod-template-hash=5d858b7fc5
test1-nginx-5d858b7fc5-x76z2   1/1     Running   0          5m28s   app=nginx,pod-template-hash=5d858b7fc5

2、验证副本维护

[root@k8s-master deployTest]# kubectl get pod 
NAME                           READY   STATUS    RESTARTS   AGE
test1-nginx-5d858b7fc5-74mps   1/1     Running   0          7m39s
test1-nginx-5d858b7fc5-kg8f7   1/1     Running   0          7m39s
test1-nginx-5d858b7fc5-ldx8s   1/1     Running   0          7m39s
test1-nginx-5d858b7fc5-x76z2   1/1     Running   0          7m39s

// 删除任意pod
[root@k8s-master deployTest]# kubectl delete pod test1-nginx-5d858b7fc5-ldx8s
pod "test1-nginx-5d858b7fc5-ldx8s" deleted

// 再次查看pod
[root@k8s-master deployTest]# kubectl get pod 
NAME                           READY   STATUS    RESTARTS   AGE
test1-nginx-5d858b7fc5-74mps   1/1     Running   0          7m59s
test1-nginx-5d858b7fc5-kg8f7   1/1     Running   0          7m59s
test1-nginx-5d858b7fc5-wj6g6   1/1     Running   0          5s
test1-nginx-5d858b7fc5-x76z2   1/1     Running   0          7m59s

三、deployment滚动更新

1、创建pod

apiVersion: apps/v1
kind: Deployment
metadata:
    name: test2-nginx
spec:
    replicas: 6
    selector:
        matchLabels:
            app: nginx2
    strategy:
        type: RollingUpdate					// 更新策略为滚动更新
        rollingUpdate:
            maxSurge: 2							// 每次更新的数量,可以是绝对数字、百分比数字
            maxUnavailable: 3				// 最大无效数量,可以是绝对数字、百分比数字
    template:
        metadata:
            labels:
                app: nginx2
        spec:
            containers:
            - name: test2-nginx
              image: nginx:1.16
              imagePullPolicy: IfNotPresent
[root@k8s-master deployTest]# kubectl get rs
NAME                     DESIRED   CURRENT   READY   AGE
test2-nginx-569d77d6cb   6         6         6       85s

[root@k8s-master deployTest]# kubectl get pod 
NAME                           READY   STATUS    RESTARTS   AGE
test2-nginx-569d77d6cb-4xmpw   1/1     Running   0          88s
test2-nginx-569d77d6cb-57xpk   1/1     Running   0          88s
test2-nginx-569d77d6cb-5bs4n   1/1     Running   0          88s
test2-nginx-569d77d6cb-9nsqb   1/1     Running   0          88s
test2-nginx-569d77d6cb-qstqr   1/1     Running   0          88s
test2-nginx-569d77d6cb-tqbkn   1/1     Running   0          88s

2、测试滚动更新流程

2.1、修改镜像版本、或者副本数

2.2、执行更新

[root@k8s-master deployTest]# kubectl apply -f test2-nginx.yaml 

2.3、查看更新过程

[root@k8s-master deployTest]# kubectl get pod
NAME                           READY   STATUS              RESTARTS   AGE
test2-nginx-569d77d6cb-46tjl   0/1     Terminating         0          6m48s
test2-nginx-569d77d6cb-88qwt   0/1     Terminating         0          6m46s
test2-nginx-569d77d6cb-8s8r9   1/1     Running             0          6m48s
test2-nginx-569d77d6cb-f7pdw   1/1     Running             0          6m48s
test2-nginx-569d77d6cb-jgtcx   0/1     Terminating         0          6m48s
test2-nginx-569d77d6cb-ttm7s   0/1     Terminating         0          6m45s
test2-nginx-569d77d6cb-vh7xh   1/1     Terminating         0          6m45s
test2-nginx-57c7d85bfd-5k87b   1/1     Running             0          4s
test2-nginx-57c7d85bfd-64vdn   1/1     Running             0          4s
test2-nginx-57c7d85bfd-9h7wv   0/1     ContainerCreating   0          2s
test2-nginx-57c7d85bfd-c9bbg   0/1     ContainerCreating   0          2s
test2-nginx-57c7d85bfd-df8nx   1/1     Running             0          4s
test2-nginx-57c7d85bfd-k7db6   0/1     ContainerCreating   0          2s
test2-nginx-57c7d85bfd-q5x2f   1/1     Running             0          4s
test2-nginx-57c7d85bfd-sbbkw   1/1     Running             0          4s
test2-nginx-57c7d85bfd-t8f5z   0/1     ContainerCreating   0          1s
test2-nginx-57c7d85bfd-x58dd   0/1     ContainerCreating   0          2s
[root@k8s-master deployTest]# kubectl describe deploy test2-nginx 
Name:                   test2-nginx
Namespace:              default
CreationTimestamp:      Thu, 25 Jul 2024 16:34:04 +0800
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 4
Selector:               app=nginx2
Replicas:               10 desired | 10 updated | 10 total | 10 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  3 max unavailable, 2 max surge
Pod Template:
  Labels:  app=nginx2
  Containers:
   test2-nginx:
    Image:        nginx:1.18
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  test2-nginx-569d77d6cb (0/0 replicas created)
NewReplicaSet:   test2-nginx-57c7d85bfd (10/10 replicas created)
Events:
  Type    Reason             Age                    From                   Message
  ----    ------             ----                   ----                   -------
  Normal  ScalingReplicaSet  13m                    deployment-controller  Scaled up replica set test2-nginx-569d77d6cb to 6
  Normal  ScalingReplicaSet  9m51s                  deployment-controller  Scaled up replica set test2-nginx-57c7d85bfd to 2
  Normal  ScalingReplicaSet  9m51s                  deployment-controller  Scaled down replica set test2-nginx-569d77d6cb to 3 from 6
  Normal  ScalingReplicaSet  9m51s                  deployment-controller  Scaled up replica set test2-nginx-57c7d85bfd to 5 from 2
  Normal  ScalingReplicaSet  9m49s                  deployment-controller  Scaled down replica set test2-nginx-569d77d6cb to 2 from 3
  Normal  ScalingReplicaSet  9m49s                  deployment-controller  Scaled up replica set test2-nginx-57c7d85bfd to 6 from 5
  Normal  ScalingReplicaSet  9m48s                  deployment-controller  Scaled down replica set test2-nginx-569d77d6cb to 1 from 2
  Normal  ScalingReplicaSet  9m48s                  deployment-controller  Scaled down replica set test2-nginx-569d77d6cb to 0 from 1
  Normal  ScalingReplicaSet  9m1s                   deployment-controller  Scaled up replica set test2-nginx-57c7d85bfd to 10 from 6
  Normal  ScalingReplicaSet  2m17s (x18 over 9m1s)  deployment-controller  (combined from similar events): Scaled up replica set test2-nginx-57c7d85bfd to 5 from 2

2.4、查看更新历史

[root@k8s-master deployTest]# kubectl rollout history deploy test2-nginx 
deployment.apps/test2-nginx 
REVISION  CHANGE-CAUSE
2         <none>
3         <none>

2.5、版本回退

[root@k8s-master deployTest]# kubectl rollout undo deployment test2-nginx --to-revision=2 
deployment.apps/test2-nginx rolled back

2.6、暂停更新

[root@k8s-master deployTest]# kubectl rollout pause deployment test2-nginx 
  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值