DevOps
  • DevOps
  • Docker
    • Overview
  • Vagrant
    • Overview
    • Installation
    • Basic Commands
    • Vagrantfile
      • Example
    • Deploying Multiple Machine
    • Distribution
    • Reference
  • Running SQL Server on Windows Docker
    • Introduction
      • Windows Container & Hyper-V Container
    • System Requirements and Prerequisites
    • Version Compatibility
    • Enable Hyper-V & Containers and Install Docker on Window Servers
    • Using Docker to Run MSSQL Image
    • Install SSMS for remoting MSSQL Server Image
    • Using Docker-Compose to Build Up a MSSQL Image
    • Increase the default Windows container's storage
    • Reference
    • Appendix
  • Kubernetes
    • Install Kubernetes with kubeasz
    • Use Hyper-V Manager to create VM for K8S
      • Specification of the VM
      • Use Hyper-V Manager to turn on a VM for K8S
    • Installation
      • Install VirtualBox and its extension on Ubuntu
      • Install kubectl
      • Install minikube
    • Getting Started
      • Running Kubernetes Locally via Minikube
    • Assign Memory Resources to Containers and Pods
    • Assign CPU Resources to Containers and Pods
    • Quality of Services (QoS)
    • Volume for Storage
      • Configure a Pod to Use a Volume for Storage
      • *Configure a Pod to Use a PersistentVolume for Storage
    • In a nutshell...
    • Reference
  • Ansible
    • What is Ansible?
    • Set Up OS Environments for Ansible
    • Install Ansible
    • Structure for ansible
      • Inventory
      • Playbook
      • Modules
      • Variables
      • Conditions
    • Security - Ansible Fault
    • 實際操作測試1 - 使用ssh
    • 實際操作測試2 - Docker容器部署
    • Appendix
    • Reference
  • Azure
    • Overview
    • Terms
    • Getting Started
    • Azure DevOps Services
    • Reference
Powered by GitBook
On this page
  • What is a Pod?
  • Before you begin
  • Create a namespace
  • Specify a memory request and a memory limit
  • Exceed a Container’s memory limit

Was this helpful?

  1. Kubernetes

Assign Memory Resources to Containers and Pods

PreviousRunning Kubernetes Locally via MinikubeNextAssign CPU Resources to Containers and Pods

Last updated 6 years ago

Was this helpful?

What is a Pod?

A Pod (a group of sea mammals LOL) is a group of one or more (such as Docker containers), with shared storage/network, and a specification for how to run the containers. (即是一群搬運工人 - Docker!) A Pod’s contents are always co-located and co-scheduled, and run in a shared context. A Pod models an application-specific “logical host” - it contains one or more application containers which are relatively tightly coupled — in a pre-container world, being executed on the same physical or virtual machine would mean being executed on the same logical host.

Before you begin

If you are running Minikube, run the following command to enable the metrics-server:

minikube addons enable metrics-server

To see whether the metrics-server is running, or another provider of the resource metrics API (metrics.k8s.io), run the following command:

kubectl get apiservices

If the resource metrics API is available, the output includes a reference to metrics.k8s.io.

NAME      
v1beta1.metrics.k8s.io

Create a namespace

Create a namespace so that the resources you create in this exercise are isolated from the rest of your cluster.

kubectl create namespace mem-example

Specify a memory request and a memory limit

To specify a memory request for a Container, include the resources:requests field in the Container’s resource manifest. To specify a memory limit, include resources:limits.

In this exercise, you create a Pod that has one Container. The Container has a memory request of 100 MiB and a memory limit of 200 MiB. Here’s the configuration file for the Pod( I changed a bit for becoming my version):

apiVersion: v1
kind: Pod
metadata:
  name: syrus-memory-demo
  namespace: syrusk8s
spec:
  containers:
  - name: syrus-demo-ctr
    image: polinux/stress
    resources:
      limits:
        memory: "200Mi"
      requests:
        memory: "100Mi"
    command: ["stress"]
    args: ["--vm", "1", "--vm-bytes", "150M", "--vm-hang", "1"]

The args section in the configuration file provides arguments for the Container when it starts. The "--vm-bytes", "150M" arguments tell the Container to attempt to allocate 150 MiB of memory.

Create the Pod(I created a file called 'share' on the root path and put the yaml inside it):

kubectl apply -f /share/memory-request-limit.yaml --namespace=syrusk8s

Verify that the Pod Container is running:

kubectl get pod syrus-memory-demo --namespace=syrusk8s

View detailed information about the Pod:

kubectl get pod memory-demo --output=yaml --namespace=mem-example

The output shows that the one Container in the Pod has a memory request of 100 MiB and a memory limit of 200 MiB.

...
resources:
  limits:
    memory: 200Mi
  requests:
    memory: 100Mi
...

Run kubectl top to fetch the metrics for the pod:

kubectl top pod memory-demo --namespace=mem-example

The output shows that the Pod is using about 162,900,000 bytes of memory, which is about 150 MiB. This is greater than the Pod’s 100 MiB request, but within the Pod’s 200 MiB limit.

NAME                        CPU(cores)   MEMORY(bytes)
memory-demo                 <something>  162856960

Delete your Pod:

kubectl delete pod memory-demo --namespace=mem-example

Exceed a Container’s memory limit

A Container can exceed its memory request if the Node has memory available. But a Container is not allowed to use more than its memory limit. If a Container allocates more memory than its limit, the Container becomes a candidate for termination. If the Container continues to consume memory beyond its limit, the Container is terminated. If a terminated Container can be restarted, the kubelet restarts it, as with any other type of runtime failure.

In this exercise, you create a Pod that attempts to allocate more memory than its limit. Here is the configuration file for a Pod that has one Container with a memory request of 50 MiB and a memory limit of 100 MiB:

apiVersion: v1
kind: Pod
metadata:
  name: syrus-memory-demo-2
  namespace: syrusk8s
spec:
  containers:
  - name: syrus-demo-2-ctr
    image: polinux/stress
    resources:
      limits:
        memory: "100Mi"
      requests:
        memory: "50Mi"
    command: ["stress"]
    args: ["--vm", "1", "--vm-bytes", "250M", "--vm-hang", "1"]

In the args section of the configuration file, you can see that the Container will attempt to allocate 250 MiB of memory, which is well above the 100 MiB limit.

Create the Pod:

kubectl apply -f https://k8s.io/examples/pods/resource/memory-request-limit-2.yaml --namespace=mem-example

View detailed information about the Pod:

kubectl get pod memory-demo-2 --namespace=mem-example

At this point, the Container might be running or killed. Repeat the preceding command until the Container is killed:

NAME            READY     STATUS      RESTARTS   AGE
memory-demo-2   0/1       OOMKilled   1          24s

Get a more detailed view of the Container status:

kubectl get pod memory-demo-2 --output=yaml --namespace=mem-example

The output shows that the Container was killed because it is out of memory (OOM):

lastState:
   terminated:
     containerID: docker://65183c1877aaec2e8427bc95609cc52677a454b56fcb24340dbd22917c23b10f
     exitCode: 137
     finishedAt: 2017-06-20T20:52:19Z
     reason: OOMKilled
     startedAt: null

The Container in this exercise can be restarted, so the kubelet restarts it. Repeat this command several times to see that the Container is repeatedly killed and restarted:

kubectl get pod memory-demo-2 --namespace=mem-example

The output shows that the Container is killed, restarted, killed again, restarted again, and so on:

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

View detailed information about the Pod history:

kubectl describe pod memory-demo-2 --namespace=mem-example

The output shows that the Container starts and fails repeatedly:

... Normal  Created   Created container with id 66a3a20aa7980e61be4922780bf9d24d1a1d8b7395c09861225b0eba1b1f8511
... Warning BackOff   Back-off restarting failed container

View detailed information about your cluster’s Nodes:

kubectl describe nodes

The output includes a record of the Container being killed because of an out-of-memory condition:

Warning OOMKilling Memory cgroup out of memory: Kill process 4481 (stress) score 1994 or sacrifice child

Delete your Pod:

kubectl delete pod memory-demo-2 --namespace=mem-example
containers