如何配置StatefulSet的side-car使用主容器的persistent-volume?

问题描述 投票:0回答:1

我有一个statefulset,我想为这个statefulset添加另一个容器-2 & 我想让这个容器-2访问容器-1的pv's(使用 volumeClaimTemplates。)

有什么方法可以实现上述功能?

kubernetes google-kubernetes-engine kubernetes-helm kubernetes-pod kubernetes-statefulset
1个回答
1
投票

你问的问题很笼统,没有任何具体的要求(只有GKE标签),2个容器使用相同的PV。

我已经测试了非常简单的例子 状态集.当你使用 statefulset 你要记住 状态集限制.

为了这个测试,我已经使用了Kubernetes文档的例子从 此处 (使用busybox代替nginx)和 GKEv1.14.10-gke.36.请记住

下面是Statefulset和service 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 所以你不需要创建PV和PVC,因为Cloud Provider已经做了。希望这能回答你的问题。

© www.soinside.com 2019 - 2024. All rights reserved.