반응형

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