[技術ブログvol.35] HAProxy 1.6 の新機能を試す

HAProxy1.6リリース!

つい先日(2015/10/14)にHAProxy1.6がリリースされました。
その際に新機能がいくつか追加されました。
その中にDNSの名前解決について新しいリリースがありましたので今回はこれを試してみます。

HAProxyの名前解決

HAProxyは、これまでのバージョンではプロセスの起動時に名前解決を行い、その時に得たIPアドレスをキャッシュし続けるという仕様でした。

そのため振り分け先のサーバをIPアドレスではなくドメイン名で書いた場合、そのドメイン名のAレコードが変更されても、HAProxyは最初のキャッシュを保持し続ける為、Aレコードを変化した場合は都度HAProxyをリロードもしくは再起動する必要がありました。

今回のアップデートで仕様が変更され、リゾルバをコンフィグ内で指定した場合は、HAProxyを再起動することなく動的に名前解決ができるようになりました。ただしリゾルバをコンフィグ内で設定していない場合は、今までのバージョンと同じ挙動になりますので注意が必要です。

HAProxyのインストール

今回はCentOS6.7でrpmを作成しインストールを行います。

事前準備
必要なパッケージの導入
yum install -y rpm-build pecl-devel openssl-devel
ビルド用のディレクトリの作成
mkdir -p ~/rpmbuild/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}
ソースファイルの取得
wget http://www.haproxy.org/download/1.6/src/haproxy-1.6.0.tar.gz
tar ballの展開
tar zxf haproxy-1.6.0.tar.gz
ビルド前の準備
cd haproxy-1.6.0/examples
rpmに仕込むファイルがなぜかないのでとりあえずからファイルを作っとく
touch haproxy.cfg
起動スクリプトの一部に"{"が欠けていてスクリプトがこけるので事前に修正
--- haproxy.init        2015-10-17 02:27:09.469998329 +0900
+++ /etc/init.d/haproxy 2015-10-17 01:04:26.000000000 +0900
@@ -94,7 +94,7 @@
$BIN -c -q -V -f $CFG
}

-quiet_check()
+quiet_check() {
$BIN -c -q -f $CFG
}
specのファイルをコピー
cp -p haproxy.spec ~/rpmbuild/SPECS/
コピーしたspecファイルの編集
--- haproxy.spec        2015-10-14 01:52:22.000000000 +0900
+++ /root/rpmbuild/SPECS/haproxy.spec 2015-10-17 02:29:34.570997467 +0900
@@ -33,7 +33,7 @@
%define __perl_requires /bin/true

%build
-%{__make} USE_PCRE=1 DEBUG="" ARCH=%{_target_cpu} TARGET=linux26
+%{__make} USE_PCRE=1 DEBUG="" ARCH=%{_target_cpu} TARGET=linux26 USE_OPENSSL=1

%install
[ "%{buildroot}" != "/" ] && %{__rm} -rf %{buildroot}
ビルド用のディレクトリにtarballを作成
cd ../..

tar zcf ~/rpmbuild/SOURCES/haproxy-1.6.0.tar.gz haproxy-1.6.0
ビルド
cd ~/rpmbuild/SPECS/
rpmbuild -ba haproxy.spec
インストール
cd ~/rpmbuild/RPMS/x86_64/
rpm -Uvh haproxy-1.6.0-1.x86_64.rpm
コンフィグの設定

コンフィグは/etc/haproxy/haproxy.cfgにあります。

global
## ログの設定syslog経由で出力されるのでその設定
log 127.0.0.1 local2 debug

## pidファイルの出力先を設定。デフォルトのまま
pidfile /var/run/haproxy.pid

## 1プロセスあたりの接続数。デフォルト4000
maxconn 4000

## 起動するユーザ
user haproxy

## 起動してくるグループ
group haproxy

## デーモンとしてバックグラウンドで起動
daemon

## 統計情報を見るためのSocket設定。デフォルトのまま
stats socket /var/lib/haproxy/stats

## リゾルバの設定
resolvers awsvpc

## AWSが用意しているVPCのリゾルバに見に行くよう設定
nameserver vpc 172.31.0.2:53

## プロキシ動作のデフォルト設定
defaults

## L4モードで動作
mode tcp

## ログ出力の動作
log global

## リトライ回数
retries 3

## 接続タイムアウトの時間
timeout connect 10s

## 各タイムアウトの設定
timeout client 1m
timeout server 1m
timeout check 10s

### 1プロセスあたりの接続数
maxconn 3000


## readReplica用の設定
listen mysql

## bindするIPアドレス、ポートを指定
bind 127.0.0.1:3306

## L4モードで動作
mode tcp

## mysql-checkオプションを利用してチェック用ユーザにhaproxyを指定
option mysql-check user haproxy

## ラウンドロビンモードで振り分け
balance roundrobin

## 振り分け先のサーバの指定
## check:ヘルスチェックの有効化
## port: ヘルスチェックするポート
## resolvers: ヘルスチェック時に使うリゾルバの設定
## inter:ヘルスチェックの間隔
## fall:して回数ヘルスチェックに失敗したら切り離し
## backup: バックアップ以外のサーバが全部死んだときに振り分けられる
server read1 {rdsendpoint} check port 3306 resolvers awsvpc inter 10000 fall 2
server read2 {rdsendpoint} check port 3306 resolvers awsvpc inter 10000 fall 2
server master {rdsendpoint} check port 3306 resolvers awsvpc backup

キモは

## リゾルバの設定
resolvers awsvpc

## AWSが用意しているVPCのリゾルバに見に行くよう設定
nameserver vpc 172.31.0.2:53

    ## 振り分け先のサーバの指定
## check:ヘルスチェックの有効化
## port: ヘルスチェックするポート
## resolvers: ヘルスチェック時に使うリゾルバの設定
## inter:ヘルスチェックの間隔
## fall:して回数ヘルスチェックに失敗したら切り離し
## backup: バックアップ以外のサーバが全部死んだときに振り分けられる
server read1 {rdsendpoint} check port 3306 resolvers awsvpc inter 10000 fall 2
server read2 {rdsendpoint} check port 3306 resolvers awsvpc inter 10000 fall 2
server master {rdsendpoint} check port 3306 resolvers awsvpc backup

です。

HAProxyのステータスなどは以下のようなコマンドで確認できます。
echo "show stat" | socat stdio /var/lib/haproxy/stats
ほかにも以下のようなコマンドが用意されています
  clear counters : clear max statistics counters (add 'all' for all counters)
clear table : remove an entry from a table
help : this message
prompt : toggle interactive mode with prompt
quit : disconnect
show backend : list backends in the current running config
show info : report information about the running process
show pools : report information about the memory pools usage
show stat : report counters for each proxy and server
show errors : report last request and response errors for each proxy
show sess [id] : report the list of current sessions or dump this session
show table [id]: report table usage stats or dump this table's contents
show servers state [id]: dump volatile server information (for backend <id>)
get weight : report a server's current weight
set weight : change a server's weight
set server : change a server's state, weight or address
set table [id] : update or create a table entry's data
set timeout : change a timeout setting
set maxconn : change a maxconn setting
set rate-limit : change a rate limiting value
disable : put a server or frontend in maintenance mode
enable : re-enable a server or frontend which is in maintenance mode
shutdown : kill a session or a frontend (eg:to release listening ports)
show acl [id] : report avalaible acls or dump an acl's contents
get acl : reports the patterns matching a sample for an ACL
add acl : add acl entry
del acl : delete acl entry
clear acl <id> : clear the content of this acl
show map [id] : report avalaible maps or dump a map's contents
get map : reports the keys and values matching a sample for a map
set map : modify map entry
add map : add map entry
del map : delete map entry
clear map <id> : clear the content of this map
set ssl <stmt> : set statement for ssl

動作チェックは、RDSのマスターを主導フェイルオーバーさせつつ、watchコマンドで状態をみていると変化がよくわかります。

watch "echo "show stat" | socat stdio /var/lib/haproxy/stats"

これでエンドポイントアクセスしかできないサービスでも安心して使えますね。

技術ブログ中の人
自前で再起動の仕組みを作る必要がなくなったので
さらに便利になりました。

返信を残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA