使用 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

ReplicationController -- 旧方法

创建副本化应用程序的首选方法是使用 Deployment,Deployment 又会使用 ReplicaSet。在 Deployment 和 ReplicaSet 被添加到 Kubernetes 之前,副本化应用程序是使用 ReplicationController 配置的。

接下来

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