配置过程如下,
假设有两台主机作为集群的节点,在两台主机上如下安装好keepalived
如果系统中有心跳服务需要先将其关闭,
/etc/init.d/heartbeat stop
chkconfig heartbeat off
然后下载好keepalived的压缩包(操作中使用的是keepalived-1.3.5.tar.gz),然后进行压缩解压缩
tar zxf keepalived-1.3.5.tar.gz
cd keepalived-1.3.5
如果出现版本错误或者需要重新安装的情况,那么 rm -fr keepalived/、make clean(cd keepalived-1.3.5)
随后进入解压后生成的目录,开始源码安装
./configure --prefix=/usr/local/keepalived --with-init=SYSV
make && make install
制作软链接使keepalived命令可以直接在shell中执行
ln -s /usr/local/keeplived/sbin/keepalived /sbin/
ln -s /usr/local/keeplived/etc/keepalived/ /etc/
ln -s /usr/local/keeplived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/keeplived/etc/rc.d/init.d/keepalived /etc/init.d/
进入keepalived的目录下,修改配置文件
cd /etc/keepalived/
vim keepalived.conf
修改结果如下
global_defs {
notification_email {
root@localhost
}
notification_email_from keeplived@server1
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 39
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.25.39.100
}
}
virtual_server 172.25.39.100 80 {
delay_loop 6
lb_algo rr
lb_kind DR
# persistence_timeout 50
protocol TCP
real_server 172.25.39.2 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 172.25.39.3 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
然后将配置好的文件发送给另一个节点
scp -r keepalived/ server4:/usr/local/
同时开启两个主机的keepalived服务
/etc/init.d/keepalived start
测试
ip addr
curl 172.25.39.10
ipvsadm -L
注意::iptables -L curl 172.25.40.100 #虚拟服务器ip vip
iptables -F #将火墙策略清除干净
在keepalived上面添加ftp协议
关闭ldrirectord服务
/etc/init.d/ldirectord stop
chkconfig ldirectord off
开启keepalived服务
/etc/init.d/keepalived start
在配置文件中添加ftp协议
vim keepalived.conf
修改如下
57 virtual_server 172.25.39.100 21 {(ftp的端口是21)
58 delay_loop 6
59 lb_algo rr
60 lb_kind DR
61 persistence_timeout 50(开启后持续连接50s,此期间不会被负载均衡)
62 protocol TCP
63 (为什么不会被负载均衡?原因如下:
ftp有两个端口20和21,在协议磋商的时候开启20,持续连接。如果关闭此项,那么一直转换,始终在协议磋商,不能连接)
64 real_server 172.25.39.2 21 {
65 weight 1
66 TCP_CHECK {
67 connect_timeout 3 68 nb_get_retry 3
69 delay_before_retry 3
70 }
71 }
72
73 real_server 172.25.39.3 21 {
74 weight 1
75 TCP_CHECK {
76 connect_timeout 3
77 nb_get_retry 3
78 delay_before_retry 3
79 }
80 }
81 }
让服务重新读取配置文件
/etc/init.d/keepalived reload
在负载均衡主机上安装并开启vsftpd服务
yum install -y vsftpd
/etc/init.d/vsftpd start
.在主节点上查看策略是否生效
ipvsadm -L
结果为
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.25.39.100:ftp rr persistent 50
TCP 172.25.39.100:http rr
-> server2:http Route 1 0 0
-> server3:http Route 1 0 0