Kubernetes 存储管理
Kubernetes 存储管理
存储概念
PersistentVolume (PV)
PV 是集群级别的存储资源,由管理员预先配置或使用 StorageClass 动态创建:
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Retain
storageClassName: standard
hostPath:
path: /mnt/data
PersistentVolumeClaim (PVC)
PVC 是用户对存储资源的请求:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
storageClassName: standard
StorageClass
StorageClass 定义存储类型,支持动态创建 PV:
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: fast
provisioner: kubernetes.io/aws-ebs
parameters:
type: gp2
reclaimPolicy: Retain
allowVolumeExpansion: true
volumeBindingMode: WaitForFirstConsumer
使用存储
在 Pod 中挂载 PVC
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: nginx
volumeMounts:
- name: my-storage
mountPath: /data
volumes:
- name: my-storage
persistentVolumeClaim:
claimName: my-pvc
在 Deployment 中使用
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: my-app
volumeMounts:
- name: data
mountPath: /app/data
volumes:
- name: data
persistentVolumeClaim:
claimName: my-pvc
存储访问模式
- ReadWriteOnce (RWO): 单节点读写
- ReadOnlyMany (ROX): 多节点只读
- ReadWriteMany (RWX): 多节点读写
- ReadWriteOncePod (RWOP): 单 Pod 读写
动态存储配置
使用 StorageClass
# 查看 StorageClass
kubectl get storageclass
# 设置默认 StorageClass
kubectl patch storageclass standard \
-p '{"metadata":{"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
创建 PVC 动态创建 PV
kubectl apply -f pvc.yaml
# PV 会自动创建并绑定
kubectl get pv
kubectl get pvc
常用操作
# 查看 PV
kubectl get pv
kubectl describe pv my-pv
# 查看 PVC
kubectl get pvc
kubectl describe pvc my-pvc
# 扩展 PVC
kubectl patch pvc my-pvc -p '{"spec":{"resources":{"requests":{"storage":"20Gi"}}}}'
# 回收 PV
kubectl patch pv my-pv -p '{"spec":{"persistentVolumeReclaimPolicy":"Delete"}}'
存储类型
NFS
apiVersion: v1
kind: PersistentVolume
metadata:
name: nfs-pv
spec:
capacity:
storage: 10Gi
accessModes:
- ReadWriteMany
nfs:
server: nfs-server.example.com
path: "/exports/data"
AWS EBS
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: aws-gp2
provisioner: ebs.csi.aws.com
parameters:
type: gp2
fsType: ext4
本地存储
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
实践案例
数据库持久化存储
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-data
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
storageClassName: fast
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:8.0
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-secret
key: root-password
volumes:
- name: mysql-data
persistentVolumeClaim:
claimName: mysql-data
常见问题
PVC 一直 Pending
# 检查 PVC 状态
kubectl describe pvc my-pvc
# 检查 StorageClass
kubectl describe storageclass standard
# 检查是否有可用 PV
kubectl get pv | grep Available
PV 无法绑定
# 检查访问模式
kubectl describe pv my-pv
kubectl describe pvc my-pvc
# 检查容量
kubectl get pv -o custom-columns=NAME:.metadata.name,CAPACITY:.spec.capacity.storage
最佳实践
- 使用 StorageClass 实现动态存储配置
- 设置合理的存储容量和访问模式
- 定期备份重要数据
- 使用回收策略保护数据
- 考虑存储性能和成本
总结
Kubernetes 存储管理通过 PV、PVC 和 StorageClass 实现了灵活的持久化存储。合理使用存储资源可以确保应用数据的安全性和可靠性。