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 &

  

 

转载于:https://www.cnblogs.com/Panda-python/p/6905249.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值