반응형

LVS 구성_CentOS 7

(pacemaker + ldirectord)

 

 

Pacemaker ?

Cluster 관리를 위해 근래 apache storm, openstack 등에 사용하는 분산 코디네이터

 

Corosync ?

클러스터 내의 노드 간 Discovery, 통신, 동기화 작업 등을 담당

 

1. 사전 준비

* 서버 3대 (LVS master, LVS stantby, web 서버)


* ip 할당
VIP : xxx.xxx.xxx.70
LVSM : xxx.xxx.xxx.78
LVSS : xxx.xxx.xxx.117
WEB1 : xxx.xxx.xxx.121


* 방화벽 허용(master / slave 동일)
호스트 간 통신에 필요한 포트
TCP : 2224, 3121, 21064
UDP : 5405


- TCP 2224 : 클러스터의 노드 간 통신 시 필요
- TCP 3121 : 클러스터에 Pacemaker 원격 노드가 있는 경우 모든 노드에 필요
- TCP 21064 : 클러스터에 DLM 이 필요한 리소스가 포함된 경우에 필요
- UDP 5404 : corosync 노드에 필요


Hostname 설정 (master / slave 동일)

xxx.xxx.xxx.78 lvsm
xxx.xxx.xxx.117 lvss
xxx.xxx.xxx.121 www.hwlee.shop

 

 Master 서버 설정

 

1. cluster package 설치 / pcs 데몬 시작 (master/slave 동일)

# yum install -y pacemaker corosync pcs psmisc policycoreutils-python

# systemctl enable pcsd
# systemctl enable corosync
# systemctl enable pacemaker
# systemctl start pcsd

 

2. 클러스터 계정 설정 (master / slave 동일)

#passwd hacluster

 

3. 각 노드에 대한 hacluster 사용자 인증(master)

 # pcs cluster auth lvsm lvss
Username: hacluster
Password:
lvss: Authorized
lvsm: Authorized

 

4. 클러스터 생성/실행 (master)

#pcs cluster setup --name auth_cluster lvsm lvss
(명령을 통해 corosync를 구성하고 다른 노드와 동기화)


#pcs cluster start –all
(클러스터 실행)

 

5. STONITH 비활성화 (master)

# pcs property set stonith-enabled=false
# pcs property set no-quorum-policy=ignore

- STONITH는 서비스가 중복으로 실행되어 충돌 나는 것을 방지하기 위해 오류가 발생한 호스트를 죽이는 Fencing 기능
- cluster를 실행하면 데이터의 무결성을 확보하기 위해 기본으로 STONITH가 활성화 되어 있음 (활성화 -> 비활성화)

- no-quorum-policy: 클러스터에 quorum이 없을 때의 수행 작업을 설정


※ quorum 이란 ?
분산 시스템에서 작업을 수행하기 위해 분산 트랜잭션이 획득해야 하는 최소 투표 수로 노드가 2개인 클러스터에는 불필요하기 때문에 no-quorum-policy=ignore 설정으로 무시하도록 설정

 

 

6. Active/Passive 클러스터 설정 - 가상IP 생성 (master)

#pcs resource create VirtualIP ocf:heartbeat:IPaddr2 ip=xxx.xxx.xxx.70 cidr_netmask=32 op monitor interval=30s


#pcs resource enable VirtualIP

 

7. ldirectord 설치(master / slave 동일)

# rpm -ivh https://ko.osdn.net/projects/linux-ha/downloads/71663/pacemaker-repo-1.1.21-1.1.el7.x86_64.rpm
# yum -y install ldirectord
# yum erase -y pacemaker-repo

 

8. ocf:heartbeat resource 추가(master / slave 동일)

 

9. sysctl.conf 설정

# vi /etc/sysctl.conf
--------------------------
net.ipv4.ip_forward = 1
--------------------------
패킷 포워딩

# sysctl -p

 

10. ldirectord 설정

#vi /etc/ha.d/ldirectord.cf
-------------------------------------------
checktimeout=10
checkinterval=10
autoreload=no
quiescent=no


# Web Server LVS
virtual = xxx.xxx.xxx.70:80
real=xxx.xxx.xxx.121:80 gate 10
service=http
request="lvs"
receive="auth"
scheduler=wlc
persistent=7200
protocol=tcp
checkport=80
checktype=negotiate
------------------------------------------

 

11. web 서비스 resource 추가(master)

# pcs resource create WebService ocf:heartbeat:ldirectord op monitor interval="20" timeout="10"

 

12. 리소스 통합(master)
- VIP 와 웹 리소스가 나누어져 있어 한쪽으로 통합

# pcs constraint colocation add WebService with VirtualIP INFINITY

 

13. master 서버와 web 서버가 같은 네트워크 대역 대가 아니므로 라우팅 설정 추가(master)

# route add -net xxx.xxx.xxx.112 netmask 255.255.255.240 dev eth0

 

14. 확인








 # ipvsadm -Lnc


 

 

 

Slave 서버 설정
1. cluster package 설치 / pcs 데몬 시작 (master/slave 동일)

2. 클러스터 계정 설정 (master / slave 동일)
3. ldirectord 설치(master / slave 동일)
4. ocf:heartbeat resource 추가(master / slave 동일)
5. sysctl.conf 설정(master / slave 동일)
6. ldirectord 설정(master / slave 동일)

 

7. failover 테스트

# pcs cluster stop lvsm
(lvsm 노드 stop)


- lvss 로 넘어옴



#pcs cluster start lvsm

- lvsm 노드 start 하여도 lvsm 로 넘어오지 않음(failback)



※ auto_failback off
실 서버 lvsm 서버에 문제가 생겨 리소스가 강제로 lvss 로 넘어간 경우, lvsm 서버 상태가 완벽하게 확인이 되지 않은 상태로 다시 넘어오게 하지 않기 위해 failback 기능을 사용하지 않는다.

 

※ constraint ?

Pacemaker 소스를 선호하는 위치로 설정이 가능


#pcs constraint location WebService prefers lvsm=50
(lvsm 에 우선순위 50지정)


1) 설정 값 확인
# crm_simulate -sL



# pcs constraint show



#pcs cluster stop lvsm

- lvs M stop  lvsm → lvss 로 넘어감



# pcs cluster start lvsm

- lvs M start lvss → lvsm 으로 넘어옴

8. 확인
- 웹 에서 vip 접근 시




# ipvsadm -Lnc


 

 

Web 서버 설정

1. sysctl 설정

# vi /etc/sysctl.conf
--------------------------------------------------
- 패킷 포워딩
net.ipv4.ip_forward = 1


- 실 서버에 ARP 문제 해결을 위해 ARP에 대한 응답 없음 설정 추가
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2


net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2


net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2


net.ipv4.conf.eth0.arp_ignore = 1
net.ipv4.conf.eth0.arp_announce = 2
-----------------------------------------------------------------------------


# sysctl -p
(설정 적용)

 

2. 리얼 서버(web)는 가상 IP(VIP)를 로컬 주소로 인식하도록 lo:0 설정

# ifconfig lo:0 xxx.xxx.xxx.70 netmask 255.255.255.255 up

 

3. vip 확인

 

 

반응형
반응형

LVS(Linux Virtual Server) ?

- 리눅스 커널 기반 운영 체제를 위한 부하 분산 소프트웨어
- 서버 접근하는 이용자가 많을 때 서비스를 부하 분산하기 위해 이용
- 여러 서버에서 서비스를 제공하여 로드 밸런싱 가능

 

 

1. 사전 준비

* 서버 3(LVS master, LVS stantby, web 서버)


* ip 할당
VIP : xxx.xxx.xxx.70
LVSM : xxx.xxx.xxx.78
LVSS : xxx.xxx.xxx.xxx117
WEB1 : xxx.xxx.xxx.xxx.121

 

 

2. LVS master 구성

1) hostname 설정

# vi /etc/hosts(LVS master, slave, web 동일)
-----------------------------------------------
xxx.xxx.xxx.78 lvsm.hwlee.hsop
xxx.xxx.xxx.117 lvss.hwlee.shop
xxx.xxx.xxx.121 www.hwlee.shop
-----------------------------------------------

# hostname
------------------------
lvsm.hwlee.shop
------------------------

# vi /etc/sysconfig/network
-------------------------------------------
NETWORKING=yes
HOSTNAME=lvsm.hwlee.shop
GATEWAYDEV=eth0
-------------------------------------------

2) sysctl 설정(패킷 포워딩)
# vi /etc/sysctl.conf
------------------------------------------------
net.ipv4.ip_forward = 1
net.ipv4.vs.secure_tcp=3
net.ipv4.vs.drop_packet=0
net.ipv4.vs.drop_entry=0
net.ipv4.vs.am_droprate=10
net.ipv4.vs.amemthresh=1024
------------------------------------------------

- > 설정 적용
-------------------
# sysctl -p
-------------------



3)  heartbeat + ldirectord 설치(LVS master / slave 동일)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# wget https://archives.fedoraproject.org/pub/archive/epel/6/x86_64/
# wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
# rpm -Uvh remi-release-6*.rpm epel-release-6*.rpm


# yum -y install heartbeat
# yum -y install heartbeat-*
# yum -y install ipvsadm

# yum -y install perl-IO-Socket-INET6
# yum -y install perl-Email-Send
# yum -y install perl-MailTools
# yum -y install perl-Net-SSLeay
# yum -y install perl-libwww-perl



# wget ftp://ftp.pbone.net/mirror/ftp5.gwdg.de/pub/opensuse/repositories/network:/ha-clustering:/Stable/CentOS_CentOS-6/x86_64/ldirectord-3.9.5-3.1.x86_64.rpm
# rpm -ivh ldirectord-3.9.5-3.1.x86_64.rpm
------------------------------------------------------------------------------------------------------------------------------------------------------------------------


4) ha.cf 설정(Master 상태 체크)
# vi /etc/ha.d/ha.cf
----------------------------------------------------------------------------------------------------------------------
debugfile /var/log/ha-debug.log
logfile /var/log/ha.log
keepalive 2
deadtime 10
udpport 694


node lvsm.hwlee.shop
node lvss.hwlee.shop


auto_failback on


ucast eth0 xxx.xxx.xxx.117
---------------------------------------------------------------------------------------------------------------------

- debugfile : 디버깅 메시지가 기록될 파일 위치 지정
- logfile : 로그 파일 위치 지정
- keepalive : 두 노드 간(master/salve) 얼마나 자주 heartbeat를 주고 받을 것인가 설정
- deadtime : slave 서버가 죽었다고 판단하는 시간
- udpport : UDP heartbeat 패킷을 보낼 포트
- node lvsm.hwlee.shop : 시스템의 한 장치를 의미, Primary 노드
- node lvss.hwlee.shop : Secondary 노드


- auto_failback : 자동 복구 여부 설정
- ucast eth0 xxx.xxx.xxx.117 : Heartbeat를 보낼 인터페이스



5) auto_failback 테스트
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ex ) master(on) / slave(off)
=> master heartbeat stop slave 로 넘어감
=> master heartbeat start master 로 넘어옴


ex ) master(off) / slave(off)
=> master heartbeat stop slave 로 넘어감
=> master heartbeat start master 로 넘어오지 않고 그대로 유지


ex ) master(off) /slave(on)
=> master heartbeat stop slave 로 넘어감

=> master heartbeat start master 로 넘어오지 않고 그대로 유지


ex ) master(on) / slave(on)
=> master heartbeat stop slave 로 넘어감
=> master heartbeat start master 로 넘어옴

결론
- master 서버 stop 으로 slave 에 넘어간 상태에서 master 서버의 auto_failback on일 경우 Primary(master)가 다시 살아나면 자동으로 VIPPrimary(master)로 넘어간다.
(현재 Primary 서버의 설정 값을 따라간다.)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------



6)  VIP 설정 (줄바꿈 뒤에 공백 없어야 함)
# vi /etc/ha.d/haresources 설정(LVS master, slave 설정 동일)
-----------------------------------------------------------------------------------------------------
lvsm.hwlee.shop \
ldirectord::ldirectord.cf \
Ipaddr2::xxx.xxx.xxx.70/32/eth0 => eth0으로 설정

Ipaddr2::xxx.xxx.xxx.70/32/eth0:0 => eth0:0으로 설정
-----------------------------------------------------------------------------------------------------
- 노드간에 공유할 자원 및 스크립트 지정


7)  LVS 인증 파일 설정
# vi /etc/ha.d/authkeys
-----------------------
auth 2
2 sha1 lvs
-----------------------
- 노드간의 인증 방법을 지정
보안 강화순서(crc < md5 < sha1)


8) root 만 읽고 쓸 수 있도록 변경
# chmod 600 /etc/ha.d/authkeys




9) ldirectord 설정 (LVS master / slave 설정 동일)
# vi /etc/ha.d/conf/ldirectord.cf
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
checktimeout=10
checkinterval=10
autoreload=no
logfile="/var/log/ldirectord.log"
quiescent=no


# Web Server LVS
virtual=xxx.xxx.xxx.70:80
real=xxx.xxx.xxx.121:80 gate 10
service=http
request="lvsm.hwlee.shop"
receive="lvss.hwlee.shop"
scheduler=rr
persistent=3600
protocol=tcp
checktype=nogotiate
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- checktimeout : 정해진 시간 동안 통신이 안되면 노드 가중치를 0으로 설정

- checkinterval : 테스트 페이지 요청 주기
- autoreload : 다시 통신이 될 경우 노드 가중치 설정
- logfile : 로그 파일 경로 설정
- quiescent (yes/no)
  • yes : weight 값만 0으로 만들어 분리함
  • no : lvs 테이블에서 다운 된 웹 서버의 ip를 삭제하여 라우팅 제거
- virtual : 가상 서비스 네트워크
- real : 리얼 서버 ip 및 포트, 포워드 방식
  • gate = Direct Routing (상대의 ip 주소를 변경하지 않고 MAC 주소만 리얼서버의 주소로 변환해서 전송)
  • masq = Network address translation (상대의 ip주소를 리얼서버 주소로 변환해서 전송)
  • ipip = IP Tunneling (수신한 패킷을 리얼서버의 ip 헤더를 캡슐화해서 전송)
- service : 서비스 프로토콜 (ftp, smtp, dns, http, pop, imap, mysql )
- request : 서버가 정상 작동 하는지 확인 요청할 페이지 이름
- receive : 요청 페이지에서 찾을 문자열

- scheduler : 스케줄링 방식
  • rr = Round-Robin (모든 클라이언트는 동일하게 취급되며 실 서버 혹은 경로는 같은 처리량을 보유해야 한다)
  • wrr = Weighted Round-Robin (서로 다른 처리 능력을 가진 서버가 있을 경우 사용한다. 가중치를 주어 많이 처리할 수 있는 서버로 많은 트래픽을 할당한다)
  • lc = Least-Connection (연결 수가 가장 적은 서버에 네트워크 연결 방향을 정한다.)
  • wlc = Weighted Least-Connection (서버의 처리 능력을 고려하여 가중치를 부여하면서 least-connection scheduling을 적용하여 트래픽을 고르게 분산시키는 방법이다)


※ Weighted(가중치) ?
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- 각 서버에 부여되는 중요도나 수치를 말함
ex) 서버 A가 가중치 1로 되어 있고, 서버 B가 가중치 3으로 되어 있을 경우 가중치 1로 된 서버가 1번 연결될 때 마다 가중치 3로 되어 있는 서버는 3번 연결


* 가중치 설정 방법(/etc/ha.d/conf/ldirectord.cf)
=> real=xxx.xxx.xxx.121:80 gate 10


-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

- persistent : 지속적으로 클라이언트 연결을 위한 시간 ()
- protocol : 프로토콜 종류 (tcp, udp, fwm)
  • tcp / udp = 사용할 프로토콜 종류, ip 주소 및 포트로 지정된 경우에 설정
  • fwm = 방화벽 표시인 경우 설정


- checktype : 연결 확인 방법 (기본 값 negoitate)
  • connect = 오직 TCP/IP 로만 연결 확인
  • negotiate = 요청을 보내고 수신 문자열을 일치 시킴
  • on = 점검 on, 실제 서버가 항상 활성화
  • off = 점검 off , 예비 서버가 활성화 되지 않음을 의미
  • ping = ICMP ping이 실제 서버의 가용성을 테스트하는데 사용


※ FWM 설정 방법
# vi ldirectord.cf
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
virtual=1
real=xxx.xxx.xxx.121 gate 1
real=xxx.xxx.xxx.117 gate 3
service=http
request="lvsm.hwlee.shop"
receive="lvss.hwlee.shop"
scheduler=rr
persistent=3600
protocol=fwm
checkport=80
checktype=negotiate
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
- virtualVIP:PORT 대신 mark 숫자를 표시
- checkport = 체크할 포트 설정 (서비스 포트와 다를 수 있음)


- 특정 패킷에 대한 태그를 표시하도록 LVS 에서 iptables 설정
# iptables -t mangle -A PREROUTING -p tcp -s 0.0.0.0/0 -d 121.254.175.70/32 --dport 80 -j MARK --set-mark 1

=> 전체 ip 대역 대에서 xxx.xxx.xxx.70/32 ip 의 목적지 포트 80으로 가는 패킷을 MARK 1로 라우팅 설정.

Mangle : packet 내부의 TOS, TTL, MARKpacket 정보 변경을 위한 rule과 해당 action(target)의 리스트가 저장되어 있는 iptables 종류 중 하나 이다.
(mangle 테이블은 통과하는 패킷의 헤더 정보를 변경하고자 할때 사용)



10) ldirectord 적용
# /etc/ha.d/resource.d/ldirectord /etc/ha.d/conf/ldirectord.cf reload




11) 방화벽 허용
- LVS master / slave / web 서버 허용


3. LVS slave 구성

1) hostname 설정
# vi /etc/hosts(LVS master, slave, web 동일)

# hostname
----------------------
lvss.hwlee.shop
----------------------


# vi /etc/sysconfig/network
----------------------------------------------
NETWORKING=yes
HOSTNAME=lvss.hwlee.shop
----------------------------------------------


2) sysctl 설정(LVS master, slave 동일)

3)  heartbeat + ldirectord 설치(LVS master / slave 동일)


4) ha.cf 설정(Master 상태 체크)
# vi /etc/ha.d/ha.cf
-----------------------------------------------------------------------------------------
LVS master, slave 설정 동일

ucast eth0 xxx.xxx.xxx.117 → ucast eth0 xxx.xxx.xxx.78
설정 변경
-----------------------------------------------------------------------------------------


5) 리소스 설정
# /etc/ha.d/haresources 설정(LVS master, slave 설정 동일)



6) LVS 인증 파일 설정(/etc/ha.d/authkeys)
(LVS master, slave 설정 동일)


7) ldirectord 설정 (LVS master / slave 설정 동일)


8) 방화벽 허용 (LVS master /salve 설정 동일)
- LVS master / slave / web 서버 허용


* 추가 사항
- LVS master(xxx.xxx.xxx.78) 서버는 vip 와 다른 네트워크에 있으므로 routing 설정
# route add -net xxx.xxx.xxx.64 netmask 255.255.255.240 dev eth0



 

 

4. WEB1 서버 구성

1) hostname 설정
# /etc/hosts(LVS master, slave, web 동일)


# hostname

-----------------------
www.hwlee.shop
-----------------------


# vi /etc/sysconfig/network
-----------------------------------------------------------------
NETWORKING=yes
HOSTNAME=www.hwlee.shop
-----------------------------------------------------------------


2) sysctl 설정

# vi /etc/sysctl.conf
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
- 패킷 포워딩
net.ipv4.ip_forward = 1


- 실서버에 ARP 문제 해결을 위해 ARP에 대한 응답 없음 설정 추가
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2


net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2


net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2


net.ipv4.conf.eth0.arp_ignore = 1
net.ipv4.conf.eth0.arp_announce = 2
------------------------------------------------------------------------------------------------------------------------------------------------------------------------

# sysctl -p (설정 적용)



3) 리얼 서버(web)는 가상 IP(VIP)를 로컬 주소로 인식하도록 lo:0 설정
-------------------------------------------------------------------------------------------------------------------------
# ifconfig lo:0 xxx.xxx.xxx.70 netmask 255.255.255.255 up
-------------------------------------------------------------------------------------------------------------------------

 

 

5. LVS 확인

Master 확인 방법(VIP master 할당)

# vi lvs.sh
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
watch -n1 -t "echo ; ip addr ; echo ; ipvsadm -Ln ; ipvsadm -Ln --rate ; ipvsadm -Lnc"
------------------------------------------------------------------------------------------------------------------------------------------------------------------------

# sh lvs.sh(TCP)




# sh lvs.sh(FWM)




웹 페이지 접속(vip로 접속)

 

 

 

6. Master -> Slave 전환 테스트

Master 서버에서 heartbeat 종료
# /etc/init.d/heartbeat stop


- sh lvs.sh 실행 시 VIP 제외된 부분 확인



- Slave 서버에 VIP 올라온 부분 확인

 

 

반응형

+ Recent posts