Is there any option in Kubernetes to run pod in a specific cgroup directory?

7/14/2021

I am trying to limit the resource usage of each container in a pod dynamically. For docker, I used --cgroup-parent to put containers in a specific cgroup directory. However, in Kubernetes, I haven't found any option that I can do this.

-- Chhun Socheat
docker
kubernetes
kubernetes-pod
linux
minikube

1 Answer

7/14/2021

You can use the cgroupfs driver supported by Kubernetes.

But perhaps you just want to set the Pod and Container resources alone? Kubernetes allows setting the limits for specific containers as well, therefore you can have app with 500Mi RAM and logs with 50Mi RAM (requests), but expand both to 1Gi if necessary (limits):

spec:
  containers:
  - name: app
    image: images.my-company.example/app:v4
    resources:
      requests:
        memory: "64Mi"
        cpu: "250m"
      limits:
        memory: "128Mi"
        cpu: "500m"
  - name: log-aggregator
    image: images.my-company.example/log-aggregator:v6
    resources:
      requests:
        memory: "64Mi"
        cpu: "250m"
      limits:
        memory: "128Mi"
        cpu: "500m"

Update:

As mentioned by David Maze, you can also utilize autoscalers such as Vertical Pod Autoscaler which will adjust the Pod resources. For that however a CustomResourceDefinition is necessary, therefore even the appropriate permissions to the cluster for creating CRDs and deploying it (thus may not work if you have limited access and you would need to contact the cluster admin).

... it will set the requests automatically based on usage ... It will also maintain ratios between limits and requests that were specified in initial containers configuration.

apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: my-app-vpa
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind:       Deployment
    name:       my-app
  updatePolicy:
    updateMode: "Auto"

or even Horizontal autoscaling in case you need to have separate instances of the applications (thus is managing the Pod count).

-- Peter Badida
Source: StackOverflow