k8s 持久化存储
编辑于 2021-09-05 16:31:17 阅读 1378
为什么需要持久化存储:
1、使得使用资源的pod的生命周期与存储卷的生命周期分开 2、使得使用资源的pod在被重启后仍然能够使用之前的存储卷 3、使得使用资源的pod在被调度到其它节点后仍然能够使用之前的存储卷
Host类型volume
测试用 - 仅适用于单节点k8s
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
volumeMounts:
- name: www-storage
mountPath: /usr/share/nginx/html
volumes:
- name: www-storage
hostPath:
path: /data/www
如上:pod内是目录(/usr/share/nginx/html)挂载到宿主机目录(/data/www)
PersistentVolumes
PV定义
apiVersion: v1
kind: PersistentVolume
metadata:
name: www-pv
spec:
accessModes:
- ReadWriteMany
capacity:
storage: 2Gi
nfs:
path: /nfs/data/www
server: 192.168.10.99
PVC定义
# 用于消费PV
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: www-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 2Gi
在pod中使用
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 1
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
volumeMounts:
- name: www-storage
mountPath: /usr/share/nginx/html
volumes:
- name: www-storage
persistentVolumeClaim:
claimName: www-pvc
StorageClass
详见 k8s 使用 StorageClass 动态生成 NFS 类型的 PV