Home > Back-end >  Setting Ressource limit for ElasticSearch in Kubernetes creates bug
Setting Ressource limit for ElasticSearch in Kubernetes creates bug

Time:03-24

I have a web App, that I am trying to deploy with Kubernetes. It's working correctly, but when I try to add resource limits, the ElasticSearch will not deploy.

elasticsearch-deployment.yaml:

apiVersion: v1
kind: Service
metadata:
  name: elasticsearch-service
spec:
  type: NodePort
  selector:
    app: elasticsearch
  ports:
  - port: 9200
    targetPort: 9200
    name: serving
  - port: 9300
    targetPort: 9300
    name: node-to-node
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: elasticsearch-deployment
  labels:
    app: elasticsearch
spec:
  replicas: 1
  selector:
    matchLabels:
      app: elasticsearch
  template:
    metadata:
      labels:
        app: elasticsearch
    spec:
      containers:
      - name: elasticsearch
        image: elasticsearch:7.9.0
        ports:
        - containerPort: 9200
        - containerPort: 9300
        env:
        - name: discovery.type
          value: single-node
        # resources:
        #   limits:
        #     memory: 8Gi
        #     cpu: "4"
        #   requests:
        #     memory: 4Gi
        #     cpu: "2"

If I uncomment the resources section of the file, the pod is stuck in pending:

> kubectl get pods
NAME                                        READY   STATUS    RESTARTS       AGE
backend-deployment-bd4f98697-rxsz8          1/1     Running   1 (6m9s ago)   6m40s
elasticsearch-deployment-644475545b-t75pp   0/1     Pending   0              6m40s
frontend-deployment-8bc989f89-4g6v7         1/1     Running   0              6m40s
mysql-0                                     1/1     Running   0              6m40s

If I check the events:

> kubectl get events
...
Warning   FailedScheduling  pod/elasticsearch-deployment-54d9cdd879-k69js  0/1 nodes are available: 1 Insufficient cpu.
Warning   FailedScheduling  pod/elasticsearch-deployment-54d9cdd879-rjj24  0/1 nodes are available: 1 Insufficient cpu.
...

The events says that the pod has Insufficient cpu, but I tried to change the resource limits to :

resources:
  limits:
    memory: 8Gi
    cpu: "18"
  requests:
    memory: 4Gi
    cpu: "18"

Still doesn't works, the only way for it to works is to remove the resource limit, but why?

CodePudding user response:

It is because of the request, not the limit. It means your node doesn't have enough memory to schedule a pod that requests 2 CPUs. You need to set the value to a lower one (e.g. 500m).

You can check your server's allocatable CPUs. The sum of all Pod's CPU requests should be lower than this.

# kubectl describe nodes
...
Allocatable:
  cpu:                28
...

CodePudding user response:

In Addition to Daigo

Requests and limits are on a per-container basis. Each container in the Pod gets its own individual limit and request. When adding the limits and requests for each container together, you will get an aggregate value for the Pod.

Combining the requests and limits value of each container will represent the Pod requests which is 500m cpu and 128Mi memory and Pod limits is 1 cpu and 256Mi memory.

Requests are what the container is guaranteed to get. If a container requests a resource, k8s will only schedule it on a node that can give it that resource.

Limits, on the other hand, make sure a container won't go above a certain value or limit.

Without requests and limits defined, the scheduler might place the Pod on a node that has less than 1 GiB memory available.

  • Related