今天抽时间吧这个实验整理了一下。
实验环境:
LVS+keepalived:172.16.11.2;172.16.11.3, vip: 172.16.11.8
后台web Server:172.16.11.6,;172.16.11.7
安装部分:
LVS的安装
使用wget下载ipvsadm。我使用的是:
# wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24-5.src.rpm
下载后安装,因为是rpm源码,先使用rpm安装:
# rpm -ivh ipvsadm-1.24-5.src.rpm
再把安装后的tar压缩包拷贝到src目录中。
#cp /usr/src/redhat/SOURCES/ipvsadm-1.24.tar.gz /usr/local/src/
#创建软链接
# ln -s /usr/src/kernels/2.6.18-194.el5-x86_64/ /usr/src/linux
最后就安装吧
#make && make install
keepalived安装:
使用wget下载ipvsadm。我使用的是:
http://www.keepalived.org/software/keepalived-1.2.7.tar.gz
安装:
#tar -zxvf keepalived-1.2.7.tar.gz
#cd keepalived-1.2.7
#yum install -y openssl-*.X86_64 openssl-devle
#./configure --prefix=/usr/local/keepalived
#make && make install
#cp /usr/local/etc/rc.d//init.d/keepalived /etc/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/
启动keepaived。
#service keepalive start
编辑keepalived.conf配置文件:
[root@LVS01 ~]# cat /etc/keepalived/keepalived.conf #另外一台lvs配置类似。
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server smtp.163.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance keepalived_1 {
state MASTER #另外一台lvs是BACKUP
interface eth0 #注意网卡接口
virtual_router_id 61 #另外一台也是61.不同就表示不是同一组双机。
priority 100 #另外一个优先级是99.数学越大越优先。
advert_int 1
authentication {
auth_type PASS #两个keepalived之间验证方式。
auth_pass password #验证密码。
}
virtual_ipaddress {
172.16.11.8 #vip
}
}
virtual_server 172.16.11.8 80 {
delay_loop 6
lb_algo wrr #访问后台web服务区的方式
lb_kind DR #lvs部署方式
persistence_timeout 50
protocol TCP
inhibit_on_failure
real_server 172.16.11.6 80 { #定义后台web服务区
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 172.16.11.7 80 { #定义后台web服务区
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
后台web服务器配置:
A,更改/etc/sysctl.conf配置文件中的参数。
# vi /etc/sysctl.conf
在最后添加如下内容:
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
执行下面命令让参数生效:
# sysctl -p
B,在Loopback网卡上添加子接口,并分配IP地址。
# ifconfig lo:0 172.16.11.8 netmask 255.255.255.255 broadcast 172.16.11.8 up
添加路由条目:
#route add -host 172.16.11.8 dev lo:0
在客户端访问LVS的VIP的80端口,查看访问结果。在LVS01上查看80请求分配情况:
配置完成后,在两个lvs上重新启动keepalived服务。并查看log。
# service keepalived restart
#tail /var/log/messages
Aug 2 23:26:55 LVS01 Keepalived_healthcheckers[12916]: Opening file '/etc/keepalived/keepalived.conf'.
Aug 2 23:26:55 LVS01 Keepalived_healthcheckers[12916]: Configuration is using : 14214 Bytes
Aug 2 23:26:55 LVS01 Keepalived_healthcheckers[12916]: Using LinkWatch kernel netlink reflector...
Aug 2 23:26:55 LVS01 Keepalived_healthcheckers[12916]: Activating healthchecker for service [172.16.11.6]:80
Aug 2 23:26:55 LVS01 Keepalived_healthcheckers[12916]: Activating healthchecker for service [172.16.11.7]:80
Aug 2 23:26:55 LVS01 Keepalived_vrrp[12917]: VRRP sockpool: [ifindex(2), proto(112), fd(11,12)]
Aug 2 23:26:56 LVS01 Keepalived_vrrp[12917]: VRRP_Instance(keepalived_1) Transition to MASTER STATE
Aug 2 23:26:57 LVS01 Keepalived_vrrp[12917]: VRRP_Instance(keepalived_1) Entering MASTER STATE
Aug 2 23:26:57 LVS01 Keepalived_vrrp[12917]: VRRP_Instance(keepalived_1) setting protocol VIPs.
Aug 2 23:26:57 LVS01 Keepalived_healthcheckers[12916]: Netlink reflector reports IP 172.16.11.8 added
Aug 2 23:26:57 LVS01 avahi-daemon[2971]: Registering new address record for 172.16.11.8 on eth0.
Aug 2 23:26:57 LVS01 Keepalived_vrrp[12917]: VRRP_Instance(keepalived_1) Sending gratuitous ARPs on eth0 for 172.16.11.8
Aug 2 23:26:57 LVS01 Keepalived_vrrp[12917]: Netlink reflector reports IP 172.16.11.8 added
Aug 2 23:27:02 LVS01 Keepalived_vrrp[12917]: VRRP_Instance(keepalived_1) Sending gratuitous ARPs on eth0 for 172.16.11.8