什么是负载均衡:
负载均衡(Load Balance)是分布式系统架构设计中必须考虑的因素之一,它通常是指,将请求数据【均匀】分摊到多个操作单元上执行,负载均衡的关键在于【均匀】。
这里我用nginx做负载均衡,nginx不单可以作为强大的web服务器,也可以作为一个反向代理服务器,而且nginx还可以按照调度规则实现动态、静态页面的分离,可以按照轮询、ip哈希、权重等多种方式对后端服务器做负载均衡,同时还支持后端服务器的健康检查。
什么是keepalived:
Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的web服务器。
配置环境:
服务器均为CENTOS7的系统
192.168.1.162: 主Keepalived、nginx 服务器
192.168.1.163: 备keepalived、nginx 服务器
192.168.1.164: 虚拟IP地址
192.168.1.147: tomcat应用服务器(后端应用服务器)
关闭防火墙并关闭开机启动:
systemctl stop firewalld.service
systemctl disable firewalld.service
关闭SELinux安全机制(为避免出现不必要麻烦):
1.临时关闭:
setenforce 0 ##(设置SELinux 成为permissive模式)
##(setenforce 1 设置SELinux 成为enforcing模式)
2.永久关闭(修改配置文件需要重启机器):
修改/etc/selinux/config
文件将SELINUX=enforcing改为SELINUX=disabled
重启机器即可
分别在162、163服务器上面安装keepalived、nginx。
安装keepalived:
yum –y install keepalived
修改配置文件:
主keepalived配置:
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak ##(将原配置文件备份,防止出错。)
vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
admin@lvtao.net
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server mail.smtp.163.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script check_nginx {
script "/home/nginx_pid.sh" ##(监控NGINX 服务运行shell脚本)
interval 2 ##(时间间隔为2秒检测一次)
weight -15 ##(当nginx的进程不存在了,就把当前的权重-15)
}
vrrp_instance VI_1 {
state MASTER ##(主机为MASTER,备用机为BACKUP)
interface eth0 ##(网络适配器名称)
virtual_router_id 51 ##(同一实例下virtual_router_id必须相同,MASTRE/BACKUP 设置值要一样)
priority 200 ##(定义优先级,数字越大,优先级越高,把此份Conf拷贝到另一台机器上时,设置的priority值要比MASTRE权重值低)
advert_int 1 ##(#MASTER与BACKUP负载均衡器之间同步检查的时间间隔,单位是秒)
authentication {
auth_type PASS
auth_pass 1111 ##(验证类型和密码,认证密码主备服务器之间一定要一致,否则出错)
}
track_script {
check_nginx ##(检测脚本,即上面的check_nginx)
}
virtual_ipaddress {
192.168.1.164 ##(#设置虚拟IP,可以有多个地址,每个地址占一行,不需掩码。)
}
}
这里不做LVS,删除后面的virtual_server内容部分
上面 nginx_pid.sh 脚本内容:
#!/bin/bash
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
/usr/local/nginx/sbin/nginx
sleep 5
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
killall keepalived
fi
fi
从keeplived的配置文件:
! Configuration File for keepalived
global_defs {
notification_email {
admin@lvtao.net
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server mail.smtp.163.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script check_nginx {
script "/home/nginx_pid.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_nginx
}
virtual_ipaddress {
192.168.1.164
}
}
检查keeplived是否生效:
Master:
启动keepalived之前查看IP addr:
[root@zabbix162 ~]# ip addr list
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
link/ether 00:15:5d:01:13:0f brd ff:ff:ff:ff:ff:ff
inet 192.168.1.162/24 brd 192.168.1.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::215:5dff:fe01:130f/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
启动keepalived:
[root@zabbix162 ~]# ip addr list
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 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
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000
link/ether 00:15:5d:01:13:0f brd ff:ff:ff:ff:ff:ff
inet 192.168.1.162/24 brd 192.168.1.255 scope global eth0
valid_lft forever preferred_lft forever
inet 192.168.1.164/32 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::215:5dff:fe01:130f/64 scope link tentative dadfailed
valid_lft forever preferred_lft forever
可以看到多了一个虚拟IP:
inet 192.168.1.164/32 scope global eth0
将MASTER的keepalived停止,启动BACKUP服务器上的keepalived,查看是否接管192.168.1.164的虚拟IP,以此方式测试。
设置keepalived 开机启动:
systemctl enable keepalived.service
参考网站:
http://linux.blog.163.com/blog/static/9297023201099102637777/
http://www.cnblogs.com/liuquan/p/5583816.html
http://www.linuxde.net/2013/04/13381.html