《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镜像的部署:

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: # 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
    

    输出类似下面信息:

     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. 列出部署所创建的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。

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 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:

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 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来配置的。

下一节

原创文章,转载请注明: 转载自并发编程网 – ifeve.com本文链接地址: 《kubernetes官方文档》使用部署运行一个无状态应用

  • Trackback 关闭
  • 评论 (0)
  1. 暂无评论

return top