一、高可用介绍
用户通过虚拟IP访问Web服务,keepalived使用脚本自动检测Nginx 01的测存活状态,如果Nginx 01挂掉,自动切换至Nginx 02提供访问服务
二、高可用配置示例
环境准备:
Nginx01:192.168.10.103
Nginx02:192.168.10.104
Tomcat01:192.168.10.103
Tomcat02:192.168.10.104
192.168.10.103和192.168.10.104两台服务器都安装keepalived软件、
[root@hadoop103 ~]# yum -y install keepalived
[root@hadoop104 ~]# yum -y install keepalived
(配置文件所在位置:/etc/keepalived/keepalived.conf)
三、Keepalived+Nginx(主从模式)
在103/104f服务器上分别修改keepalived主配置文件:
[root@hadoop103 ~]# cd /etc/keepalived/
[root@hadoop103 keepalived]#
vim /etc/keepalived/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.10.103
smtp_connect_timeout 30
router_id LVS_DEVEL # 访问到的主机(/etc/hosts文件里添加“127.0.0.1 LVS_DEVEL”配置行)
}
vrrp_script chk_http_port {
script "/usr/local/src/nginx_check.sh" # 检测Nginx存活脚本文件
interval 2 # 检测脚本执行的间隔
weight 2
}
vrrp_instance VI_1 {
state MASTER # 备份服务器上将 MASTER 改为 BACKUP
interface ens33 # 网卡
virtual_router_id 51 # 主、备机的 virtual_router_id 必须相同
priority 100 # 主、备机取不同的优先级,主机值较大,备份机值较小
advert_int 1 # 服务器存活心跳检测,间隔1S
authentication { # 权限校验方式,PASS类型/密码:PASS/1111
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.100 # VRRP H 虚拟地址 (虚拟IP地址)
}
}
在104服务器上:
[root@hadoop104 ~]# cd /etc/keepalived/
[root@hadoop104 keepalived]#
vim /etc/keepalived/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.10.104
smtp_connect_timeout 30
router_id LVS_DEVEL # 访问到的主机(/etc/hosts文件里添加“127.0.0.1 LVS_DEVEL”配置行)
}
vrrp_script chk_http_port {
script "/usr/local/src/nginx_check.sh" # 检测Nginx存活脚本文件
interval 2 # 检测脚本执行的间隔,2s
weight 2
}
vrrp_instance VI_1 {
state MASTER # 备份服务器上将 MASTER 改为 BACKUP
interface ens33 # 网卡
virtual_router_id 51 # 主、备机的 virtual_router_id 必须相同
priority 99 # 主、备机取不同的优先级,主机值较大,备份机值较小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.10.100 # VRRP H 虚拟地址 (虚拟IP地址)
}
}
编写Nginx存活检测脚本:(在103、104服务器上)
[root@hadoop103 keepalived]# vim /usr/local/src/nginx_check.sh
#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
104服务器上:
[root@hadoop104 keepalived]# vim /usr/local/src/nginx_check.sh
#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
分别启动nginx、keepalived
[root@hadoop103 keepalived]# systemctl start keepalived.service
[root@hadoop103 keepalived]# systemctl status keepalived.service
[root@hadoop104 keepalived]# systemctl start keepalived.service
[root@hadoop104 keepalived]# systemctl status keepalived.service
浏览器访问nginx测试高可用:
在103服务器上:(将主机名和 IP 写入/usr/local/nginx/html/index.html文件)
[root@hadoop103 ~]# echo “hostname
ifconfig ens33 |sed -n 's#.*inet \(.*\)netmask.*#\1#p'
“ >/usr/local/nginx/html/index.html
[root@hadoop103 ~]# cd /usr/local/nginx/sbin/
[root@hadoop103 sbin]# ./nginx -s reload
在104服务器上:(将主机名和 IP 写入/usr/local/nginx/html/index.html文件)
[root@hadoop104 ~]# echo “hostname
ifconfig ens33 |sed -n 's#.*inet \(.*\)netmask.*#\1#p'
“ >/usr/local/nginx/html/index.html
[root@hadoop104 ~]# cd /usr/local/nginx/sbin/
[root@hadoop104 sbin]# ./nginx -s reload
浏览器访问虚拟IP:http://192.168.10.100/
[root@hadoop103 sbin]# ip address
将103主服务器上的 keepalived停掉,浏览器继续访问虚拟IP:http://192.168.10.100/
[root@hadoop103 sbin]# systemctl stop keepalived
[root@hadoop103 sbin]# systemctl start keepalived
[root@hadoop104 sbin]# ip address