ReplicaSet 的目的是维护一组在任何时候都处于运行状态的 Pod 副本的稳定集合。 因此,它通常用来保证给定数量的、完全相同的 Pod 的可用性。
一、ReplicaSet原理
ReplicaSet 是通过一组字段来定义的,包括一个用来识别可获得的 Pod 的集合的选择算符、一个用来标明应该维护的副本个数的数值、一个用来指定应该创建新 Pod 以满足副本个数条件时要使用的 Pod 模板等等。 每个 ReplicaSet 都通过根据需要创建和删除 Pod 以使得副本个数达到期望值, 进而实现其存在价值。当 ReplicaSet 需要创建新的 Pod 时,会使用所提供的 Pod 模板。
ReplicaSet 通过 Pod 上的 metadata.ownerReferences 字段连接到附属 Pod,该字段给出当前对象的属主资源。 ReplicaSet 所获得的 Pod 都在其 ownerReferences 字段中包含了属主 ReplicaSet 的标识信息。正是通过这一连接,ReplicaSet 知道它所维护的 Pod 集合的状态, 并据此计划其操作行为。
ReplicaSet 使用其选择算符来辨识要获得的 Pod 集合。如果某个 Pod 没有 OwnerReference 或者其 OwnerReference 不是一个控制器, 且其匹配到某 ReplicaSet 的选择算符,则该 Pod 立即被此 ReplicaSet 获得。
kubectl get pod coredns-76f75df574-dvwd5 -n kube-system -o yaml可看到跟Replicaset相关的信息
ownerReferences:
- apiVersion: apps/v1
blockOwnerDeletion: true
controller: true
kind: ReplicaSet
name: coredns-76f75df574
uid: f062e1d5-1591-4867-9935-97139fd12315
二、ReplicaSet资源清单YAML文件编写技巧
[root@k8s-master01 ~]# kubectl explain ReplicaSet
GROUP: apps
KIND: ReplicaSet
VERSION: 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 <ObjectMeta>
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 <ReplicaSetSpec>
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 <ReplicaSetStatus>
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
ReplicaSet案例
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: frontend
namespace: test
labels:
app: guestbook
tier: frontend
spec:
replicas: 3
selector:
matchLabels:
tier: frontend
template:
metadata:
name: test
labels:
tier: frontend
spec:
containers:
- name: php-redis
image: yecc/gcr.io-google_sample-gb-frontend:v3
imagePullPolicy: IfNotPresent
三、ReplicaSet实现Pod缩扩容
方法一:直接更改yaml文件的replicas数量,然后kubectl apply -f xx.yaml
使更改生效。
方法二:kubectl edit rs xxx
四、更新pod镜像
最直接的想法:直接更改yaml文件的镜像,然后apply -f或者kubectl edit更改镜像地址。但是这样会有个问题, 对于存量pod并不会更新。只有删除存量的Pod,才能产生新版本的pod
对此,我们引入了deployment。