文章目录
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]# cd /etc/
[root@centos2 etc]# ls keep*
keepalived.conf
4 .完成高可用配置(主从配置)
(1)修改/etc/keepalived/keepalived.conf
192.168.70.128
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.70.128
smtp_connect_timeout 30
router_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 # 备份服务器上将 MASTER 改为 BACKUP
interface ens33 # 网卡
virtual_router_id 51 # 主、备机的 virtual_router_id 必须相同
priority 90 # 主、备机取不同的优先级,主机值较大,备份机值较小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.70.135 # VRRP H 虚拟地址
}
}
192.168.70.130
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.70.130
smtp_connect_timeout 30
router_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 # 备份服务器上将 MASTER 改为 BACKUP
interface ens33 # 网卡
virtual_router_id 51 # 主、备机的 virtual_router_id 必须相同
priority 80 # 主、备机取不同的优先级,主机值较大,备份机值较小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.70.135 # VRRP H 虚拟地址
}
}
(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]# ls
nginx_check.sh
[root@centos2 src]# chmod +755 nginx_check.sh
[root@centos2 src]# ls
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]# cd /usr/local/src
[root@centos3 src]# ls
[root@centos3 src]# vi nginx_check.sh
[root@centos3 src]# ls
nginx_check.sh
[root@centos3 src]# chmod +755 nginx_check.sh
[root@centos3 src]# ll
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]# service nginx stop
Stopping nginx (via systemctl): [ OK ]
[root@centos2 src]# systemctl stop keepalived.service


至此为止,Nginx的高可用搭建完毕!
1605



被折叠的 条评论
为什么被折叠?



