使用keepalived来实现nginx的高可用
2016-01-30 11:42
585 查看
本次配置的环境如下
Linux: centos7.0 virtual ip: 192.168.30.50 master: 192.168.30.61 backup: 192.168.30.62
1、下载keepalived
cd /usr/local/src wget http://keepalived.org/software/keepalived-1.2.19.tar.gz[/code]2、解压安装
tar -zxvf /usr/local/keepalived-1.2.19.tar.gz cd keepalived-1.2.19 ./configure --prefix=/usr/local/keepalived make && make install
如果没有安装openssl-devel的话会报错!!! OpenSSL is not properly installed on your system. !!! !!! Can not include OpenSSL headers files."
使用yum -y install openssl-devel安装openssl-devel 解决这个问题cd /usr/local/keepalived cp sbin/keepalived /usr/sbin/ cp etc/keepalived/keepalived.conf /etc/keepalived/ cp etc/sysconfig/keepalived /etc/sysconfig/ cp etc/rc.d/init.d/keepalived /etc/init.d/3、编写监控脚本
mkdir /usr/local/keepalived/scripts/ -p touch /usr/local/keepalived/scripts/monitor_nginx.sh vi /usr/local/keepalived/scripts/monitor_nginx.sh
脚本内容如下# 监控nginx进程,若nginx主进程不存在则启动nginx # 若5s后nginx进程还是不存在的话kill掉keepalived进程,防止nginx没运行该主机的keepalived还接管虚拟IP #!/bin/bash if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ] then /usr/local/nginx/sbin/nginx sleep 5 if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ] then killall keepalived fi fi
增加脚本的执行权限chmod +x /usr/local/keepalived/scripts/monitor_nginx.sh4、配置master
vi /etc/keepalived/keepalived.conf,master的配置文件如下global_defs { notification_email { example@foxmail.com } notification_email_from example@foxmail.com smtp_server 127.0.0.1 stmp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script Monitor_Nginx { # 监控脚本 script "/usr/local/keepalived/scripts/monitor_nginx.sh" interval 2 weight 2 } vrrp_instance VI_1 { state MASTER interface enp0s3 #网卡名称,根据实际情况配置 virtual_router_id 51 priority 100 advert_int 1 track_interface { enp0s3 } authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.30.50 } track_script { Monitor_Nginx } }5、配置backup
vi /etc/keepalived/keepalived.confglobal_defs { notification_email { example@foxmail.com } notification_email_from example@foxmail.com smtp_server 127.0.0.1 stmp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script Monitor_Nginx { script "/usr/local/keepalived/scripts/monitor_nginx.sh" interval 2 weight 2 } vrrp_instance VI_1 { state BACKUP interface enp0s3 virtual_router_id 51 priority 100 advert_int 1 track_interface { enp0s3 } authentication { auth_type PASS auth_pass 123456 } virtual_ipaddress { 192.168.30.50 } track_script { Monitor_Nginx } }
至此,keepalived已配置完成,在主备两机器上使用service keepalived start启动keepalived
使用ip a命令查看keepalived是否起作用enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 08:00:27:c2:6c:02 brd ff:ff:ff:ff:ff:ff inet 192.168.30.61/24 brd 192.168.30.255 scope global enp0s3 valid_lft forever preferred_lft forever inet ***192.168.30.50***/32 scope global enp0s3 valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:fec2:6c02/64 scope link valid_lft forever preferred_lft forever
查看系统日志tail -n 100 /var/log/messages |grep KeepalivedJan 29 00:02:33 localhost Keepalived_vrrp[3693]: VRRP_Instance(VI_1) Transition to MASTER STATE Jan 29 00:02:34 localhost Keepalived_vrrp[3693]: VRRP_Instance(VI_1) Entering MASTER STATE Jan 29 00:02:34 localhost Keepalived_vrrp[3693]: VRRP_Instance(VI_1) setting protocol VIPs. Jan 29 00:02:34 localhost Keepalived_vrrp[3693]: VRRP_Instance(VI_1) Sending gratuitous ARPs on enp0s3 for 192.168.30.50
测试的时候可以在nginx的默认index页面加入ip信息方便观察
nginx的默认页面所在目录是$NGINX_HOME/html/index.html
相关文章推荐
- nginx+lua实现IP黑白名单机制
- 详解Nginx与Apache共用80端口的配置方法
- iOS ffmpeg 之Linux下搭建rtmp nginx服务器
- CentOS配置Nginx SLL
- Nginx配置
- ubuntu 安装nginx
- aio nginx gfs
- nginx服务器请求建立链接的过程
- CentOS6.5 Nginx安装
- centos下解决nginx 无法连接gitlab socket
- Nginx配置文件nginx.conf中文详解
- Nginx基础
- LNMP环境搭建-nginx
- Nginx+Varnish 实现动静分离,为服务器分流,降低服务器负载
- Nginx高并发配置思路(轻松应对1万并发量)
- Nginx基础入门之gzip常用配置项说明
- nginx添加第三方模块
- Apache和Nginx对比
- Nginx环境搭建步骤
- nginx 访问 php 502