• Kubernetes---使用 Deployment 运行一个无状态应用


    使用 Deployment 运行一个无状态应用


    一、创建Deployment

    创建一个 Kubernetes Deployment 对象来运行一个应用, 在一个 YAML 文件中描述 Deployment。例如, 下面这个 YAML 文件描述了一个运行 nginx:1.14.2 Docker 镜像的 Deployment:

    [root@master k8s]# cat deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-deployment
    spec:
      selector:
        matchLabels:
          app: nginx
      replicas: 2 # 告知 Deployment 运行 2 个与该模板匹配的 Pod
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: nginx:1.14.2
            ports:
            - containerPort: 80
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    通过 YAML 文件创建一个 Deployment:

    [root@master k8s]# kubectl apply -f deployment.yaml
    deployment.apps/nginx-deployment created
    
    • 1
    • 2

    显示该 Deployment 的相关信息:

    [root@master k8s]# kubectl describe deployment nginx-deployment
    Name:                   nginx-deployment
    Namespace:              default
    CreationTimestamp:      Wed, 24 Aug 2022 15:59:16 +0800
    Labels:                 <none>
    Annotations:            deployment.kubernetes.io/revision: 1
    Selector:               app=nginx
    Replicas:               2 desired | 2 updated | 2 total | 0 available | 2 unavailable
    StrategyType:           RollingUpdate
    MinReadySeconds:        0
    RollingUpdateStrategy:  25% max unavailable, 25% max surge
    Pod Template:
      Labels:  app=nginx
      Containers:
       nginx:
        Image:        nginx:1.14.2
        Port:         80/TCP
        Host Port:    0/TCP
        Environment:  <none>
        Mounts:       <none>
      Volumes:        <none>
    Conditions:
      Type           Status  Reason
      ----           ------  ------
      Available      False   MinimumReplicasUnavailable
      Progressing    True    ReplicaSetUpdated
    OldReplicaSets:  <none>
    NewReplicaSet:   nginx-deployment-9456bbbf9 (2/2 replicas created)
    Events:
      Type    Reason             Age   From                   Message
      ----    ------             ----  ----                   -------
      Normal  ScalingReplicaSet  12s   deployment-controller  Scaled up replica set nginx-deployment-9456bbbf9 to 2
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    列出该 Deployment 创建的 Pod:

    [root@master k8s]# kubectl get pods -l app=nginx
    NAME                               READY   STATUS              RESTARTS   AGE
    nginx-deployment-9456bbbf9-fcfbz   0/1     ContainerCreating   0          26s
    nginx-deployment-9456bbbf9-w2sqn   0/1     ContainerCreating   0          24s
    # 此时正在拉取镜像创建中
    
    • 1
    • 2
    • 3
    • 4
    • 5
    [root@master k8s]# kubectl get pods -l app=nginx
    NAME                               READY   STATUS    RESTARTS   AGE
    nginx-deployment-9456bbbf9-fcfbz   1/1     Running   0          14m
    nginx-deployment-9456bbbf9-w2sqn   1/1     Running   0          14m
    # 此时已创建成功
    
    • 1
    • 2
    • 3
    • 4
    • 5

    展示某一个 Pod 信息:

    kubectl describe pod <pod-name>  # 这里的  是某一Pod的名称
    
    • 1
    [root@master k8s]# kubectl describe pod nginx 
    Name:         nginx-deployment-9456bbbf9-fcfbz
    Namespace:    default
    Priority:     0
    Node:         node1/192.168.76.101
    Start Time:   Wed, 24 Aug 2022 15:59:19 +0800
    Labels:       app=nginx
                  pod-template-hash=9456bbbf9
    Annotations:  cni.projectcalico.org/podIP: 10.244.166.129/32
                  cni.projectcalico.org/podIPs: 10.244.166.129/32
    Status:       Running
    IP:           10.244.166.129
    IPs:
      IP:           10.244.166.129
    Controlled By:  ReplicaSet/nginx-deployment-9456bbbf9
    Containers:
      nginx:
        Container ID:   docker://eb15ca37ee972d4b0761041a3cb2b04baed2f6ef2e28fc20c16f90441b4fbc4c
        Image:          nginx:1.14.2
        Image ID:       docker-pullable://nginx@sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
        Port:           80/TCP
        Host Port:      0/TCP
        State:          Running
          Started:      Wed, 24 Aug 2022 16:02:55 +0800
        Ready:          True
        Restart Count:  0
        Environment:    <none>
        Mounts:
          /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-kkfjm (ro)
    Conditions:
      Type              Status
      Initialized       True
      Ready             True
      ContainersReady   True
      PodScheduled      True
    Volumes:
      kube-api-access-kkfjm:
        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  15m   default-scheduler  Successfully assigned default/nginx-deployment-9456bbbf9-fcfbz to node1
      Normal  Pulling    14m   kubelet            Pulling image "nginx:1.14.2"
      Normal  Pulled     12m   kubelet            Successfully pulled image "nginx:1.14.2" in 1m47.763090495s
      Normal  Created    12m   kubelet            Created container nginx
      Normal  Started    12m   kubelet            Started container nginx
    
    Name:         nginx-deployment-9456bbbf9-w2sqn
    Namespace:    default
    Priority:     0
    Node:         node2/192.168.76.102
    Start Time:   Wed, 24 Aug 2022 15:59:20 +0800
    Labels:       app=nginx
                  pod-template-hash=9456bbbf9
    Annotations:  cni.projectcalico.org/podIP: 10.244.104.1/32
                  cni.projectcalico.org/podIPs: 10.244.104.1/32
    Status:       Running
    IP:           10.244.104.1
    IPs:
      IP:           10.244.104.1
    Controlled By:  ReplicaSet/nginx-deployment-9456bbbf9
    Containers:
      nginx:
        Container ID:   docker://324d787fa9f90c4f3270bc9ae22dd65a770b8d53d333d0632b63526c20371380
        Image:          nginx:1.14.2
        Image ID:       docker-pullable://nginx@sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
        Port:           80/TCP
        Host Port:      0/TCP
        State:          Running
          Started:      Wed, 24 Aug 2022 16:03:57 +0800
        Ready:          True
        Restart Count:  0
        Environment:    <none>
        Mounts:
          /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-ssdsf (ro)
    Conditions:
      Type              Status
      Initialized       True
      Ready             True
      ContainersReady   True
      PodScheduled      True
    Volumes:
      kube-api-access-ssdsf:
        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  15m   default-scheduler  Successfully assigned default/nginx-deployment-9456bbbf9-w2sqn to node2
      Normal  Pulling    12m   kubelet            Pulling image "nginx:1.14.2"
      Normal  Pulled     11m   kubelet            Successfully pulled image "nginx:1.14.2" in 1m34.930550489s
      Normal  Created    11m   kubelet            Created container nginx
      Normal  Started    11m   kubelet            Started container nginx
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108

    二、更新 Deployment

    应用一个新的 YAML 文件来更新 Deployment。下面的 YAML 文件指定该 Deployment 镜像更新为 nginx 1.16.1。

    [root@master k8s]# cat deployment-update.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-deployment
    spec:
      selector:
        matchLabels:
          app: nginx
      replicas: 2
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: nginx:1.16.1 # 将 nginx 版本从 1.14.2 更新为 1.16.1
            ports:
            - containerPort: 80
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    应用新的 YAML:

    [root@master k8s]# kubectl apply -f deployment-update.yaml
    deployment.apps/nginx-deployment configured
    
    • 1
    • 2

    查看该 Deployment 以新的名称创建 Pod 同时删除旧的 Pod:

    [root@master k8s]# kubectl get pods -l app=nginx
    NAME                               READY   STATUS              RESTARTS   AGE
    nginx-deployment-9456bbbf9-fcfbz   1/1     Running             0          22m
    nginx-deployment-9456bbbf9-w2sqn   1/1     Terminating         0          22m
    nginx-deployment-ff6655784-nzq29   0/1     ContainerCreating   0          8s
    nginx-deployment-ff6655784-vzl68   1/1     Running             0          102s
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    [root@master k8s]# kubectl get pods -l app=nginx
    NAME                               READY   STATUS    RESTARTS   AGE
    nginx-deployment-ff6655784-nzq29   1/1     Running   0          8m7s
    nginx-deployment-ff6655784-vzl68   1/1     Running   0          9m41s
    
    • 1
    • 2
    • 3
    • 4
    [root@master k8s]# kubectl describe pod nginx
    Name:         nginx-deployment-ff6655784-nzq29
    Namespace:    default
    Priority:     0
    Node:         node1/192.168.76.101
    Start Time:   Wed, 24 Aug 2022 16:21:18 +0800
    Labels:       app=nginx
                  pod-template-hash=ff6655784
    Annotations:  cni.projectcalico.org/podIP: 10.244.166.130/32
                  cni.projectcalico.org/podIPs: 10.244.166.130/32
    Status:       Running
    IP:           10.244.166.130
    IPs:
      IP:           10.244.166.130
    Controlled By:  ReplicaSet/nginx-deployment-ff6655784
    Containers:
      nginx:
        Container ID:   docker://e35b4dd370a899ebf9a6a8f5ef18fe39cf1d03eca40bbdf3a858af98afe1fe58
        Image:          nginx:1.16.1
        Image ID:       docker-pullable://nginx@sha256:d20aa6d1cae56fd17cd458f4807e0de462caf2336f0b70b5eeb69fcaaf30dd9c
        Port:           80/TCP
        Host Port:      0/TCP
        State:          Running
          Started:      Wed, 24 Aug 2022 16:23:21 +0800
        Ready:          True
        Restart Count:  0
        Environment:    <none>
        Mounts:
          /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-8pj6k (ro)
    Conditions:
      Type              Status
      Initialized       True
      Ready             True
      ContainersReady   True
      PodScheduled      True
    Volumes:
      kube-api-access-8pj6k:
        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  8m56s  default-scheduler  Successfully assigned default/nginx-deployment-ff6655784-nzq29 to node1
      Normal  Pulling    8m37s  kubelet            Pulling image "nginx:1.16.1"
      Normal  Pulled     6m55s  kubelet            Successfully pulled image "nginx:1.16.1" in 1m41.169627748s
      Normal  Created    6m53s  kubelet            Created container nginx
      Normal  Started    6m53s  kubelet            Started container nginx
    
    Name:         nginx-deployment-ff6655784-vzl68
    Namespace:    default
    Priority:     0
    Node:         node2/192.168.76.102
    Start Time:   Wed, 24 Aug 2022 16:19:44 +0800
    Labels:       app=nginx
                  pod-template-hash=ff6655784
    Annotations:  cni.projectcalico.org/podIP: 10.244.104.2/32
                  cni.projectcalico.org/podIPs: 10.244.104.2/32
    Status:       Running
    IP:           10.244.104.2
    IPs:
      IP:           10.244.104.2
    Controlled By:  ReplicaSet/nginx-deployment-ff6655784
    Containers:
      nginx:
        Container ID:   docker://ee8e71c7ad3def0c31fce59417ce91d5644998c9f8872e55084ad3deca568751
        Image:          nginx:1.16.1
        Image ID:       docker-pullable://nginx@sha256:d20aa6d1cae56fd17cd458f4807e0de462caf2336f0b70b5eeb69fcaaf30dd9c
        Port:           80/TCP
        Host Port:      0/TCP
        State:          Running
          Started:      Wed, 24 Aug 2022 16:21:15 +0800
        Ready:          True
        Restart Count:  0
        Environment:    <none>
        Mounts:
          /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-c22g5 (ro)
    Conditions:
      Type              Status
      Initialized       True
      Ready             True
      ContainersReady   True
      PodScheduled      True
    Volumes:
      kube-api-access-c22g5:
        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  10m    default-scheduler  Successfully assigned default/nginx-deployment-ff6655784-vzl68 to node2
      Normal  Pulling    10m    kubelet            Pulling image "nginx:1.16.1"
      Normal  Pulled     9m5s   kubelet            Successfully pulled image "nginx:1.16.1" in 1m21.970003066s
      Normal  Created    9m2s   kubelet            Created container nginx
      Normal  Started    8m59s  kubelet            Started container nginx
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108

    三、通过增加副本数来扩缩应用

    通过应用新的 YAML 文件来增加 Deployment 中 Pod 的数量。 下面的 YAML 文件将 replicas 设置为 4,指定该 Deployment 应有 4 个 Pod:

    [root@master k8s]# cat deployment-scale.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-deployment
    spec:
      selector:
        matchLabels:
          app: nginx
      replicas: 4 # 将副本数从 2 更新为 4
      template:
        metadata:
          labels:
            app: nginx
        spec:
          containers:
          - name: nginx
            image: nginx:1.16.1
            ports:
            - containerPort: 80
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    应用新的 YAML 文件:

    [root@master k8s]# kubectl apply -f deployment-scale.yaml
    deployment.apps/nginx-deployment configured
    
    • 1
    • 2
    [root@master k8s]# kubectl get pods -l app=nginx
    NAME                               READY   STATUS    RESTARTS   AGE
    nginx-deployment-ff6655784-6nl58   1/1     Running   0          30s
    nginx-deployment-ff6655784-8ws9j   1/1     Running   0          30s
    nginx-deployment-ff6655784-nzq29   1/1     Running   0          11m
    nginx-deployment-ff6655784-vzl68   1/1     Running   0          13m
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    四、删除Deployment

    基于名称删除 Deployment:

    [root@master k8s]# kubectl delete deployment nginx-deployment
    deployment.apps "nginx-deployment" deleted
    
    • 1
    • 2
    [root@master k8s]# kubectl get pods -l app=nginx
    No resources found in default namespace.
    # 删除成功
    
    • 1
    • 2
    • 3

  • 相关阅读:
    ABP中关于Swagger的一些配置
    golang slice/array无重复取随机内容
    腾讯云遭受爆破攻击(记录)
    C++之lambda匿名函数总结(二百四十五)
    开发测试运维哪个好?分别有什么特点?
    阿里云AliGenie开发天猫语音功能-入门篇
    行业追踪,2023-09-26
    [附源码]SSM计算机毕业设计血库管理系统JAVA
    基于Springboot实现点餐平台网站管理系统项目【项目源码+论文说明】分享
    面试经典150题——Day20
  • 原文地址:https://blog.csdn.net/AnNan1997/article/details/126506915