2025-01-19 14:07:10
HAProxyで負荷分散したときにスループットは上がるのかを実感してみる
Dockerコンテナ環境上で Apache Benchmark (abコマンド) を使って実感してみる
Apache Benchmark (abコマンド) で総アクセス数を固定して、並列数を変更したときにスループットに違いは出るのかを体験して実感します。
Docker コンテナ構成
過去の投稿と同じ以下の構成です。HAProxyコンテナから2つのhttpdコンテナに振り分けが可能です。
スループットが上がるかの確認では、振り分け先を1から2に増やして確認します。
確認方法
ab コマンドによって、総アクセス数を20に固定して、並列数を変更して実行していきます。以下は ab コマンドの実行例です。 -c
に続く値を変更して実行していきます。
ab -n 20 -c 1 -l http://www.nao000.internal/
各結果の「Time taken for tests」の値を見てスループットが上がるのかを確認します。
テストが終わる時間が短くなれば、1処理の時間が短くなるのでスループットが上がると考えています。
HAProxy の負荷分散アルゴリズムは roundrobin です。
アプリの処理内容
PHP で1秒待つだけのアプリです。コードは以下です。
<?php sleep(1); ?>
php-fpmのプロセス数
最大5プロセスです。 HAProxy と Apache httpd の設定などは最後に記載しています。php-fpmがボトルネックになる構成になっている認識なので蛇足かと思い、最後に記載しています。
root@74cefbefb871:/# cat /etc/php/8.1/fpm/pool.d/www.conf | grep pm. | grep -v ";"
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
root@74cefbefb871:/#
確認する
ab コマンドを実行してきます。以下は実行例です。
ab -n 20 -c 1 -l http://www.nao000.internal/
表 「Time taken for tests」の結果
並列数 | 振り分け先が1つ | 振り分け先が2つ |
---|---|---|
1 | 20.055 seconds | 20.037 seconds |
2 | 11.020 seconds | 11.019 seconds |
4 | 6.011 seconds | 6.013 seconds |
6 | 5.236 seconds | 5.010 seconds |
8 | 5.431 seconds | 4.009 seconds |
10 | 5.993 seconds | 4.006 seconds |
12 | 5.795 seconds | 4.005 seconds |
14 | 5.130 seconds | 4.005 seconds |
結果
振り分け先が1から2に増えたときに、並列数8以降で約1秒の違いが出てきました。
表にしてみると振り分け先が2だとスループットが上がることが実感できました。
おわり
HAProxy を含めた各ミドルウェアの設定はあまり分かっていませんが、ちゃんと負荷分散はされていることを実感できました。
おわりです。
各ミドルウェアの主な設定など(HAProxy, apache httpd , php-fpm のプロセス数の設定)
HAProxy
HAProxy が1プロセスでどのくらい捌けるのか分かっていないです。apache httpd と php-fpm の処理時間よりも非常に短い処理時間で捌けると思っています。ので、HAProxy のスループットは気にしてません。
設定ファイルにプロセス数やスレッド数などは特に記載なさそうですが以下に掲載します。
root@b340133a0059:/# cat /etc/haproxy/haproxy.cfg
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
stats timeout 30s
user haproxy
group haproxy
daemon
# Default SSL material locations
ca-base /etc/ssl/certs
crt-base /etc/ssl/private
# See: https://ssl-config.mozilla.org/#server=haproxy&server-version=2.0.3&config=intermediate
ssl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384
ssl-default-bind-ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256
ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets
defaults
log global
mode http
option httplog
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000
errorfile 400 /etc/haproxy/errors/400.http
errorfile 403 /etc/haproxy/errors/403.http
errorfile 408 /etc/haproxy/errors/408.http
errorfile 500 /etc/haproxy/errors/500.http
errorfile 502 /etc/haproxy/errors/502.http
errorfile 503 /etc/haproxy/errors/503.http
errorfile 504 /etc/haproxy/errors/504.http
frontend myfrontend
bind 0.0.0.0:80
default_backend myservers
backend myservers
balance roundrobin
server server1 httpd-1:80 check
server server2 httpd-2:80 check # 振り分け先を1にするときはここをコメントアウトします
root@b340133a0059:/#
apache httpd
mpm は event で稼働しています。
root@9d0642963a31:/# apache2ctl -V
Server version: Apache/2.4.52 (Ubuntu)
Server built: 2024-07-17T18:57:26
Server's Module Magic Number: 20120211:126
Server loaded: APR 1.7.0, APR-UTIL 1.6.1
Compiled using: APR 1.7.0, APR-UTIL 1.6.1
Architecture: 64-bit
Server MPM: event
threaded: yes (fixed thread count)
forked: yes (variable process count)
Server compiled with....
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_PROC_PTHREAD_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=256
-D HTTPD_ROOT="/etc/apache2"
-D SUEXEC_BIN="/usr/lib/apache2/suexec"
-D DEFAULT_PIDLOG="/var/run/apache2.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="mime.types"
-D SERVER_CONFIG_FILE="apache2.conf"
root@9d0642963a31:/#
event の設定です。
root@9d0642963a31:/# cat /etc/apache2/mods-enabled/mpm_event.conf
# event MPM
# StartServers: initial number of server processes to start
# MinSpareThreads: minimum number of worker threads which are kept spare
# MaxSpareThreads: maximum number of worker threads which are kept spare
# ThreadsPerChild: constant number of worker threads in each server process
# MaxRequestWorkers: maximum number of worker threads
# MaxConnectionsPerChild: maximum number of requests a server process serves
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 0
</IfModule>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
root@9d0642963a31:/#