码迷,mamicode.com
首页 > 其他好文 > 详细

centos7安装配置nginx+keepalived集群

时间:2018-06-13 19:36:37      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:安装配置nginx+keepalived

关闭防火墙(如果因为其他原因不关闭,也请不要禁止80端口):systemctl stop firewalld.service
禁止防火墙自动启动:systemctl disable firewalld.service
查看系统版本
cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
查看内核
uname -r
3.10.0-862.3.2.el7.x86_64
跟新系统及系统包
更新yum:yum update -y

安装基础包
yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

安装nginx
(下载路径自己选择)
下载nginx1.14.0稳定版本
wget http://nginx.org/download/nginx-1.14.0.tar.gz
解压:tar -zxvf nginx-1.14.0.tar.gz -C /usr/local/
进入解压好后的源码目录:cd /usr/local/nginx-1.14.0/
使用默认配置:./configure
编译并安装nginx:make && make install
启动nginx:/usr/local/nginx/sbin/nginx
快速停止nginx:/usr/local/nginx/sbin/nginx -s stop
优雅停止nginx(会将正在处理中的请求处理完毕):/usr/local/nginx/sbin/nginx -s quit
检测配置是否错误:/usr/local/nginx/sbin/nginx -t
重新加载配置:/usr/local/nginx/sbin/nginx -s reload

安装keepalived
yum -y install keepalived

vi /etc/keepalived/keepalived.conf
master配置文件如下
! Configuration file for keepalived
global_defs {
notification_email {br/>admin@wf.com
<="">}
notification_email_from admin@wf.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
#监控nginx
vrrp_script chk_nginx {
script "/etc/keepalived/monitor_nginx.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER #主从设置 MASTER
interface ens32 #网卡名
virtual_router_id 100
mcast_src_ip 192.168.254.128 #本机ip
priority 51 #从机小于主机
advert_int 1
authentication {
auth_type PASS
auth_pass chtopnet
}
virtual_ipaddress {
192.168.254.88 #VIP 的IP
}
track_script {
chk_nginx #检测脚本
}
}

backup配置文件如下
! Configuration file for keepalived
global_defs {
notification_email {br/>admin@wf.com
<="">}
notification_email_from admin@wf.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
#监控NGINX
vrrp_script chk_nginx {
script "/etc/keepalived/monitor_nginx.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP #主从设置 MASTER
interface ens32 #网卡名
virtual_router_id 100
mcast_src_ip 192.168.254.129 #本机ip
priority 50 #从机小于主机
advert_int 1
authentication {
auth_type PASS
auth_pass chtopnet
}
virtual_ipaddress {
192.168.254.88 #VIP 的IP
}
track_script {
chk_nginx #检测脚本
}
}

监测脚本如下(相同)
vi /etc/keepalived/monitor_nginx.sh
#!/bin/bash
a=ps -C nginx --no-header | wc -l
if [ $a -eq 0 ];then
/usr/local/nginx/sbin/nginx
if [ ps -C nginx --no-header| wc -l -eq 0 ];then
systemctl stop keepalived
fi
fi

在两台nginx分别执行脚本,绑定虚拟IP,抑制ARP广播
vi ip-config
#!/bin/bash
#description: Config realserver
VIP=192.168.254.88 #绑定你要设置的VIP
/etc/rc.d/init.d/functions
case "$1" in
start)
/sbin/ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/sbin/route add -host $VIP dev lo:0
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
sysctl -p >/dev/null 2>&1
echo "RealServer Start OK"
;;
stop)
/sbin/ifconfig lo:0 down
/sbin/route del $VIP >/dev/null 2>&1
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "RealServer Stoped"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
执行绑定脚本(脚本随便放哪里都可以)
./ip-config start
之后就是测试环境了!

centos7安装配置nginx+keepalived集群

标签:安装配置nginx+keepalived

原文地址:http://blog.51cto.com/10158955/2129046

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!