目的:利用keepalived 实现http的负载均衡 用户访问192.168.1.153,实现对sr1和sr2的负载均衡。
实验环境:
sr1: 192.168.1.151 http服务
sr2: 192.168.1.152 http服务
ka1: 192.168.1.150 主keepalived
ka2: 192.168.1.149 备keepalived
vip: 192.168.1.153 虚拟IP
操作步骤:
一:sr1、sr2 安装 httpd 服务、配置虚拟IP
yum -y install httpd
mkdir /etc/sh
vim /etc/sh/dr.sh
#!/bin/bash
vip=192.168.1.153
ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
route add -host $vip lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
更改权限并运行
chmod 700 /etc/sh/dr.sh;bash /etc/sh/dr.sh
ifconfig 查看lo:0处于up状态。
二:ka1、ka2安装keepalived、ipvsadm服务
yum -y install keepalived
yum -y install ipvsadm
三:配置主keepalived
vim /etc/keepalived/keeplived.conf
! Configuration File for keepalived
global_defs { #全局配置
notification_email {
[email protected] #如有故障时,收件的email邮箱,可不填
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.1.202
smtp_connect_timeout 30 #邮件服务器连接超时的最长时间
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER #主keepalived 为 MASTER,备keepalived设为BACKUP
interface eth0
virtual_router_id 51
priority 100 #主keepalived的的优先级,备keepalived的可设为90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.153 #虚拟IP
}
}
virtual_server 192.168.1.153 80 {
delay_loop 6 #每6秒检查一次服务是否正常
lb_algo rr #负载均衡的调度算法方式,一般使用rr或者wlc
lb_kind DR #负载均衡的模式,TUN、NTA、DR三种
nat_mask 255.255.255.0
persistence_timeout 2 #会话保持时间,单位:秒。如果是动态服务,建议开启。
protocol TCP #通讯模式。TCP UDP
real_server 192.168.1.151 80 { #真实服务的IP
weight 100 #权重值,数值越大,权重越高,分发的可能越大 TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 80 #端口
}
}
real_server 192.168.1.152 80 {
weight 10
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
四:配置备keepalived
vim /etc/keepalived/keepalived.conf
在主keepalived.conf 基础上把
1:state MASTER 修改为 state BACKUP
2:priority 100 修改为 priority 90
五:先启动ka1,再启动ka2
ka1 : /etc/init.d/keepalived start;
ka2 : /etc/init.d/keepalived start;
六:浏览器访问http://192.168.1.153 进行测试。