使用 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
基於 YAML 檔案建立 Deployment
kubectl apply -f https://k8s.io/examples/application/deployment.yaml
顯示有關 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.
列出 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
顯示 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
應用新的 YAML 檔案
kubectl apply -f https://k8s.io/examples/application/deployment-update.yaml
觀察 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
應用新的 YAML 檔案
kubectl apply -f https://k8s.io/examples/application/deployment-scale.yaml
驗證 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 進行配置。
下一步
- 瞭解更多關於 Deployment 物件 的資訊。