Configure CPU and Memory for Kubernetes Pods

This post will show how to configure Kubernetes Pods with a specific amount of RAM and CPU, which will ensure the pods are getting enough resources all the time.

Resource Management

Kubernetes gives us the option to allocate a specific amount of memory and CPU to our Pods to ensure applications and services performance is not degraded.

Resources specification

Using the resources specification tag, we can manage resources by setting a minimum (requests) amount or resources or limit the maximum (limits) about a Pod can consume memory and CPU.

The following code shows we can set a minimum memory and CPU on a Pod deployment using the resources specification tag shown under the resources: specification tag.

apiVersion: v1
kind: Pod
metadata:
   name: mypod
spec:
  containers:
    - image: nginx
      name: pod1
      ports:
       - containerPort: 8080
         name: http
         protocol: TCP
      resources: 
           requests:
              memory: "64Mi"
              cpu: "250m" 

In the following code, I do the opposite and set the maximum amount of memory and CPU.

apiVersion: v1
kind: Pod
metadata:
   name: mypod
spec:
  containers:
    - image: nginx
      name: pod1
      ports:
       - containerPort: 8080
         name: http
         protocol: TCP
      resources:
          limits: 
             cpu: "500m"
             memory: "256Mi"  
           

Just for reference, remember the 500m CPU means 0.5 vCPU core also known as 500 millicpu. You can also set both requests and limit together and have minimum and maximum memory and CPU configuration.

Processing…
Success! You're on the list.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.