负载均衡
vm1:192.168.10.51 80
vm2:192.168.10.52 80
vip:192.168.10.55 80
一、下载安装
1)、wget
tar zxf
cd
./confifure --prefix=/usr/local/services/keepalived
make&&make install
2)、修改文件路径
sudo cp /usr/local/services/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
sudo cp /usr/local/services/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
sudo mkdir /etc/keepalived
sudo cp /usr/local/services/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
sudo cp /usr/local/services/keepalived/sbin/keepalived /usr/sbin/
二、配置
vim keepalived.conf
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER //BACKUP
interface eth0
virtual_router_id 51
priority 100 //90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.55
}
}
virtual_server 192.168.10.55 80 {
delay_loop 3
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 192.168.10.51 80 {
weight 1
TCP_CHECK {
connection_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.10.52 80 {
weight 1
TCP_CHECK {
connection_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
三、启动
sudo /etc/init.d/keepalived start
四、验证
ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:8e:7e:4b brd ff:ff:ff:ff:ff:ff
inet 192.168.10.51/24 brd 192.168.10.255 scope global eth0
inet 192.168.10.55/32 scope global eth0
inet6 fe80::20c:29ff:fe8e:7e4b/64 scope link
valid_lft forever preferred_lft forever
浏览器访问
================================================================
高可用
vim keepalived.conf
global_defs {
router_id LVS_DEVEL
}
vrrp_script check_http_port {
script "/etc/check_http_port.sh"
interval 5
weight 2
}
vrrp_instance VI_1 {
state master //backup
interface eth0
virtual_router_id 10
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 111111
}
track_script {
check_http_port
}
virtual_ipaddress {
192.168.10.55
}
}
vim /etc/check_http_port.sh
...
prog_exists=$(ps -ef | grep "${prog_flag}" | grep -v grep | wc -l)
if [ "${prog_exists}" -eq 0 ];
then
VIP_exists=$(/sbin/ip addr | grep ""$VIP"" | wc -l)
if [ "${VIP_exists}" -ge 1 ];
then
/etc/init.d/keepalived stop
fi
fi