keepalived+nginx的主备架构模式,始终会有一台服务器处于空闲状态,可以通过keepalived+nginx主主架构来实现两台服务器的合理利用,主主服务器对外有两个VIP地址来接收客户端的请求。
环境192.168.2.118 nginx主/keepalived-master1 192.168.2.119 nginx从/keepalived-master2 192.168.2.112 lap
配置主主在上一章已经在118和119服部署好了keepalived服务。接下来配置双主结构 备份好118和119的keepalived配置文件
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak118的keepalived配置文件内容如下
! Configuration File for keepalived global_defs { notification_email { abc@163.com } notification_email_from abc@163.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script chk_nginx { script "/data/sh/check_nginx.sh" interval 2 weight 2 } vrrp_instance VI_1 { state MASTER interface eth0 lvs_sync_daemon_inteface eth0 virtual_router_id 50 priority 100 advert_int 5 nopreempt authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.2.130 } track_script { chk_nginx } } vrrp_instance VI_2 { state BACKUP interface eth0 lvs_sync_daemon_inteface eth0 virtual_router_id 51 priority 90 advert_int 5 nopreempt authentication { auth_type PASS auth_pass 2222 } virtual_ipaddress { 192.168.2.131 } track_script { chk_nginx } }119服的keepalived配置文件参考上面的参数,把V1的state改成BACKUP,优先级priority改成90,把V2的state改成MASTER,优先级priority改成100
nginx检测脚本check_nginx.sh(注意执行权限chmod u+x check_nginx.sh)
#!/bin/bash #2017年12月5日21:50:40 #auto check nginx #by lee ################### killall -0 nginx if [[ $? -ne 0 ]];then /etc/init.d/keepalived stop fi启动118和119的keepalived服务并查看日志
service keepalived start tail -f /var/log/messages可以看到进入了master
查看ip
ip addr list118服:
119服:
浏览器分别访问:
手动停止118的nginx服务,然后再访问192.168.2.130,可以正常的打开web界面
查看VIP,可以看到VIP:192.168.2.130移到了119服下:
再次手动启动118的服务
[root@mysqlm keepalived]# /usr/local/nginx/sbin/nginx [root@mysqlm keepalived]# service keepalived start可以看到VIP:192.168.2.130又返回到118服下
至此keepalived+nginx双主结构配置完成。