[转载]LVS+Keepalived之三大模式

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
LVS + Keepalived之三大模式
======================================================================================
NAT模式:
======================================================================================
一、仅lvs服务器端安装
yum install ipvsadm openssl-devel popt popt-devel libnl-devel
cd /opt/src/
tar xzvf keepalived-1.2.7.tar.gz
cd keepalived-1.2.7
./configure
make && make install
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/sbin/keepalived /usr/sbin/
  
修改/etc/keepalived/keepalived.conf文件
[root@WEB ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived
  
global_defs {
   notification_email {
     
     123@123.com
   }
   notification_email_from 123@123.com
   smtp_server mail.123.com
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
  
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
       88.88.88.88
    }
}
vrrp_instance LAN_GATEWAY {
    state MASTER              
    interface eth1
    virtual_router_id 52
    priority 100     
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.88
    }
}
virtual_server 88.88.88.88 80 {
    delay_loop 6
    lb_algo lc
    #lb_kind DR
    #lb_kind TUN
    lb_kind NAT
    persistence_timeout 60
    protocol TCP
     
    real_server 192.168.1.18 80 {
        weight 3           
        TCP_CHECK {
        connect_timeout 30  
        nb_get_retry 3
        delay_before_retry 2
        connect_port 80
        }
    }
    real_server 192.168.1.68 80 {
        weight 3
        TCP_CHECK {
        connect_timeout 30
        nb_get_retry 3
        delay_before_retry 2
        connect_port 80
        }
     }
 }
[root@WEB ~]#
[root@WEB ~]# service ipvsadm start
[root@WEB ~]# service keepalived start
  
注意:
echo "1" > /proc/sys/net/ipv4/ip_forward
  
二、实际服务器端的操作
将网关设置为lanvip的地址192.168.1.88
  
三、lvs查看
  
ipvsadm -ln
ipvsadm -lnc
ipvsadm -l -n --stats
  
调度算法:rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq
======================================================================================
TUN模式:
======================================================================================
一、仅lvs服务器端安装
yum install ipvsadm openssl-devel popt popt-devel libnl-devel
cd /opt/src/
tar xzvf keepalived-1.2.7.tar.gz
cd keepalived-1.2.7
./configure
make && make install
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/sbin/keepalived /usr/sbin/
  
修改/etc/keepalived/keepalived.conf文件
[root@WEB ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived
  
global_defs {
   notification_email {
     123@123.com
   }
   notification_email_from 123@123.com
   smtp_server mail.123.com
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
  
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
       192.168.1.88
    }
}
virtual_server 192.168.1.88 80 {
    delay_loop 6
    lb_algo lc
    lb_kind TUN
    persistence_timeout 60
    protocol TCP
     
    real_server 192.168.2.18 80 {
        weight 3           
        TCP_CHECK {
        connect_timeout 30  
        nb_get_retry 3
        delay_before_retry 2
        connect_port 80
        }
    }
    real_server 192.168.3.68 80 {
        weight 3
        TCP_CHECK {
        connect_timeout 30
        nb_get_retry 3
        delay_before_retry 2
        connect_port 80
        }
     }
 }
[root@WEB ~]#
[root@WEB ~]# service ipvsadm start
[root@WEB ~]# service keepalived start
  
注意:
打开转发或关闭,dr/tun模式无影响
echo "1" > /proc/sys/net/ipv4/ip_forward
  
二、实际服务器端的操作
[root@]# cat /usr/local/vip_tun.sh
#!/bin/bash
#srcipt to start LVS TUN realserver
  
VIP=192.168.1.88
case "$1" in
  
start)
#start LVS-TUN real server on this machine
echo 1 >/proc/sys/net/ipv4/conf/tunl0/arp_ignore
echo 2 >/proc/sys/net/ipv4/conf/tunl0/arp_announce
echo 1 >/proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 >/proc/sys/net/ipv4/conf/all/arp_announce
echo 0 >/proc/sys/net/ipv4/conf/tunl0/rp_filter
echo 0 >/proc/sys/net/ipv4/conf/all/rp_filter
sysctl -p >/dev/null 2>&1
/sbin/modprobe ipip
/sbin/ifconfig tunl0 $VIP broadcast $VIP netmask 255.255.255.255  up
/sbin/route add -host $VIP dev tunl0
/sbin/ifconfig eth0 mtu 1440
  
;;
stop)
#stop lvs-TUN real server loopback device
echo 0 > /proc/sys/net/ipv4/conf/tunl0/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/tunl0/arp_announce
echo 1 > /proc/sys/net/ipv4/conf/tunl0/rp_filter
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 1 > /proc/sys/net/ipv4/conf/all/rp_filter
/sbin/ifconfig tunl0 down
/sbin/modprobe -r ipip
/sbin/ifconfig eth0 mtu 1500
 
;;
status)
islothere='/sbin/ifconfig tunl0 |grep $VIP'
isrothere='netstat -rn |grep "tunl0"|grep $VIP'
if [ ! "$islothere" -o ! "$isrothere" ];then
echo "LVS-TUN real server stopped"
else echo "LVS-TUN Running"
fi
;;
*)
echo "$0: Usage: $0 {start|stop|status}"
exit 1
;;
esac
[root@localhost ~]#  /usr/local/vip.sh start|stop|status
 
三、lvs查看
  
ipvsadm -ln
ipvsadm -lnc
ipvsadm -l -n --stats
  
调度算法:rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq
======================================================================================
DR模式:
======================================================================================
一、仅lvs服务器端安装
yum install ipvsadm openssl-devel popt popt-devel libnl-devel
cd /opt/src/
tar xzvf keepalived-1.2.7.tar.gz
cd keepalived-1.2.7
./configure
make && make install
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/sbin/keepalived /usr/sbin/
  
修改/etc/keepalived/keepalived.conf文件
[root@WEB ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived
  
global_defs {
   notification_email {
     
     123@123.com
   }
   notification_email_from 123@123.com
   smtp_server mail.123.com
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
  
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 52
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
       192.168.1.88
    }
}
virtual_server 192.168.1.88 80 {
    delay_loop 6
    lb_algo lc
    lb_kind DR
    persistence_timeout 60
    protocol TCP
     
    real_server 192.168.1.18 80 {
        weight 3           
        TCP_CHECK {
        connect_timeout 30  
        nb_get_retry 3
        delay_before_retry 2
        connect_port 80
        }
    }
    real_server 192.168.1.68 80 {
        weight 3
        TCP_CHECK {
        connect_timeout 30
        nb_get_retry 3
        delay_before_retry 2
        connect_port 80
        }
     }
 }
[root@WEB ~]#
[root@WEB ~]# service ipvsadm start
[root@WEB ~]# service keepalived start
  
注意:
打开转发或关闭,dr/tun模式无影响
echo "1" > /proc/sys/net/ipv4/ip_forward
  
二、实际服务器端的操作
[root@localhost ~]# cat /usr/local/vip.sh
#!/bin/bash
# Script to start LVS DR real server.
# description: LVS DR real server
/etc/rc.d/init.d/functions
VIP=192.168.1.88
#host=`/bin/hostname`
case "$1" in
start)
       # Start LVS-DR real server on this machine.
        /sbin/ifconfig lo down
        /sbin/ifconfig lo up
        echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
        echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
        /sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
        /sbin/route add  $VIP dev lo:0
;;
stop)
        # Stop LVS-DR real server loopback device(s).
        /sbin/ifconfig lo:0 down
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
        echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
;;
status)
        # Status of LVS-DR real server.
        islothere=`/sbin/ifconfig lo:0 | grep $VIP`
        isrothere=`netstat -rn | grep "lo:0" | grep $VIP`
        if [ ! "$islothere" -o ! "isrothere" ];then
            # Either the route or the lo:0 device
            # not found.
            echo "LVS-DR real server Stopped."
        else
            echo "LVS-DR real server Running."
        fi
;;
*)
            # Invalid entry.
            echo "$0: Usage: $0 {start|status|stop}"
            exit 1
;;
esac
[root@localhost ~]#  /usr/local/vip.sh start|stop|status
 
三、lvs查看
  
ipvsadm -ln
ipvsadm -lnc
ipvsadm -l -n --stats
  
调度算法:rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq
 
注意:
    1.TUN模式使用的IP都要是公网IP.
    2.VIP的port要和后端realserver的port保持一致,但是不会影响VIP本机的port被其他服务使用.
 
转载自:http://www.gaizaoren.com/archives/998
  

 

附加:
LVS可以使用iptables同时做多个服务的负载均衡并实现持久化连接,使用iptables对包打上标签之后再进行匹配处理。配置如下:

 

posted @   wsjhk  阅读(378)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示