Brief introduction
This article focuses on how to set the number of memory requests and memory of the container limit. Operation to ensure that a container must be greater than the number of memory requests to the memory of the container, but the container can not exceed the limit.
NOTE: This document is to be understood with reference to their official documents, and. If misleading, please criticism.
Create a namespace
# kubectl create namespace mem-example
Defined number of memory requests and memory limits the number of
Create a Pod, wherein the args
block --vm-bytes
parameter request 150MiB
memory. file name:memory-request-limit.yaml
apiVersion: v1
kind: Pod
metadata:
name: memory-demo
namespace: mem-example
spec:
containers:
- name: memory-demo-ctr
image: polinux/stress
resources:
limits:
memory: "200Mi"
requests:
memory: "100Mi"
command: ["stress"]
args: ["--vm", "1", "--vm-bytes", "150M", "--vm-hang", "1"]
# kubectl apply -f /root/k8s-example/pods/memory-request-limit.yaml --namespace=mem-example
Confirm Pod container is running
# kubectl get pod memory-demo --output=yaml --namespace=mem-example
View details Pod
# kubectl get pod memory-demo --output=yaml --namespace=mem-example
From the results, the number of change requests Pod memory 200Mi
, limit the number of100Mi
...
resources:
limits:
memory: 200Mi
requests:
memory: 100Mi
...
Execute kubectl top
command, view the current Pod easy to use the CPU and memory
# kubectl top pod memory-demo --namespace=mem-example
Note: perform kubectl top
first ensure that the installation metrics server
, refer to the following if not installed, the need to ensure can download k8s.gcr.io/metrics-server-amd64:v0.3.1
Mirror:
# git clone https://github.com/kodekloudhub/kubernetes-metrics-server.git
# kubectl create -f kubernetes-metrics-server/
From kubectl top
can be seen that the results using Pod current 150Mi
byte memory request value is greater than the Pod is 100MiB
smaller than the limit value of the Pod 200MiB
:
NAME CPU(cores) MEMORY(bytes)
memory-demo 138m 150Mi
Remove the Pod
# kubectl delete pod memory-demo --namespace=mem-example
Exceed the memory limits of the container
If the container has more memory can be allocated, the container may exceed the memory request custom. If the container requires more memory than the memory limit custom, the container will be a candidate to be terminated. If the container continue to consume more memory than the memory limit custom, the container is forced to stop operating. After the container is terminated run, kubelet restarted change container.
Create a memory allocated memory exceeds the limit of the Pod, a configuration, memory requests 50MiB
, the memory limit 100MiB
. file name:memory-request-limit-2.yaml
apiVersion: v1
kind: Pod
metadata:
name: memory-demo-2
namespace: mem-example
spec:
containers:
- name: memory-demo-2-ctr
image: polinux/stress
resources:
requests:
memory: "50Mi"
limits:
memory: "100Mi"
command: ["stress"]
args: ["--vm", "1", "--vm-bytes", "250M", "--vm-hang", "1"]
# kubectl apply -f /root/k8s-example/pods/memory-request-limit-2.yaml --namespace=mem-example
# kubectl get pod memory-demo-2 --namespace=mem-example
From the results, due to memory overflow, resulting in Pod directly kill off.
# kubectl get pod memory-demo-2 --namespace=mem-example NAME READY STATUS RESTARTS AGE
memory-demo-2 0/1 OOMKilled 1 4s
View details Pod
# kubectl get pod memory-demo-2 --output=yaml --namespace=mem-example
From the results, since the last state OOMKilled
i.e., Out Of Memory
cause the container to be kill
lastState:
terminated:
containerID: docker://0d4d5580e28df2a936f28cbef8580ac21c1014e1fad2814e28cfa28417bd3bb0
exitCode: 1
finishedAt: "2020-01-10T00:39:55Z"
reason: OOMKilled
startedAt: "2020-01-10T00:39:55Z"
Delete Pod
# kubectl delete pod memory-demo-2 --namespace=mem-example
Define memory node memory request is greater than the container
Memory requests, and the number of memory limitations associated with the container, and therefore, for the Pod, a memory request and set the appropriate limit is necessary. Pod number of memory request equal to the sum of the number of memory requests within all containers Pod. Pod memory limit is the sum of all the memory limits the number of containers within the Pod.
Pod scheduling is request scheduling based. Running a Pod Pod depending on which change if the Node satisfy memory requests of Pod.
In the following links, create a memory request is much greater than the total number of node memory Pod, Pod number of memory requests in the following examples is defined 1000GiB
. file name:memory-request-limit-3.yaml
apiVersion: v1
kind: Pod
metadata:
name: memory-demo-3
namespace: mem-example
spec:
containers:
- name: memory-demo-3-ctr
image: polinux/stress
resources:
limits:
memory: "1000Gi"
requests:
memory: "1000Gi"
command: ["stress"]
args: ["--vm", "1", "--vm-bytes", "150M", "--vm-hang", "1"]
# kubectl apply -f /root/k8s-example/pods/memory-request-limit-3.yaml --namespace=mem-example
View Yung Pod status
# kubectl get pod memory-demo-3 --namespace=mem-example
NAME READY STATUS RESTARTS AGE
memory-demo-3 0/1 Pending 0 15s
View details Pod resources
# kubectl describe pod memory-demo-3 --namespace=mem-example
From the results, seven node node does not have enough memory, because I was three master node high availability, k8s cluster node node 4
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning FailedScheduling <unknown> default-scheduler 0/7 nodes are available: 7 Insufficient memory.
Delete the name space
Memory Unit
Memory resources that bytes
measure bytes. Units E
, P
, T
, G
, M
, K
, Ei
, Pi
, Ti
, Gi
, Mi
, Ki
. For example, the following expressions are the same value
128974848, 129e6, 129M , 123Mi
Delete Pod resources
# kubectl delete pod memory-demo-3 --namespace=mem-example
Not defined memory limit
If there is no memory limit defined container, one of the following applies:
- The amount of memory used by its container no upper limit. Containers can use all available memory on the node it is running, and thus can be called OOM Killer. In addition, if the OOM Kill occurs, there is no resource constraints container will have a greater chance of being killed.
- Run container has a default namespace memory limitations in the system will automatically assign default limit for the container. Cluster administrators can
LimitRange
value defined memory limit.
The reason memory request limits
Memory request and memory limit through the container in a cluster configuration, memory resources can be used reasonably node cluster node. Pod memory request if the guarantee within a reasonable range, the Pod can be reasonably scheduled to the appropriate node in the cluster node.