k8s基础中如何通过Volume保持数据持久
Kubernetes通过使用Volumes来保持数据持久。Volumes可以在容器之间共享,并且可以在容器重启后继续存在。
要使用Volumes,首先需要在Kubernetes集群中创建一个名为“my-vol”的Volume:
$ kubectl create -f my-vol.yaml
my-vol.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-vol
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Recycle
nfs:
server: 10.0.0.1
path: /exports
Once the Volume has been created, it can be used by any number of Pods. To use the Volume, simply specify the name of the Volume in the Pod specification:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: busybox
command: ["sleep", "1000000"]
volumeMounts:
- name: my-vol
mountPath: /data
volumes:
- name: my-vol
persistentVolumeClaim:
claimName: my-vol
When the Pod is created, Kubernetes will automatically mount the Volume at the specified path inside the container. Any data written to the Volume will be persisted across container restarts.
相关文章