LVS+Keepalived高可用负载群集
Balancer 01的设置
1.设置网络环境
[root@localhost ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:BD:E7:65
inet addr:172.16.10.11 Bcast:172.16.10.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:febd:e765/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:50 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 b) TX bytes:7516 (7.3 KiB)
Interrupt:18 Base address:0x2000
[root@localhost ~]# ifconfig eth1
eth1 Link encap:Ethernet HWaddr 00:0C:29:BD:E7:6F
inet addr:192.168.10.11 Bcast:192.168.10.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:febd:e76f/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:126 errors:0 dropped:0 overruns:0 frame:0
TX packets:73 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:12173 (11.8 KiB) TX bytes:10660 (10.4 KiB)
Interrupt:16 Base address:0x2080
2.安装Keepalived所需软件
[root@localhost ~]# ln -s /usr/src/kernels/2.6.32-131.0.15.el6.i686/ /usr/src/linux
[root@localhost ~]# cd /soft/LSS-2/chapter08/
[root@localhost chapter08]# ls
keepalived-1.2.2.tar.gz rkhunter-1.3.8.tar.gz
[root@localhost chapter08]# cd keepalived-1.2.2
[root@localhost keepalived-1.2.2]# ./configure ; make ; make install
3.安装后的调整
[root@localhost keepalived-1.2.2]# cd
[root@localhost ~]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
[root@localhost ~]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@localhost ~]# mkdir /etc/keepalived
[root@localhost ~]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
[root@localhost ~]# cp /usr/local/sbin/keepalived /usr/sbin/
[root@localhost ~]# service keepalived start
正在启动 keepalived: [确定]
4.配置VIP脚本
[root@localhost ~]# vi /opt/vip.sh
#!/bin/bash
VIP=192.168.10.19
/sbin/ifconfig eth1:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev eth1:0
echo "1" > /proc/sys/net/ipv4/ip_forward
[root@localhost ~]# chmod +x /opt/vip.sh
[root@localhost ~]# /opt/vip.sh
5.安装ipvsadm命令工具,设置DR模式的负载均衡群集
[root@localhost ~]# mount /dev/cdrom /media/
[root@localhost ~]# cd /media/Packages/
[root@localhost Packages]# rpm -ivh ipvsadm-1.25-9.el6.i686.rpm
warning: ipvsadm-1.25-9.el6.i686.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing... ########################################### [100%]
1:ipvsadm ########################################### [100%]
[root@localhost ~]# ipvsadm -A -t 192.168.10.19:80 -s rr
WARNING: All config files need .conf: /etc/modprobe.d/vmware-tools, it will be ignored in a future release.
[root@localhost ~]# ipvsadm -a -t 192.168.10.19:80 -r 192.168.10.13:80 -g
[root@localhost ~]# ipvsadm -a -t 192.168.10.19:80 -r 192.168.10.14:80 -g
[root@localhost ~]# ipvsadm -l
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.10.19:http rr
-> 192.168.10.13:http Route 1 0 0
-> 192.168.10.14:http Route 1 0 0
6.保存ipvsadm设置到/etc/sysconfig/ipvsadm文件以后,下次启动可以直接执行“service ipvsadm start”命令,不再需要逐条输入命令。
[root@localhost ~]# ipvsadm-save > /etc/sysconfig/ipvsadm
7.配置/etc/keepalived/keepalived.conf主配置文档
[root@localhost ~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL_1
}
vrrp_instance VI_1 {
state MASTER
interface eth1
virtual_router_id 51
priority 100
advert_int 6
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.19
}
}
virtual_server 192.168.10.19 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
real_server 192.168.10.13 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.10.14 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
8.启动keepalived服务
[root@localhost ~]# service keepalived restart
停止 keepalived: [确定]
正在启动 keepalived:
……………………………………………………………………………………………………………………
Balancer 02的设置
1.设置网络环境
[root@localhost ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:80:40:63
inet addr:172.16.10.12 Bcast:172.16.10.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe80:4063/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1752 errors:0 dropped:0 overruns:0 frame:0
TX packets:275 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:206538 (201.6 KiB) TX bytes:51374 (50.1 KiB)
Interrupt:18 Base address:0x2000
[root@localhost ~]# ifconfig eth1
eth1 Link encap:Ethernet HWaddr 00:0C:29:80:40:6D
inet addr:192.168.10.12 Bcast:192.168.10.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe80:406d/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2696 errors:0 dropped:0 overruns:0 frame:0
TX packets:214 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:295776 (288.8 KiB) TX bytes:25420 (24.8 KiB)
Interrupt:16 Base address:0x2080
2.安装Keepalived所需软件(需要提前将keepalived-1.2.2.tar.gz包上传到/soft/LSS-2/chapter08/)
[root@localhost ~]# ln -s /usr/src/kernels/2.6.32-131.0.15.el6.i686/ /usr/src/linux
[root@localhost ~]# cd /soft/LSS-2/chapter08/
[root@localhost chapter08]# ls
keepalived-1.2.2.tar.gz rkhunter-1.3.8.tar.gz
[root@localhost chapter08]# cd keepalived-1.2.2
[root@localhost keepalived-1.2.2]# ./configure ; make ; make install
3.安装后的调整
[root@localhost keepalived-1.2.2]# cd
[root@localhost ~]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
[root@localhost ~]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
[root@localhost ~]# mkdir /etc/keepalived
[root@localhost ~]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
[root@localhost ~]# cp /usr/local/sbin/keepalived /usr/sbin/
[root@localhost ~]# service keepalived start
正在启动 keepalived: [确定]
4.在Balancer01上,传送/opt/vip.sh到Balancer02上的/opt/下
[root@localhost ~]# scp /opt/vip.sh [email protected]:/opt/
The authenticity of host '192.168.10.12 (192.168.10.12)' can't be established.
RSA key fingerprint is 15:ec:b9:77:4f:53:b4:98:bc:01:c3:a1:32:68:eb:97.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.10.12' (RSA) to the list of known hosts.
[email protected]'s password:
vip.sh
5.执行/opt/vip.sh脚本
[root@localhost ~]# /opt/vip.sh
[root@localhost ~]# ifconfig eth1:0
eth1:0 Link encap:Ethernet HWaddr 00:0C:29:80:40:6D
inet addr:192.168.10.19 Bcast:192.168.10.19 Mask:255.255.255.255
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Interrupt:16 Base address:0x2080
6.安装ipvsadm-1.24-8.1.i386.rpm
[root@localhost ~]# mount /dev/cdrom /media/
mount: block device /dev/sr0 is write-protected, mounting read-only
[root@localhost ~]# cd /media/Packages/
[root@localhost Packages]# rpm -ivh ipvsadm-1.25-9.el6.i686.rpm
warning: ipvsadm-1.25-9.el6.i686.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing... ########################################### [100%]
1:ipvsadm ########################################### [100%]
7.配置/etc/keepalived/keepalived.conf主配置文档
[root@localhost ~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL_2
}
vrrp_instance VI_1 {
state BACKUP
interface eth1
virtual_router_id 51
priority 80
advert_int 6
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.19
}
}
virtual_server 192.168.10.19 80 {
delay_loop 6
lb_algo rr
lb_kind DR
protocol TCP
real_server 192.168.10.13 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.10.14 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
[root@localhost ~]# /opt/vip.sh
SIOCSIFFLAGS: 无法指定被请求的地址
SIOCADDRT: 没有那个设备