k8s–基础–13–replicaset
1、概念
- 是一个控制器,用于维护 同一个Pod 的数量。
- 被Deployment取代了,只要了解就行
1.1、作用
- 维持一组Pod副本的运行。
- 持续监听Pod的运行状态,在Pod发生故障时重启pod,pod数量减少时重新运行新的Pod,保证Pod的数量。
2、怎么工作?
- ReplicaSet 由字段定义
- ReplicaSet 包括一个选择器,该选择器指定如何找到它所管理的Pod、维护多少个pod,以及pod的模板。
- ReplicaSet 通过 创建和删除Pod 来满足期望的pod数量。
- ReplicaSet 需要创建新的Pod时,它将使用其Pod模板。
- ReplicaSet 通过Pods的metadata.ownerReferences字段链接到其Pod,该字段指定当前对象所拥有的资源。
- Pod在其ownerReferences字段中都有其自己的ReplicaSet的标识信息。通过此链接,ReplicaSet可以知道它正在维护的Pod的状态。
- ReplicaSet 通过使用其选择器标识要获取的新Pod。
- 如果存在没有OwnerReference的Pod或OwnerReference不是控制器,并且它与ReplicaSet的选择器匹配,它将由所述的ReplicaSet立即获取
- ReplicaSet 通过Pods的metadata.ownerReferences字段链接到其Pod,该字段指定当前对象所拥有的资源。
3、什么时候使用replicaset
- 一般我们不直接使用replicaset,除非需要对 Kubernetes 进行一些定制化开发
- 一般我们直接使用Deployment来代替replicaset。
3.1、为什么使用Deployment
Deployment 是一个高级 API 对象,底层由replicaset实现,虽然replicaset可以独立使用,但如今,主要由Deployments用作协调Pod创建,删除和更新的机制。因此,建议你在需要副本集时使用deployment。
4、查看 部署一个replicaset时需要哪些字段
4.1、命令
# 查看 replicaset 的定义
kubectl explain replicaset
# 查看 spec 的定义
kubectl explain replicaset.spec
# 查看 template 的定义
kubectl explain replicaset.spec.template
# 查看 template.spec 的定义
kubectl explain replicaset.spec.template.spec
结果:这里只显示replicaset 的定义
[root@master1 test]# kubectl explain replicaset
KIND: ReplicaSet
VERSION: apps/v1
DESCRIPTION:
ReplicaSet ensures that a specified number of pod replicas are running at
any given time.
FIELDS:
apiVersion <string>
APIVersion defines the versioned schema of this representation of an
object. Servers should convert recognized schemas to the latest internal
value, and may reject unrecognized values. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
kind <string>
Kind is a string value representing the REST resource this object
represents. Servers may infer this from the endpoint the client submits
requests to. Cannot be updated. In CamelCase. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
metadata <Object>
If the Labels of a ReplicaSet are empty, they are defaulted to be the same
as the Pod(s) that the ReplicaSet manages. Standard object's metadata. More
info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
spec <Object>
Spec defines the specification of the desired behavior of the ReplicaSet.
More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
status <Object>
Status is the most recently observed status of the ReplicaSet. This data
may be out of date by some window of time. Populated by the system.
Read-only. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
说明
- 需要apiVersion,kind,metadata,spec字段
- kind只能是replicaset
- 在kubernetes1.9+之后,apiversion默认的版本是apps/v1,apps/v1beta2已经被废弃了。
4.2、重要字段说明
4.2.1、replicaset.spec.template
# 定义
kubectl explain replicaset.spec.template
- 定义一个Pod模板,且需要给pod模板设置标签。
- 注意:不要与其他控制器的选择器重叠。
- 这个就是当pod挂掉的时候,新启动的pod的模板
4.2.2、replicaset.spec.template.spec.restartPolicy
# 定义
kubectl explain replicaset.spec.template.spec.restartPolicy
- 重启策略
- 默认值:Always,也是唯一允许的取值
4.2.3、replicaset.spec.selector
# 定义
kubectl explain replicaset.spec.selector
- 标签选择器。
- 可以选择它所匹配的拥有相同标签的pod。
- 当pod挂掉的时候,replicaset就通过标签选择器,选择指定标签的pod模板,再通过pod模板创建pod。
- 在ReplicaSet中,replicaset.spec.template.metadata.labels必须匹配replicaset.spec.selector,否则将被API拒绝。
4.2.4、replicaset.spec.replicas
# 定义
kubectl explain replicaset.spec.replicas
- 指定要同时运行多少个 Pod。
- 在任何时间运行的 Pod 数量可能高于或低于 replicaset.spec.replicas 指定的数量,例如在副本刚刚被增加或减少后、或者 Pod 正在被优雅地关闭、以及替换提前开始。
- 默认值:1
4.3、 ReplicaSet 重要的3个定义
- replicas: Pod 副本数目
- selector: 选择器
- template:Pod 模板
4.3.1、selector
replicaSet使用 selector(选择器)中的定义,查找对应的Pod模板,通过pod模板,创建或删除Pod,下面就是持有三个 Pod对象的 Replica 拓扑图:
被 ReplicaSet 持有的Pod有一个metadata.ownerReferences指针,该指针指向当前的 ReplicaSet,表示当前Pod的所有者,这个引用主要会被集群中的垃圾收集器使用以清理失去所有者的Pod对象。
5、实现原理
- 所有 ReplicaSet 对象的增删改查都是由 ReplicaSetController(控制器) 完成的
- ReplicaSetController(控制器) 会通过 Informer 监听 ReplicaSet 和Pod的变更事件并将其加入持有的待处理队列
- ReplicaSetController 中的 queue 其实就是一个存储 待处理 ReplicaSet 的 对象池,它运行的几个 Goroutine 会从队列中取出最新的数据进行处理,上图展示了事件从发生到被处理的流向。
6、案例:使用replicaset部署一个应用
6.1、配置文件
vim /root/test/replicaset_nginx.yaml
内容
apiVersion: apps/v1
kind: ReplicaSet
metadata:
# ReplicaSet 的名称
name: replicaset-nginx
# ReplicaSet 的标签
labels:
k1: k1_la
k2: k2_la
spec:
# 副本数目
replicas: 3
# 选择器
selector:
# 选择器匹配的标签
matchLabels:
nginx_pod: nginx_pod_la
# Pod 模板
template:
metadata:
# Pod的标签
labels:
nginx_pod: nginx_pod_la
spec:
containers:
# 容器名称
- name: nginx
# 镜像
image: nginx
# 镜像策略
imagePullPolicy: IfNotPresent
# 容器端口
ports:
- containerPort: 80
6.2、启动
kubectl apply -f /root/test/replicaset_nginx.yaml
6.3、验证
6.3.1、查看replicaset
kubectl get replicaset
# rs是replicaset的缩写
# kubectl get rs
6.3.2、查看replicaset对应的pod
kubectl get pods
6.3.3、 删除一个 replicaSet 持有的 Pod,那么replicaSet会重新启动一个新的Pod
kubectl get pods | grep replicaset-nginx
kubectl delete pods replicaset-nginx-rtrgg
kubectl get pods | grep replicaset-nginx
6.3.4、 删除一个 replicaSet, 所有相关的Pod也都会被删除
kubectl delete -f /root/test/replicaset_nginx.yaml
kubectl get pods | grep replicaset-nginx