首页 > 代码库 > Nginx负载均衡+Keepalived高可用集群
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高可用集群
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。