0

I want to set up a service in a Pod, composed of 3 different containers. In this container, there is a process that need to write in a file. The 3 containers write the same thing at almost the same time in those file. In order to avoid concurrent writes, is it possible to set up those 3 containers in a pod (replica: 3), with 3 different volumes on which each container will be writing ?

It seems quite difficult to do so far with my comprehension.

Thanks for the help !

Arnaud
  • 3
  • 1
  • Hi, Would it be ok if it were one container per pod? Then it could be done with `StatefulSet` that allows to configure `volumeClaimTemplates` that are attached to each pod replica. – Piotr Malec Jan 07 '20 at 18:23
  • Hello, Thanks for the proposal. Could I manage the number of pod easily with 1 container per pod ? I mean would I be able to perform auto-scaling, auto-repair and monitoring of the service deployed across many "one-container pods" ? – Arnaud Jan 08 '20 at 09:47
  • Yes, that is actually recommended method. Kubernetes is all about automation and auto-healing. Horizontal scaling (more pods but smaller) is more convenient for this rather than vertical scaling (less pods but larger). For example in case of pod termination, pod is deleted after all containers within the pod are terminated, so if one is not responding it could slow down termination process for whole pod. – Piotr Malec Jan 08 '20 at 11:39
  • More info about "Pods that run a single container" vs "Pods that run multiple containers that need to work together" can be found here: https://kubernetes.io/docs/concepts/workloads/pods/pod-overview/#understanding-pods – Piotr Malec Jan 08 '20 at 11:41

1 Answers1

1

As I mentioned in comments this can be done with help of StatefulSets.

According to kubernetes documentation about StatefulSets.:

Using StatefulSets

StatefulSets are valuable for applications that require one or more of the following.

  • Stable, unique network identifiers.
  • Stable, persistent storage.
  • Ordered, graceful deployment and scaling.
  • Ordered, automated rolling updates.

In the above, stable is synonymous with persistence across Pod (re)scheduling. If an application doesn’t require any stable identifiers or ordered deployment, deletion, or scaling, you should deploy your application using a workload object that provides a set of stateless replicas. Deployment or ReplicaSet may be better suited to your stateless needs.

Limitations

  • The storage for a given Pod must either be provisioned by a PersistentVolume Provisioner based on the requested storage class, or pre-provisioned by an admin.
  • Deleting and/or scaling a StatefulSet down will not delete the volumes associated with the StatefulSet. This is done to ensure data safety, which is generally more valuable than an automatic purge of all related StatefulSet resources.
  • StatefulSets currently require a Headless Service to be responsible for the network identity of the Pods. You are responsible for creating this Service.
  • StatefulSets do not provide any guarantees on the termination of pods when a StatefulSet is deleted. To achieve ordered and graceful termination of the pods in the StatefulSet, it is possible to scale the StatefulSet down to 0 prior to deletion.
  • When using Rolling Updates with the default Pod Management Policy (OrderedReady), it’s possible to get into a broken state that requires manual intervention to repair.

The example below demonstrates the components of a StatefulSet.

apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    name: web
  clusterIP: None
  selector:
    app: nginx
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: web
spec:
  selector:
    matchLabels:
      app: nginx # has to match .spec.template.metadata.labels
  serviceName: "nginx"
  replicas: 3 # by default is 1
  template:
    metadata:
      labels:
        app: nginx # has to match .spec.selector.matchLabels
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      - name: nginx
        image: k8s.gcr.io/nginx-slim:0.8
        ports:
        - containerPort: 80
          name: web
        volumeMounts:
        - name: www
          mountPath: /usr/share/nginx/html
  volumeClaimTemplates:
  - metadata:
      name: www
    spec:
      accessModes: [ "ReadWriteOnce" ]
      storageClassName: "my-storage-class"
      resources:
        requests:
          storage: 1Gi

In the above example:

  • A Headless Service, named nginx, is used to control the network domain.
  • The StatefulSet, named web, has a Spec that indicates that 3 replicas of the nginx container will be launched in unique Pods.
  • The volumeClaimTemplates will provide stable storage using PersistentVolumes provisioned by a PersistentVolume Provisioner.

Also from same documentation page:

Stable Storage

Kubernetes creates one PersistentVolume for each VolumeClaimTemplate. In the nginx example above, each Pod will receive a single PersistentVolume with a StorageClass of my-storage-class and 1 Gib of provisioned storage. If no StorageClass is specified, then the default StorageClass will be used. When a Pod is (re)scheduled onto a node, its volumeMounts mount the PersistentVolumes associated with its PersistentVolume Claims. Note that, the PersistentVolumes associated with the Pods’ PersistentVolume Claims are not deleted when the Pods, or StatefulSet are deleted. This must be done manually.

Piotr Malec
  • 271
  • 1
  • 5
  • Wow, thank you very Piotr, that helps me choose the adequate deployment method! So far, aside from the Rolling Updates that could fail, it seems to match perfectly with my use case. If I could use your experience and knowledge a bit more, from your point of view, are StatefulSets reliable ? Stateful "containers/pods/etc" are often seen as not as reliable as stateless "containers/pods/etc". What's your take on that ? Thanks a lot anyway for this recap! – Arnaud Jan 13 '20 at 13:48
  • There is interesting [article](https://www.magalix.com/blog/kubernetes-patterns-the-stateful-service-pattern) about stateful service pattern and [this](https://stackoverflow.com/questions/5329618/stateless-vs-stateful-i-could-use-some-concrete-information) question on stack also has useful information. If my help was appreciated please accept the answer as it means a lot to me. – Piotr Malec Jan 14 '20 at 13:19