掌握K8s ReplicaSet:资源定义、控制机制与实战示例全解析

掌握K8s ReplicaSet:资源定义、控制机制与实战示例全解析

1 基本认识

作用:保持指定数量的pod存活,和rc的相似,功能扩展性强,更轻量。

#基本用法
[root@k8s-master01 /data/manifests/rs]# kubectl explain rs.spec
GROUP:      apps
KIND:       ReplicaSet
VERSION:    v1

FIELD: spec <ReplicaSetSpec>


DESCRIPTION:
    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
    ReplicaSetSpec is the specification of a ReplicaSet.
    
FIELDS:
  minReadySeconds	<integer>
    Minimum number of seconds for which a newly created pod should be ready
    without any of its container crashing, for it to be considered available.
    Defaults to 0 (pod will be considered available as soon as it is ready)

  replicas	<integer>
    Replicas is the number of desired pods. This is a pointer to distinguish
    between explicit zero and unspecified. Defaults to 1. More info:
    https://kubernetes.io/docs/concepts/workloads/controllers/replicaset

  selector	<LabelSelector> -required-
    Selector is a label query over pods that should match the replica count.
    Label keys and values that must match in order to be controlled by this
    replica set. It must match the pod template's labels. More info:
    https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors

  template	<PodTemplateSpec>
    Template is the object that describes the pod that will be created if
    insufficient replicas are detected. More info:
    https://kubernetes.io/docs/concepts/workloads/controllers/replicaset/#pod-template

2 创建rs资源实例

2.1 案例一:基于matchLabels实现相较于rc类似功能

2.1.1 编写资源清单

#编写资源清单,实现rc相似的功能
[root@k8s-master01 /data/manifests/rs]# cat 01-rs-matchLabels.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: dinginx-rs
spec:
  #表示rs启动后的副本数量
  replicas: 3
  #定义如何关联pod
  selector:
    #基于标签关联pod
    matchLabels:
      apps: web
  #定义pod的模版
  template:
    metadata:
      labels:
        apps: web
        version: v1
    spec:
      containers:
      - image: harbor.dinginx.org/dinginx/app-dinginx:v1
        name: dinginx-web001

2.1.2 创建资源并检查

#创建资源清单
[root@k8s-master01 /data/manifests/rs]# kubectl apply -f .
replicaset.apps/dinginx-rs created、
#查看资源
[root@k8s-master01 /data/manifests/rs]# kubectl get pods --show-labels 
NAME               READY   STATUS    RESTARTS   AGE   LABELS
dinginx-rs-f2wgf   1/1     Running   0          62s   apps=web,version=v1
dinginx-rs-vkgnz   1/1     Running   0          62s   apps=web,version=v1
dinginx-rs-vwl26   1/1     Running   0          63s   apps=web,version=v1

2.1.3 测试删除资源

[root@k8s-master01 /data/manifests/rs]# kubectl delete pods -l apps=web
pod "dinginx-rs-4gc9m" deleted from default namespace
pod "dinginx-rs-k5mr5" deleted from default namespace
pod "dinginx-rs-rgrws" deleted from default namespace

#pod自动创建
[root@k8s-master01 /data/manifests/rs]# kubectl get pods -owide
NAME               READY   STATUS    RESTARTS   AGE   IP             NODE                     NOMINATED NODE   READINESS GATES
dinginx-rs-f2wgf   1/1     Running   0          2s    10.244.2.2     k8s-node02.dinginx.org   <none>           <none>
dinginx-rs-vkgnz   1/1     Running   0          2s    10.244.2.3     k8s-node02.dinginx.org   <none>           <none>
dinginx-rs-vwl26   1/1     Running   0          3s    10.244.1.186   k8s-node01.dinginx.org   <none>           <none>

2.2 案例二:基于matchExporessions实现基于条件表达式的标签匹配机制案例

2.2.1 功能说明

#功能说明,此处使用matchExpressions基于条件表达式的标签匹配机制
[root@k8s-master01 /data/manifests/rs]# kubectl explain rs.spec.selector
GROUP:      apps
KIND:       ReplicaSet
VERSION:    v1

FIELD: selector <LabelSelector>

DESCRIPTION:
    Selector is a label query over pods that should match the replica count.
    Label keys and values that must match in order to be controlled by this
    replica set. It must match the pod template's labels. More info:
    https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors
    A label selector is a label query over a set of resources. The result of
    matchLabels and matchExpressions are ANDed. An empty label selector matches
    all objects. A null label selector matches no objects.
    
FIELDS:
  matchExpressions	<[]LabelSelectorRequirement>
    matchExpressions is a list of label selector requirements. The requirements
    are ANDed.

  matchLabels	<map[string]string>
    matchLabels is a map of {key,value} pairs. A single {key,value} in the
    matchLabels map is equivalent to an element of matchExpressions, whose key
    field is "key", the operator is "In", and the values array contains only
    "value". The requirements are ANDed.

2.2.2 准备测试环境

#准备测试环境
[root@k8s-master01 /data/manifests/rs]# for i in {1..4};do kubectl run  dinginx0$i --image=harbor.dinginx.org/dinginx/app-dinginx:v1 --labels apps=v$i;done
pod/dinginx01 created
pod/dinginx02 created
pod/dinginx03 created
#查看资源信息
[root@k8s-master01 /data/manifests/rs]# kubectl get pods -owide --show-labels 
NAME        READY   STATUS    RESTARTS   AGE   IP             NODE                     NOMINATED NODE   READINESS GATES   LABELS
dinginx01   1/1     Running   0          8s    10.244.2.36    k8s-node02.dinginx.org   <none>           <none>            apps=v1
dinginx02   1/1     Running   0          8s    10.244.2.37    k8s-node02.dinginx.org   <none>           <none>            apps=v2
dinginx03   1/1     Running   0          8s    10.244.1.212   k8s-node01.dinginx.org   <none>           <none>            apps=v3

2.2.3 编写rs资源清单

#编写rs-matchexpression资源清单
[root@k8s-master01 /data/manifests/rs]# cat 02-rs-matchExpressions.yaml 
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: dinginx-rs-matchexpressions
spec:
  #表示rs启动后的副本数量
  replicas: 5
  #定义如何关联pod
  selector:
    #基于标签表达式关联pod
    matchExpressions:
      #代表的是标签的key
    - key: apps
      #代表的标签的value
      values:
      - web
      - v1
      - v2
      - v3
      #指定key和value之间的关系: In, NotIn, Exists and DoesNotExist.
      # In:
      #     key的值必须在value的列表中。
      # NothIn:
      #     和In相反
      # Exists:
      #     只要存在key即可,value任意,因此可以省略value字段
      # DoesNotExist:
      #     不存在指定的key,和Exits相反,因此可以省略value字段
      operator: In
  #定义pod的模版
  template:
    metadata:
      labels:
        apps: web
        version: v1
        run: dinginx
    spec:
      containers:
      - image: harbor.dinginx.org/dinginx/app-dinginx:v1
        name: dinginx-web001

2.2.4 创建资源并检查

[root@k8s-master01 /data/manifests/rs]# kubectl apply -f 02-rs-matchExpressions.yaml 
replicaset.apps/dinginx-rs-matchexpressions created
[root@k8s-master01 /data/manifests/rs]# kubectl get pods -owide --show-labels 
NAME                                READY   STATUS    RESTARTS   AGE    IP             NODE                     NOMINATED NODE   READINESS GATES   LABELS
dinginx-rs-matchexpressions-hnl7c   1/1     Running   0          7s     10.244.2.38    k8s-node02.dinginx.org   <none>           <none>            apps=web,run=dinginx,version=v1    #新拉起的pod
dinginx-rs-matchexpressions-xsgqb   1/1     Running   0          7s     10.244.1.213   k8s-node01.dinginx.org   <none>           <none>            apps=web,run=dinginx,version=v1    #新拉起的pod
dinginx01                           1/1     Running   0          119s   10.244.2.36    k8s-node02.dinginx.org   <none>           <none>            apps=v1
dinginx02                           1/1     Running   0          119s   10.244.2.37    k8s-node02.dinginx.org   <none>           <none>            apps=v2
dinginx03                           1/1     Running   0          119s   10.244.1.212   k8s-node01.dinginx.org   <none>           <none>            apps=v3

至此,基于标签表示器关联pod成功创建。

posted @ 2026-04-18 17:28  dinginx  阅读(4)  评论(0)    收藏  举报