In Kubernetes, container resource management is handled through resource requests and limits.
Resource requests specify the minimum amount of resources that a container requires to run, while resource limits specify the maximum amount of resources that a container can use. By setting resource requests and limits, Kubernetes can ensure that containers have access to the resources they need and prevent them from using too many resources and disrupting other containers running on the same node.
CPU limits:
Kubernetes allows you to set CPU limits for containers using the resources field in the YAML manifest file for the Pod or Deployment. For example, to set a CPU limit of 0.5 CPU cores for a container, you can add the following to the manifest file:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: my-image
resources:
limits:
cpu: 500m
In this example, the resources.limits.cpu field is set to 500m, which means that the container is limited to using 0.5 CPU cores.
Memory limits:
Kubernetes allows you to set memory limits for containers using the resources field in the YAML manifest file for the Pod or Deployment. For example, to set a memory limit of 512 MB for a container, you can add the following to the manifest file:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: my-image
resources:
limits:
memory: 512Mi
In this example, the resources.limits.memory field is set to 512Mi, which means that the container is limited to using 512 MB of memory.
Resource requests:
Kubernetes also allows you to set resource requests for containers using the resources field in the YAML manifest file. Resource requests specify the minimum amount of resources that a container requires to run, and Kubernetes uses this information to schedule containers on nodes that have enough resources to meet the requests. For example, to set a CPU request of 0.25 CPU cores and a memory request of 256 MB for a container, you can add the following to the manifest file:
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: my-container
image: my-image
resources:
requests:
cpu: 250m
memory: 256Mi
In this example, the resources.requests.cpu field is set to 250m, which means that the container requires at least 0.25 CPU cores to run, and the resources.requests.memory field is set to 256Mi, which means that the container requires at least 256 MB of memory to run.
In conclusion, Kubernetes uses resource requests and limits to manage container resource usage, preventing containers from using too many resources and disrupting other containers running on the same node. By setting resource requests and limits, you can ensure that your containers have access to the resources they need and prevent them from using too many resources and causing performance issues.