ReplicaSet简称RS,随着Kubernetes的高速发展,官方已经推荐我们使用RS和Deployment来代替RC了,实际上RS和RC的功能基本一致,目前唯一的一个区别就是RC只支持基于等式selector(env=dev environment!=qa),但RS还支持基于集合的selector(version in (v1.0, v2.0)),这对复杂的运维管理就非常方便了。

  1. [root@master01 ~]# kubectl explain ReplicaSet
  2. KIND: ReplicaSet
  3. VERSION: apps/v1
  4. DESCRIPTION:
  5. ReplicaSet ensures that a specified number of pod replicas are running at
  6. any given time.
  7. FIELDS:
  8. apiVersion <string>
  9. APIVersion defines the versioned schema of this representation of an
  10. object. Servers should convert recognized schemas to the latest internal
  11. value, and may reject unrecognized values. More info:
  12. https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
  13. kind <string>
  14. Kind is a string value representing the REST resource this object
  15. represents. Servers may infer this from the endpoint the client submits
  16. requests to. Cannot be updated. In CamelCase. More info:
  17. https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
  18. metadata <Object>
  19. If the Labels of a ReplicaSet are empty, they are defaulted to be the same
  20. as the Pod(s) that the ReplicaSet manages. Standard object's metadata. More
  21. info:
  22. https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
  23. spec <Object>
  24. Spec defines the specification of the desired behavior of the ReplicaSet.
  25. More info:
  26. https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
  27. status <Object>
  28. Status is the most recently observed status of the ReplicaSet. This data
  29. may be out of date by some window of time. Populated by the system.
  30. Read-only. More info:
  31. https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status

创建RS

  1. cat > myapp-rs.yaml << EOF
  2. apiVersion: apps/v1
  3. kind: ReplicaSet
  4. metadata:
  5. name: myapp-replicaset
  6. namespace: default
  7. labels:
  8. app: myapp
  9. type: replicaset
  10. spec:
  11. replicas: 2
  12. selector:
  13. matchLabels:
  14. app: myapp
  15. type: replicaset
  16. template:
  17. metadata:
  18. name: myapp-pod
  19. labels:
  20. app: myapp
  21. type: replicaset
  22. spec:
  23. containers:
  24. - name: myapp
  25. image: ikubernetes/myapp:v1
  26. ports:
  27. - name: http
  28. containerPort: 80
  29. EOF
kubectl apply -f myapp-rs.yaml

查看RS

[root@master01 ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
myapp-replicaset-m9zg9   1/1     Running   0          88s
myapp-replicaset-vmslb   1/1     Running   0          88s

[root@master01 ~]# kubectl get rs
NAME               DESIRED   CURRENT   READY   AGE
myapp-replicaset   2         2         2       92s

查看RS具体信息

[root@master01 ~]# kubectl describe rs myapp-replicaset 
Name:         myapp-replicaset
Namespace:    default
Selector:     app=myapp,type=replicaset
Labels:       app=myapp
              type=replicaset
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"apps/v1","kind":"ReplicaSet","metadata":{"annotations":{},"labels":{"app":"myapp","type":"replicaset"},"name":"myapp-replic...
Replicas:     2 current / 2 desired
Pods Status:  2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
  Labels:  app=myapp
           type=replicaset
  Containers:
   myapp:
    Image:        ikubernetes/myapp:v1
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Events:
  Type    Reason            Age    From                   Message
  ----    ------            ----   ----                   -------
  Normal  SuccessfulCreate  2m57s  replicaset-controller  Created pod: myapp-replicaset-vmslb
  Normal  SuccessfulCreate  2m56s  replicaset-controller  Created pod: myapp-replicaset-m9zg9

删除RS

kubectl delete rs myapp-replicaset

使用RS的更富表达力的标签选择器

用较简单的matchLabels 选择器来确认ReplicaSet 与ReplicationController 没有区别。现在,你将用更强大的
matchExpressions 属性来重写选择器

[root@master01 ~]# kubectl explain ReplicaSet.spec.selector
KIND:     ReplicaSet
VERSION:  apps/v1

RESOURCE: selector <Object>

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    <[]Object>
     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.
[root@master01 ~]# kubectl explain ReplicaSet.spec.selector.matchExpressions
KIND:     ReplicaSet
VERSION:  apps/v1

RESOURCE: matchExpressions <[]Object>

DESCRIPTION:
     matchExpressions is a list of label selector requirements. The requirements
     are ANDed.

     A label selector requirement is a selector that contains values, a key, and
     an operator that relates the key and values.

FIELDS:
   key    <string> -required-
     key is the label key that the selector applies to.

   operator    <string> -required-
     operator represents a key's relationship to a set of values. Valid
     operators are In, NotIn, Exists and DoesNotExist.

   values    <[]string>
     values is an array of string values. If the operator is In or NotIn, the
     values array must be non-empty. If the operator is Exists or DoesNotExist,
     the values array must be empty. This array is replaced during a strategic
     merge patch.
eplicaSet
VERSION:  apps/v1

FIELD:    operator <string>

DESCRIPTION:
     operator represents a key's relationship to a set of values. Valid
     operators are In, NotIn, Exists and DoesNotExist.

如示例, 每个表达式都必须包含一个key 、一个operator (运算符〉,并且可能还有一个values 的列表(取决于运算符)。

你会看到四个有效的运算符:

  • In : Label 的值必须与其中一个指定的values 匹配。
  • NotIn : Label 的值与任何指定的values 不匹配。
  • Exists : pod 必须包含一个指定名称的标签(值不重要)。使用此运算符时,不应指定values 字段。
  • DoesNotExist : pod 不得包含有指定名称的标签。values 属性不得指定。
cat > myapp-rs.yaml << EOF
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: myapp-replicaset
  namespace: default
  labels:
    app: myapp
    type: replicaset
spec:
  replicas: 3
  selector:
    matchExpressions:
    - {key: app, operator: In, values: [myapp]}
    - {key: type, operator: In, values: [replicaset]}
  template:
    metadata:
      name: myapp-pod
      labels:
        app: myapp
        type: replicaset
    spec:
      containers:
      - name: myapp
        image: ikubernetes/myapp:v1
        ports:
        - name: http
          containerPort: 80
EOF
[root@master01 ~]# kubectl apply -f myapp-rs.yaml 
replicaset.apps/myapp-replicaset created
[root@master01 ~]# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
myapp-replicaset-7b6j6   1/1     Running   0          6s
myapp-replicaset-l9mkm   1/1     Running   0          6s
myapp-replicaset-z928n   1/1     Running   0          6s
[root@master01 ~]# kubectl get rs
NAME               DESIRED   CURRENT   READY   AGE
myapp-replicaset   3         3         3       10s