Nginx+Keepalived在Ubuntu上实现负载均衡

keepon 2011-02-17

一、nginx反向代理配置

打开/etc/nginx/nginx.conf配置文件,在http下加入:

upstream ip{  #对外虚拟IP

server IP1;

server IP2;

}

server{

listen 80;

server_name ip; #i对外虚拟IP

index index.php;index.html;index.htm;

location / {

  proxy_pass http://ip;

}

}

二、keepalived的配置

1安装Keepalived

sudo apt-get install keepalived

2配置keepalived

将IP1作为负载均衡的主服务器,将IP2作为“从服务器”。这是通过配置文件中的priority来实现的。在“主服务器”上设置为:101,“从服务器”上设置为:100.

在IP1上,设置/etc/keepalived/keepalived.conf

$ sudo nano /etc/keepalived/keepalived.conf

vrrp_script chk_haproxy{

      script "killall -0 haproxy"

      interval 2

      weight 2

}

vrrp_instance VI_1 {

       interface eth0

       state MASTER

       virtual_router_id 51

      priority 101   #101为“主”,100为“从”

     virtual_ipaddress {

       ip    #对外虚拟IP    

}

track_script {

       chk_haproxy

       }

}

运行keepalived服务

suto /etc/init.d/keepalived start

IP2和IP1的配置基本上一样,只需要把优先级该成:100

相关推荐