您提出的问题很笼统,没有任何具体要求(仅 GKE 标签)和使用相同 PV 的 2 个容器。
我已经测试了statefulset 的非常简单的示例。
当您使用statefulset 时,您必须记住一些statefulset limitations。
在这个测试中,我使用了来自here(使用busybox而不是nginx)和GKE集群v1.14.10-gke.36的Kubernetes文档示例。
请记住,
Statefulset 和服务 yaml 如下:
apiVersion: v1
kind: Service
metadata:
name: busybox-service
labels:
app: busybox
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: busybox
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: busybox
spec:
serviceName: busybox
replicas: 1
selector:
matchLabels:
app: busybox
template:
metadata:
labels:
app: busybox
spec:
containers:
- image: busybox
args: [/bin/sh, -c, 'sleep 9999' ]
volumeMounts:
- mountPath: /test
name: busybox-volume
name: busybox
- image: busybox
args: [/bin/sh, -c, 'sleep 9999' ]
volumeMounts:
- mountPath: /test
name: busybox-volume
name: busybox-two
volumeClaimTemplates:
- metadata:
name: busybox-volume
spec:
accessModes: [ "ReadWriteOnce" ]
resources:
requests:
storage: 1Gi
输出:
$ kubectl get pods,pv,pvc
NAME READY STATUS RESTARTS AGE
pod/busybox-0 2/2 Running 0 2m38s
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/pvc-3fe7dbe8-96ce-11ea-ae2e-42010a9a012f 1Gi RWO Delete Bound default/busybox-volume-busybox-0 standard 2m36s
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/busybox-volume-busybox-0 Bound pvc-3fe7dbe8-96ce-11ea-ae2e-42010a9a012f 1Gi RWO standard 2m39s
测试:
$ kubectl exec -ti busybox-0 -c busybox -- /bin/sh
/ # ls
bin dev etc home proc root sys test tmp usr var
/ # cd test
/test # ls
lost+found
/test # echo "This is test message, container buxybox" >> msg.txt
/test # cat msg.txt
This is test message, container buxybox
/test # exit
$ kubectl exec -ti busybox-0 -c busybox-two -- /bin/sh
/ # ls
bin dev etc home proc root sys test tmp usr var
/ # cd test/
/test # ls
lost+found msg.txt
/test # cat msg.txt
This is test message, container buxybox
/test # echo "This is message from container busybox-two" >> msg.txt
/test # exit
$ kubectl exec -ti busybox-0 -c busybox -- /bin/sh
/ # cat /test/msg.txt
This is test message, container buxybox
This is message from container busybox-two
此示例在GKE 上进行了测试,因此您无需像 Cloud Provider 那样创建 PV 和 PVC。
希望这能回答您的问题。