使用部署运行无状态应用程序

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

目标

  • 创建 nginx 部署。
  • 使用 kubectl 列出有关部署的信息。
  • 更新部署。

开始之前

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

您的 Kubernetes 服务器必须是 v1.9 或更高版本。要检查版本,请输入 kubectl version

创建和探索 nginx 部署

您可以通过创建 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. 列出由部署创建的 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 的名称。

更新部署

您可以通过应用新的 YAML 文件来更新部署。此 YAML 文件指定应将部署更新为使用 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. 观察部署创建具有新名称的 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
    

删除部署

按名称删除部署

kubectl delete deployment nginx-deployment

ReplicationControllers - 旧方法

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

下一步

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