开源是一种精神,分享是一种美德!

Nginx+keepalived 配置负载均衡

Linux 爱疯熊猫 933℃ 0评论

安装依赖包

#yum -y install gcc pcre-devel zlib-devel openssl-devel

1、两台接入服务器分别安装NginX和keepalied:

#yum -y install nginx

按照以前提供的LNMP安装方法配置好,http://blog.sina.com.cn/s/blog_5fd841bf0101mwfz.html

keepalived安装,执行以下命令。

wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz
tar xzf keepalived-1.2.15.tar.gz
cd keepalived-1.2.15
./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/

cp /usr/local/sbin/keepalived /usr/sbin/
mkdir /etc/keepalived
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

chkconfig keepalived on

2、配置Nginx和keepalived

2.1 配置Nginx

两台接入服务器的NginX的配置完全一样,主要是配置/etc/nginx/conf.d/虚拟主机配置文件。其中多域名指向是通过虚拟主机(配置http下面的server)实现;同一域名的不同虚拟目录通过每个server下面的不同location实现;到后端的服务器在http下面配置upstream,然后在server或location中通过proxypass引用。要实现前面规划的接入方式,http的配置如下:

   upstream dev.hysec.com {
       server 50.1.1.21:80;
   }
   upstream www.hysec.com {
     ip_hash;
     server 50.1.1.10:80;
     server 50.1.1.11:80;
     server 50.1.1.12:80;
   }

   upstream oa.hysec.com {
     ip_hash;
     server 50.1.1.13:8080;
     server 50.1.1.14:8080;
    

   server {
       listen      80;
       server_name dev.hysec.com;
       location /svn {
           proxy_pass http://dev.hysec.com;
       }

       location /submin {
           proxy_pass http://dev.hysec.com;
       }
   }

   server {
       listen       80;
       server_name  www.hysec.com;
       location / {
           proxy_pass http://www.hysec.com;
       }
   server {
       listen       80;
       server_name  oa.hysec.com;
       location / {
           proxy_pass http://oa.hysec.com;
       }
3.2 配置keepalived
按照上面的安装方法,keepalived的配置文件在/etc/keepalived/keepalived.conf。主、从服务器的配置相关联但有所不同。如下:

global_defs {
  notification_email {
    admin@centos.bz
  }
  notification_email_from keepalived@domain.com
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL
}
vrrp_script chk_http_port {
               script “/opt/nginx_pid.sh”
               interval 2
               weight 2
}
vrrp_instance VI_1 {
   state MASTER        ############ 辅机为 BACKUP
   interface eth0
   virtual_router_id 51
   mcast_src_ip 192.168.1.103
   priority 102                  ########### 权值要比 back 高
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass 1111
   }
track_script {
       chk_http_port ### 执行监控的服务
       }
   virtual_ipaddress {
      192.168.1.110
   }
}

从服务器:

global_defs {
  notification_email {
    admin@centos.bz
  }
  notification_email_from keepalived@domain.com
  smtp_server 127.0.0.1
  smtp_connect_timeout 30
  router_id LVS_DEVEL
}
vrrp_script chk_http_port {
               script “/opt/nginx_pid.sh”
               interval 2
               weight 2
}
vrrp_instance VI_1 {
   state BACKUP
   interface eth0
   virtual_router_id 51
   mcast_src_ip 192.168.1.101
   priority 101              ##########权值 要比 master 低。。
   advert_int 1
   authentication {
       auth_type PASS
       auth_pass 1111
   }
track_script {
       chk_http_port ### 执行监控的服务
       }
   virtual_ipaddress {
      192.168.1.110
   }
}

之后分别在主从服务器建立nginx的监控脚本:
vi /opt/nginx_pid.sh
#!/bin/bash
A=`ps -C nginx –no-header |wc -l`              
if [ $A -eq 0 ];then                                      
               /usr/local/nginx/sbin/nginx
               sleep 3
               if [ `ps -C nginx –no-header |wc -l` -eq 0 ];then
                      killall keepalived
               fi
fi

然后分别启动主从服务器的keepalived:
service keepalived start
keepalived的测试
我们在主服务器上执行命令ip a,显示如下:
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
   link/ether 00:0c:29:aa:a1:e4 brd ff:ff:ff:ff:ff:ff
   inet 192.168.1.103/24 brd 255.255.255.255 scope global eth0
   inet 192.168.1.110/32 scope global eth0
证明主服务器已经绑定了虚拟ip 192.168.1.110
在从服务器上执行命令ip a,显示如下:
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
   link/ether 00:0c:29:2b:94:3b brd ff:ff:ff:ff:ff:ff
   inet 192.168.1.101/24 brd 255.255.255.255 scope global eth0
显示表明从服务器上没有绑定vip 192.168.1.110,只有本机真实ip192.168.1.101
下面我们停止主服务器的nginx进程,再看看ip绑定情况:
主服务器的情况:
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
   link/ether 00:0c:29:aa:a1:e4 brd ff:ff:ff:ff:ff:ff
   inet 192.168.1.103/24 brd 255.255.255.255 scope global eth0
从服务器的情况:
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
   link/ether 00:0c:29:2b:94:3b brd ff:ff:ff:ff:ff:ff
   inet 192.168.1.101/24 brd 255.255.255.255 scope global eth0
   inet 192.168.1.110/32 scope global eth0
由此可见vip已经指向了从服务器。

转载请注明:LINUX! » Nginx+keepalived 配置负载均衡

喜欢 (0)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址