k8s 持久化存储

为什么需要持久化存储:

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

参考

https://blog.csdn.net/cuixhao110/article/details/105858553

感谢阅读这篇文章,如果你喜欢,或者遇到了问题,可以关注我的公众号