Kubernetes:深入了解Deployment

Kubernetes:深入了解Deployment

kuberntes部署应用最常用的一个控制器,Deployment可以部署无状态应用,像web、api就是无状态应用

Pod与controllers的关系

pod对象作为一个最小的调度单元,很少直接创建,一般使用控制器来完成一个应用的部署,控制器在集群上管理和运行容器的对象,这个对象是高级的,pod是一个抽象,不是一个实际存在的,抽象管理其他容器,为了亲密性的应用而存在的,控制器能完成更高级更负责的功能,比如弹性伸缩、滚动升级。

控制器还有另一个学名叫工作负载,通过标签相互的关联对象

controllers: 在集群上管理和运行容器的对象
通过label-selector相关联
Pod通过控制器实现应用的运维,如伸缩、滚动升级等

Kubernetes:深入了解Deployment

Deployment功能与应用场景

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

使用Deployment部署应用

[ yml]# cat deployment.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: web
spec:
  selector:
    matchLabels:
      app: web
  replicas: 3
  template:
    metadata:
      labels:
        app: web
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
[ yml]# kubectl create -f deployment.yaml 
deployment.apps/nginx-deployment created
[ yml]# kubectl get pods
NAME                               READY   STATUS    RESTARTS   AGE
nginx-deployment-79cfcb457-bqcfg   1/1     Running   0          34s
nginx-deployment-79cfcb457-k6n9z   1/1     Running   0          34s
nginx-deployment-79cfcb457-mqvvp   1/1     Running   0          34s

[ yml]# kubectl get deploy
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3/3     3            3           57s
[ yml]#

应用升级

这是一个滚动升级的操作,现在部署了三个副本他会先起一个容器,这个容器为新的镜像,当新的镜像容器为运行的状态他将打标记的旧的容器删除掉,以此类推,升级第二个容器,最终将旧的容器替换为新的

[ yml]# kubectl set image deployments.apps nginx-deployment nginx=nginx:1.16.1
deployment.apps/nginx-deployment image updated
[ yml]# kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-7bfdd85555-hfw74   1/1     Running   0          47s
nginx-deployment-7bfdd85555-qclqj   1/1     Running   0          37s
nginx-deployment-7bfdd85555-s99vf   1/1     Running   0          41s
[ yml]#

查看升级状态

[ yml]# kubectl rollout status deployment nginx-deployment 
deployment "nginx-deployment" successfully rolled out
[ yml]#

应用回滚

[ yml]# kubectl rollout undo deployment nginx-deployment 
deployment.apps/nginx-deployment rolled back
[ yml]# 

#回滚指定版本
[ yml]# kubectl rollout undo deployment nginx-deployment --to-revision=1
deployment.apps/nginx-deployment rolled back
[ yml]#

应用扩容

[ yml]# kubectl scale deployment nginx-deployment --replicas=5
deployment.apps/nginx-deployment scaled
[ yml]# kubectl get pods
NAME                               READY   STATUS    RESTARTS   AGE
nginx-deployment-79cfcb457-fq9n8   1/1     Running   0          8m52s
nginx-deployment-79cfcb457-hh87r   1/1     Running   0          8m44s
nginx-deployment-79cfcb457-hksbw   1/1     Running   0          83s
nginx-deployment-79cfcb457-hxdnd   1/1     Running   0          83s
nginx-deployment-79cfcb457-pc64r   1/1     Running   0          8m41s
[ yml]#

应用缩容

[ yml]# kubectl scale deployment nginx-deployment --replicas=2
deployment.apps/nginx-deployment scaled
[ yml]# kubectl get pods
NAME                               READY   STATUS    RESTARTS   AGE
nginx-deployment-79cfcb457-fq9n8   1/1     Running   0          9m49s
nginx-deployment-79cfcb457-hh87r   1/1     Running   0          9m41s
[ yml]#

replicasets

每一次创建一个deployment都会创建一个rs,而rs会去关联多个pod,rs对判断副本和当前运行的副本数进行比对,帮deployment控制副本数的功能,和历史版本记录的功能

滚动更新时,触发了一个滚动更新策略,创建一个新的rs,用新的镜像起Pod,在滚动升级期间新pod都会被关联到services,新创建的pod准备就绪了,会删除一个旧rs下的pod在创建一个新pod,直到就的pod为0

[ yml]# kubectl get replicasets
NAME                          DESIRED   CURRENT   READY   AGE
nginx-deployment-5db69b7577   0         0         0       50m
nginx-deployment-79cfcb457    2         2         2       84m
nginx-deployment-7bfdd85555   0         0         0       49m
[ yml]#

相关推荐