使用 Deployment 執行無狀態應用程式

本頁說明如何使用 Kubernetes Deployment 物件執行應用程式。

目標

  • 建立 nginx 部署。
  • 使用 kubectl 列出關於部署的資訊。
  • 更新部署。

準備開始

您需要有一個 Kubernetes 叢集,並且必須設定 kubectl 命令列工具以與您的叢集通訊。建議在至少有兩個節點且這些節點不充當控制平面主機的叢集上執行本教學課程。如果您還沒有叢集,可以使用 minikube 建立一個,或者您可以使用以下 Kubernetes playground 之一

您的 Kubernetes 伺服器版本必須為 v1.9 或更新版本。若要檢查版本,請輸入 kubectl version

建立與探索 nginx 部署

您可以透過建立 Kubernetes Deployment 物件來執行應用程式,並且可以在 YAML 檔案中描述 Deployment。例如,這個 YAML 檔案描述一個 Deployment,它執行 nginx:1.14.2 Docker 映像檔

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 的名稱。

更新部署

您可以透過套用新的 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. 觀察 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,而 Deployment 又會使用 ReplicaSet。在 Kubernetes 中加入 Deployment 和 ReplicaSet 之前,複寫應用程式是使用 ReplicationController 來設定的。

下一步

上次修改時間:2023 年 8 月 24 日下午 6:38 PST:Use code_sample shortcode instead of code shortcode (e8b136c3b3)