Home > Software engineering >  Kubernetes resource quota, have non schedulable pod staying in pending state
Kubernetes resource quota, have non schedulable pod staying in pending state

Time:11-12

So I wish to limit resources used by pod running for each of my namespace, and therefor want to use resource quota. I am following this tutorial. It works well, but I wish something a little different. When trying to schedule a pod which will go over the limit of my quota, I am getting a 403 error. What I wish is the request to be scheduled, but waiting in a pending state until one of the other pod end and free some resources.

Any advice ?

CodePudding user response:

Instead of using straight pod definitions (kind: Pod) use deployment.

Why?

Pods in Kubernetes are designed as relatively ephemeral, disposable entities:

You'll rarely create individual Pods directly in Kubernetes—even singleton Pods. This is because Pods are designed as relatively ephemeral, disposable entities. When a Pod gets created (directly by you, or indirectly by a controller), the new Pod is scheduled to run on a Node in your cluster. The Pod remains on that node until the Pod finishes execution, the Pod object is deleted, the Pod is evicted for lack of resources, or the node fails.

Kubernetes assumes that for managing pods you should a workload resources instead of creating pods directly:

Pods are generally not created directly and are created using workload resources. See Working with Pods for more information on how Pods are used with workload resources. Here are some examples of workload resources that manage one or more Pods:

By using deployment you will get very similar behaviour to the one you want.

Example below:

Let's suppose that I created pod quota for a custom namespace, set to "2" as in this example and I have two pods running in this namespace:

kubectl get pods -n quota-demo
NAME           READY   STATUS    RESTARTS   AGE
quota-demo-1   1/1     Running   0          75s
quota-demo-2   1/1     Running   0          6s

Third pod definition:

apiVersion: v1
kind: Pod
metadata:
  name: quota-demo-3
spec:
  containers:
  - name: quota-demo-3
    image: nginx
    ports:
    - containerPort: 80

Now I will try to apply this third pod in this namespace:

kubectl apply -f pod.yaml -n quota-demo
Error from server (Forbidden): error when creating "pod.yaml": pods "quota-demo-3" is forbidden: exceeded quota: pod-demo, requested: pods=1, used: pods=2, limited: pods=2

Not working as expected.

Now I will change pod definition into deployment definition:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: quota-demo-3-deployment
  labels:
    app: quota-demo-3
spec:
  selector:
    matchLabels:
      app: quota-demo-3
  template:
    metadata:
      labels:
        app: quota-demo-3
    spec:
      containers:
      - name: quota-demo-3
        image: nginx
        ports:
        - containerPort: 80

I will apply this deployment:

kubectl apply -f deployment-v3.yaml -n quota-demo
deployment.apps/quota-demo-3-deployment created

Deployment is created successfully, but there is no new pod, Let's check this deployment:

kubectl get deploy -n quota-demo
NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
quota-demo-3-deployment   0/1     0            0           12s

We can see that a pod quota is working, deployment is monitoring resources and waiting for the possibility to create a new pod.

Let's now delete one of the pod and check deployment again:

kubectl delete pod quota-demo-2 -n quota-demo
pod "quota-demo-2" deleted
kubectl get deploy -n quota-demo
NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
quota-demo-3-deployment   1/1     1            1           2m50s

The pod from the deployment is created automatically after deletion of the pod:

kubectl get pods -n quota-demo
NAME                                       READY   STATUS    RESTARTS   AGE
quota-demo-1                               1/1     Running   0          5m51s
quota-demo-3-deployment-7fd6ddcb69-nfmdj   1/1     Running   0          29s

It works the same way for memory and CPU quotas for namespace - when the resources are free, deployment will automatically create new pods.

  • Related