Junior — Middle
How can resource usage (CPU and memory) be managed and limited inside containers in Kubernetes?
sobes.tech AI
Answer from AI
In Kubernetes, resource management for CPU and memory for containers is handled using the resources specification in Pod or Deployment manifests. You can specify:
- requests — the minimum amount of resources guaranteed to be allocated to the container.
- limits — the maximum amount of resources the container can use.
Example configuration:
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: example-container
image: nginx
resources:
requests:
memory: "128Mi"
cpu: "250m"
limits:
memory: "256Mi"
cpu: "500m"
Here:
requeststells Kubernetes that the container needs at least 128 MB of memory and 0.25 CPU.limitsrestricts usage to 256 MB of memory and 0.5 CPU.
If the container exceeds the memory limit, it may be killed (OOMKilled). If it exceeds CPU, it will be throttled by the scheduler.
Thus, using requests and limits, you can effectively manage and limit resource usage, ensuring stability and predictability of application performance in the cluster.