使用 Deployment 运行无状态应用程序

本页展示了如何使用 Kubernetes Deployment 对象运行应用程序。

目标

  • 创建一个 nginx deployment。
  • 使用 kubectl 列出有关 deployment 的信息。
  • 更新 deployment。

开始之前

您需要拥有一个 Kubernetes 集群,并且 kubectl 命令行工具必须配置为与您的集群通信。 建议在至少具有两个非控制平面主机的集群上运行本教程。 如果您还没有集群,可以使用 minikube 创建一个,或者可以使用以下 Kubernetes 游乐场

您的 Kubernetes 服务器必须是 v1.9 或更高版本。

要检查版本,请输入 kubectl version

创建和探索 nginx deployment

您可以通过创建一个 Kubernetes Deployment 对象来运行应用程序,并且可以使用 YAML 文件描述 Deployment。 例如,此 YAML 文件描述了一个运行 nginx:1.14.2 Docker 镜像的 Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
  1. 基于 YAML 文件创建 Deployment

    kubectl apply -f https://k8s.io/examples/application/deployment.yaml
    
  2. 显示 Deployment 的信息

    kubectl describe deployment nginx-deployment
    

    输出类似于此

    Name:     nginx-deployment
    Namespace:    default
    CreationTimestamp:  Tue, 30 Aug 2016 18:11:37 -0700
    Labels:     app=nginx
    Annotations:    deployment.kubernetes.io/revision=1
    Selector:   app=nginx
    Replicas:   2 desired | 2 updated | 2 total | 2 available | 0 unavailable
    StrategyType:   RollingUpdate
    MinReadySeconds:  0
    RollingUpdateStrategy:  1 max unavailable, 1 max surge
    Pod Template:
      Labels:       app=nginx
      Containers:
        nginx:
        Image:              nginx:1.14.2
        Port:               80/TCP
        Environment:        <none>
        Mounts:             <none>
      Volumes:              <none>
    Conditions:
      Type          Status  Reason
      ----          ------  ------
      Available     True    MinimumReplicasAvailable
      Progressing   True    NewReplicaSetAvailable
    OldReplicaSets:   <none>
    NewReplicaSet:    nginx-deployment-1771418926 (2/2 replicas created)
    No events.
    
  3. 列出 deployment 创建的 Pod

    kubectl get pods -l app=nginx
    

    输出类似于此

    NAME                                READY     STATUS    RESTARTS   AGE
    nginx-deployment-1771418926-7o5ns   1/1       Running   0          16h
    nginx-deployment-1771418926-r18az   1/1       Running   0          16h
    
  4. 显示 Pod 的信息

    kubectl describe pod <pod-name>
    

    其中 <pod-name> 是您的 Pod 之一的名称。

更新 deployment

您可以通过应用新的 YAML 文件来更新 deployment。 此 YAML 文件指定应将 deployment 更新为使用 nginx 1.16.1。

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 # Update the version of nginx from 1.14.2 to 1.16.1
        ports:
        - containerPort: 80
  1. 应用新的 YAML 文件

    kubectl apply -f https://k8s.io/examples/application/deployment-update.yaml
    
  2. 观察 deployment 创建具有新名称的 Pod 并删除旧 Pod

    kubectl get pods -l app=nginx
    

通过增加副本计数来扩展应用程序

您可以通过应用新的 YAML 文件来增加 Deployment 中的 Pod 数量。 此 YAML 文件将 replicas 设置为 4,指定 Deployment 应该有四个 Pod

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 4 # Update the replicas from 2 to 4
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.16.1
        ports:
        - containerPort: 80
  1. 应用新的 YAML 文件

    kubectl apply -f https://k8s.io/examples/application/deployment-scale.yaml
    
  2. 验证 Deployment 是否有四个 Pod

    kubectl get pods -l app=nginx
    

    输出类似于此

    NAME                               READY     STATUS    RESTARTS   AGE
    nginx-deployment-148880595-4zdqq   1/1       Running   0          25s
    nginx-deployment-148880595-6zgi1   1/1       Running   0          25s
    nginx-deployment-148880595-fxcez   1/1       Running   0          2m
    nginx-deployment-148880595-rwovn   1/1       Running   0          2m
    

删除 deployment

按名称删除 deployment

kubectl delete deployment nginx-deployment

ReplicationControllers -- 旧方法

创建复制应用程序的首选方法是使用 Deployment,后者反过来使用 ReplicaSet。 在添加 Deployment 和 ReplicaSet 之前,复制应用程序是使用 ReplicationController 配置的。

接下来

最后修改时间 2023 年 8 月 24 日下午 6:38 PST:使用 code_sample shortcode 代替 code shortcode (e8b136c3b3)