1. 程式人生 > >《kubernetes官方文件》使用部署執行一個無狀態應用

《kubernetes官方文件》使用部署執行一個無狀態應用

本頁展示如何使用Kubernetes部署物件(Kubernetes Deployment object)執行一個應用程式。

目標

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

準備工作

您需要有一個Kubernetes叢集,並且必須配置kubectl命令列工具來與您的叢集通訊。如果您還沒有叢集,您可以使用Minikube建立一個叢集,或者您可以使用這些Kubernetes平臺:

您的Kubernetes伺服器版本必須是v1.9。檢視版本,請輸入kubectl version。

建立和體驗nginx部署

您可以通過建立Kubernetes部署物件來執行應用程式,還可以在YAML檔案中描述部署。例如,通過YAML檔案描述了一個執行nginx:1.7.9 Docker映象的部署:

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: # create pods using pod definition in this template
metadata: # unlike pod-nginx.yaml, the name is not included in the meta data as a unique name is # generated from the deployment name labels: app: nginx spec: containers: - name: nginx image: nginx:1.7.9 ports: - containerPort
: 80
  1. 基於YAML檔案建立部署:
    kubectl apply -f https://k8s.io/docs/tasks/run-application/deployment.yaml
    
  2. 顯示部署的相關資訊:
    kubectl describe deployment nginx-deployment
    

    輸出類似下面資訊:

     [email protected]:~/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. 列出部署所建立的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的名稱.

更新部署

您可以通過使用一個新的YAML檔案來更新部署。這個YAML檔案指定應該更新部署以使用nginx 1.8。

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 https://k8s.io/docs/tasks/run-application/deployment-update.yaml
    
  2. 觀察部署建立的新pod,並刪除舊的pods:
    kubectl get pods -l app=nginx
    

通過增加副本數來擴充套件應用程式

您可以通過應用一個新的YAML檔案來增加部署的數量。這個YAML檔案將副本replicas設定為4,該檔案指定部署應該有4個pods:

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 https://k8s.io/docs/tasks/run-application/deployment-scale.yaml
    
  2. 校驗部署有4個pods:
    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

副本控制器——老方法

建立副本應用程式的首選方法是使用部署(Deployment),而部署(Deployment)反過來又使用了ReplicaSet。在將部署和副本新增到Kubernetes之前,副本的應用程式是通過使用副本控制器 ReplicationController來配置的。

下一節