
この記事では、Ubuntu 18.04 LTS に HAProxy Load Balancer をインストールして構成するために必要な手順について説明しました。このチュートリアルを続行する前に、sudo 権限を持つユーザーとしてログインしていることを確認してください。このチュートリアルのすべてのコマンドは、root 以外のユーザーとして実行する必要があります。
HAProxy はオープンソースの Linux ツールで、TCP および HTTP ベースのネットワーク アプリケーションに高可用性の負荷分散とプロキシ サービスを提供します。既存のアーキテクチャへの容易な統合、トラフィックの多い Web サイトへの適合性、極端な信頼性、および上位互換性への注力により、ほとんどの主流の Linux ディストリビューションでデフォルトで出荷されています。
Ubuntu に HAProxy をインストールする
ステップ 1. ネットワークの詳細
簡単にするために、インスタンスに次の IP アドレスとホスト名を想定します:
- haproxy-server :パブリック IP アドレス 198.18.0.1
- backend-server1 :プライベート IP アドレス 172.16.0.1 、パブリック IP アドレス 198.18.0.1
- backend-server2 :プライベート IP アドレス 172.16.0.2 、パブリック IP アドレス 198.18.0.2
ステップ 2. まず、Ubuntu サーバーにパッケージをインストールする前に、すべてのシステム パッケージが更新されていることを確認することを常にお勧めします。
sudo apt update sudo apt upgrade
ステップ 3. Ubuntu 18.04 LTS に HaProxy をインストールします。
HaProxy は Ubuntu ソフトウェア リポジトリで利用できるため、以下のコマンドを実行してパッケージ マネージャーを使用してインストールできます。
sudo add-apt-repository ppa:vbernat/haproxy-1.8 sudo apt-get update sudo apt-get install haproxy
ステップ 4. HAProxy を使用した負荷分散の構成。
haproxy のデフォルト設定ファイル /etc/haproxy/haproxy.cfg を編集して、設定を開始します:
sudo nano /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
stats timeout 30s
user haproxy
group haproxy
daemon
# Default SSL material locations
ca-base /etc/ssl/certs
crt-base /etc/ssl/private
# Default ciphers to use on SSL-enabled listening sockets.
# For more information, see ciphers(1SSL). This list is from:
# https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256::RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS
ssl-default-bind-options no-sslv3
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 ourwebsitefrontend
bind *:80
mode http
default_backend ourwebsiteendpoin bind パラメータは、接続のためにポート 80 をリッスンするように HaProxy に指示します。テキストの最後で、エンドポイントが配置されているディレクティブとして ourwebsiteendpoint を指定しました。これで、次のようにバックエンド構成の詳細を追加できます:
backend ourwebsiteendpoint
balance roundrobin
option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
option httpchk HEAD / HTTP/1.1\r\nHost:localhost
server backend-server1 172.16.0.1:8080 check
server backend-server2 172.16.0.2:8080 check 必要に応じて、HAProxy 構成ファイルに次の構成を追加して、Haproxy 統計を有効にすることができます:
listen stats
bind :32600
stats enable
stats uri /
stats hide-version
stats auth username:password ステップ 5. 最終的な HAProxy 構成ファイル。
最終的な構成ファイルは次のようになります:
global
log /dev/log local0
log /dev/log local1 notice
chroot /var/lib/haproxy
stats socket /run/haproxy/admin.sock mode 660 level admin
stats timeout 30s
user haproxy
group haproxy
daemon
# Default SSL material locations
ca-base /etc/ssl/certs
crt-base /etc/ssl/private
# Default ciphers to use on SSL-enabled listening sockets.
# For more information, see ciphers(1SSL). This list is from:
# https://hynek.me/articles/hardening-your-web-servers-ssl-ciphers/
ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM$
ssl-default-bind-options no-sslv3
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 ourwebsitefrontend
bind *:80
mode http
default_backend ourwebsiteendpoint
backend ourwebsiteendpoint
balance roundrobin
option forwardfor
http-request set-header X-Forwarded-Port %[dst_port]
http-request add-header X-Forwarded-Proto https if { ssl_fc }
option httpchk HEAD / HTTP/1.1\r\nHost:localhost
server backend-server1 172.16.0.1:8080 check
server backend-server2 172.16.0.2:8080 check
listen stats
bind :32600
stats enable
stats uri /
stats hide-version
stats auth username:password 次に、HaProxy サーバーを再起動して変更をリロードします。
sudo service haproxy restart
ステップ 6. 構成のテスト。
この段階で、完全に機能する HAProxy のセットアップが完了しました。各 Web サーバー ノードには、サーバーのホスト名を示すデモの index.html ページがあります。これにより、サーバーの Web ページを簡単に区別できます。
ここで、Web ブラウザーで IP 198.18.0.1 のポート 80 (上記で構成) にアクセスし、更新をクリックします。 HAProxy がリクエストをバックエンド サーバーに 1 つずつ送信していることがわかります (ラウンド ロビン アルゴリズムに従って)。
Ubuntu 18.04 に HAProxy Load をインストールするために必要なことはこれだけです。この簡単なヒントがお役に立てば幸いです。質問や提案がある場合は、下にコメントを残してください。