一. Keepalived 相关
----------------------------------
1. keepalived的实现原理:
2.Keepalived的下载:
http://www.keepalived.org/download.html
wget http://www.keepalived.org/software/keepalived-1.2.24.tar.gz
3.Keepalived的安装:
ps: 推荐用 "yum install keepalived" 一键安装(这样第4步不用执行)
ln -s /usr/src/kernels/2.6.32-431.el6.x86_64/ /usr/src/linux
tar -zxvf keepalived-1.2.24.tar.gz
cd keepalived-1.2.24
./configure
make && make install
4.配置规范启动:
cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
chmod +x /etc/init.d/keepalived
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/
5.配置Keepalived:
vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.200.1
smtp_connect_timeout 30 #上面是告警邮件配置,可以不用管它
router_id LVS_01 #实例名称,主备要不一样
}
vrrp_instance VI_1 {
state MASTER #主用MASTER,备用BACKUP
interface eth0
lvs_sync_daemon_inteface eth0 #负载均衡之间心跳监听的接口
virtual_router_id 51 #虚拟路由ID,范围0-250
priority 150 #设置优先级,主用150,备用100
advert_int 1
authentication {
auth_type PASS
auth_pass 112112 #通信密码,使用纯数字
}
virtual_ipaddress {
172.16.1.215/24
172.16.1.216/24
} # vip的地址,可以配置多个
}
6.修改日志文件位置:
vim /etc/sysconfig/keepalived : KEEPALIVED_OPTIONS="-D -d -S 0"
echo "local0.* /var/log/keepalived.log" >>/etc/rsyslog.conf
/etc/init.d/rsyslog restart
一. LVS 相关
----------------------------------
1.LVS介绍:http://www.linuxvirtualserver.org/zh/lvs1.html 官方网站
ip addr add 172.16.1.216/24 dev eth0 label eth0:1 #添加个VIP
ipvsadm -C #清空配置
ipvsadm -A -t 172.16.1.216:80 -s rr #添加一个LVS负载均衡Vserver,使用轮询算法
ipvsadm -a -t 172.16.1.216:80 -r 172.16.1.213:80 -g #在创建的Vserver中添加real server节点,-g 是选用DR模式
ipvsadm -a -t 172.16.1.216:80 -r 172.16.1.214:80 -g
ipvsadm -Ln #查看配置结果
ip addr add 172.16.1.216/
32 dev lo label lo:1 #子网掩码要用32,否则会IP冲突
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
9.查看LVS后端请求统计信息:
ipvsadm -Ln --stats
10.通过keepalived来管理lvs:
vim /etc/keepalived/keepalived.conf 后面添加
virtual_server 172.16.1.215 80 {
delay_loop 6
lb_algo wrr #加权轮询算法
lb_kind DR #工作模式
nat_mask 255.255.255.0
#persistence_timeout 30 #30秒内回话保持
protocol TCP
real_server 172.16.1.213 80 {
weight 100 #权重
TCP_CHECK {
connect_timeout 3 #健康检查超时时间
nb_get_retry 3
delay_before_retry 3
connect_port 80 #健康检查端口
}
}
real_server 172.16.1.214 80 {
weight 100
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
PS:使用keepalived同样需要执行7,8步,来添加VIP和抑制ARP。