参考文章:
https://kubernetes.io/docs/concepts/storage/volumes/
https://www.cnblogs.com/styshoo/p/6731425.html
容器中的磁盘文件是短暂的,这为在容器中运行重要的应用程序带来了一些问题。
第一 当容器崩溃时,Kubelet将会重启容器,这时候数据文件就会丢失。
第二 在同一个Pod中的容器通常需要共享数据。
Kubernetes使用Volume抽象解决了这两个问题。
Kubernetes supports several types of Volumes:
awsElasticBlockStore
azureDisk
azureFile
cephfs
configMap
csi
downwardAPI
emptyDir
fc
(fibre channel)flocker
gcePersistentDisk
gitRepo
glusterfs
hostPath
iscsi
local
nfs
persistentVolumeClaim
projected
portworxVolume
quobyte
rbd
scaleIO
secret
storageos
vsphereVolume
NFS PersistentVolume
前提条件:搭建NFS服务器,K8s node安装nfs客户端
pv-yaml
apiVersion: v1 kind: PersistentVolume metadata: name: nginx-data spec: capacity: storage: 1Gi accessModes: - ReadWriteMany nfs: path: /nfs-data server: 172.16.65.200 persistentVolumeReclaimPolicy: Recycle
pvc-yaml
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: nginx-data spec: accessModes: - ReadWriteMany resources: requests: storage: 1Gi
deployment-yaml
apiVersion: apps/v1 kind: Deployment metadata: name: nginx-web spec: replicas: 3 selector: matchLabels: name: nginx-web template: metadata: labels: name: nginx-web spec: containers: - name: nginx-web image: nginx volumeMounts: - mountPath: /usr/share/nginx/html name: nginx-data ports: - containerPort: 80 volumes: - name: nginx-data persistentVolumeClaim: claimName: nginx-data
service-yaml
apiVersion: v1 kind: Service metadata: name: nginx-service spec: selector: name: nginx-web type: NodePort ports: - port: 80 protocol: TCP targetPort: 80 name: http nodePort: 30080