forked from docs/doc-exports
Reviewed-by: Eotvos, Oliver <oliver.eotvos@t-systems.com> Co-authored-by: Dong, Qiu Jian <qiujiandong1@huawei.com> Co-committed-by: Dong, Qiu Jian <qiujiandong1@huawei.com>
75 lines
6.7 KiB
HTML
75 lines
6.7 KiB
HTML
<a name="cce_bestpractice_00002"></a><a name="cce_bestpractice_00002"></a>
|
|
|
|
<h1 class="topictitle1">Properly Allocating Container Computing Resources</h1>
|
|
<div id="body8662426"><p id="cce_bestpractice_00002__en-us_topic_0183030542_p24441586012">If a node has sufficient memory resources, a container on this node can use more memory resources than requested, but no more than limited. If the memory allocated to a container exceeds the upper limit, the container is stopped first. If the container continuously uses memory resources more than limited, the container is terminated. If a stopped container is allowed to be restarted, kubelet will restart it, but other types of run errors will occur.</p>
|
|
<div class="section" id="cce_bestpractice_00002__en-us_topic_0183030542_section084113417487"><h4 class="sectiontitle">Scenario 1</h4><p id="cce_bestpractice_00002__en-us_topic_0183030542_p2024195015485">The node's memory has reached the memory limit reserved for the node. As a result, OOM killer is triggered.</p>
|
|
<p id="cce_bestpractice_00002__en-us_topic_0183030542_p174631133316"><strong id="cce_bestpractice_00002__en-us_topic_0183030542_b3462156185313">Solution</strong></p>
|
|
<p id="cce_bestpractice_00002__en-us_topic_0183030542_p43761944426">You can either scale up the node or migrate the pods on the node to other nodes.</p>
|
|
</div>
|
|
<div class="section" id="cce_bestpractice_00002__en-us_topic_0183030542_section1190644417499"><h4 class="sectiontitle">Scenario 2</h4><p id="cce_bestpractice_00002__en-us_topic_0183030542_p4584753184911">The upper limit of resources configured for the pod is too small. When the actual usage exceeds the limit, OOM killer is triggered.</p>
|
|
<p id="cce_bestpractice_00002__en-us_topic_0183030542_p8951891316"><strong id="cce_bestpractice_00002__en-us_topic_0183030542_b18861827686">Solution</strong></p>
|
|
<p id="cce_bestpractice_00002__en-us_topic_0183030542_p147841204315">Set a higher upper limit for the workload.</p>
|
|
</div>
|
|
<div class="section" id="cce_bestpractice_00002__en-us_topic_0183030542_section991734310316"><h4 class="sectiontitle">Example</h4><p id="cce_bestpractice_00002__en-us_topic_0183030542_p73781251436">A pod will be created and allocated memory that exceeds the limit. As shown in the following configuration file of the pod, the pod requests 50 MiB memory and the memory limit is set to 100 MiB.</p>
|
|
<p id="cce_bestpractice_00002__en-us_topic_0183030542_p1898362217517">Example YAML file (memory-request-limit-2.yaml):</p>
|
|
<pre class="screen" id="cce_bestpractice_00002__en-us_topic_0183030542_screen590314361861">apiVersion: v1
|
|
kind: Pod
|
|
metadata:
|
|
name: memory-demo-2
|
|
spec:
|
|
containers:
|
|
- name: memory-demo-2-ctr
|
|
image: vish/stress
|
|
resources:
|
|
requests:
|
|
memory: 50Mi
|
|
limits:
|
|
memory: "100Mi"
|
|
args:
|
|
- -mem-total
|
|
- 250Mi
|
|
- -mem-alloc-size
|
|
- 10Mi
|
|
- -mem-alloc-sleep
|
|
- 1s</pre>
|
|
</div>
|
|
<p id="cce_bestpractice_00002__en-us_topic_0183030542_p36642273244">The <strong id="cce_bestpractice_00002__en-us_topic_0183030542_b15265193415510">args</strong> parameters indicate that the container attempts to request 250 MiB memory, which exceeds the pod's upper limit (100 MiB).</p>
|
|
<p id="cce_bestpractice_00002__en-us_topic_0183030542_p1664182716249">Creating a pod:</p>
|
|
<pre class="screen" id="cce_bestpractice_00002__en-us_topic_0183030542_screen113961536162416">kubectl create -f https://k8s.io/docs/tasks/configure-pod-container/memory-request-limit-2.yaml --namespace=mem-example </pre>
|
|
<p id="cce_bestpractice_00002__en-us_topic_0183030542_p18874514243">Viewing the details about the pod:</p>
|
|
<pre class="screen" id="cce_bestpractice_00002__en-us_topic_0183030542_screen10187658102418">kubectl get pod memory-demo-2 --namespace=mem-example </pre>
|
|
<p id="cce_bestpractice_00002__en-us_topic_0183030542_p1149119162517">In this stage, the container may be running or be killed. If the container is not killed, repeat the previous command until the container is killed.</p>
|
|
<pre class="screen" id="cce_bestpractice_00002__en-us_topic_0183030542_screen11131141215250">NAME READY STATUS RESTARTS AGE
|
|
memory-demo-2 0/1 OOMKilled 1 24s</pre>
|
|
<p id="cce_bestpractice_00002__en-us_topic_0183030542_p111492942510">Viewing detailed information about the container:</p>
|
|
<pre class="screen" id="cce_bestpractice_00002__en-us_topic_0183030542_screen3866193962511">kubectl get pod memory-demo-2 --output=yaml --namespace=mem-example </pre>
|
|
<p id="cce_bestpractice_00002__en-us_topic_0183030542_p5704846162419">This output indicates that the container is killed because the memory limit is exceeded.</p>
|
|
<pre class="screen" id="cce_bestpractice_00002__en-us_topic_0183030542_screen12454717272">lastState:
|
|
terminated:
|
|
containerID: docker://7aae52677a4542917c23b10fb56fcb2434c2e8427bc956065183c1879cc0dbd2
|
|
exitCode: 137
|
|
finishedAt: 2020-02-20T17:35:12Z
|
|
reason: OOMKilled
|
|
startedAt: null</pre>
|
|
<p id="cce_bestpractice_00002__en-us_topic_0183030542_p11955134213254">In this example, the container can be automatically restarted. Therefore, kubelet will start it again. You can run the following command several times to see how the container is killed and started:</p>
|
|
<pre class="screen" id="cce_bestpractice_00002__en-us_topic_0183030542_screen11885955192711">kubectl get pod memory-demo-2 --namespace=mem-example</pre>
|
|
<p id="cce_bestpractice_00002__en-us_topic_0183030542_p111921812818">The preceding command output indicates how the container is killed and started back and forth:</p>
|
|
<pre class="screen" id="cce_bestpractice_00002__en-us_topic_0183030542_screen1516551712811">$ kubectl get pod memory-demo-2 --namespace=mem-example
|
|
NAME READY STATUS RESTARTS AGE
|
|
memory-demo-2 0/1 OOMKilled 1 37s
|
|
$ kubectl get pod memory-demo-2 --namespace=mem-example
|
|
NAME READY STATUS RESTARTS AGE
|
|
memory-demo-2 1/1 Running 2 40s</pre>
|
|
<p id="cce_bestpractice_00002__en-us_topic_0183030542_p74428162812">Viewing the historical information of the pod:</p>
|
|
<pre class="screen" id="cce_bestpractice_00002__en-us_topic_0183030542_screen15673115295">kubectl describe pod memory-demo-2 --namespace=mem-example </pre>
|
|
<p id="cce_bestpractice_00002__en-us_topic_0183030542_p13714213286">The following command output indicates that the pod is repeatedly killed and started.</p>
|
|
<pre class="screen" id="cce_bestpractice_00002__en-us_topic_0183030542_screen14279141153017">... Normal Created Created container with id 66a3a20aa7980e61be4922780bf9d24d1a1d8b7395c09861225b0eba1b1f8511
|
|
... Warning BackOff Back-off restarting failed container</pre>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="cce_bestpractice_0051.html">Container</a></div>
|
|
</div>
|
|
</div>
|
|
|