k8s--基础--13--replicaset

k8s–基础–13–replicaset


1、概念

  1. 是一个控制器,用于维护 同一个Pod 的数量。
  2. 被Deployment取代了,只要了解就行

1.1、作用

  1. 维持一组Pod副本的运行。
  2. 持续监听Pod的运行状态,在Pod发生故障时重启pod,pod数量减少时重新运行新的Pod,保证Pod的数量。

2、怎么工作?

  1. ReplicaSet 由字段定义
  2. ReplicaSet 包括一个选择器,该选择器指定如何找到它所管理的Pod、维护多少个pod,以及pod的模板。
  3. ReplicaSet 通过 创建和删除Pod 来满足期望的pod数量。
  4. ReplicaSet 需要创建新的Pod时,它将使用其Pod模板。
    1. ReplicaSet 通过Pods的metadata.ownerReferences字段链接到其Pod,该字段指定当前对象所拥有的资源。
      1. Pod在其ownerReferences字段中都有其自己的ReplicaSet的标识信息。通过此链接,ReplicaSet可以知道它正在维护的Pod的状态。
    2. ReplicaSet 通过使用其选择器标识要获取的新Pod。
      1. 如果存在没有OwnerReference的Pod或OwnerReference不是控制器,并且它与ReplicaSet的选择器匹配,它将由所述的ReplicaSet立即获取

3、什么时候使用replicaset

  1. 一般我们不直接使用replicaset,除非需要对 Kubernetes 进行一些定制化开发
  2. 一般我们直接使用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

说明
  1. 需要apiVersion,kind,metadata,spec字段
  2. kind只能是replicaset
  3. 在kubernetes1.9+之后,apiversion默认的版本是apps/v1,apps/v1beta2已经被废弃了。

4.2、重要字段说明

4.2.1、replicaset.spec.template

# 定义
kubectl explain replicaset.spec.template
  1. 定义一个Pod模板,且需要给pod模板设置标签。
    1. 注意:不要与其他控制器的选择器重叠。
  2. 这个就是当pod挂掉的时候,新启动的pod的模板

4.2.2、replicaset.spec.template.spec.restartPolicy

# 定义
kubectl explain replicaset.spec.template.spec.restartPolicy
  1. 重启策略
  2. 默认值:Always,也是唯一允许的取值

4.2.3、replicaset.spec.selector

# 定义
kubectl explain replicaset.spec.selector
  1. 标签选择器。
  2. 可以选择它所匹配的拥有相同标签的pod。
  3. 当pod挂掉的时候,replicaset就通过标签选择器,选择指定标签的pod模板,再通过pod模板创建pod。
  4. 在ReplicaSet中,replicaset.spec.template.metadata.labels必须匹配replicaset.spec.selector,否则将被API拒绝。

4.2.4、replicaset.spec.replicas

# 定义
kubectl explain replicaset.spec.replicas
  1. 指定要同时运行多少个 Pod。
  2. 在任何时间运行的 Pod 数量可能高于或低于 replicaset.spec.replicas 指定的数量,例如在副本刚刚被增加或减少后、或者 Pod 正在被优雅地关闭、以及替换提前开始。
  3. 默认值:1

4.3、 ReplicaSet 重要的3个定义

在这里插入图片描述

  1. replicas: Pod 副本数目
  2. selector: 选择器
  3. template:Pod 模板

4.3.1、selector

replicaSet使用 selector(选择器)中的定义,查找对应的Pod模板,通过pod模板,创建或删除Pod,下面就是持有三个 Pod对象的 Replica 拓扑图:

在这里插入图片描述

被 ReplicaSet 持有的Pod有一个metadata.ownerReferences指针,该指针指向当前的 ReplicaSet,表示当前Pod的所有者,这个引用主要会被集群中的垃圾收集器使用以清理失去所有者的Pod对象。

5、实现原理

在这里插入图片描述

  1. 所有 ReplicaSet 对象的增删改查都是由 ReplicaSetController(控制器) 完成的
  2. ReplicaSetController(控制器) 会通过 Informer 监听 ReplicaSet 和Pod的变更事件并将其加入持有的待处理队列
  3. 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

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值