페이지 트리

이 문서는 Docker CPU, Memory 리소스 제한 및 부하 테스트 내용을 공유하기 위해 작성되었다.


도구명docker-compose
버전2.16.0
OScentOS 8
비고




docker-compose CPU 제한 및 부하 테스트

1.  목표 설정

Docker Compose에서 실행되는 컨테이너의 CPU 사용률과 메모리 사용률 테스트


2. 테스트 구성

docker-compose upgrade

  • docker-compose deploy의 resource를 사용하기 위해 docker-compose upgrade

docker-compose 버전이 1.X 일 경우 해당 부분 지원하지 않음

#docker-compose 버전 확인
docker-compose -v

sudo rm /usr/local/bin/docker-compose
# curl + grep
VERSION=$(curl --silent https://api.github.com/repos/docker/compose/releases/latest | grep -Po '"tag_name": "\K.*\d')

# curl + jq
VERSION=$(curl --silent https://api.github.com/repos/docker/compose/releases/latest | jq .name -r)
DESTINATION=/usr/local/bin/docker-compose
sudo curl -L https://github.com/docker/compose/releases/download/${VERSION}/docker-compose-$(uname -s)-$(uname -m) -o $DESTINATION
sudo chmod 755 $DESTINATION

docker-compose.yml 예시

services:
  service_name:
    ...
    deploy:
      resources:
        limits:
          cpus: '0.5' # half a CPU
          memory: 256M # 256 MB of RAM
        reservations:
          cpus: '0.25' # a quarter of a CPU
          memory: 128M # 128 MB of RAM

docker-compose 실행 명령어

docker-compose -f do-limits.yml up -d

docker 상태 확인

docker stats [OPTIONS] [CONTAINER...]


3. 테스트

  • 컨테이너 내부 접속 및 필요 command 설치

#접속
docker exec -it [Name 및 Container ID] /bin/bash

#종료
exit

#OS 버전 확인 후 필요한 패키지 설치
cat /etc/*-release
apt-get update &&
apt-get -y install vim &&
apt-get -y install stress


  • Container 내부 bash 접속 후 shell script 부하 테스트

    docker container 내부에 접속, 해당 스크립트 작성 후 실행
cpu 부하주는 bash 스크립트 
#vi cpu_stress.sh
#chmod 777 cpu_stress.sh

#!/bin/bash

# Function to check if a number is prime
is_prime() {
  local number=$1
  for ((i=2; i<=$((number/2)); i++)); do
    if [ $((number%i)) -eq 0 ]; then
      return 1
    fi
  done
  return 0
}

# Infinite loop to calculate prime numbers
while true; do
  for ((i=2; i<=100000; i++)); do
    if is_prime $i; then
      echo $i
    fi
  done
done
memory 부하주는 bash 스크립트
#vi mem_stress.sh
#chmod 777 mem_stress.sh  

#!/bin/bash

# Run the memory stress test loop
while true
do
	#head -c ${memory}k </dev/zero> /dev/null
	</dev/zero head -c 512m | tail
	#</dev/zero head | tail
	#(</dev/zero head -c 512m) <(sleep 300) | tail
done


  • Container 내부 bash 접속 후 stress 명령어 부하 테스트

    docker container 내부 접속 후 stress 명령어 실행
#CPU 부하
stress --cpu 2 --timeout 20s

#Memory 부하
stress --verbose --vm 1 --vm-bytes 512M --timeout 20s


  • docker-compose.yml entrypoint, command 부하 테스트

    entrupoint, command: container 실행 시 실행되는 명령어(entrypoint > command)
version: "3"
services:
  web1:
    container_name: test_1
    image: nginx
    #command: sh -c "apt update&&apt-get install vim -y&&apt-get install stress -y&&stress --verbose --vm 1 --vm-bytes 512M"
    entrypoint: sh -c "apt update&&apt-get install vim -y&&apt-get install stress -y&&stress --verbose --vm 1 --vm-bytes 512M"
    ports:
      - "8083:8080"
    deploy:
      resources:
        limits:
          cpus: '1' # half a CPU
          memory: 256M # 256 MB of RAM
        reservations:
          cpus: '0.5' # a quarter of a CPU
          memory: 256M # 128 MB of RAM
  db:
    image: postgres:12
    container_name: postgres12
    environment:
      POSTGRES_USER: sonar
      POSTGRES_PASSWORD: sonar
    deploy:
      resources:
        limits:
          cpus: '1.5' # half a CPU
          memory: 1024M # 256 MB of RAM
        reservations:
          cpus: '0.6' # a quarter of a CPU
          memory: 256M # 128 MB of RAM
  web3:
    container_name: test_3
    image: nginx
    ports:
      - "8082:8080"
    deploy:
      resources:
        limits:
          cpus: '1.5' # half a CPU
          memory: 1024M # 256 MB of RAM
        reservations:
          cpus: '0.6' # a quarter of a CPU
          memory: 256M # 128 MB of RAM


4. 부하 테스트 수행 결과

4-1. Container 내부 bash 접속 후 shell script 부하 테스트

  • CPU 부하 스크립트 결과-1(docker stats)

  • CPU 부하 스크립트 결과-2(htop - 호스트)


  • Memory 부하 스크립트 결과-1(docker stats)

  • Memory 부하 스크립트 결과-2(htop-호스트)

  • 부하에 따른 script 부하 kill


4-2. Container 내부 bash 접속 후 stress 명령어 부하 테스트

  • CPU 부하 command: stress --cpu 2 --timeout 20s

  • Memory 부하 command: stress --verbose --vm 1 --vm-bytes 512M

  • 부하에 따른 stress 명령 kill    #stress무한루프 : stress --verbose --vm 1 --vm-bytes 512M


4-3. docker-compose.yml entrypoint, command 부하 테스트

  • docker-compose.yml entrypoint, command 부하 테스트

  • while true;do timeout 2s bash -c "docker stats" >> test.txt;done   #test.txt 파일


5. 결과 분석

  • cpu의 경우

  1. cpu의 경우 컨테이너 종료 없이 풀 차지

  • memory의 경우

  1. memory의 경우 단순 stress 명령어나 스크립트로 부하를 줄 경우 limit 설정한 임계 값에 도달 할 시 stress 명령이나 스크립트가 종료처리 됨(kill)
    • 컨테이너는 종료되지 않음
  2. docker-compose.yml에 entrypoint로 stress 명령을 줄 경우 임계 값에 도달 시 stress 명령이 종료 처리되면서 컨테이너가 종료됨
  3. stress명령 및 스크립트로 postgresql에 계속적인 부하를 준 경우 postgresql 성능은 매우 느려지나 컨테이너는 안꺼짐



참조

docker-compose 버전 upgrade


memory script 


stress 명령어

  • 레이블 없음