RHEL6平台Keepalived+LVS+iSCSI+GFS搭建高可用负载均衡(2)


分别在 realserver 上执行以下命令
vi /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.lo.arp_announce = 2
sysctl -p
ifconfig eth0:0 192.168.0.200 netmask 255.255.255.255 up
route add -host 192.168.0.200 dev eth0:0

vi /etc/rc.local
ifconfig eth0:0 192.168.0.200 netmask 255.255.255.255 up
route add -host 192.168.0.200 dev eth0:0


echo `hostname` > /var/www/html/index.html
service httpd start
测试:
访问 ,看到页面在两个 realserver 上切换表示成功!
你也可以通过 ipvsadm -Lnc 查看详细连接情况!
-------------------------------------------------------------------------------------------------------------------
二、mysql服务器
-------------------------------------------------------------------------------------------------------------------
mysql双主高可用
-------------------------------------
系统环境:
RHEL6.0_I386
VIP 192.168.0.51
real server1 192.167.0.7
real server2 192.168.0.8
-------------------------------------
1.server1和server2安装好mysql并修改配置文件:
yum install mysql-server
vi /etc/my.cnf:
[mysqld]
log-bin=MySQL-bin
server-id=1 ##在server2上配置为server-id=2

-------------------------------------
2.server1和server2相互设置为主从同步,(双主).
server1:
mysql> grant replication slave on *.* to 'cluster'@'%' identified by 'cluster';
mysql> show master status;
-------------------------
MySQL-bin.000001   236
-------------------------
------------------------------------
server2:
mysql> change master to
    -> master_host='192.168.0.7',
    -> master_user='cluster',
    -> master_password='cluster',
    -> master_log_file='MySQL-bin.000001',
    -> master_log_pos=236;

mysql> start slave;
mysql> show slave status;
在server2和server1上执行相反的操作,使其互为主从.
--------------------------------------
3.安装软件包
yum install gcc popt-devel kernel-devel openssl-devel ipvsadm make
tar xf keepalived-***.tar.gz
cd keepalived-*

./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-71.el6.i686
make && make install

modprobe ip_vs #此处如果系统没有自动加载此模块将导致keepalived启动后无法找到lvs负载均衡协议
mkdir /etc/keepalived/
ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
ln -s /usr/local/keepalived/bin/* /bin/
ln -s /usr/local/keepalived/sbin/* /sbin/
---------------------------------------------------
4.修改server1/server2配置文件
server1:
vi /etc/keepalived/keepalived.conf:


! Configuration File for keepalived

global_defs {
   notification_email {
     root@example.com
   }
   notification_email_from root@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id MYSQL-HA ##确保和server2相同
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51 ##确保和server2相同,同网内不同集群此项必须不同,否则发生冲突
    priority 100   ##此处server2上设置为50
    advert_int 1
    nopreempt  ##不抢占,只在priority高的server1上设置,server2上此项注释掉
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.0.51
    }
}

virtual_server 192.168.0.51 3306 {
    delay_loop 2
    lb_algo wrr
    lb_kind DR
    persistence_timeout 60
    protocol TCP

real_server 192.168.0.7 3306 {
##server2上此处改为192.168.0.8(即server2本机ip)
        weight 3
        notify_down /usr/local/keepalived/bin/mysql.sh
            TCP_CHECK {
            connect_timeout 10
            nb_get_retry 3
            delay_before_retry 3
            connect_port 3306
        }
    }
}
--------------------------------
server1和server2上都添加此检测脚本,作用是当mysql停止工作时自动关闭本机的keepalived
从而实现将故障机器踢出(因每台机器上keepalived只添加了本机为realserver).
vi /usr/local/keepalived/bin/mysql.sh:
#!/bin/sh
pkill keepalived
--------------------------------
vi /etc/rc.local:
modprobe ip_vs ##此模块如果无法自动加载则需手动加载
--------------------------------
server1和server2启动keepalived守护进程.
/etc/init.d/keepalived start

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/ppydp.html