1.Deployment概念
Deployment控制器为 Pod 和 ReplicaSet 提供了一个声明式更新的方法,在Deployment对象中描述一个期望的状态,Deployment控制器就会按照一定的控制速率把实际状态改成期望状态,通过定义一个Deployment控制器会创建一个新的ReplicaSets控制器,通过replicaset创建pod,删除Deployment控制器,也会删除Deployment控制器下对应的ReplicaSet控制器和pod资源。
deplopyment为更高级一些的控制器,支持更多的功能字段:paused,revisionHistoryLimit,strategy 历史版本 滚动更新 丰富的更新策略
典型的使用场景:
1)创建无状态的应用
2)滚动更新和回滚
[root@master ~]# vim deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
动态观察pod创建
[root@master ~]# kubectl apply -f deployment.yaml
[root@master ~]# kubectl get pods -w
NAME READY STATUS RESTARTS AGE
nginx-deployment-d46f5678b-2m662 0/1 ContainerCreating 0 8s
nginx-deployment-d46f5678b-7bszd 0/1 ContainerCreating 0 8s
nginx-deployment-d46f5678b-psc25 0/1 ContainerCreating 0 8s
nginx-deployment-d46f5678b-psc25 1/1 Running 0 20s
nginx-deployment-d46f5678b-2m662 1/1 Running 0 32s
nginx-deployment-d46f5678b-7bszd 1/1 Running 0 45s
pod 的查看
[root@master ~]# kubectl get pod -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-d46f5678b-2m662 1/1 Running 0 57s 10.244.1.41 node1
nginx-deployment-d46f5678b-7bszd 1/1 Running 0 57s 10.244.1.42 node1
nginx-deployment-d46f5678b-psc25 1/1 Running 0 57s 10.244.1.40 node1
[root@master ~]# curl 10.244.1.41
Welcome to nginx!
[root@master ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 3m
[root@master ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deployment-678645bf77 3 3 3 4m
[root@master ~]# kubectl describe deployment nginx-deployment
列出deployment创建的拥有标签是app=nginx的pods
[root@master ~]# kubectl get pods -l app=nginx
[root@master ~]# kubectl scale deployment nginx-deployment --replicas=1
[root@master ~]# kubectl edit deployments.apps nginx-deployment
replicas: 4
第一个spec.replicas数据修改,如果没有定义镜像拉取策略,默认总是拉取,需要修改spec.template.spec.containers.imagePullPolicy:IfNotPresent
[root@master ~]# kubectl rollout history deployment nginx-deployment
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
1 <none>
2 <none>
[root@master ~]# kubectl rollout undo deployment nginx-deployment --to-revision=2
[root@master ~]# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deployment-558fc78868 2 2 2 17m
nginx-deployment-d46f5678b 0 0 0 25m
[root@master ~]# kubectl delete -f nginx.yaml
假设定义的pod是5个,滚动更新的设置值如下
Max Surge :1 #最多超出1,此时最多6
Max Unavailable: 1 #最大不可用1,此时最少保留4个
创建1new,杀死6-4=2old,再创建2new,杀死2old,再创建2new,杀死1old,此时5new
[root@master ~]# kubectl explain deployment.spec.strategy.rollingUpdate
KIND: Deployment
VERSION: apps/v1
RESOURCE: rollingUpdate <Object>
DESCRIPTION:
Rolling update config params. Present only if DeploymentStrategyType =
RollingUpdate.
Spec to control the desired behavior of rolling update.
FIELDS:
maxSurge <string>
The maximum number of pods that can be scheduled above the desired number
of pods. Value can be an absolute number (ex: 5) or a percentage of desired
pods (ex: 10%). This can not be 0 if MaxUnavailable is 0. Absolute number
is calculated from percentage by rounding up. Defaults to 25%. Example:
when this is set to 30%, the new ReplicaSet can be scaled up immediately
when the rolling update starts, such that the total number of old and new
pods do not exceed 130% of desired pods. Once old pods have been killed,
new ReplicaSet can be scaled up further, ensuring that total number of pods
running at any time during the update is at most 130% of desired pods.
maxUnavailable <string>
The maximum number of pods that can be unavailable during the update. Value
can be an absolute number (ex: 5) or a percentage of desired pods (ex:
10%). Absolute number is calculated from percentage by rounding down. This
can not be 0 if MaxSurge is 0. Defaults to 25%. Example: when this is set
to 30%, the old ReplicaSet can be scaled down to 70% of desired pods
immediately when the rolling update starts. Once new pods are ready, old
ReplicaSet can be scaled down further, followed by scaling up the new
ReplicaSet, ensuring that the total number of pods available at all times
during the update is at least 70% of desired pods.
解释:
核心作用在于代用户创建指定数量的pod副本,并确保pod副本一直处于满足用户期望的数量,起到多退少补的作用,并且还具有自动扩容缩容等机制。但是ReplicaSet并不是我们直接使用的控制器
ReplicaSet这个控制器主要由三个部分组成:
(1)用户期望的pod副本数:用来定义由这个控制器管控的pod副本有几个
(2)标签选择器:选定哪些pod是自己管理的,如果通过标签选择器选到的pod副本数量少于我们指定的数量,需要用到下面的字段
(3)pod资源模板:如果集群中现存的pod数量不够我们定义的副本中期望的数量怎么办,需要新建pod,这就需要pod模板
Deployment控制器是工作在ReplicaSet之上的,Deployment通过控制ReplicaSet控制pod的,并不是直接控制pod,ReplicaSet有的功能它都有,支持自动扩容缩容,滚动更新和回滚等机制,并提供了一个声明式的定义功能,这种声明式定义使得我们将来创建资源时可以基于声明的逻辑来定义,我们那些所有定义的资源可以随时重新进行声明,随时改变我们在apiserver上的定义的目标期望状态,只要那些资源支持动态运行时修改,都能修改;是帮我们管理无状态的应用的最好的控制器;