저번 포스팅의 pacemaker 설치에 이어서 프로세스 이중화를 위해 resource를 구성해보도록 하겠습니다.
resource 등록 방식에는 크게 ocf방식과 lsb 방식이 존재합니다.
ocf 방식은 pacemaker가 기본으로 제공하며 리소스 등록이 간편하다는 장점이 있고
lsb 방식은 ocf에서 제공되지 않은 리소스를 등록 가능하다는 장점이 있습니다.
mariadb, apacheHttpd, tomcat은 pacemaker에서 제공하는 ocf/heartbeat 프로바이더를 사용하고 nodeJS는 ocf로 등록이 안되어 있어 직접 스크립트를 작성하고 lsb 방식으로 구성하도록 하겠습니다.
먼저, 이중화를 위해 vip를 설정해야 합니다.
pacemaker에서 제공하는 ocf/heartbeat/IPaddr2 resource agent를 이용하도록 하겠습니다.
또한 프로세스 failover 시에 vip가 다른 서버로 넘어가게 하기 위해 group을 등록해줘야 합니다.
# pcs resource create cluster_ip ocf:heartbeat:IPaddr2 ip=<vip> cidr_netmask=24 op monitor interval=30s --group test-grp
리소스는 pcs status 명령어로 확인해볼 수 있습니다.
# pcs status
Cluster name: hacluster
Stack: corosync
Current DC: host2 (version 1.1.23-1.el7_9.1-9acf116022) - partition with quorum
Last updated: Mon Aug 21 17:34:29 2023
Last change: Fri Aug 18 17:20:45 2023 by hacluster via crmd on host1
2 nodes configured
1 resource instances configured
Online: [ host1 host2 ]
Full list of resources:
Resource Group: test-grp
cluster_ip (ocf::heartbeat:IPaddr2): Started host1
Daemon Status:
corosync: active/enabled
pacemaker: active/enabled
pcsd: active/enabled
vip가 정상적으로 적용되었는지는 ip addr 명령어로 확인합니다.
pcs resource create mariadb ocf:heartbeat:mysql binary="/usr/libexec/mysqld" config="/etc/my.cnf" datadir="/var/lib/mysql" pid="/var/run/mariadb/mariadb.pid" socket="/var/lib/mysql/mysql.sock" op start timeout=60s op stop timeout=60s op monitor interval=20s timeout=30s --group test-grp
pcs resource create apacheHttpd ocf:heartbeat:apache configfile=/etc/httpd/conf/httpd.conf statusurl="http://127.0.0.1:80/admin/config.php" op monitor interval=20 --group test-grp
pcs resource create tomcat ocf:heartbeat:tomcat java_home="/usr/local/jdk" catalina_home="/usr/local/tomcat" tomcat_user="root" op monitor interval="20s" --group test-grp
pcs status 명령어로 리소스가 잘 등록되었는지 확인합니다.
# pcs status
Cluster name: hacluster
Stack: corosync
Current DC: host2 (version 1.1.23-1.el7_9.1-9acf116022) - partition with quorum
Last updated: Tue Aug 22 13:59:10 2023
Last change: Fri Aug 18 17:20:45 2023 by hacluster via crmd on host1
2 nodes configured
4 resource instances configured
Online: [ host1 host2 ]
Full list of resources:
Resource Group: test-grp
cluster_ip (ocf::heartbeat:IPaddr2): Started host1
mariadb (ocf::heartbeat:mysql): Started host1
apacheHttpd (ocf::heartbeat:apache): Started host1
tomcat (ocf::heartbeat:tomcat): Started host1
Daemon Status:
corosync: active/enabled
pacemaker: active/enabled
pcsd: active/enabled
nodeJS 스크립트를 생성합니다.
저는 forever 모듈을 사용 중이어서 forever를 이용하여 기동/중지 하는 스크립트를 만들어보겠습니다.
아래는 스크립트 예시입니다.
#!/bin/sh
APP_HOME="/usr/local/nodejs"
APP_SCRIPT="$APP_HOME/test.js"
PORT=7777
start() {
if is_running; then
echo "Application is already running."
exit 1
fi
cd "$APP_HOME" || exit 1
forever start "$APP_SCRIPT"
echo "Application started."
while :
do
if is_running; then
exit 0
break
else
echo "waiting for node application boot"
sleep 1
fi
done
}
stop() {
local pid=$(get_pid)
if [ -z "$pid" ]; then
echo "Application is not running."
exit 0
fi
forever stop "$pid"
echo "Application stopped."
exit 0
}
status() {
local pid=$(get_pid)
if [ -n "$pid" ]; then
echo "Application is running with PID: $pid"
exit 0
else
echo "Application is not running."
exit 3
fi
}
is_running() {
local pid=$(get_pid)
[ -n "$pid" ]
}
get_pid() {
local pid=$(lsof -i :${PORT} -t)
echo "$pid"
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 1
start
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
스크립트를 생성하였으면 해당 스크립트로 리소스를 등록해줍니다.
# pcs resource create nodeJS_7777 lsb:/usr/local/bin/nodeJS_7777 op monitor interval=30s op start timeout=60s --group test-grp
같은 방식으로 다른 포트(7778)에 올라와 있는 nodeJS 스크립트를 생성하고, 리소스를 등록해주겠습니다.
마찬가지로 pcs status 명령어를 이용하여 확인할 수 있습니다.
# pcs status
Cluster name: hacluster
Stack: corosync
Current DC: host2 (version 1.1.23-1.el7_9.1-9acf116022) - partition with quorum
Last updated: Tue Aug 22 13:59:10 2023
Last change: Fri Aug 18 17:20:45 2023 by hacluster via crmd on host1
2 nodes configured
6 resource instances configured
Online: [ host1 host2 ]
Full list of resources:
Resource Group: test-grp
cluster_ip (ocf::heartbeat:IPaddr2): Started host1
mariadb (ocf::heartbeat:mysql): Started host1
apacheHttpd (ocf::heartbeat:apache): Started host1
tomcat (ocf::heartbeat:tomcat): Started host1
nodeJS_7777 (lsb:/usr/local/bin/nodeJS_7777): Started host1
nodeJS_7778 (lsb:/usr/local/bin/nodeJS_7778): Started host1
Daemon Status:
corosync: active/enabled
pacemaker: active/enabled
pcsd: active/enabled
이제 기본적인 리소스 등록은 끝났지만 마지막으로 하나만 더 생성하도록 하겠습니다.
지금 이 설정대로면 inbound 트래픽은 vip를 통하여 들어오지만 outbound 트래픽은 서버에서 직접 나가게 되므로 패킷의 ip가 서버ip로 나오게 될 것입니다.
outbound 트래픽도 vip를 통하여 나가도록 변경해보겠습니다.
pacemaker에서 제공하는 IPsrcaddr resource agent를 사용하겠습니다.
pcs resource create clusterOut_IP ocf:heartbeat:IPsrcaddr ipaddress="<vip>" op monitor OCF_CHECK_LEVEL="0" timeout="20s" interval="10s" --group test-grp
pacemaker는 resource의 순서가 중요하므로 vip(inbound) 아래로 위치를 이동시키겠습니다.
pcs resource group add test-grp clusterOut_IP --after cluster_ip
vip가 정상적으로 적용되었는지는 ip route 명령어로 확인합니다.
모든 리소스가 등록된 status는 다음과 같습니다.
# pcs status
Cluster name: hacluster
Stack: corosync
Current DC: host2 (version 1.1.23-1.el7_9.1-9acf116022) - partition with quorum
Last updated: Tue Aug 22 13:59:10 2023
Last change: Fri Aug 18 17:20:45 2023 by hacluster via crmd on host1
2 nodes configured
7 resource instances configured
Online: [ host1 host2 ]
Full list of resources:
Resource Group: test-grp
cluster_ip (ocf::heartbeat:IPaddr2): Started host1
clusterOut_IP (ocf::heartbeat:IPsrcaddr): Started host1
mariadb (ocf::heartbeat:mysql): Started host1
apacheHttpd (ocf::heartbeat:apache): Started host1
tomcat (ocf::heartbeat:tomcat): Started host1
nodeJS_7777 (lsb:/usr/local/bin/nodeJS_7777): Started host1
nodeJS_7778 (lsb:/usr/local/bin/nodeJS_7778): Started host1
Daemon Status:
corosync: active/enabled
pacemaker: active/enabled
pcsd: active/enabled
다음 포스팅에서는 실제로 failover 테스트를 진행해보도록 하겠습니다.