리소스 제한 & 모니터링
1. 컨테이너 하드웨어 리소스 제한
2. 컨테이너 사용 리소스를 확인하는 모니터링 툴
기본적으로 컨테이너는 호스트 하드웨어 리소스의 사용 제한을 받지 않는다.
컨테이너가 필요로 하는 만큼의 리소스만 할당 해야한다.
Docker command를 통해 제한할수 있는 리소스
docker run --help
totoro@docker-ubuntu:~$ docker run --help
Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Create and run a new container from an image
Aliases:
docker container run, docker run
Options:
# 중략
# 메모리 관련 옵션
-m, --memory bytes Memory limit
--memory-reservation bytes Memory soft limit
--memory-swap bytes Swap limit equal to memory plus swap: '-1' to enable unlimited swap
--memory-swappiness int Tune container memory swappiness (0 to 100) (default -1)
--mount mount Attach a filesystem mount to the container
--name string Assign a name to the container
--network network Connect a container to a network
--network-alias list Add network-scoped alias for the container
--no-healthcheck Disable any container-specified HEALTHCHECK
--oom-kill-disable Disable OOM Killer
--oom-score-adj int Tune host's OOM preferences (-1000 to 1000)
--pid string PID namespace to use
--pids-limit int Tune container pids limit (set -1 for unlimited)
--platform string Set platform if server is multi-platform capable
--privileged Give extended privileges to this container
Memory 리소스 제한
옵션
제한 단위는 b, k, m, g로 할당
# 컨테이너가 사용할 최대 메모리 양을 지정
--memory, -m
docker run -d -m 512m nginx:1.14
# 컨테이너가 사용할 스왑 메모리 영역에 대한 설정 메모리+스왑. 생략시 메모리의 2배가 설정됨
--memory-swap
docker run -d -m 200m --memory-swap 300m nginx:1.14
# --memory 값보다 적은 값으로 구성하는 소프트 제한 값 설정
--memory-reservation
docker run -d -m 1g --memory-reservation 500m nginx:1.14
# OOM Killer가 프로세스 kill하지 못하도록 보호
--oom-kill-disable
docker run -d -m 200m --oom-kill-disable nginx:1.14
CPU 리소스 제한
# 컨테이너에 할당할 CPU core수를 지정 --cpus="1.5" 컨테이너가 최대 1.5개의 CPU 파워 사용가능
--cpus
docker run -d --cpus=".5" ubuntu:1.14
# 컨테이너가 사용할 수 있는 CPU나 코어를 할당. cpu index는 0부터. --cpuset-cpus=0-4
--cpuset-cpus
docker run -d --cpu-shares 2048 ubuntu:1.14
# 컨테이너가 사용하는 CPU 비중을 1024 값을 기반으로 설정, --cpu-share 2048 기본값보다 두 배 많은 CPU자원을 할당
--cpu-share
docker run -d --cpuset-cpus 0-3 ubuntu:1.14
Block I/O 제한
# Block IO의 Quota를 설정할 수 있으며 100~1000까지 선택 default 500
--blkio-weight
--blkio-weight-device
docker run -it --rm --blkio-weight 100 ubuntu:latest /bin/bash
# 특정 디바이스에 대한 읽기와 쓰기 작업의 초당 제한을 kb, mb, gb 단위로 설정
--device-read-bps
--device-write-bps
doker run -it --rm -device-write-bps/dev/vda:1mb ubuntu:latest /bin/bash
doker run -it --rm -device-write-bps/dev/vda:10mb ubuntu:latest /bin/bash
# 컨테이너의 read/write 속도의 쿼터를 설정한다 초당 quota를 제한해서 I/O를 발생시킴. 0이상의 정수로 표시 초당 데이터 전송량 = IOPS*블럭크기(단위 데이터 용량)
--device-read-iops
--deive-write-iops
doker run -it --rm -device-write-iops/dev/vda:10 ubuntu:latest /bin/bash
doker run -it --rm -device-write-iops/dev/vda:100 ubuntu:latest /bin/bash