運行應用-使用Deployment運行無狀態應用程序

如下介紹如何使用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
相關文章
相關標籤/搜索