本文主要介绍deployment的使用,文中提到的deploy关键字也是指deployment,这两个关键字都可以在命令行中使用

查找deployment文档
方式1:百度搜索

百度搜索框输入: kubernetes deployment就可以找到deployment的帮助文档了。
百度搜多deploy帮助文档

方式2:直接访问官方文档

点击帮助文档来查看官方解释

deployment的相关文档为https://kubernetes.io/zh/docs/concepts/workloads/controllers/deployment/

deployment、ReplicaSet与pod的关系

根据文档解释,deployment是通过控制ReplicaSet这个控制器来管理pod的。通过在deployment的spec字段中设置期望的状态来完成deployment的声明。
来看看官方的原文解释:大意就是我们可以通过deployment来管理ReplicaSet从而达到控制pod的目的。

A ReplicaSet ensures that a specified number of pod replicas are running at any given time. However, a Deployment is a higher-level concept that
manages ReplicaSets and provides declarative updates to Pods along with a lot of other useful features. Therefore, we recommend using 
Deployments instead of directly using ReplicaSets, unless you require custom update orchestration or don't require updates at all.

This actually means that you may never need to manipulate ReplicaSet objects: use a Deployment instead, and define your application in the 
spec section.
[k8s@k8s-master deployment]$ kubectl get pods #获取pod
NAME                             READY   STATUS    RESTARTS   AGE
c-nginx                          1/1     Running   1          3d23h
nginx-deploy                     1/1     Running   0          3d1h
nginx-deploy-69d7bbc58-z77mn     1/1     Running   0          144m
nginx-deploy2-6cbb6b6c84-8lpkd   1/1     Running   0          38m
nginx-loadbalancer               1/1     Running   0          2d23h
[k8s@k8s-master deployment]$ kubectl get deploy #获取Deployments
NAME            READY   UP-TO-DATE   AVAILABLE   AGE	
nginx-deploy    1/1     1            1           144m
nginx-deploy2   1/1     1            1           38m
[k8s@k8s-master deployment]$ kubectl get rs  #获取ReplicaSet
NAME                       DESIRED   CURRENT   READY   AGE
nginx-deploy-69d7bbc58     1         1         1       143m
nginx-deploy2-6cbb6b6c84   1         1         1       38m
deployment与service的关系

service是比deployment更高一层的抽象层。deployment是通过ReplicaSet来管理一批功能相同的pod,如果有pod数量很多的话,我们一个个去访问很不方便。 因此,k8s将deployment下的一批pod应用抽象出来作为一个抽象层,并自动进行负载均衡,这样我们需要访问pod应用时只需访问service抽象层即可。


deployment命令式创建(陈述式创建)
# deployment陈述式创建
kubectl create deployment NAME --image=image [--dry-run=server|client|none] [options]
[k8s@k8s-master deployment]$ kubectl get deploy
NAME           READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deploy   1/1     1            1           105m
[k8s@k8s-master deployment]$ kubectl create deploy nginx-deploy2 --image=nginx:alpine
deployment.apps/nginx-deploy2 created
[k8s@k8s-master deployment]$ kubectl get deploy  #稍等片刻后查看,即可发现deployment已经被创建完成
NAME            READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deploy    1/1     1            1           107m
nginx-deploy2   1/1     1            1           79s
deployment声明式创建
1.创建一个名称为nginx-deploy的deployment
2.replicas为1表示deployment后端的pod数量为1
3.通过selector字段来选择后端pod(使用matchLabels来过滤标签),使用`kubectl get pods --show-labels`来查看标签
4.通过template字段来定义后端pod的属性
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deploy # 注意名称的规则
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      type: web-server
  strategy:
    rollingUpdate:
      maxUnavailable: 1
  template:
    metadata:
      name: nginx-pod
      namespace: default
      labels:
        type: web-server
    spec:
      containers:
      - name: c-nginx-pod # 注意名称的规则
        image: nginx:alpine
        imagePullPolicy: IfNotPresent

deployment的更新、扩缩容、回滚

deployment更新
默认情况下,deployment只有在template字段发生改变时才会触发deployment更新。
(1)更新镜像(建议使用edit命令或者直接修改源yaml文件来更新deployment)
例如,将nginx的镜像从nginx:alpine修改至nginx:1.9.1

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deploy # 名称的规则
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      type: web-server
  strategy:
    rollingUpdate:
      maxUnavailable: 1
  template:
    metadata:
      name: nginx-pod
      namespace: default
      labels:
        type: web-server
    spec:
      containers:
      - name: c-nginx-pod # 名称的规则
        image: nginx:1.9.1      #修改此处
        imagePullPolicy: IfNotPresent

修改完后,使用kubectl apply -f nginx-deploy.yaml来使配置重新生效,届时,原pod会被终结并删除,并创建新pod

[k8s@k8s-master deployment]$ kubectl get pods
NAME                             READY   STATUS              RESTARTS   AGE
c-nginx                          1/1     Running             1          3d23h
nginx-deploy                     1/1     Running             0          3d2h
nginx-deploy-69d7bbc58-z77mn     1/1     Terminating         0          3h3m
nginx-deploy-76cd74bb6d-4qjzt    0/1     ContainerCreating   0          26s
nginx-deploy2-6cbb6b6c84-8lpkd   1/1     Running             0          77m
nginx-loadbalancer               1/1     Running             0          3d

由上述输出可以看到,在更新deployment时,先终结原pod,并重新创建新pod,此处设置的更新策略为rollingUpdate,并且maxUnavailable设置为1,表示为滚动更新,并且pod最大的不可用数量为1,也就是一个pod一个pod的滚动更新更新。

deployment扩缩容
扩缩容主要是修改后端pod的数量,相关字段名为replicas。
(1)手动根据数量扩缩容

[k8s@k8s-master deployment]$ kubectl scale deployment.v1.apps/nginx-deploy --replicas=3
deployment.apps/nginx-deploy scaled
[k8s@k8s-master deployment]$ kubectl rollout status deployment.v1.apps/nginx-deploy
Waiting for deployment "nginx-deploy" rollout to finish: 1 of 3 updated replicas are available...
Waiting for deployment "nginx-deploy" rollout to finish: 2 of 3 updated replicas are available...
deployment "nginx-deploy" successfully rolled out
[k8s@k8s-master deployment]$ kubectl get pods
NAME                             READY   STATUS    RESTARTS   AGE
c-nginx                          1/1     Running   1          4d1h
nginx-deploy                     1/1     Running   0          3d4h
nginx-deploy-76cd74bb6d-4qjzt    1/1     Running   0          90m
nginx-deploy-76cd74bb6d-8cjxq    1/1     Running   0          21s #新增
nginx-deploy-76cd74bb6d-mh9qd    1/1     Running   0          21s #新增
nginx-deploy2-6cbb6b6c84-8lpkd   1/1     Running   0          167m
nginx-loadbalancer               1/1     Running   0          3d1h

(2)自动扩缩容
业务有高峰期也有低谷期,高峰期应当适量地增加pod数量来均衡一下负载,而低谷期应该删除一些pod来避免不必要的资源浪费, 在kubernetes里面,可以通过自动扩缩容来完成以上需求。

kubectl autoscale deployment.v1.apps/nginx-deploy --min=3 --max=15 --cpu-percent=80

(3)回滚
回滚是业务部署很可能出现的操作,当新版本不满足预期时就可以进行回滚。回滚后,回滚历史上会增加一个版本。

通过kubectl rollout history deployment.v1.apps/deploy_name 来查看历史更新版本
通过kubectl rollout undo deployment.v1.apps/deploy_name来回滚到上一个版本
通过kubectl rollout undo deployment.v1.apps/deploy_name --to-revision=REVISION来回滚到指定版本,这个REVISION通过history来查看

a.查看历史版本kubectl rollout history deployment.v1.apps/nginx-deploy

[k8s@k8s-master deployment]$ kubectl rollout history deployment.v1.apps/nginx-deploy
deployment.apps/nginx-deploy 
REVISION  CHANGE-CAUSE
4         <none>
5         <none>

b.查看当前版本下的pod镜像版本kubectl describe pod nginx-deploy-69d7bbc58-cn6dw,假设当前nginx镜像版本为1.9.1版本。
c.更新nginx版本至最新kubectl set image deployment.v1.apps/nginx-deploy c-nginx-pod=nginx:latest

#语法 kubectl set image deployment.v1.apps/nginx-deploy container_name=image_version

(1)先找到nginx-deploy这个deployment的容器名称
[k8s@k8s-master deployment]$ kubectl describe deploy nginx-deploy
Name:                   nginx-deploy
Namespace:              default
CreationTimestamp:      Mon, 27 Jul 2020 15:41:53 +0800
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 5
Selector:               type=web-server
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  1 max unavailable, 25% max surge
Pod Template:
  Labels:  type=web-server
  Containers:
   c-nginx-pod:   #这个就是容器的名称
    Image:        nginx:alpine
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
...

(2)更新nginx镜像:kubectl set image deployment.v1.apps/nginx-deploy c-nginx-pod=nginx:latest
(3)查看新pod的nginx镜像:kubectl describe pod,此处不做演示

d.查看更新历史kubectl rollout history deployment.v1.apps/nginx-deploy

[k8s@k8s-master deployment]$ kubectl rollout history deployment.v1.apps/nginx-deploy
deployment.apps/nginx-deploy 
REVISION  CHANGE-CAUSE
4         <none>
5         <none>
6         <none> #我们发现,新增了一个版本

e.新版本上线后,发现不符合预期,需要进行回滚kubectl rollout undo deployment.v1.apps/nginx-deploy

回滚完成后,再查看nginx的镜像版本,发现已经回到了原先的nginx:1.9.1

f.回滚到指定版本kubectl rollout undo deployment.v1.apps/deploy_name --to-revision=REVISION,此处不做赘述

bonus
kubectl rollout pause deployment.v1.apps/nginx-deployment # 暂停某个deploy
kubectl rollout resume deployment.v1.apps/nginx-deployment # 恢复某个deploy的运行
kubectl patch deployment.v1.apps/nginx-deployment -p '{"spec":{"progressDeadlineSeconds":600}}' # 打补丁,重新设置某个字段
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐