Files
Kubernetes/使用文档/StatefulSet的使用.md
offends 8a87b699ba
All checks were successful
continuous-integration/drone Build is passing
first commit
2025-12-13 18:06:23 +08:00

185 lines
5.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

> 本文作者:丁辉
# StatefulSet的使用
[官方文档](https://kubernetes.io/zh-cn/docs/concepts/workloads/controllers/statefulset/)
## 示例
```yaml
cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
selector:
matchLabels:
app: nginx # 必须匹配 .spec.template.metadata.labels
serviceName: "nginx"
replicas: 3 # 默认值是 1
minReadySeconds: 10 # 默认值是 0
template:
metadata:
labels:
app: nginx # 必须匹配 .spec.selector.matchLabels
spec:
# 指定 Pod 在接收到终止信号后,系统等待容器优雅关闭的时间(以秒为单位)。默认值为 30 秒
terminationGracePeriodSeconds: 10
containers:
- name: nginx
image: nginx:alpine
ports:
- containerPort: 80
name: web
volumeMounts:
- name: www
mountPath: /usr/share/nginx/html
volumeClaimTemplates:
- metadata:
name: www
spec:
accessModes: [ "ReadWriteOnce" ]
storageClassName: "standard"
resources:
requests:
storage: 1Gi
---
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx
EOF
```
## 参数介绍
| 特性 | ReadWriteOnce (RWO) | ReadWriteOncePod (RWOP) |
| :-------------: | :-------------------: | :--------------------------: |
| **挂载单位** | 节点级别 | Pod级别 |
| **多个Pod访问** | 允许(同节点) | 绝对禁止 |
| **安全性** | 较低同节点Pod可共享 | 高,严格隔离 |
| **K8s版本** | 所有版本 | 1.22+beta1.27+(稳定) |
| **使用场景** | 常规有状态应用 | 需要严格隔离的应用 |
| **存储类支持** | 广泛支持 | 需要CSI驱动支持 |
## 更新策略
- RollingUpdate(滚动更新) - 默认策略
```yaml
spec:
updateStrategy:
type: RollingUpdate
```
- OnDelete(删除时更新)
```yaml
spec:
updateStrategy:
type: OnDelete
```
## 金丝雀发布
`partition` 参数用于控制更新的分界点,实现金丝雀发布或分阶段更新。
```yaml
spec:
updateStrategy:
type: RollingUpdate
rollingUpdate:
partition: 2 # 只有序号 >=2 的 Pod 会被更新
```
**工作原理**
- `partition: 2` → 更新 pod-2, pod-3, ...
- `partition: 1` → 更新 pod-1, pod-2, ...
- `partition: 0` → 更新所有 Pod默认值
## PersistentVolumeClaim 保留
在 StatefulSet 的生命周期中,可选字段 `.spec.persistentVolumeClaimRetentionPolicy` 控制是否删除以及如何删除 PVC。 使用该字段,你必须在 API 服务器和控制器管理器启用 `StatefulSetAutoDeletePVC` [特性门控](https://kubernetes.io/zh-cn/docs/reference/command-line-tools-reference/feature-gates/)。 启用后,你可以为每个 StatefulSet 配置两个策略:
- `whenDeleted`
配置删除 StatefulSet 时应用的卷保留行为。
- `whenScaled`
配置当 StatefulSet 的副本数减少时应用的卷保留行为;例如,缩小集合时。
对于你可以配置的每个策略,你可以将值设置为 `Delete` 或 `Retain`。
- `Delete`
对于受策略影响的每个 Pod基于 StatefulSet 的 `volumeClaimTemplate` 字段创建的 PVC 都会被删除。 使用 `whenDeleted` 策略,所有来自 `volumeClaimTemplate` 的 PVC 在其 Pod 被删除后都会被删除。 使用 `whenScaled` 策略,只有与被缩减的 Pod 副本对应的 PVC 在其 Pod 被删除后才会被删除。
- `Retain`(默认)
来自 `volumeClaimTemplate` 的 PVC 在 Pod 被删除时不受影响。这是此新功能之前的行为。
请记住,这些策略仅适用于由于 StatefulSet 被删除或被缩小而被删除的 Pod。 例如,如果与 StatefulSet 关联的 Pod 由于节点故障而失败, 并且控制平面创建了替换 Pod则 StatefulSet 保留现有的 PVC。 现有卷不受影响,集群会将其附加到新 Pod 即将启动的节点上。
### 示例
1. whenDeleted - StatefulSet 被删除时
- 保留 PVC (默认)
```yaml
apiVersion: apps/v1
kind: StatefulSet
...
spec:
persistentVolumeClaimRetentionPolicy:
whenDeleted: Retain
```
- 删除 PVC
```yaml
apiVersion: apps/v1
kind: StatefulSet
...
spec:
persistentVolumeClaimRetentionPolicy:
whenDeleted: Delete
```
2. whenScaled - 缩减副本数时
- 保留被删除 Pod 的 PVC
```yaml
apiVersion: apps/v1
kind: StatefulSet
...
spec:
persistentVolumeClaimRetentionPolicy:
whenScaled: Retain
```
- 删除被删除 Pod 的 PVC
```yaml
apiVersion: apps/v1
kind: StatefulSet
...
spec:
persistentVolumeClaimRetentionPolicy:
whenScaled: Delete
```