7
6

More than 3 years have passed since last update.

FreeBSDでロードバランサ(DSR)を構築

Last updated at Posted at 2019-12-12

Linux(LVS+Keepalived)で構築できる下記のようなロードバランサ(DSR)をFreeBSDで構築してみます。
http://klock-3rd.hatenablog.com/entry/2016/01/24/165848
ネタ元は下記のURLです。
http://www.undeadly.org/cgi?action=article&sid=20080617010016

下記の環境で構築します。OSは全部FreeBSD12.1です。

サーバ IPアドレス
仮想IPアドレス 192.168.201.131
Web用サーバ1 192.168.201.132
Web用サーバ2 192.168.201.133
ロードバランサ用サーバ 192.168.201.134

ロードバランサ用サーバでは relayd + pf を、Webサーバでは何でもいいんですが nginx を使います。
仮想IPアドレスはロードバランサ用サーバのIPアドレスとは別に用意します。

Web用サーバの構築

nginxをインストール&起動して、ヘルスチェック用のHTMLを置きます。

# pkg install -y nginx
# /usr/local/etc/rc.d/nginx onestart
# echo `hostname` > /usr/local/www/nginx/hc.html

ループバックインターフェースの設定

# ifconfig lo0 alias 192.168.201.131/32

# 恒久的に設定する場合は下記のコマンドを実行
# sysrc ifconfig_lo0_alias0="inet 192.168.201.131/32"

ロードバランサー用サーバの構築

仮想IPアドレスの設定

# ifconfig em0 alias 192.168.201.131/32

pfの設定を行います。/etc/pf.confを下記の内容で作成します

rdr-anchor "relayd/*"
anchor "relayd/*"

pfの設定と起動を行います

# sysrc pf_rules="/etc/pf.conf"
# sysrc pf_enable="YES"
# /etc/rc.d/pf start

relaydのインストールをします

# pkg install -y relayd

/usr/local/etc/relayd.confを下記の内容で作成します

# 仮想IPアドレス
vipaddress="192.168.201.131"
# Web用サーバのIPアドレス
webhost1="192.168.201.132"
webhost2="192.168.201.133"
# 3秒に一度ヘルスチェックを行う
interval 3
table <webhosts> { $webhost1 $webhost2 }
redirect "nginx" {
        listen on $vipaddress port 80 interface em0
        route to <webhosts> port 80 check http "/hc.html" code 200 interface em0
}

relaydを起動

# /usr/local/etc/rc.d/relayd onestart

relayctlコマンドでWeb用サーバの状態を確認

# relayctl show summary
Id      Type        Name                        Avlblty Status
1       redirect    nginx                               active
1       table       webhosts:80                         active (2 hosts)
1       host        192.168.201.132             100.00% up
2       host        192.168.201.133             100.00% up

# relayctl show hosts
Id      Type        Name                        Avlblty Status
1       table       webhosts:80                         active (2 hosts)
1       host        192.168.201.132             100.00% up
                total: 1195/1195 checks
2       host        192.168.201.133             100.00% up
                total: 1195/1195 checks

Web用サーバの /var/log/nginx/access.log を見てみると、
relaydが3秒に1回アクセスしていることが確認できます

192.168.201.134 - - [03/Dec/2019:14:45:56 +0900] "HEAD /hc.html HTTP/1.0" 200 0 "-" "-"
192.168.201.134 - - [03/Dec/2019:14:45:59 +0900] "HEAD /hc.html HTTP/1.0" 200 0 "-" "-"
192.168.201.134 - - [03/Dec/2019:14:46:02 +0900] "HEAD /hc.html HTTP/1.0" 200 0 "-" "-"
192.168.201.134 - - [03/Dec/2019:14:46:05 +0900] "HEAD /hc.html HTTP/1.0" 200 0 "-" "-"
192.168.201.134 - - [03/Dec/2019:14:46:08 +0900] "HEAD /hc.html HTTP/1.0" 200 0 "-" "-"

動作確認

適当なホストから

$ curl 'http://192.168.201.131/hc.html'
web1
$ curl 'http://192.168.201.131/hc.html'
web2

ロードバランスはされてるっぽい

7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6