文章目录
- 1.Keepalived+Nginx 高可用集群 (主从模式)
- 2.实验环境
- 3.配置高可用的准备工作
- 3.1.准备两台测试服务器
- 3.2.在两台服务器安装 nginx
- 3.3.在两台服务器安装 keepalived
- 4 .完成高可用配置(主从配置)
- 5.最终测试
1.Keepalived+Nginx 高可用集群 (主从模式)
集群架构图:
2.实验环境
(1)需要两台 nginx 服务器
(2)需要 keepalived
(3)需要虚拟 ip
3.配置高可用的准备工作
(1)需要两台服务器 192.168.70.128 和 192.168.70.130
(2)在两台服务器安装 nginx
(3)在两台服务器安装 keepalived
3.1.准备两台测试服务器
192.168.70.128
192.168.70.130
3.2.在两台服务器安装 nginx
192.168.70.128
192.168.70.130
3.3.在两台服务器安装 keepalived
(1)使用 yum 命令进行安装
yum install keepalived –y
(2)安装之后,在etc 里面生成目录 keepalived,有文件 keepalived.conf
[root@centos2 init.d]
[root@centos2 etc]
keepalived.conf
4 .完成高可用配置(主从配置)
(1)修改/etc/keepalived/keepalived.conf
192.168.70.128
global_defs {notification_email {acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 192.168.70.128smtp_connect_timeout 30router_id LVS_DEVEL
}vrrp_script chk_http_port {script "/usr/local/src/nginx_check.sh"interval 2 weight 2
}vrrp_instance VI_1 {state MASTER interface ens33 virtual_router_id 51 priority 90 advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.70.135 }
}
192.168.70.130
global_defs {notification_email {acassen@firewall.locfailover@firewall.locsysadmin@firewall.loc}notification_email_from Alexandre.Cassen@firewall.locsmtp_server 192.168.70.130smtp_connect_timeout 30router_id LVS_DEVEL
}vrrp_script chk_http_port {script "/usr/local/src/nginx_check.sh"interval 2 weight 2
}vrrp_instance VI_1 {state BACKUP interface ens33 virtual_router_id 51 priority 80 advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.70.135 }
}
(2 )在/usr/local/src添加脚本
192.168.70.128
#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
/opt/myapp/soft/tengine-2.1.0/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
[root@centos2 src]
nginx_check.sh
[root@centos2 src]
[root@centos2 src]
nginx_check.sh
192.168.70.130
#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
/opt/myapp/soft/tengine-2.1.0/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
[root@centos3 sbin]
[root@centos3 src]
[root@centos3 src]
[root@centos3 src]
nginx_check.sh
[root@centos3 src]
[root@centos3 src]
total 4
-rwxr-xr-x. 1 root root 194 Apr 13 01:04 nginx_check.sh
(3 )把两台服务器上 nginx 和 和 keepalived 启动
启动 nginx :
service nginx reload
启动 keepalived :
systemctl start keepalived.service
5.最终测试
192.168.70.128
192.168.70.130
(1)在浏览器地址栏输入 虚拟 ip 地址 http://192.168.70.135/
(2)把主服务器(192.168.70.128 )nginx 和 和 keepalived 停止,再输入http://192.168.70.135/
[root@centos2 src]
Stopping nginx (via systemctl): [ OK ]
[root@centos2 src]
至此为止,Nginx的高可用搭建完毕!