环境:需要4台服务器,一台keeplived主服务器,一台从keeplived服务器,两台realserver服务器,4块网卡,1个vip虚拟ip 。
虚拟ip:192.168.31.110
realserverip:192.168.31.101,192.168.31.100
安装软件:两个keepalived服务器上面都需要安装vsadm 和keepalived两个软件,两个realserver上面安装上nginx或者apache即可
yum install -y keepalived yum install -y ipvsadm
vim /etc/keepalived/keepalived.conf //加入如下:
vrrp_instance VI_1 {
state MASTER #备用服务器上为 BACKUP
interface eth0 #用eth0这个网卡去监听vip的虚拟网卡
virtual_router_id 51
priority 100 #备用服务器上为90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.31.110 (虚拟ip)
}
}
virtual_server 192.168.31.110 80 {
delay_loop 6 #(每隔10秒查询realserver状态)
lb_algo wlc #(lvs 算法)
lb_kind DR #(Direct Route)
persistence_timeout 60 #(同一IP的连接60秒内被分配到同一台realserver)
protocol TCP #(用TCP协议检查realserver状态)
real_server 192.168.31.100 80 { #(realserver的ip)
weight 100 #(权重)
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.31.101 80 { #(realserver的ip)
weight 100
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
复制主配置文件,然后更改下面两行即可
state MASTER -> state BACKUP
priority 100 -> priority 90
配置完keepalived后,需要开启端口转发(主从都要做):
echo 1 > /proc/sys/net/ipv4/ip_forward
最后,两个director上启动keepalived服务(先主后从):
/etc/init.d/keepalived start
启动keepalived服务会自动生成vip和ipvsadm规则
## 4测试
当某一个keepadlive服务器坏掉,或者某一个rs坏掉,发现都不会影响网站的运行(这一切都是自动实现的)在浏览器中输入http://192.168.31.110即可
原文:https://www.cnblogs.com/huningfei/p/12747129.html