[Solved] must specify limits cpu- Pod Deployment Error in Kubernetes

[Solved]: must specify limits.cpu- Pod Deployment Error in Kubernetes

In this tutorial, we will learn about  how to resolve the error must specify limits cpu– pod deployment error in Kubernetes. I was recently  deploying a microservice application  on RHEL OpenShift cluster. My deployment did not throw any error yet pod did not come up. After troubleshooting the error, I discovered that this has to do with the resource utilization configuration that has been done in the pod yaml file.

More precisely, it  indicates that the resource utilization request and limit is missing in the pod configuration file. In Kubernetes, resource specifically refers to the CPU and memory usage by a pod along with other resources. In the upcoming section of this tutorial, we will first reproduce the error and then look at some fixes to it. Let us begin the tutorial.

 

Resource Management in Kubernetes Overview

In practice When we deploy any pod, we do not really bother about the resource utilization a pod will take. But when we deploy the pods( which contains containerized applications) it is a necessity to configure the amount of resource for each pod. The most common resources to specify are CPU and memory(RAM). There are other resources as well like ephemeral storage.

 

Why “must specify limits cpu” error occur?

When you deploy any pod in a cluster where resource utilization configuration is mandatory, you will encounter this error if it is not configured. This setting is required because based on resource usage capacity of a Pod, Kube-scheduler decides in which node it will schedule the pod. Let us create below Statefulset in our cluster without setting the resource utilization requirement.

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: test-pod
  namespace: linuxnasa
spec:
  replicas: 1
  selector:
    matchLabels:
       app: nginx
  template:
    metadata:
      annotations:
        sidecar.istio.io/inject: "false"
      labels:
        app: nginx
    spec:
      containers:
      - name: alpine
        image: alpine
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80

 

Deploy the Statefulset resource which will create a pod using below command. If there are no error, it will print on console that the pod has been created.

[[email protected]]# oc create -f test-pod.yaml 
statefulset.apps/test-pod created

 

Next,  list Statefulset in your namespace and check the status of Statefulset. If it is in 0/1 state, that means pod is not in ready state.

[[email protected]]# oc get all -n linuxnasa | grep test
pod/test-pod-0     0/1   CrashLoopBackOff    94 (2m49s ago) 8h
statefulset.apps/test-pod    0/1   8h

 

Next, describe the Statefulset and see what is the error reported using below command.

[[email protected]]# oc describe statefulset.apps/test-pod 
Name: test-pod
Namespace: linuxnasa
CreationTimestamp: Thu, 09 Nov 2023 20:26:40 +0530
Selector: app=nginx
Labels: <none>
Annotations: <none>
Replicas: 1 desired | 0 total
Update Strategy: RollingUpdate
Partition: 0
Pods Status: 0 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: app=nginx
Annotations: sidecar.istio.io/inject: false
Containers:
alpine:
Image: alpine
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Volume Claims: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning FailedCreate 32s (x16 over 3m16s) statefulset-controller create Pod test-pod-0 in StatefulSet test-pod failed 
error: pods "test-pod-0" is forbidden: failed quota: linuxnasa-quota: must specify limits.cpu for: alpine; limits.memory for: alpine; requests.cpu for: alpine; requests.memory for: alpine

 

[Solved] must specify limits cpu- Pod Deployment Error in Kubernetes

We will now look at a way to address and  fix the error. We will add the resource requirement configuration  in the pod yaml  file. We have added two section under the resources variable in the below yaml file. These are requests and limits. limits will decide the maximum resource size beyond which pod can not use the resource. If it requires the resource more than the defined  limit, then also the pod deployment will fail. requests variable then request the resources which must be less than or equal to the defined limit. Let us make below changes in our pod yaml file and redeploy the pod.

Also read: [ MariaDB ] How to Reset root User Password

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: test-pod
  namespace: linuxnasa
spec:
  replicas: 1
  selector:
    matchLabels:
       app: nginx
  template:
    metadata:
      annotations:
        sidecar.istio.io/inject: "false"
      labels:
        app: nginx
    spec:
      containers:
      - name: alpine
        image: alpine
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 80
        resources:
          requests:
            memory: 100Mi
            cpu: 50m
          limits:
            memory: 100Mi
            cpu: 50m

 

Next,  uninstall the previous pod deployment using below command.
[[email protected]]# oc delete -f test-pod.yaml
statefulset.apps "test-pod" deleted

 

Next, deploy the pod using pod yaml file again using same command which we used previously.
[[email protected]]# oc create -f test-pod.yaml
statefulset.apps/test-pod created

 

Next, check the status of statefulset and pod again. This time Statefulset will be in 1/1 state which means pod is now up and in running state.
[[email protected]]# oc get all -n linuxnasa | grep test
pod/test-pod-0     1/1    Running   0 (48s ago)  99s
statefulset.apps/test-pod   1/1   99s

 

Summary

Reference Kubernetes resource management.

Leave a Comment