码迷,mamicode.com
首页 > 其他好文 > 详细

keepalived双机实现高可用和负载均衡

时间:2014-09-02 18:08:35      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:keepalived+lvs

安装keepalived

# ./configure --sysconf=/etc --with-kernel-dir=/usr/src/kernels/2.6.32-358.el6.x86_64/ && make && make install
#ln  -s  /usr/local/sbin/keepalived /sbin/
# chkconfig  keepalived  on


修改网卡配置

# vim /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.1.2
NETMASK=255.255.255.255
# If you‘re having problems with gated making 127.0.0.0/8 a martian,
# you can change this to something else (255.255.255.255, for example)
BROADCAST=192.168.1.2
ONBOOT=yes

 

修改内核文件配置

# vim /etc/sysctl.conf
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
# sysctl -p

 

修改keepalived配置文件

# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
 
vrrp_script chk_mysql_port {
                script "/var/shell/mysql_check.sh"
                interval 2
                weight 2
}
global_defs {
   notification_email {
        445970860@qq.com
   }
 
   notification_email_from root@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
 
vrrp_instance VI_1 {
    state MASTER    (BACKUP备用)
    interface eth0
    virtual_router_id 51
    priority 100     (backup须优先级低于master)
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
                chk_mysql_port
        }
    virtual_ipaddress {
        192.168.1.2
    }
 
}
virtual_server 192.168.1.2 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    nat_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP
 
    real_server 192.168.1.1 80 {
        weight 3
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
    real_server 192.168.1.3 80 {
        weight 3
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}


编写脚本(监控mysql,当mysql出现问题不能重新启动时停止nginx服务,keepalived服务会将出现问题的web服务器从集群中剔除出去)

# vim mysql_check.sh
#!/bin/bash
MYSQL_SLAVE=`ps -C mysqld | grep -v PID | wc -l`
if [ $MYSQL_SLAVE -eq 0 ];then
        /etc/init.d/mysqld restart
        sleep 3
        if [ `ps -C mysqld | grep -v PID | wc -l` -eq 0 ];then
                pkill -9 nginx
        fi
fi


keepalived双机实现高可用和负载均衡

标签:keepalived+lvs

原文地址:http://8588192.blog.51cto.com/8578192/1547871

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!