天下風雲出我輩
一入江湖歲月催

Deployment

简述

Deployment 为 Pod 和 ReplicaSet 提供了一个声明式定义 (declarative) 方法,用来替代以前的 ReplicationController 来方便的管理应用。

  • 部署无状态应用
  • 管理Pod和ReplicaSet
  • 具有上线部署、副本设定、滚动升级、回滚等功能
  • 提供声明式更新,例如只更新一个新的Image

应用场景:Web服务,微服务


使用deployment部署nignx:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: default
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.16
        ports:
        - containerPort: 80

保存为yaml文件并执行命令:

kubectl create -f nginx-deployment.yaml  --record  或者使用 kubectl apply

#查看deploy
$ kubectl get deployments
NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3         3         3            3           18s

#查看pod
$ kubectl get pods --show-labels
NAME                                READY     STATUS    RESTARTS   AGE     LABELS
nginx-deployment-2035384211-7ci7o   1/1       Running   0          18s     app=nginx,pod-template-hash=2035384211
nginx-deployment-2035384211-kzszj   1/1       Running   0          18s     app=nginx,pod-template-hash=2035384211
nginx-deployment-2035384211-qqcnn   1/1       Running   0          18s    app=nginx,pod-template-hash=2035384211

更新

#更新nginx的版本,默认使用滚动更新策略
$ kubectl set image deployment/nginx-deployment nginx=nginx:1.18
deployment "nginx-deployment" image updated

#或者使用edit
$ kubectl edit deployment/nginx-deployment
deployment "nginx-deployment" edited

#查看 rollout 的状态,只要执行:
$ kubectl rollout status deployment/nginx-deployment
Waiting for rollout to finish: 2 out of 3 new replicas have been updated...
deployment "nginx-deployment" successfully rolled out

回退

#检查下 Deployment 的 revision:

$ kubectl rollout history deployment/nginx-deployment
deployments "nginx-deployment":
REVISION    CHANGE-CAUSE
1           kubectl create -f docs/user-guide/nginx-deployment.yaml --record
2           kubectl set image deployment/nginx-deployment nginx=nginx:1.16
3           kubectl set image deployment/nginx-deployment nginx=nginx:1.18

#因为我们创建 Deployment 的时候使用了 —recored 参数可以记录命令,我们可以很方便的查看每次 revison 的变化
$ kubectl rollout history deployment/nginx-deployment --revision=2
deployments "nginx-deployment" revision 2
  Labels:       app=nginx
          pod-template-hash=1159050644
  Annotations:  kubernetes.io/change-cause=kubectl set image deployment/nginx-deployment nginx=nginx:1.16
  Containers:
   nginx:
    Image:      nginx:1.16
    Port:       80/TCP
     QoS Tier:
        cpu:      BestEffort
        memory:   BestEffort
    Environment Variables:      <none>
  No volumes.

#回退到历史版本
$ kubectl rollout undo deployment/nginx-deployment
deployment "nginx-deployment" rolled back

#也可以使用 --to-revision 参数指定某个历史版本:
$ kubectl rollout undo deployment/nginx-deployment --to-revision=2
deployment "nginx-deployment" rolled back

扩容

#使用命令扩容 Deployment:
$ kubectl scale deployment nginx-deployment --replicas 10
deployment "nginx-deployment" scaled

#使用 (HPA),你可以给 Deployment 设置一个 autoscaler,基于当前 Pod 的 CPU 利用率选择最少和最多的 Pod 数。
$ kubectl autoscale deployment nginx-deployment --min=10 --max=15 --cpu-percent=80
deployment "nginx-deployment" autoscaled
赞(1) 打赏
未经允许不得转载:Anonym0x1 » Deployment

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏