Nginx+keepalive 负载均衡
1 规划和准备
|
两台相同配置的web |
|
|
用途 |
IP |
|
MASTER |
192.168.1.100 |
|
BACKUP |
192.1681.101 |
2 安装
两台接入服务器分别安装NginX和keepalived:
准备依赖包:
yum -y install gcc pcre-devel zlib-devel openssl-devel
yum -y install popt-devel
下载
wget http://nginx.org/download/nginx-1.2.4.tar.gz
wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz
安装NginX
安装keepalive
tar zxvf keepalived-1.2..tar.gz
cd keepalived-1.2.
./configure
make
make install cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/sbin/keepalived /usr/sbin/
加入启动服务
echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local
echo "/etc/init.d/keepalived start" >> /etc/rc.local
3 配置
3.1 配置NginX
两台接入服务器的NginX的配置完全一样,主要是配置/usr/local/nginx/conf/nginx.conf的http。其中多域名指向是通过虚拟主机(配置http下面的server)实现;同一域名的不同虚拟目录通过每个server下面的不同location实现;到后端的服务器在http下面配置upstream,然后在server或location中通过proxypass引用。要实现前面规划的接入方式,http的配置如下:
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
upstream dev.hysec.com {
server 50.1.1.21:;
}
upstream opslinux.com {
ip_hash;
server 192.168.1.102:
server 192.168.1.103:
server 192.168.1.104:
}
server {
listen ;
server_name opslinux.com;
location / {
proxy_pass http://opslinux.com;
}
}
验证方法:
首先用IP访问前表中各个应用服务器的url
再用域名和路径访问前表中各个应用系统的域名/虚拟路径
3.2 配置keepalived
按照上面的安装方法,keepalived的配置文件在/etc/keepalived/keepalived.conf。主、从服务器的配置相关联但有所不同。如下:
Master:
! Configuration File for keepalived
global_defs {
router_id NGINX_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id
priority
advert_int
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.168.1.100
}
}
Backup:
! Configuration File for keepalived
global_defs {
router_id NGINX_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id
priority
advert_int
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.168.1.100
}
}
验证:
先后在主、从服务器上启动keepalived: /etc/init.d/keepalived start
在主服务器上查看是否已经绑定了虚拟IP: ip addr
停止主服务器上的keepalived: /etc/init.d/keepalived stop 然后在从服务器上查看是否已经绑定了虚拟IP:
启动主服务器上的keepalived,看看主服务器能否重新接管虚拟IP
3.3 让keepalived监控NginX的状态
经过前面的配置,如果主服务器的keepalived停止服务,从服务器会自动接管VIP对外服务;一旦主服务器的keepalived恢复,会重新接管VIP。 但这并不是我们需要的,我们需要的是当NginX停止服务的时候能够自动切换。
keepalived支持配置监控脚本,我们可以通过脚本监控NginX的状态,如果状态不正常则进行一系列的操作,最终仍不能恢复NginX则杀掉keepalived,使得从服务器能够接管服务。
如何监控NginX的状态
最简单的做法是监控NginX进程,更靠谱的做法是检查NginX端口,最靠谱的做法是检查多个url能否获取到页面。
如何尝试恢复服务
如果发现NginX不正常,重启之。等待3秒再次校验,仍然失败则不再尝试。
根据上述策略很容易写出监控脚本。这里使用nmap检查nginx端口来判断nginx的状态,记得要首先安装nmap。监控脚本如下:
#!/bin/bash
# check nginx server status
NGINX=/usr/local/nginx/sbin/nginx
PORT= nmap localhost -p $PORT | grep "$PORT/tcp open"
#echo $?
if [ $? -ne ];then
$NGINX -s stop
$NGINX
sleep
nmap localhost -p $PORT | grep "$PORT/tcp open"
[ $? -ne ] && /etc/init.d/keepalived stop
fi
不要忘了设置脚本的执行权限,否则不起作用。
假设上述脚本放在/opt/chk_nginx.sh,则keepalived.conf中增加如下配置:
主keepalived
vrrp_script chk_http_port {
script "/opt/chk_nginx.sh"
interval
weight -
} track_script {
chk_http_port
} 例子:
! Configuration File for keepalived global_defs {
router_id NGINX_UPSTEAM
} vrrp_script chk_http_port {
script "/opt/chk_nginx.sh"
interval
weight -
} vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id
priority
advert_int
authentication {
auth_type PASS
auth_pass
}
virtual_ipaddress {
192.168.1.100
} track_script {
chk_http_port
} }
更进一步,为了避免启动keepalived之前没有启动nginx , 可以在/etc/init.d/keepalived的start中首先启动nginx:
start() {
/usr/local/nginx/sbin/nginx
sleep
echo -n $"Starting $prog: "
daemon keepalived ${KEEPALIVED_OPTIONS}
RETVAL=$?
echo
[ $RETVAL -eq ] && touch /var/lock/subsys/$prog
}
Nginx+keepalive 负载均衡的更多相关文章
- [转载] nginx的负载均衡
原文:http://www.srhang.me/blog/2014/08/27/nginx-loabbalance/ Nginx负载均衡 一.特点 1.1 应用情况 Nginx做为一个强大的Web服务 ...
- Nginx+Keepalived负载均衡高可用
Nginx+Keepalived负载均衡高可用方案: Nginx 使用平台:unix.linux.windows. 功能: A.www web服务 http 80 b.负载均衡(方向代理proxy) ...
- 死磕nginx系列--使用nginx做负载均衡
使用nginx做负载均衡的两大模块: upstream 定义负载节点池. location 模块 进行URL匹配. proxy模块 发送请求给upstream定义的节点池. upstream模块解读 ...
- Nginx安装负载均衡配置 fair check扩展
前言 本文主要是针对Nginx安装.负载均衡配置,以及fair智能选举.check后端节点检查扩展功能如何扩展,进行讲解说明. fair模块: upstream-fair,“公平的”Nginx 负载均 ...
- 【nginx】配置Nginx实现负载均衡
一文中已经提到,企业在解决高并发问题时,一般有两个方向的处理策略,软件.硬件,硬件上添加负载均衡器分发大量请求,软件上可在高并发瓶颈处:数据库+web服务器两处添加解决方案,其中web服务器前面一层最 ...
- 配置Nginx实现负载均衡
在关于高并发负载均衡一文中已经提到,企业在解决高并发问题时,一般有两个方向的处理策略,软件.硬件,硬件上添加负载均衡器分发大量请求,软件上可在高并发瓶颈处:数据库+web服务器两处添加解决方案,其中w ...
- Keepalived+Nginx实现负载均衡高可用
一.负载均衡高可用 Nginx作为负载均衡器,所有请求都到了Nginx,可见Nginx处于非常重点的位置,如果Nginx服务器宕机后端web服务将无法提供服务,影响严重. 为了避免负载均衡服务器的宕机 ...
- Nginx四层负载均衡概述
目录 Nginx四层负载均衡概述 什么是负载均衡 负载均衡应用场景 四层,七层集群架构 四层负载均衡总结 Nginx如何配置四层负载均衡 nginx四层负载均衡端口转发 Nginx四层负载均衡概述 什 ...
- nginx实现负载均衡、缓存功能实战
nginx实现负载均衡.缓存功能实战 什么是正向代理?应用场景:翻墙 什么是反向代理?例如:haproxy和nginx Nginx实现反向代理 nginx代理基于是ngx_http_proxy_m ...
随机推荐
- halcon控制显示精度(精确到小数点后6位,精度足够了)
实践应用 set_tposition (WindowHandle3,50, 50) write_string (WindowHandle3, '半径 D1=' +Ra[i]$'#f') set_tpo ...
- parentNode,parentElement,offsetParent
offsetParent直接的将是影响元素位置的上级element,而parentElement与位置显示无关时dom中的上级element. 例如: <BODY> <div sty ...
- nginx 配置ajax跨域访问php接口
在nginx.conf里面,找到server项,并在里面添加如下配置 location ~ \.php?($|/) { #try_files $uri =; #handel cosr by mao a ...
- webHttpBinding、basicHttpBinding和wsHttpBinding区别
webHttpBinding is the REST-style binding, where you basically just hit a URL and get back a truckloa ...
- Node.js的优点和缺点(转载)
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:FengqiAsia链接:http://www.zhihu.com/question/19653241/answer/1599 ...
- for 续7
----------siwuxie095 for 中的变量: FOR 变量参照的替换已被增强.您现在可以使用下列选项语法: ~I - 删除任何引号(" ...
- ICG游戏:证明,先手不是必胜就是必败。
简介: ICG游戏:Impartial Combinatorial Games,公平的组合游戏. 以下是定义,来自网络,可能不够严谨: 1.两名选手:2.两名选手轮流行动,每一次行动可以在有限合法操作 ...
- eigen安装
https://blog.csdn.net/liuxiaoheng1992/article/details/54410148
- android studio使用真机测试时点击Debug调试模式时报Error running app:No target device found,点击运行模式却是启动正常的
原因是adb没检测到设备(包括真机和虚拟机). 在Terminal执行adb devices命令,查看有没有连接到的设备. 如果没有设备,确认虚拟机是否正确打开,真机是否连接打开USB调试并安装驱动. ...
- jquery怎么根据后台传过来的值动态设置下拉框、单选框选中
$(function(){ var sex=$("#sex").val(); var marriageStatus=$("#marriageStatus").v ...