K8s控制器Replicaset

K8s控制器Replicaset

1 Replicaset控制器:概念、原理解读

1.1 Replicaset概述

ReplicaSet是kubernetes中的一种副本控制器,简称rs,主要作用是控制由其管理的pod,使pod副本的数量始终维持在预设的个数。它的主要作用就是保证一定数量的Pod能够在集群中正常运行,它会持续监听这些Pod的运行状态,在Pod发生故障时重启pod,pod数量减少时重新运行新的Pod副本。

官方推荐不要直接使用ReplicaSet,用Deployments取而代之,Deployments是比ReplicaSet更高级的概念,它会管理ReplicaSet并提供很多其它有用的特性,最重要的是Deployments支持声明式更新,声明式更新的好处是不会丢失历史变更。所以Deployment控制器不直接管理Pod对象,而是由Deployment 管理ReplicaSet,再由ReplicaSet负责管理Pod对象。

K8s控制器Replicaset_Pod

1.2 工作原理:如何管理Pod?

Replicaset核心作用在于代用户创建指定数量的pod副本,并确保pod副本一直处于满足用户期望的数量, 起到多退少补的作用,并且还具有自动扩容缩容等机制。

1.3 ReplicaSet组成部分

Replicaset控制器主要由三个部分组成:

  • replicas: 用户期望的pod副本数:用来定义由这个控制器管控的pod副本有几个;
  • selector: 标签选择器:选定哪些pod是自己管理的,如果通过标签选择器选到的pod副本数量少于我们指定的数量,需要用到下面的组件;
  • template: pod资源模板:如果集群中现存的pod数量不够我们定义的副本中期望的数量怎么办,需要新建pod,这就需要pod模板,新建的pod是基于模板来创建的;

K8s控制器Replicaset_nginx_02

2 资源清单文件编写技巧

1、#查看定义Replicaset资源需要的字段有哪些?
[root@xianchaomaster1 ~]# kubectl explain rs
KIND:     ReplicaSet
VERSION:  apps/v1
DESCRIPTION:
     ReplicaSet ensures that a specified number of pod replicas are running at
     any given time.
FIELDS:
   apiVersion  <string>  #当前资源使用的api版本,跟VERSION:  apps/v1保持一致
   kind  <string>     #资源类型,跟KIND: ReplicaSet保持一致
   metadata  <Object> #元数据,定义Replicaset名字的
   spec  <Object>     ##定义副本数、定义标签选择器、定义Pod模板
   status  <Object> #状态信息,不能改

2、#查看replicaset的spec字段如何定义?
[root@xianchaomaster1 ~]# kubectl explain rs.spec
KIND:     ReplicaSet
VERSION:  apps/v1
RESOURCE: spec <Object>
DESCRIPTION:
     Spec defines the specification of the desired behavior of the ReplicaSet.
     More info:
     https://git.k8s.io/community/contributors/devel/sig-architecture/#spec-and-status
     ReplicaSetSpec is the specification of a ReplicaSet.
FIELDS:
   minReadySeconds  <integer>
   replicas  <integer>  #定义的pod副本数,根据我们指定的值创建对应数量的pod
   selector  <Object> -required-  #用于匹配pod的标签选择器
   template  <Object>      #定义Pod的模板,基于这个模板定义的所有pod是一样的

3、#查看replicaset的spec.template字段如何定义?
#对于template而言,其内部定义的就是pod,pod模板是一个独立的对象
[root@xianchaomaster1 ~]# kubectl explain rs.spec.template
KIND:     ReplicaSet
VERSION:  apps/v1
RESOURCE: template <Object>
DESCRIPTION:
     Template is the object that describes the pod that will be created if
     insufficient replicas are detected。PodTemplateSpec describes the data a pod should have when created from a
     template
FIELDS:
   metadata  <Object>
   spec  <Object>
   
[root@xianchaomaster1 ~]# kubectl explain rs.spec.template.spec
#通过上面可以看到,ReplicaSet资源中有两个spec字段。第一个spec声明的是ReplicaSet定义多少个Pod副本(默认将仅部署1个Pod)、匹配Pod
标签的选择器、创建pod的模板。第二个spec是spec.template.spec:主要用于Pod里的容器属性等配置。 
#.spec.template里的内容是声明Pod对象时要定义的各种属性,所以这部分也叫做PodTemplate(Pod模板)。还有一个值得注意的地方是:在
.spec.selector中定义的标签选择器必须能够匹配到spec.template.metadata.labels里定义的Pod标签,否则Kubernetes将不允许创建ReplicaSet。

3 使用案例

vim replicaset.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx
  namespace: default
  labels:
    app: nginx
    tier: frontend
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        resources:
          requests:
            memory: "100Mi"
            cpu: "250m"
          limits:
            memory: "100Mi"
            cpu: "250m"
        startupProbe:
          initialDelaySeconds: 20
          periodSeconds: 5
          timeoutSeconds: 10
          httpGet:
            scheme: HTTP
            port: 80
            path: /index.html
        readinessProbe:
          initialDelaySeconds: 20
          periodSeconds: 5
          timeoutSeconds: 10
          httpGet:
            scheme: HTTP
            port: 80
            path: /index.html
        livenessProbe:
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
          httpGet:
            scheme: HTTP
            port: 80
            path: /index.html

kubectl apply -f replicaset.yaml

K8s控制器Replicaset_Pod_03

K8s控制器Replicaset_nginx_04

4 资源清单详细说明

#资源清单详细说明
apiVersion: apps/v1  #ReplicaSet 这个控制器属于的核心群组
kind: ReplicaSet  #创建的资源类型
metadata:
  name: frontend  #控制器的名字
  labels:
    app: guestbook
    tier: frontend
spec:
  replicas: 3   #管理的pod副本数量
  selector:
    matchLabels:
      tier: frontend  #管理带有tier=frontend标签的pod
  template:  #定义pod的模板
    metadata:
      labels:
        tier: frontend 
    #pod标签,一定要有,这样上面控制器就能找到它要管理的pod是哪些了
    spec:
      containers: #定义pod里运行的容器
      - name: php-redis #定义容器的名字
        image: yecc/-google_samples-gb-frontend:v3
        ports:    #定义端口
        - name: http  #定义容器的名字
          containerPort:  80 #定义容器暴露的端口

5 管理pod:扩容、缩容、更新

5.1 实现pod的动态扩容

ReplicaSet最核心的功能是可以动态扩容和回缩,如果我们觉得两个副本太少了,想要增加,只需

要修改配置文件replicaset.yaml里的replicas的值即可,原来replicas: 2,现在变成replicaset: 4,修改之后,执行如下命令更新:

kubectl apply -f replicaset.yaml

K8s控制器Replicaset_nginx_05

5.2 实现pod的动态缩容

如果我们觉得5个Pod副本太多了,想要减少,只需要修改配置文件replicaset.yaml里的版权声明,replicas的值即可,把replicaset:4 变成replicas: 3,修改之后,执行如下命令更新:

K8s控制器Replicaset_Pod_06

在降低集合规模时,ReplicaSet控制器通过对可用的所有Pod进行排序来优先选择要被删除的那些Pod。 其一般性算法如下:

  1. 首先选择剔除悬决(Pending,且不可调度)的各个Pod;
  2. 如果设置了/pod-deletion-cost 注解,则注解值较小的优先被裁减掉;
  3. 所处节点上副本个数较多的Pod优先于所处节点上副本较少者;
  4. 如果Pod 的创建时间不同,最近创建的 Pod 优先于早前创建的 Pod 被裁减(创建时间是按整数幂级来分组的);
  5. 如果以上比较结果都相同,则随机选择;

5.3 实现pod的更新

两个node节点给nginx:latest打个tag。

docker tag nginx:latest nginx:latest02

修改yaml配置文件。

image: nginx:latest02

kubectl apply -f replicaset.yaml

K8s控制器Replicaset_nginx_07

执行完发现pod启动时间都没有从新开始,也就不可能使用新的镜像。

上面可以看到,虽然replicaset.yaml修改了镜像,执行了kubectl apply -f replicaset.yaml,但是pod还是用的nginx:latest这个镜像,没有实现自动更新。

手动杀死一个pod,让其重新生成pod,镜像地址也换成新的了

kubectl delete pod nginx-8w26n

K8s控制器Replicaset_Pod_08

5.4 总结

生产环境如果升级,可以删除一个pod,观察一段时间之后没问题再删除另一个pod,但是这样需要人工干预多次;实际生产环境一般采用蓝绿发布,原来有一个rs1,再创建一个rs2(控制器),通过修改service标签,修改service可以匹配到rs2的控制器,这样才是蓝绿发布,这个也需要我们精心的部署规划,我们有一个控制器就是建立在rs之上完成的,叫做Deployment。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值