kubernetes入门之Deployments

Deployment 提供了声明式的pods和Replica配置。只需要描述一个部署对象,即可根据该描述参建或者更资源。 使用场景:

  • 参建副本集和 Pods
  • 检查部署状态
  • 更新升级Pods
  • 回滚到历史版本
  • 暂停或恢复部署

创建Deployment

新建文件nginx-deployment.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

参建deployment

kubectl create -f nginx-deployment.yml --record

--record 可以不加,但是添加这个可以将当前命令记录在正在创建或更新的资源的注释中。 有助于查看在每个部署版本中执行的命令。

$ kubectl get deployments
NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   2         2         2            2           4m

查看部署的pods和RS

kubectl get rs
kubectl get pods
$ kubectl get pods --show-labels
NAME                                READY     STATUS    RESTARTS   AGE       LABELS
nginx-deployment-4087004473-2wphk   1/1       Running   0          12m       app=nginx,pod-template-hash=4087004473
nginx-deployment-4087004473-cx2tv   1/1       Running   0          12m       app=nginx,pod-template-hash=4087004473

app=nignx 标签 ; hash值4087004473 RC name = <the name of the Deployment>-<hash value of the pod template> 注意:要设置和合适的选择器(app=nginx)和spec模板,不要和其他deployment、rs,Replication Controllers等重叠。

Deployment状态

在对一个 Deployment升级时,想确认是否成功,最简单的方式是通过kubectl rollout status查看,如:

kubectl rollout status deployment/nginx-deployment
kubectl rollout status -f nginx-deployment

比如副本从2个升级到4个时:

$ kubectl rollout status deployment/nginx-deployment
Waiting for deployment spec update to be observed...
Waiting for rollout to finish: 2 out of 4 new replicas have been updated...
Waiting for rollout to finish: 2 out of 4 new replicas have been updated...
Waiting for rollout to finish: 2 of 4 updated replicas are available...
Waiting for rollout to finish: 3 of 4 updated replicas are available...
deployment "nginx-deployment" successfully rolled out

Deployment 更新

变更部署对象时,可以直接更改yml部署文件更改,让后使用apply更新。也可以直接使用命令指定更新,如使用其他版本的nginx镜像。

$ kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1
deployment "nginx-deployment" image updated

要查看deployment的历史更新记录,可以通过describe:

kubectl describe deployments
Name:			nginx-deployment
Namespace:		default
CreationTimestamp:	Mon, 20 Mar 2017 22:12:37 +0800
Labels:			app=nginx
Selector:		app=nginx
Replicas:		4 updated | 4 total | 4 available | 0 unavailable
StrategyType:		RollingUpdate
MinReadySeconds:	0
RollingUpdateStrategy:	1 max unavailable, 1 max surge
Conditions:
  Type		Status	Reason
  ----		------	------
  Available 	True	MinimumReplicasAvailable
OldReplicaSets:	<none>
NewReplicaSet:	nginx-deployment-4087004473 (4/4 replicas created)
Events:
  FirstSeen	LastSeen	Count	From				SubObjectPath	Type		Reason			Message
  ---------	--------	-----	----				-------------	--------	------			-------
  50m		50m		1	{deployment-controller }			Normal		ScalingReplicaSet	Scaled up replica set nginx-deployment-4087004473 to 2
  25m		25m		1	{deployment-controller }			Normal		ScalingReplicaSet	Scaled up replica set nginx-deployment-4087004473 to 333
  21m		21m		1	{deployment-controller }			Normal		ScalingReplicaSet	Scaled down replica set nginx-deployment-4087004473 to 3
  10m		10m		1	{deployment-controller }			Normal		ScalingReplicaSet	Scaled down replica set nginx-deployment-4087004473 to 2
  10m		10m		1	{deployment-controller }			Normal		ScalingReplicaSet	Scaled up replica set nginx-deployment-4087004473 to 4

上例可以看到replica set的扩容和收缩记录。

Deployment回退

如果某个部署版本不稳定,可以回退到历史版本。默认情况下,将保存所有历史版本,也可以通过设置.spec.revisionHistoryLimit作限制,如果值为0,表示清除历史版本记录。需要注意的是,只有在.spec.template发生改变时(如:更新标签或镜像),才会有版本记录。对于扩容等非template改变将不会有版本记录。

比如,我把nignx从1.7.9升级到latest

$ kubectl set image deployment/nginx-deployment nginx=nginx:latest
$ kubectl rollout history -f nginx-deployment.yml
deployments "nginx-deployment"
REVISION	CHANGE-CAUSE
1		kubectl apply -f nginx.yml --record
2		kubectl set image deployment/nginx-deployment nginx=nginx:latest

知道版本后,我们可以通过undo回退到上一个版本或者指定版本。

 kubectl rollout undo deployment/nginx-deployment
 kubectl rollout undo deployment/nginx-deployment --to-revision=2

Deployment扩缩容

直接修改yml部署文件中的replicas或者使用scale命令都能实现扩缩容的效果。

kubectl scale deployment nginx-deployment --replicas 3

在集群环境中,可以设置自动伸缩。

$ kubectl autoscale deployment nginx-deployment --min=10 --max=15 --cpu-percent=80
deployment "nginx-deployment" autoscaled

min: 最小副本数 max:最大副本数 –cpu-percent=80

Deployment 暂停和恢复

可以通过控制deployment的执行过程实现“金丝雀发布”,

$ kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1; kubectl rollout pause deployment/nginx-deployment
deployment "nginx-deployment" image updated
deployment "nginx-deployment" paused

同事有两个版本在跑

kubectl get rs
NAME                          DESIRED   CURRENT   READY     AGE
nginx-deployment-2273492681   2         2         2         42m
nginx-deployment-4087004473   2         2         2         12h
nginx-deployment-4182293765   0         0         0         1h
$ kubectl rollout status deployment/nginx-deployment 
# 恢复deploy
$ kubectl rollout resume deployment/nginx-deployment

Deployment 配置

查看正则运行的版本配置可以通过以下命令:

kubectl get deployment nginx-deployment -o yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "5"
    kubectl.kubernetes.io/last-applied-configuration: '{"kind":"Deployment","apiVersion":"extensions/v1beta1","metadata":{"name":"nginx-deployment","creationTimestamp":null},"spec":{"replicas":3,"template":{"metadata":{"creationTimestamp":null,"labels":{"app":"nginx"}},"spec":{"containers":[{"name":"nginx","image":"nginx:1.7.9","ports":[{"containerPort":80}],"resources":{}}]}},"strategy":{},"revisionHistoryLimit":5},"status":{}}'
    kubernetes.io/change-cause: kubectl apply -f nginx.yml --record
  creationTimestamp: 2017-03-21T03:36:49Z
  generation: 13
  labels:
    app: nginx
  name: nginx-deployment
  namespace: default
  resourceVersion: "96882"
  selfLink: /apis/extensions/v1beta1/namespaces/default/deployments/nginx-deployment
  uid: 9dfe0a3f-0de7-11e7-a5ac-080027f0adcd
spec:
  replicas: 3
  revisionHistoryLimit: 5
  selector:
    matchLabels:
      app: nginx
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:1.7.9
        imagePullPolicy: IfNotPresent
        name: nginx
        ports:
        - containerPort: 80
          protocol: TCP
        resources: {}
        terminationMessagePath: /dev/termination-log
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 3
  conditions:
  - lastTransitionTime: 2017-03-21T03:40:01Z
    lastUpdateTime: 2017-03-21T03:40:01Z
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  observedGeneration: 13
  replicas: 3
  updatedReplicas: 3

通过此配置,我们也可以参考调整yml配置然后apply -f

Pod Template .spec.template

  • metadata: 设置labels等。.spec.template.metadata.labels.spec.selector相等,才会被Replication Controller管理。
  • restartPolicy: Always默认
  • spec.containers: docker容器相关配置(镜像,环境变量,数据卷…)
CONTENTS