In a production environment, you need to be able to control the resources that your applications use. This is especially important in a containerized environment like Kubernetes, where many applications can be running on the same cluster.
There are two main ways to do this in Kubernetes: setting limits and requests. Limits define the maximum amount of resources that a pod can use, while requests define the minimum amount of resources that a pod needs.
In this article, we'll take a look at how to set limits and requests for your pods. We'll also look at some best practices for using these settings.
Limits and requests are set as annotations on a pod. You can set them when you create a pod, or you can set them later using the kubectl annotate command.
Here's an example of how to set limits and requests when creating a pod:
apiVersion: v1
kind: Pod
metadata:
name: mypod
annotations:
"limits.cpu": "1",
"requests.cpu": "0.5"
spec:
containers:
- name: mycontainer
image: myimage
In this example, we're setting a limit of 1 CPU and a request of 0.5 CPU.
You can also set these annotations using the kubectl annotate command:
kubectl annotate pod mypod "limits.cpu=1" "requests.cpu=0.5"
There are a few best practices to keep in mind when setting limits and requests:
In this article, we've looked at how to set limits and requests for your pods. We've also looked at some best practices for using these settings. By following these best practices, you can ensure that your pods get the resources they need without using more resources than necessary.