Docker Container Resource Management: CPU, RAM and IO: Part 2

cpu-shares Proportional to Other Containers

docker container run -d --cpu-shares=4 --name mycpu1024a alpine:3.8 /bin/sh -c 'time dd if=/dev/urandom bs=1M count=100 | md5sum'
docker container run -d --cpu-shares=4 --name mycpu1024b alpine:3.8 /bin/sh -c 'time dd if=/dev/urandom bs=1M count=100 | md5sum'
docker container run -d --cpu-shares=4 --name mycpu1024c alpine:3.8 /bin/sh -c 'time dd if=/dev/urandom bs=1M count=100 | md5sum'
docker logs mycpu1024a
docker logs mycpu1024b
docker logs mycpu1024c
docker container prune -f

cpu-shares: Only Enforced When CPU Cycles Are Constrained

docker container run -d --cpu-shares=4 --name mycpu1024a alpine:3.8 /bin/sh -c 'time dd if=/dev/urandom bs=1M count=100 | md5sum'docker logs mycpu1024areal    0m 12.67s
user 0m 0.00s
sys 0m 12.27s
docker container run -d --cpu-shares=4000 --name mycpu1024a alpine:3.8 /bin/sh -c 'time dd if=/dev/urandom bs=1M count=100 | md5sum'
docker container prune -f

— cpus= Defines How Much of the Available CPU Resources a Container Can Use

docker container run -d --cpus=2 --name mycpu2 alpine:3.8 /bin/sh -c 'time dd if=/dev/urandom bs=1M count=50 | md5sum'
docker container run -d --cpus=1 --name mycpu1 alpine:3.8 /bin/sh -c 'time dd if=/dev/urandom bs=1M count=50 | md5sum'
docker container run -d --cpus=.5 --name mycpu.5 alpine:3.8 /bin/sh -c 'time dd if=/dev/urandom bs=1M count=50 | md5sum'
docker container run -d --cpus=.25 --name mycpu.25 alpine:3.8 /bin/sh -c 'time dd if=/dev/urandom bs=1M count=20 | md5sum'
docker container run -d --cpus=.1 --name mycpu.1 alpine:3.8 /bin/sh -c 'time dd if=/dev/urandom bs=1M count=10 | md5sum'
docker stats
CONTAINER ID        NAME                CPU %               MEM USAGE / LIMIT     MEM %               NET I/O             BLOCK I/O           PIDS
843bea7263fb mycpu2 57.69% 1.258MiB / 985.2MiB 0.13% 578B / 0B 1.33MB / 0B 0
186ba15b8258 mycpu1 55.85% 1.25MiB / 985.2MiB 0.13% 578B / 0B 1.33MB / 0B 0
3bcc26eab1ac mycpu.5 46.60% 1.262MiB / 985.2MiB 0.13% 578B / 0B 1.33MB / 0B 0
79d7d7e3c38c mycpu.25 25.43% 1.262MiB / 985.2MiB 0.13% 508B / 0B 1.33MB / 0B 0
b4ba5503a048 mycpu.1 9.76% 1.328MiB / 985.2MiB 0.13% 508B / 0B 1.33MB / 0B 0

— cpus Number of CPUs

lscpu | head -n 10Architecture:          x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 2
On-line CPU(s) list: 0,1
Thread(s) per core: 1
Core(s) per socket: 2
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
lscpu | head -n 10CPU(s):                2
On-line CPU(s) list: 0,1
Thread(s) per core: 1
Core(s) per socket: 2
Socket(s): 1
grep -E 'processor|core id' /proc/cpuinfo
processor       : 0
core id : 0
processor : 1
core id : 1
docker container run --cpus=2 --name mycpu alpine:3.8 /bin/sh -c 'time dd if=/dev/urandom bs=1M count=30 | md5sum'
real    0m 3.61s
user 0m 0.00s
sys 0m 3.50s
docker container prune -f
docker container run --cpus=1 --name mycpu alpine:3.8 /bin/sh -c 'time dd if=/dev/urandom bs=1M count=30 | md5sum'
real 0m 3.54s
user 0m 0.00s
sys 0m 3.37s
docker container prune -f
docker container run --cpus=.5 --name mycpu alpine:3.8 /bin/sh -c 'time dd if=/dev/urandom bs=1M count=30 | md5sum'
real 0m 9.97s
user 0m 0.00s
sys 0m 4.78s
docker container prune -f
docker container run --rm --cpus=.25 --name mycpu alpine:3.8 /bin/sh -c 'time dd if=/dev/urandom bs=1M count=30 | md5sum'
real 0m 19.55s
user 0m 0.00s
sys 0m 4.69s

— cpu-period and — cpu-quota

— cpuset-cpus CPUs in which to allow execution (0–3, 0,1)

docker container run --rm --cpuset-cpus=0,1 --name mycpu alpine:3.8 /bin/sh -c 'time dd if=/dev/urandom bs=1M count=30 | md5sum'Expected output : real    0m 3.44s
user 0m 0.00s
sys 0m 3.35s
docker container run --rm --cpuset-cpus=0 --name mycpu alpine:3.8 /bin/sh -c 'time dd if=/dev/urandom bs=1M count=30 | md5sum'Expected output : real 0m 4.15s
user 0m 0.00s
sys 0m 4.00s
docker container run --rm --cpuset-cpus=1 --name mycpu alpine:3.8 /bin/sh -c 'time dd if=/dev/urandom bs=1M count=30 | md5sum'Expected output : real 0m 3.40s
user 0m 0.00s
sys 0m 3.28s

Test Container Limits Using Real Benchmark Applications

mkdir dockerbench
cd dockerbench
nn DockerfileFROM centos:7RUN set -x \
&& yum -y install https://www.percona.com/redir/downloads/percona-release/redhat/0.0-1/percona-release-0.0-1.x86_64.rpm \
&& yum -y install sysbench \

&& curl http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm -o epel-release-latest-7.noarch.rpm \
&& rpm -ivh epel-release-latest-7.noarch.rpm \
&& yum -y install stress
docker build --tag centos:bench --file Dockerfile  .

— cpus Tested with Sysbench Tool

  • — threads=2 … run 2 threads so we can compare 2 CPUs versus 1 CPU
  • — events=4 … do 4 runs
  • — cpu-max-prime=800500 … calculate prime numbers up to 800500
  • — verbosity=0 … do not show detailed output
  • cpu run … run the test named CPU
docker run -it --rm --name mybench  --cpus 2 centos:bench /bin/shsh-4.2# time sysbench --threads=2 --events=4 --cpu-max-prime=800500 --verbosity=0 cpu runreal    0m1.952s
user 0m3.803s
sys 0m0.029s
sh-4.2#
sh-4.2# exit
exit
docker run -it --rm --name mybench  --cpus 1 centos:bench /bin/shsh-4.2# time sysbench --threads=2 --events=4 --cpu-max-prime=800500 --verbosity=0 cpu runreal    0m4.686s
user 0m4.678s
sys 0m0.026s
sh-4.2#
sh-4.2# exit
exit
docker run -it --rm --name mybench  --cpus 1 centos:bench /bin/sh -c 'time sysbench --threads=2 --events=4 --cpu-max-prime=800500 --verbosity=0 cpu run'real    0m4.659s
user 0m4.649s
sys 0m0.028s
docker run -it --rm --name mybench  --cpus .5 centos:bench /bin/sh -c 'time sysbench --threads=2 --events=4 --cpu-max-prime=800500 --verbosity=0 cpu run'real    0m10.506s
user 0m5.221s
sys 0m0.035s
  • 2 CPUs : real 0m1.952s
  • 1 CPU : real 0m4.659s
  • 5 CPUs : real 0m10.506s
docker run -it --rm --name mybench  --cpus 2 centos:bench /bin/sh -c 'time sysbench --threads=2 --events=4 --cpu-max-prime=8005 --verbosity=0 cpu run'
docker run -it --rm --name mybench --cpus .5 centos:bench /bin/sh -c 'time sysbench --threads=2 --events=4 --cpu-max-prime=8005 --verbosity=0 cpu run'
docker run -it --rm --name mybench --cpus 1 centos:bench /bin/sh -c 'time sysbench --threads=2 --events=4 --cpu-max-prime=8005 --verbosity=0 cpu run'
2 CPUs
real 0m0.049s
user 0m0.016s
sys 0m0.021s
1 CPUs
real 0m0.049s
user 0m0.019s
sys 0m0.020s
.5 CPU
real 0m0.051s
user 0m0.020s
sys 0m0.019s
docker run -it --rm --name mybench  --cpus 2 centos:bench /bin/sh -c 'time sysbench --threads=2 --events=4 --cpu-max-prime=100500 --verbosity=0 cpu run'
docker run -it --rm --name mybench --cpus 1 centos:bench /bin/sh -c 'time sysbench --threads=2 --events=4 --cpu-max-prime=100500 --verbosity=0 cpu run'
docker run -it --rm --name mybench --cpus .5 centos:bench /bin/sh -c 'time sysbench --threads=2 --events=4 --cpu-max-prime=100500 --verbosity=0 cpu run'
2 CPUs
real 0m0.152s
user 0m0.225s
sys 0m0.015s
1 CPU
real 0m0.277s
user 0m0.279s
sys 0m0.019s
.5 CPU
real 0m0.615s
user 0m0.290s
sys 0m0.024s

--

--

--

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Manage Data Pipelines with Apache Airflow

Two minutes NLP — Python Regular Expressions cheatsheet

Activities (Android Lesson)

Getting Started with Kubernetes | Kubernetes Network and Policy Control

How to plot color channels Histogram of an Image in Python using OpenCV

In Solutions Architecture, Perfect != Perfect

Writeup to the FLARE-ON 5 challenge

Format subtitles before exporting: the Visual Subtitle Editor from Simon Says

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com

More from Medium

Using Nginx Ingress Controller to Manage HTTP/1.1 and HTTP/2 Protocols

Integration testing AWS + Spark jobs using Localstack + Docker

Stream Landing Kafka Data to Object Storage using Terraform

Testing, our new hero