haproxy+keepalived构建高可用负载均衡

web1 IP 192.168.0.47

web2 IP 192.168.0.48

haproxy_master 192.168.0.200

haproxy_backup 192.168.0.199

VIP 192.168.0.155

一.安装keepalived

 

  • #wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz
  • #tar zxvf keepalived-1.1.15.tar.gz
  • #cd keepalived-1.1.15
  • #./configure
  • #make
  • #make install 将keepalived做成启动脚务

     

    #cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/

    #cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/

    #mkdir /etc/keepalived

    #cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/

    #cp /usr/local/sbin/keepalived /usr/sbin/

    配置keepalived.conf

    #vim /etc/keepalived/keepalived.conf

        
        
        
        
    1. print?! Configuration File for keepalived    
    2.     
    3. global_defs {    
    4.    notification_email {    
    5.      [email protected]    
    6.    }    
    7.    notification_email_from [email protected]    
    8.    smtp_server 192.168.200.1    
    9.    smtp_connect_timeout 30    
    10.    router_id LVS_DEVEL    
    11. }    
    12.     
    13. vrrp_instance VI_1 {    
    14.     state MASTER    #备用服务器上改为 BACKUP    
    15.     interface eth0    
    16.     virtual_router_id 51    
    17.     priority 100    #备用服务器上改为99    
    18.     advert_int 1    
    19.     authentication {    
    20.         auth_type PASS    
    21.         auth_pass 1111    
    22.     }    
    23.     virtual_ipaddress {    
    24.         192.168.0.155/24 dev eth0 scope global    
    25.     }    

启动keepalived

 

service keepalived start

启动之后可以用ip add 查看网卡多了一条192.168.0.155的IP

eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:ff:6c:c7 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.200/24 brd 192.168.0.255 scope global eth0
inet 192.168.0.155/24 scope global secondary eth0
inet6 fe80::20c:29ff:feff:6cc7/64 scope link
valid_lft forever preferred_lft forever

说明keepalived启动成功,配置生效

二.安装haproxy

#wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.20.tar.gz

#uname -a

Linux ip1 2.6.18-194.el5 #1 SMP Fri Apr 2 14:58:35 EDT 2010 i686 i686 i386 GNU/Linux

#make TARGET=linux26 prefix=/usr/local/haproxy install
#cd /usr/local/haproxy

配置haproxy
#vim haproxy.conf

  
  
  
  
  1. [php] view plaincopyprint?global    
  2.      maxconn  5120    
  3.      chroot   /usr/local/haproxy    
  4.      uid      100    
  5.      gid      100    
  6.      daemon    
  7.      quiet    
  8.      nbproc   2    
  9.      pidfile  /usr/local/haproxy/haproxy.pid    
  10.  ########################################################    
  11.     
  12. defaults    
  13.      log     global    
  14.      log     127.0.0.1  local3    
  15.      mode    http    
  16.      option  httplog    
  17.      option  dontlognull    
  18.      option  redispatch    
  19.      retries 3    
  20.     
  21.     maxconn 3000    
  22.      contimeout 5000    
  23.      clitimeout 50000    
  24.      srvtimeout 50000    
  25.      stats enable    
  26.      stats uri /admin    
  27.      stats auth admin:admin    
  28.      stats realm Haproxy \ statistic    
  29.  ##########################################################    
  30.     
  31. frontend www.gaoming.com    
  32.      bind *:80    
  33.      default_backend server_pool    
  34. backend server_pool    
  35.      mode    http    
  36.      balance roundrobin    
  37.      option  forwardfor    
  38.      option  httpclose    
  39.      option  httpchk GET /index.html    
  40.  server web1 192.168.0.48:80 check inter 5000 fall 1 rise 2    
  41.  server web1 192.168.0.47:80 check inter 5000 fall 1 rise 2 

haproxy的配置2台机器相同

 

启动haproxy

#/usr/local/haproxy/sbin/haproxy -f /usr/local/haproxy/haproxy.conf

域名解析到VIP上,用户访问VIP会负载到后端2台WEB上

测试高可用,关掉主haproxy,备用的会接管VIP

你可能感兴趣的:(linux,高可用,负载均衡)