ReplicaSet #
一、ReplicaSet概述 #
ReplicaSet(RS)是Kubernetes中的副本控制器,用于确保指定数量的Pod副本始终运行。
1.1 ReplicaSet功能 #
text
ReplicaSet功能
│
├── 维护Pod副本数量
│ ├── Pod数量不足时创建
│ └── Pod数量过多时删除
│
├── 标签选择器
│ └── 匹配特定标签的Pod
│
└── Pod模板
└── 定义Pod的规格
1.2 与Deployment关系 #
text
Deployment与ReplicaSet
│
└── Deployment
│
├── 创建/管理ReplicaSet
│
└── ReplicaSet
│
└── 管理Pod副本
二、创建ReplicaSet #
2.1 YAML定义 #
yaml
# nginx-replicaset.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-rs
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.25
ports:
- containerPort: 80
2.2 创建和查看 #
bash
# 创建ReplicaSet
kubectl apply -f nginx-replicaset.yaml
# 查看ReplicaSet
kubectl get rs
# 输出示例
NAME DESIRED CURRENT READY AGE
nginx-rs 3 3 3 1m
# 查看详情
kubectl describe rs nginx-rs
# 查看管理的Pod
kubectl get pods -l app=nginx
2.3 字段说明 #
| 字段 | 说明 |
|---|---|
| replicas | 期望副本数 |
| selector | Pod选择器 |
| template | Pod模板 |
三、副本管理 #
3.1 副本维护机制 #
text
副本维护流程
│
├── 1. 检查当前Pod数量
│
├── 2. 与期望副本数比较
│
├── 3. 数量不足
│ └── 根据模板创建Pod
│
└── 4. 数量过多
└── 删除多余Pod
3.2 扩缩容 #
bash
# 修改副本数
kubectl scale rs nginx-rs --replicas=5
# 编辑YAML
kubectl edit rs nginx-rs
# 查看结果
kubectl get rs nginx-rs
3.3 删除Pod自动恢复 #
bash
# 删除一个Pod
kubectl delete pod <pod-name>
# ReplicaSet自动创建新Pod
kubectl get pods -w
# 输出示例
nginx-rs-abc12 1/1 Terminating 0 5m
nginx-rs-xyz34 0/1 Pending 0 0s
nginx-rs-xyz34 1/1 Running 0 2s
四、选择器 #
4.1 matchLabels选择器 #
yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-rs
spec:
replicas: 3
selector:
matchLabels:
app: nginx
tier: frontend
template:
metadata:
labels:
app: nginx
tier: frontend
spec:
containers:
- name: nginx
image: nginx:1.25
4.2 matchExpressions选择器 #
yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx-rs
spec:
replicas: 3
selector:
matchLabels:
app: nginx
matchExpressions:
- key: tier
operator: In
values:
- frontend
- backend
- key: env
operator: Exists
template:
metadata:
labels:
app: nginx
tier: frontend
env: production
spec:
containers:
- name: nginx
image: nginx:1.25
4.3 选择器操作符 #
| 操作符 | 说明 |
|---|---|
| In | 值在集合中 |
| NotIn | 值不在集合中 |
| Exists | 键存在 |
| DoesNotExist | 键不存在 |
五、ReplicaSet与Pod #
5.1 Pod归属 #
bash
# 查看Pod的ownerReferences
kubectl get pod <pod-name> -o jsonpath='{.metadata.ownerReferences}'
# 输出示例
[{"apiVersion":"apps/v1","kind":"ReplicaSet","name":"nginx-rs","uid":"xxx"}]
5.2 孤儿Pod #
text
孤儿Pod场景
│
├── 已存在匹配标签的Pod
│
├── 没有ownerReferences
│
└── ReplicaSet会接管这些Pod
5.3 标签修改 #
bash
# 修改Pod标签
kubectl label pod <pod-name> app=other --overwrite
# ReplicaSet不再管理该Pod
# 会创建新Pod维持副本数
# 查看Pod
kubectl get pods -l app=nginx
六、更新ReplicaSet #
6.1 更新限制 #
ReplicaSet不支持滚动更新,更新Pod模板需要手动操作:
bash
# 1. 删除现有Pod
kubectl delete pods -l app=nginx
# 2. 更新ReplicaSet
kubectl apply -f nginx-replicaset.yaml
# 或者直接编辑
kubectl edit rs nginx-rs
6.2 更新镜像 #
bash
# 编辑ReplicaSet更新镜像
kubectl edit rs nginx-rs
# 删除Pod触发重建
kubectl delete pods -l app=nginx
6.3 与Deployment对比 #
| 特性 | ReplicaSet | Deployment |
|---|---|---|
| 副本管理 | 支持 | 支持 |
| 滚动更新 | 不支持 | 支持 |
| 回滚 | 不支持 | 支持 |
| 使用场景 | 简单场景 | 推荐 |
七、删除ReplicaSet #
7.1 删除方式 #
bash
# 删除ReplicaSet和Pod
kubectl delete rs nginx-rs
# 仅删除ReplicaSet,保留Pod
kubectl delete rs nginx-rs --cascade=orphan
# 从文件删除
kubectl delete -f nginx-replicaset.yaml
7.2 删除前缩容 #
bash
# 先缩容到0
kubectl scale rs nginx-rs --replicas=0
# 再删除
kubectl delete rs nginx-rs
八、完整配置示例 #
8.1 生产级ReplicaSet #
yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: web-rs
labels:
app: web
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx:1.25
ports:
- containerPort: 80
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 200m
memory: 256Mi
livenessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 10
periodSeconds: 10
readinessProbe:
httpGet:
path: /
port: 80
initialDelaySeconds: 5
periodSeconds: 5
九、故障排查 #
9.1 常见问题 #
bash
# 查看ReplicaSet状态
kubectl describe rs nginx-rs
# 查看事件
kubectl get events --field-selector involvedObject.name=nginx-rs
# 查看Pod状态
kubectl get pods -l app=nginx
9.2 问题诊断 #
| 问题 | 原因 | 解决方案 |
|---|---|---|
| DESIRED != READY | Pod创建失败 | 检查镜像和资源 |
| Pod持续重建 | 健康检查失败 | 检查探针配置 |
| 副本数不对 | 标签不匹配 | 检查选择器配置 |
十、使用建议 #
10.1 何时使用ReplicaSet #
text
使用场景
│
├── 不需要滚动更新
│
├── 简单副本管理
│
└── 学习和理解Kubernetes
10.2 推荐使用Deployment #
text
推荐使用Deployment的原因
│
├── 支持滚动更新
│
├── 支持回滚
│
├── 声明式更新
│
└── 更好的版本管理
十一、总结 #
11.1 核心要点 #
| 要点 | 说明 |
|---|---|
| 功能 | 维护Pod副本数量 |
| 选择器 | 匹配特定标签的Pod |
| 模板 | 定义Pod规格 |
| 与Deployment | Deployment管理ReplicaSet |
11.2 下一步 #
理解了ReplicaSet后,让我们学习 StatefulSet,掌握有状态应用的部署方法。
最后更新:2026-03-28