1:原理图
底层原理
2:负载集合的功能:
1)客户端传过来的请求,在负载均衡那里,根据算法,把用户的请求给指定的服务器,
2)如果负载均衡主机宕机了,备机马上接手,如果主机恢复了,备机马上退后
3)如果某个服务器挂了,该服务器马上被踢出去,负载均衡转发请求不发该服务器,如果服务器恢复了,马上添加进来,添加到负载均衡的服务器群中。
3:负载均衡搭建
3.1准备4台虚拟机,2台作为负载均衡主从机,2台作为httpd服务器
3.2获取2台服务器的ip
[root@fei etc]# ifconfig
eth1
inet addr:192.168.163.130
[root@fei etc]# ifconfig
eth1
inet addr:192.168.163.131
3.3分别为2台服务器配置vip
[root@fei ~]# cd /etc/init.d/
[root@fei ~]#touch realserver
[root@fei ~]#vim realserver
SNS_VIP=192.168.163.150
. /etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
/sbin/route add -host $SNS_VIP dev 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
sysctl -p >/dev/null 2>&1
echo "RealServer Start OK"
;;
stop)
ifconfig lo:0 down
route del $SNS_VIP >/dev/null 2>&1
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "RealServer Stoped"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
为realserver文件配置权限
chmod 750 realserver
//启动
[root@fei init.d]# service realserver start
RealServer Start OK
//效果
[root@fei ~]# ipconfig
lo:0 Link encap:Local Loopback
inet addr:192.168.163.150 Mask:255.255.255.255
UP LOOPBACK RUNNING MTU:65536 Metric:1
//2台服务器配置默认访问页面
[root@fei html]# vim /var/www/html/index.html
130 130 130
[root@fei html]# vim /var/www/html/index.html
130 130 131
//分别开启2台httpdf服务器
[root@fei html]# service httpd start
正在启动 httpd:
效果如下
以上参数说明
SNS_VIP=192.168.163.150 #定义VIP变量
. /etc/rc.d/init.d/functions #导脚本库
case "$1" in #case语句 $1传递给该shell脚本的第一个参数
start)
ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP #设置Lo:0 VIP netmask 及广播
/sbin/route add -host $SNS_VIP dev lo:0 ##route del 增加本地路由
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
sysctl -p >/dev/null 2>&1 # -p <file> (default /etc/sysctl.conf) 将标准信息输入设备空文件
echo "RealServer Start OK"
;;
stop)
ifconfig lo:0 down
route del $SNS_VIP >/dev/null 2>&1 #route del 删除本地路由
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "RealServer Stoped"
;;
*)
echo "Usage: $0 {start|stop}" #$0 是脚本本身的名字
exit 1 #表示进程正常退出
esac #case结束
exit 0 #表示进程非正常退出
1):安装 keepalived
yum –y install keepalived
2):配置 keepalived
> /etc/keepalived/keepalived.conf
vim /etc/keepalived/keepalived.conf
global_defs {
# notification_email {
# admin@toxingwang.com
# }
# notification_email_from master@toxingwang.com
# smtp_server smtp.exmail.qq.com
# smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth1
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.163.150
}
}
virtual_server 192.168.163.150 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 0
protocol TCP
real_server 192.168.163.130 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.163.131 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
启动并查看日志
[root@fei keepalived]# service keepalived start
正在启动 keepalived: [确定]
[root@fei keepalived]# tail -f /var/log/messages
ar 26 05:37:06 fei Keepalived_vrrp[37022]: VRRP_Instance(VI_1) Transition to MASTER STATE
Mar 26 05:37:06 fei Keepalived_vrrp[37022]: VRRP_Instance(VI_1) Received lower prio advert, forcing new election
Mar 26 05:37:07 fei Keepalived_vrrp[37022]: VRRP_Instance(VI_1) Entering MASTER STATE
以上就把主机的负载均衡配置好了。
3.5 重复3.4,在另外一台虚拟机上配置负载均衡备机
配置说明
global_defs { ##全局配置部分
# notification_email { ##下面几行均为全局通知配置,可以实现出现问题后报警
# admin@toxingwang.com
# }
# notification_email_from 2637282556@qq.com
# 2637282556.qq.com
# smtp_connect_timeout 30
router_id LVS_DEVEL ##设置lvs的id,在一个网络内应该是唯一的
}
vrrp_instance VI_1 { ##设置vrrp组,唯一且同一LVS服务器组要相同
state MASTER ##备份LVS服务器设置为BACKUP
interface eth0 # #设置对外服务的接口,如果虚拟机是复制的,该接口可能是eth1
virtual_router_id 51 ##设置虚拟路由标识
priority 100 #设置优先级,数值越大,优先级越高,backup设置为99,这样就能实现当master宕机后自动将backup变为master,而当原master恢复正常时,则现在的master再次变为backup。
advert_int 1 ##设置同步时间间隔
authentication { ##设置验证类型和密码,master和buckup一定要设置一样
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { ##设置VIP,可以多个,每个占一行
192.168.163.150
}
}
virtual_server 192.168.163.150 80 {/
delay_loop 6 ##健康检查时间间隔,单位s,查看httpd服务器还在不
lb_algo wrr ##负载均衡调度算法设置为加权轮叫
lb_kind DR ##负载均衡转发规则
nat_mask 255.255.255.0 ##网络掩码,DR模式要保障真实服务器和lvs在同一网段
persistence_timeout 50 ##会话保持时间,单位s,就是与httpd服务器保持的时间,在该时间段的,所有请求都跳转到该hhtpd的服务器,(session计算规则,最后一次服务器请求的时间作为开始时间)
protocol TCP ##协议
real_server 192.168.163.130 80 { ##真实服务器配置,80表示端口
weight 3 ##权重
TCP_CHECK { ##服务器检测方式设置 keepalived的健康检查方式 有:HTTP_GET|SSL_GET|TCP_CHECK|SMTP_CHECK|MISC_CHECK
connect_timeout 0 ##连接超时时间
nb_get_retry 3 ##失败重试次数
delay_before_retry 3 ##失败重试的间隔时间
connect_port 80 ##连接的后端端口
}
}
real_server 192.168.163.131 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
来源:https://www.cnblogs.com/feiZhou/p/6623124.html