In k8s, we can specify resources as:
apiVersion: v1
kind: Pod
metadata:
name: cpu-demo-2
spec:
containers:
- name: cpu-demo-ctr-2
image: vish/stress
resources:
limits:
cpu: "100"
requests:
cpu: "300"
where we can specify the requests
(k8s guarantees that container gets this much request), and maximum requests (limits
).
My question is will k8s allow this pod to be throttled until it reach it's limit if there is no load on the cluster?
Or, Is there a way to request desired cpus for a container?
The limit is passed down to the container runtime interface and in the case of CPU is set as a limit value that will utilize the runtime's native mechanisms (that is: cgroups) for throttling the CPU if it attempts to exceed that value. There won't be any limitation of the CPU prior to it hitting that value.
Memory requests and limits operate separately, and typically includes a slight delay while the control loops watch the consumption, and if the container exceeds a set limit will evict and terminate the container with an "OOMKill" message (out-of-memory kill) included as the reason.
You can find more detail at the kubernetes docs: Managing Compute Resources for Containers
Kubernetes doesn't include a means to dynamically respond to other inputs and throttle a resource, but you can change the request or limit values and make that the new desired state.
With Kubernetes 1.8 and later, there exists the concept of Pod Priority and pre-emption which can also impact the container running within a Pod if resources become constrained within the cluster.