k8s RC (Replication Controller)和RS (Replicaset)的作用和区别

一、Replication Controller(RC)

RC是pod的复制抽象,用于解决pod的扩容缩容问题,保证应用能够持续运行。

1. 作用

a.确保pod数量:

RC用来管理正常运行Pod数量,一个RC可以由一个或多个Pod组成,在RC被创建后,系统会根据定义好的副本数来创建Pod数量。在运行过程中,如果Pod数量小于定义的,就会重启停止的或重新分配Pod,反之则杀死多余的。

b.确保pod健康:

当pod不健康,运行出错或者无法提供服务时,RC也会杀死不健康的pod,重新创建新的。

c.弹性伸缩 :

在业务高峰或者低峰期的时候,可以通过RC动态的调整pod的数量来提高资源的利用率。同时,配置相应的监控功能(Hroizontal Pod Autoscaler),会定时自动从监控平台获取RC关联pod的整体资源使用情况,做到自动伸缩。

d.滚动升级:

滚动升级为一种平滑的升级方式,通过逐步替换的策略,保证整体系统的稳定,在初始化升级的时候就可以及时发现和解决问题,避免问题不断扩大。

2. RC和pod关系

RC中selector设置一个label,去关联pod的label,selector的label与pod的label相同,那么该pod就是该rc的一个实例;

RC中Replicas设置副本数大小,系统根据该值维护pod的副本数。

Replicaset在继承Pod的所有特性的同时, 它可以利用预先创建好的模板定义副本数量并自动控制, 通过改变Pod副本数量实现Pod的扩容和缩容

缺点: 无法修改template模板, 也就无法发布新的镜像版本。

3. RC模块(内置pod模块)的yaml文件

{
    "kind": "ReplicationController",
    "apiVersion": "v1",
    "metadata": {
        "name": "app-tomcat",
        "namespace": "default",
        "labels": {
            "name": "app-tomcat"
        }
    },
    "spec": {
        "replicas": 2(副本数),
        "selector": {
            "name": "app-tomcat(选择的pod的label)"
        },
        "template": {
            "metadata": {
                "labels": {
                    "name": "app-tomcat(pod的label)"
                }
            },
            "spec": {
                "containers": [
                    {
                        "name": "kaifa2-group-tomcat",
                        "image": "192.168.54.64:5000/tomcat-248:V1.0",
                        "command": [
                            "/bin/bash",
                            "/opt/apache-tomcat-7.0.57/bin/catalina.sh",
                            "run"
                        ],
                        "env": [
                            {
                                "name": "url_addr",
                                "value": "http://192.168.54.64:8080/paas/jsp/index.jsp"
                            }
                        ],
                        "resources": {
                            "limits": {
                                "cpu": "1",
                                "memory": "1073741824"
                            },
                            "requests": {
                                "cpu": "100m",
                                "memory": "104857600"
                            }
                        },
                        "imagePullPolicy": "Always"
                    }
                ],
                "restartPolicy": "Always",
                "nodeSelector": {
                    "group": "node1"
                }
            }
        }
    }
}

二、replicaset(RS)

被认为 是“升级版”的RC,在新版本的 Kubernetes 中建议使用 ReplicaSet(简称为RS )来取代 ReplicationController。

ReplicaSet 跟 ReplicationController 没有本质的不同,只是名字不一样,RS也是用于保证与label selector匹配的pod数量维持在期望状态。

 但ReplicaSet 支持集合式的 selector(ReplicationController 仅支持等式)。

Kubernetes官方强烈建议避免直接使用ReplicaSet,而应该通过Deployment来创建RS和Pod。

1. 创建Replicaset

vi replicaset.yaml

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: frontend
  labels:
    app: guestbook
    tier: frontend
spec:
  # this replicas value is default
  # modify it according to your case
  replicas: 3
  selector:
    matchLabels:
      tier: frontend
    matchExpressions:
      - {key: tier, operator: In, values: [frontend]}
  template:
    metadata:
      labels:
        app: guestbook
        tier: frontend
    spec:
      containers:
      - name: php-redis
        image: gcr.io/google_samples/gb-frontend:v3
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        env:
        - name: GET_HOSTS_FROM
          value: dns
          # If your cluster config does not include a dns service, then to
          # instead access environment variables to find service host
          # info, comment out the 'value: dns' line above, and uncomment the
          # line below.
          # value: env
        ports:
        - containerPort: 80 

kubectl create -f replicaset.yaml

以上RS描述文件中,selector除了可以使用matchLabels,还支持集合式的操作:

matchExpressions:
      - {key: tier, operator: In, values: [frontend]}

 2. 查看replicaset

kubectl get replicaset

kubectl describe replicaset

3.删除replicaset

使用kubectl delete命令会删除此RS以及它管理的Pod。在Kubernetes删除RS前,会将RS的replica调整为0,等待所有的Pod被删除后,在执行RS对象的删除。

如果希望仅仅删除RS对象(保留Pod),请使用kubectl delete命令时添加--cascade=false选项。

kubectl delete replicaset demo-rc

4. ReplicaSet的伸缩

通过修改.spec.replicas的值可以实时修改RS运行的Pod数量。

5. Horizontal Pod Autoscaler(HPA)

RS可以通过HPA来根据一些运行时指标实现自动伸缩,下面是一个简单的例子:

apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: frontend-scaler
spec:
  scaleTargetRef:
    kind: ReplicaSet
    name: frontend
  minReplicas: 3
  maxReplicas: 10
  targetCPUUtilizationPercentage: 50

上面的描述文件会创建一个名为frontend-scaler的HorizontalPodAutoscaler,它会根据CPU的运行参数来对名为frontend的RS进行自动伸缩。
 

猜你喜欢

转载自blog.csdn.net/weixin_40482816/article/details/118548178
今日推荐