0

I have a pod with 2 closely related services running as containers. I am running as a StatefulSet and have set replicas as 5. So 5 pods are created with each pod having both the containers.

Now My requirement is to have the second container run only in 1 pod. I don't want it to run in 5 pods. But my first service should still run in 5 pods.

Is there a way to define this in the deployment yaml file for Kubernetes? Please help.

Wytrzymały Wiktor
  • 11,492
  • 5
  • 29
  • 37
ckv
  • 10,539
  • 20
  • 100
  • 144
  • What's the issue if you create a separate deployment to run 1 pod with just the wanted container? – gohm'c Feb 28 '22 at 05:34
  • If it's possible to have 4 of the main application container not having a matching same-pod support container, then they're not so "closely related" they need to run in the same pod. Run the second container in a separate Deployment/StatefulSet (also with a separate Service) and you can independently control the replica counts. – David Maze Feb 28 '22 at 11:57

3 Answers3

3

a "pod" is the smallest entity that is managed by kubernetes, and one pod can contain multiple containers, but you can only specify one pod per deployment/statefulset, so there is no way to accomplish what you are asking for with only one deployment/statefulset.

however, if you want to be able to scale them independently of each other, you can create two deployments/statefulsets to accomplish this. this is imo the only way to do so.

see https://kubernetes.io/docs/concepts/workloads/pods/ for more information.

OlGe
  • 472
  • 2
  • 9
  • `but you can only specify one per deployment/statefulset` is not correct. – YwH Feb 28 '22 at 08:51
  • @Yuwei it is correct. you can only specify one pod per deployment/statefulset. any link/source from your side to let me learn otherwise? – OlGe Feb 28 '22 at 08:58
  • @OIGe sry for the misunderstanding, maybe `but you can only specify one pod per deployment/statefulset` is better. – YwH Feb 28 '22 at 09:02
  • ah, i see. good point, i edit it. thanks! – OlGe Feb 28 '22 at 09:08
1

Containers are like processes,

Pods are like VMs,

and Statefulsets/Deployments are like the supervisor program controlling the VM's horizontal scaling.

The only way for your scenario is to define the second container in a new deployment's pod template, and set its replicas to 1, while keeping the old statefulset with 5 replicas.

YwH
  • 1,050
  • 5
  • 11
1

Here are some definitions from documentations (links in the references):

Containers are technologies that allow you to package and isolate applications with their entire runtime environment—all of the files necessary to run. This makes it easy to move the contained application between environments (dev, test, production, etc.) while retaining full functionality. [1]

Pods are the smallest, most basic deployable objects in Kubernetes. A Pod represents a single instance of a running process in your cluster. Pods contain one or more containers. When a Pod runs multiple containers, the containers are managed as a single entity and share the Pod's resources. [2]

A deployment provides declarative updates for Pods and ReplicaSets. [3]

StatefulSet is the workload API object used to manage stateful applications. Manages the deployment and scaling of a set of Pods, and provides guarantees about the ordering and uniqueness of these Pods. [4]


Based on all that information - this is impossible to match your requirements using one deployment/Statefulset.

I advise you to try the idea @David Maze mentioned in a comment under your question:

If it's possible to have 4 of the main application container not having a matching same-pod support container, then they're not so "closely related" they need to run in the same pod. Run the second container in a separate Deployment/StatefulSet (also with a separate Service) and you can independently control the replica counts.


References:

  1. Documentation about Containers
  2. Documentation about Pods
  3. Documentation about Deployments
  4. Documentation about StatefulSet
kkopczak
  • 742
  • 2
  • 8