ipvsadm 으로 udp proxy, load balance 구현

Load Balancer Server 설정

설치

1
yum install ipvsadm

/etc/sysctl.conf 에 아래 입력

1
net.ipv4.ip_forward=1

적용

1
2
3
4
5
6
7
8
9
10
sysctl -p		#적용
touch /etc/sysconfig/ipvsadm # 얘가 뭐하는애지 a모르것지만 해야지 켜진다.


chkconfig ipvsadm on
chkconfig --level 345 ipvsadm on

systemctl start ipvsadm //시작
systemctl enable ipvsadm.service //상시가동
systemctl status ipvsadm.service

가상아이피 등록

1
2
3
4
ifconfig eth0:0 192.168.100.102 up

ipvsadm -A -u 192.168.100.102:9131 # A:add virtual service with options u:udp-service
ipvsadm -a -u 192.168.100.102:9131 -r 192.168.100.198:9131 -m # r:real-server m:masquerading

-m 대신 -g는 NAT 방식이냐, Direct Routing 방식이냐에 따른 차이
Direct Routing의 경우 수신한 서버에서 User에게 response를 직접 전달한다.
NAT 방식은 NAT 서버를 통해 User에게 response를 한다.

Real Server(받는쪽) 설정

/etc/sysctl.conf에 아래 추가

1
2
3
4
5
6
net.ipv4.ip_forward = 0

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

커널파라미터 적용

1
$ sysctl -p

가상아이피 등록

1
$ ifconfig eth0:0 192.168.100.102 up	# direct 방식일 경우에만 사용됨. 이부분 잊지말자 !!

참고

실시간 분배 확인하기

1
watch ipvsadm -ln

서비스 초기화

1
ipvsadm -C

서비스 상태조회

1
ipvsadm -L --stats
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
ipvsadm v1.27 2008/5/15 (compiled with popt and IPVS v1.2.1)
Usage:
ipvsadm -A|E -t|u|f service-address [-s scheduler] [-p [timeout]] [-M netmask] [--pe persistence_engine] [-b sched-flags]
ipvsadm -D -t|u|f service-address
ipvsadm -C
ipvsadm -R
ipvsadm -S [-n]
ipvsadm -a|e -t|u|f service-address -r server-address [options]
ipvsadm -d -t|u|f service-address -r server-address
ipvsadm -L|l [options]
ipvsadm -Z [-t|u|f service-address]
ipvsadm --set tcp tcpfin udp
ipvsadm --start-daemon state [--mcast-interface interface] [--syncid sid]
ipvsadm --stop-daemon state
ipvsadm -h

Commands:
Either long or short options are allowed.
--add-service -A add virtual service with options
--edit-service -E edit virtual service with options
--delete-service -D delete virtual service
--clear -C clear the whole table
--restore -R restore rules from stdin
--save -S save rules to stdout
--add-server -a add real server with options
--edit-server -e edit real server with options
--delete-server -d delete real server
--list -L|-l list the table
--zero -Z zero counters in a service or all services
--set tcp tcpfin udp set connection timeout values
--start-daemon start connection sync daemon
--stop-daemon stop connection sync daemon
--help -h display this help message

Options:
--tcp-service -t service-address service-address is host[:port]
--udp-service -u service-address service-address is host[:port]
--fwmark-service -f fwmark fwmark is an integer greater than zero
--ipv6 -6 fwmark entry uses IPv6
--scheduler -s scheduler one of rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq,
the default scheduler is wlc.
--pe engine alternate persistence engine may be sip,
not set by default.
--persistent -p [timeout] persistent service
--netmask -M netmask persistent granularity mask
--real-server -r server-address server-address is host (and port)
--gatewaying -g gatewaying (direct routing) (default)
--ipip -i ipip encapsulation (tunneling)
--masquerading -m masquerading (NAT)
--weight -w weight capacity of real server
--u-threshold -x uthreshold upper threshold of connections
--l-threshold -y lthreshold lower threshold of connections
--mcast-interface interface multicast interface for connection sync
--syncid sid syncid for connection sync (default=255)
--connection -c output of current IPVS connections
--timeout output of timeout (tcp tcpfin udp)
--daemon output of daemon information
--stats output of statistics information
--rate output of rate information
--exact expand numbers (display exact values)
--thresholds output of thresholds information
--persistent-conn output of persistent connection info
--nosort disable sorting output of service/server entries
--sort does nothing, for backwards compatibility
--ops -o one-packet scheduling
--numeric -n numeric output of addresses and ports
--sched-flags -b flags scheduler flags (comma-separated)

참고 : https://www.linux.co.kr/home/lecture/index.php?cateNo=1&secNo=152&theNo=&leccode=10904

Share