0

I have a statefulset, I want to add another container-2 for that statefulset & I want that container-2 to access container-1's pv's (created using volumeClaimTemplates:)

Any way we can achieve above?

Tummala Dhanvi
  • 3,007
  • 2
  • 19
  • 35
sudhir tataraju
  • 1,159
  • 1
  • 14
  • 30

1 Answers1

2

You asked quite general question without any specific requirements (only GKE tags) and 2 containers using the same PV.

I've tested very simple example of statefulset. When you are using statefulset you must remember about some statefulset limitations.

To this test I've used Kubernetes docs example from here (using busybox instead of nginx) and GKE cluster v1.14.10-gke.36. Please keep in mind that

Statefulset and service yaml below:

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

Output:

$ 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

Test:

$ 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

This example was tested on GKE so you didn't need to create PV and PVC as Cloud Provider did it. Hope this answerd your question.

PjoterS
  • 12,841
  • 1
  • 22
  • 54