Nginx负载均衡+Keepalived高可用集群
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx负载均衡+Keepalived高可用集群相关的知识,希望对你有一定的参考价值。
一、搭建环境及软件版本
负载均衡:Nginx 高可用:Keepalived
Linux:Centos 6.5
nginx:nginx-1.6.2
Keepalived:keepalived-1.2.13
LB-00:Nginx+Keepalived 192.168.174.40
LB-01:Nginx+Keepalived 192.168.174.41
Web1:LNMP 192.168.174.20
Web2:LAMP 192.168.174.30
二、负载均衡搭建
①解压编译安装Nginx
yum install -y pcre-devel openssl openssl-devel tar xf nginx-1.6.2.tar.gz cd nginx-1.6.2 ./configure --prefix=/application/nginx-1.6.3 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module make && make install ln -s /application/nginx-1.6.3/ /application/nginx cd /application/nginx/conf/
②修改nginx配置文件
upstream bbs_server_pools 后端real-server地址池
在nginx.conf中用include包含 单独的配置文件,单独server标签,
vim nginx.conf
worker_processes 1; events { worker_connections 1024; } http { log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘ ‘$status $body_bytes_sent "$http_referer" ‘ ‘"$http_user_agent" "$http_x_forwarded_for"‘; upstream bbs_server_pools{ server 192.168.174.20; server 192.168.174.30; } include mime.types; #include extra/lb_www.conf; include extra/lb_bbs.conf; #include extra/lb_blog.conf; }
cd extra/ touch lb_bbs.conf lb_www.conf lb_blog.conf vim lb_bbs.conf vim lb_blog.conf vim lb_www.conf
server { listen 80; server_name bbs.panda.org; location / { proxy_pass http://bbs_server_pools; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } }
③启动服务
/application/nginx/sbin/nginx
三、搭建高可用集群
LB-00 为主(master) LB-01 为备(backup)
①yum安装并配置keepalived
yum install keepalived -y vim /etc/keepalived/keepalived.conf
authentication 认证方式两边要一样
②启动服务,实现VIP自动漂移
/etc/init.d/keepalived start
③开发守护进程脚本(以负载均衡 80端口为例)
vim check_lb_nginx.sh
#!/bin/sh while true do if [ `netstat -lntup|grep nginx|wc -l` -ne 1 ];then /etc/init.d/keepalived stop fi sleep 5 done
后台运行守护脚本,停止主负载均衡的nginx,VIP立即切换
sh /home/scripts/check_lb_nginx.sh >/dev/null 2>&1 &
以上是关于Nginx负载均衡+Keepalived高可用集群的主要内容,如果未能解决你的问题,请参考以下文章
Nginx +keepalived+varnish+lamp实现高可用负载均衡集群