CentOS7でucarpを使いHA環境構築 (corosync2+pacemakerより簡単)

# 参考

* [公式サイト](http://www.pureftpd.org/project/ucarp)
* [README](http://download.pureftpd.org/pub/ucarp/README)
* [UCARP on arch linux | Ramblings of a mad geek](http://www.cageorge.com/2014/02/ucarp-on-arch-linux/)
* [[NetBSD]CARPによるサーバ冗長化 - La vie des marmotte](http://d.hatena.ne.jp/marmotte/20091020)
* carpについては[Common Address Redundancy Protocol - Wikipedia](http://ja.wikipedia.org/wiki/Common_Address_Redundancy_Protocol)

## 参考その2

* [第3の冗長化プロトコル、CARPを試す : 負け組アーキテクトの憂鬱](http://orz.makegumi.jp/archives/1008858.html)
* [Linux-HA Japan](http://linux-ha.osdn.jp/wp/dl) Pacemakerリポジトリパッケージ (RHEL7) (1.1.13-1.1)


# ソースからインストールする場合

## ビルド

```bash:インストール
yum install -y libpcap-devel gcc
cd /opt
wget http://download.pureftpd.org/pub/ucarp/ucarp-1.5.2.tar.gz
tar xzf ucarp-1.5.2.tar.gz
cd ucarp-1.5.2
./configure && make && make install
mkdir /etc/ucarp/
```

## スクリプト作成

```bash:/etc/ucarp/ucarp.sh
#!/bin/bash

UCARP=/usr/local/sbin/ucarp
UP=/etc/ucarp/up.sh
DOWN=/etc/ucarp/down.sh
DEV=enp0s3

# このサーバの実IP
SIP=192.168.100.11
#Virtual host ID number, every ucarp instance should have its own.
VHID=5
# 仮想IP
VIP=192.168.100.10
#The ucarp password shared only with the matching ucarp instance on another host
PASSWD="ucarp_pass1"
#any other flags you want to pass (refer to the man page for desired operation)
FLAGS="--shutdown --preempt --advskew=1"

#exec ucarp
exec ${UCARP} -i ${DEV} -s ${SIP} -v ${VHID} -p ${PASSWD} -a ${VIP} -u ${UP} -d ${DOWN} ${FLAGS}
```

```bash:/etc/ucarp/up.sh
#!/bin/bash

#the device and IP are passed by the ucarp daemon as $1 and $2 respectively
DEV="${1}"
IP="${2}"

/sbin/ip address add ${IP}/24 dev ${DEV}
/sbin/ip neigh flush dev ${DEV}

#start any service you need listening on your virtual IP
/bin/systemctl restart mysqld
```

```bash:/etc/ucarp/down.sh
#!/bin/bash

DEV="${1}"
IP="${2}"

/sbin/ip addr del ${IP}/24 dev ${DEV}

#optionally stop services here
```

```bash:/usr/lib/systemd/system/ucarp.service
Description=UCARP daemon
After=network.target

[Service]
ExecStart=/etc/ucarp/ucarp.sh
ExecStop=/bin/kill -TERM $MAINPID
KillMode=process
Restart=always

[Install]
WantedBy=multi-user.target
```

```bash:権限修正
chown root:root /etc/ucarp/*.sh
chmod 0700 /etc/ucarp/*.sh
```

## サービス登録

```bash:サービス登録
systemctl enable ucarp
systemctl start ucarp
```



# パッケージからインストールする場合

```bash:
yum install -y ucarp
```

```bash:
# rpm -qal ucarp
/etc/ucarp
/etc/ucarp/vip-001.conf.example
/etc/ucarp/vip-common.conf
/usr/lib/systemd/system/ucarp@.service
/usr/libexec/ucarp
/usr/libexec/ucarp/ucarp
/usr/libexec/ucarp/vip-down
/usr/libexec/ucarp/vip-up
/usr/sbin/ucarp
/usr/share/doc/ucarp-1.5.2
/usr/share/doc/ucarp-1.5.2/AUTHORS
/usr/share/doc/ucarp-1.5.2/COPYING
/usr/share/doc/ucarp-1.5.2/ChangeLog
/usr/share/doc/ucarp-1.5.2/NEWS
/usr/share/doc/ucarp-1.5.2/README
/usr/share/locale/en@boldquot/LC_MESSAGES/ucarp.mo
/usr/share/locale/en@quot/LC_MESSAGES/ucarp.mo
```

## 設定

```bash:/etc/ucarp/vip-up
#!/bin/sh
exec 2>/dev/null

/sbin/ip address add "$2"/24 dev "$1"

echo "up" `date "+%c"` \
 | mail -s `hostname`" vip001 up" root@localhost
```

```bash:/etc/ucarp/vip-down
#!/bin/sh
exec 2>/dev/null

/sbin/ip address del "$2"/24 dev "$1"

echo "down" `date "+%c"` \
 | mail -s `hostname`" vip001 down" root@localhost
```

```bash:/etc/ucarp/vip-common.conf
PASSWORD="hogehoge"
BIND_INTERFACE="eth1"
SOURCE_ADDRESS="192.168.100.11"
OPTIONS="--shutdown --preempt"
```

```bash:/etc/ucarp/vip-001.conf
UPSCRIPT=/etc/ucarp/vip-up
DOWNSCRIPT=/etc/ucarp/vip-down
VIP_ADDRESS="192.168.100.10"
```

## サービス登録

```bash:サービス登録
systemctl enable ucarp@vip-001
systemctl start ucarp@vip-001
```

参考: [Full Text Bug Listing](https://bugzilla.redhat.com/show_bug.cgi?format=multiple&id=970843)