如果您volumeClaimTemplates
在StatefulSet
k8s 中使用,则会进行动态配置并为每个 pod 创建一个 PVC 和相应的 PV,因此每个 pod 都有自己的存储空间。
你想要的是创建一个 PV 和一个 PVC 并在 Statefulset 的所有副本中使用它。
下面是关于 Kubernetes 1.10 的示例,您可以如何做到这一点,/var/www/html
所有三个 Pod 将在哪里共享,只需更改/directory/on/host
到您机器上的某个本地目录即可。我也在 minikube v0.26.0 上运行了这个例子
当然下面只是一个例子来说明这个想法,但在一个真实的例子中,Pod 中的进程应该知道同步访问共享存储。
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
---
apiVersion: v1
kind: PersistentVolume
metadata:
name: example-pv
spec:
capacity:
storage: 100Gi
# volumeMode field requires BlockVolume Alpha feature gate to be enabled.
volumeMode: Filesystem
accessModes:
- ReadWriteOnce
persistentVolumeReclaimPolicy: Delete
storageClassName: local-storage
local:
path: /directory/on/host
nodeAffinity:
required:
nodeSelectorTerms:
- matchExpressions:
- key: kubernetes.io/hostname
operator: In
values:
- minikube
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: example-local-claim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 5Gi
storageClassName: local-storage
---
apiVersion: "apps/v1beta1"
kind: StatefulSet
metadata:
name: nginx
spec:
serviceName: nginx
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx-container
image: "nginx:1.12.2"
imagePullPolicy: "IfNotPresent"
volumeMounts:
- name: localvolume
mountPath: /var/www/html
volumes:
- name: localvolume
persistentVolumeClaim:
claimName: example-local-claim