核心对象:Replicaset对象
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-set
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
一个replicaset对象,是由副本数目的定义和一个Pod模板组成的。
而且,它是被Deployment控制器操控的,如下关系
水平拓展/收缩
- Replicaset对象通过“控制器模式”确保系统中的Pod个数永远是用户期望的pod个数(配置文件中)
- 当用户通过 kubectl scale去修改replicas字段的值时,对应deployment的replicaset对象会根据修改的值,新建或删除一个Pod
$ kubectl scale deployment nginx-deployment --replicas=4
deployment.apps/nginx-deployment scaled
滚动升级
- 通过yaml文件,$ kubectl create -f nginx-deployment.yaml --record出来一个带三个pod的集群
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:1.7.9
ports:
- containerPort: 80
- 检查已经正常部署,kubectl get deployments
$ kubectl get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx-deployment 3 0 0 0 1s
- 修改etcd中对应deployment对象的api文件如下
$ kubectl edit deployment/nginx-deployment
...
spec:
containers:
- name: nginx
image: nginx:1.9.1 # 1.7.9 -> 1.9.1
ports:
- containerPort: 80
...
deployment.extensions/nginx-deployment edited
- 通过describe命令,查看events
$ kubectl describe deployment nginx-deployment
...
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
...
Normal ScalingReplicaSet 24s deployment-controller Scaled up replica set nginx-deployment-1764197365 to 1
Normal ScalingReplicaSet 22s deployment-controller Scaled down replica set nginx-deployment-3167673210 to 2
Normal ScalingReplicaSet 22s deployment-controller Scaled up replica set nginx-deployment-1764197365 to 2
Normal ScalingReplicaSet 19s deployment-controller Scaled down replica set nginx-deployment-3167673210 to 1
Normal ScalingReplicaSet 19s deployment-controller Scaled up replica set nginx-deployment-1764197365 to 3
Normal ScalingReplicaSet 14s deployment-controller Scaled down replica set nginx-deployment-3167673210 to 0
可以看到是在修改过镜像版本之后,deployment controller会新建一个replicaset对象(hashid不一样),这个对象的初始pod 副本数是0,然后先对旧的replicaset对象进行水平收缩,再对新的对象进行水平拓展,直到完成更新,这个更新过程叫做滚动更新。
滚动升级优势
- 确保在升级过程中,如果出现失败,还有旧的没有水平收缩的副本可用,提高服务可用性,运维人员可以介入进行处理
此外,deployment控制器还存在RollingUpdateStrategy字段可以配置在yaml文件中,该字段的作用是,配置滚动升级的策略,如maxSurge,maxUnavailable等,可以用数字或者百分比来配置(好灵活,好强大,我喜欢。。。)
综上,Deployment控制器,实际上控制的是replicaset的数目,已经每一个replicaset对象的属性。每一个应用版本,对应的正是一个replicaset ,这个版本的pod数目,由replicaset自身的控制器控制。