运行应用-使用Deployment运行无状态应用程序

以下介绍如何使用kubernetes的deployment对象运行应用程序。

目标 

- 创建nginx deployment。

- 使用kubectl列出有关部署的信息。

- 更新部署

 

创建和探索nginx部署

可以通过创建kubernetes部署对象来运行应用程序,还可以在yaml文件中描述部署。例如,这个yaml文件描述了运行nginx:1.7.9 docker映像的部署:

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文件创建deployment

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

 

posted @ 2019-10-12 15:53  UniqueColor  阅读(433)  评论(0编辑  收藏  举报