如下介绍如何使用kubernetes的deployment对象运行应用程序。nginx
目标 web
- 建立nginx deployment。docker
- 使用kubectl列出有关部署的信息。api
- 更新部署app
建立和探索nginx部署spa
能够经过建立kubernetes部署对象来运行应用程序,还能够在yaml文件中描述部署。例如,这个yaml文件描述了运行nginx:1.7.9 docker映像的部署:code
application/deployment.yaml
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2 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.7.9 ports: - containerPort: 80
注意:对象
selector字段定义Deployment如何查找要管理的pod。
在本例中,只需选择Pod模板中定义的标签(app:nginx)。然而,只要Pod模板自己知足规则,就可能有更复杂的选择规则。
1. 基于YAML文件建立deploymentblog
kubectl apply -f application/deployment.yaml
2. 显示有关部署的信息:部署
kubectl describe deployment nginx-deployment
输出相似于:
user@computer:~/website$ 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.7.9 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. 列出demployment建立的Pods:
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文件来更新部署。如下YAML文件将nginx更新到nginx 1.8
application/deployment-update.yaml
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2 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.8 # Update the version of nginx from 1.7.9 to 1.8 ports: - containerPort: 80
1. 应用新的yaml文件:
kubectl apply -f application/deployment-update.yaml
2. 查看使用新名称建立的Pods并删除Pods:
kubectl get pods -l app=nginx
经过增长副本计数来扩展应用程序
你能够经过应用新的yaml文件来增长部署中的Pod数量。此YAML文件将replicas设置为4,指定部署应具备四个pod:
application/deployment-scale.yaml
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2 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.8 ports: - containerPort: 80
1. 应用新的yaml文件:
kubectl apply -f application/deployment-scale.yaml
2. 验证部署是否有四个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的名称删除Deployment
kubectl delete deployment nginx-deployment