必发bf88 2

mysql+keepalive实现浮动地址自动切换,由于keepalive无自带健康检查功能,所以必须自动编写健康检查守护进程(监控DB1和DB2数据库的监控状态,来保证浮动地址双机自动切换。)

Linux下快速设定ip bond,linux设定ipbond

 在计算机网路普及的初期,很多OS系统都使用的为单网卡方式,即一个网卡使用一个IP地址。随着网络要求的不断提高,我们可以对多个网卡进行绑定聚合当一个逻辑网络接口来使用,从而大幅提升服务器的网络吞吐(I/O),同时也可以根据不同的场景和需求来设置其绑定模式。本文简要描述一下其配置过程。

 

1. 编辑ifcfg-bond0文件
###本文演示环境CentOS release 5.11,对eth0以及eth1进行bond
# vi /etc/sysconfig/network-scripts/ifcfg-bond0
DEVICE=bond0              
BOOTPROTO=static
IPADDR=192.168.9.33       
NETMASK=255.255.255.0
BROADCAST=192.168.9.255
GATEWAY=192.168.9.250
ONBOOT=yes
TYPE=Ethernet

 

2. 编辑eth0、eth1文件
# vi /etc/sysconfig/network-scripts/ifcfg-eth0
### Broadcom Corporation NetXtreme II BCM5709 Gigabit Ethernet
DEVICE=eth0
BOOTPROTO=none
HWADDR=xx:xx:xx:xx:xx:xx   #此处为对应的mac地址
ONBOOT=yes
MASTER=bond0
SLAVE=yes

# vi /etc/sysconfig/network-scripts/ifcfg-eth1
### Broadcom Corporation NetXtreme II BCM5709 Gigabit Ethernet
DEVICE=eth1
BOOTPROTO=none
HWADDR=xx:xx:xx:xx:xx:xx
ONBOOT=yes
HOTPLUG=no
MASTER=bond0
SLAVE=yes

 

3. 编辑modprobe.conf文件
# vi /etc/modprobe.conf
添加:
alias bond0 bonding
options bond0 miimon=100 mode=0

说明:
1).miimon=100
用来设定链路监测的间隔时间。即每100ms监测一次链路状态。bonding只监测主机与交换机之间链路。如果交换机出去的链路出问题而本身没有问题,那么bonding认为链路没有问题而继续使用。
2).mode=1
表示提供冗余功能。除此之外还可以为0、2、3,共四种模式。0表示负载均衡
3).更详细的可参考:深度分析Linux下双网卡绑定七种模式 http://hovertree.com/menu/linux/

 

4. 重启网络
# service network restart

 

5. 验证
通过其它机器ping
绑定的ip,ping的过程中拔掉eth0或eth1网线,确认网络是否持续正常

###参考如下的验证为启动bond后的结果
# less /proc/net/bonding/bond0 
Ethernet Channel Bonding Driver: v3.4.0-2 (October 7, 2008)

Bonding Mode: load balancing (round-robin)
MII Status: up
MII Polling Interval (ms): 100
Up Delay (ms): 0
Down Delay (ms): 0

Slave Interface: eth0
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 44:a8:42:0b:a7:a5

Slave Interface: eth1
MII Status: up
Speed: 1000 Mbps
Duplex: full
Link Failure Count: 0
Permanent HW addr: 44:a8:42:0b:a7:a6

###查看当前主机的IP配置信息
# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc
pfifo_fast master bond0 qlen 1000
    link/ether 44:a8:42:0b:a7:a5 brd ff:ff:ff:ff:ff:ff
3: eth1: <BROADCAST,MULTICAST,SLAVE,UP,LOWER_UP> mtu 1500 qdisc
pfifo_fast master bond0 qlen 1000
    link/ether 44:a8:42:0b:a7:a5 brd ff:ff:ff:ff:ff:ff
4: eth2: <BROADCAST,MULTICAST> mtu 1500 qdisc noop qlen 1000
    link/ether 44:a8:42:0b:a7:a7 brd ff:ff:ff:ff:ff:ff
5: eth3: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc
pfifo_fast qlen 1000
    link/ether 44:a8:42:0b:a7:a8 brd ff:ff:ff:ff:ff:ff
    inet 192.168.10.15/24 brd 192.168.10.255 scope global eth3
6: sit0: <NOARP> mtu 1480 qdisc noop 
    link/sit 0.0.0.0 brd 0.0.0.0
7: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500 qdisc
noqueue 
    link/ether 44:a8:42:0b:a7:a5 brd ff:ff:ff:ff:ff:ff
    inet 192.168.9.33/24 brd 192.168.9.255 scope global bond0
    inet6 fe80::46a8:42ff:fe0b:a7a5/64 scope link 
       valid_lft forever preferred_lft forever

推荐:http://www.cnblogs.com/roucheng/p/3470287.html

http://www.bkjia.com/Linuxjc/1131660.htmlwww.bkjia.comtruehttp://www.bkjia.com/Linuxjc/1131660.htmlTechArticleLinux下快速设定ip bond,linux设定ipbond
在计算机网路普及的初期,很多OS系统都使用的为单网卡方式,即一个网卡使用一个IP地址。随着网络要…

一,部署说明及拓扑架构:

  必发bf88 1

  1、mysql安装在非root用户下(Mysql 版本5.7.18)

  2、keepalive安装在root用户下

  3、两台服务器安装mysql+keepalive,DB1真实ip地址为:10.112.188.70;DB2真实IP地址为:10.112.188.71;两台数据库除了建立同步账号rep1外,需建立用于守护进程账号check权限只有selecte。

  4、DB1与DB2采用双主模式进行部署,但正常情况下只有DB1采用keepalive虚拟浮动IP:10.112.188.69提供服务;一旦DB2异常则由keepalive虚拟浮动IP地址:10.112.188.69自动切换至DB2主机上提供服务。

  5、keepalive虚拟浮动地址切换思路(编写守护进程需要);

    1)正常情况下只有DB1采用keepalive虚拟浮动IP:10.112.188.69提供服务;DB2中keepalive为stop状态。DB1采用守护进程检查自身mysql状态是否异常:mysql
-ucheck-p’check1′ -S /iddbs/mysql.sock -e “select
version();”一旦DB1自身数据库异常则自动stop keepalive
否则保证 keepalive是启动状态;且还需监控浮动虚拟地址:10.112.188.69下的mysql是否能连接成功,若能连接不成功启动keepalive(备注:监控浮动虚拟地址主要用途是:当DB1异常发生且恢复后(此时DB2已经自动启动keepalive采用虚拟IP地址接管提供服务),监控DB2是否已经释放浮动IP,保证IP地址不冲突。)

    2)正常情况下只有DB1采用keepalive虚拟浮动IP:10.112.188.69提供服务;DB2中keepalive为stop状态。DB2采用守护进程监控DB1(真实IP:10.112.188.70)Mysql数据状态(是否异常)采用mysql
-ucheck -p’check1′ -h 10.112.188.70 -e “select version();”
一旦DB1中MYSQL数据库出现异常(或者宕机)DB2则启动keepalive并浮动VIP为:10.112.188.69

  6、keepalive浮动地址切换逻辑图:

  必发bf88 2

    

二、mysql双主部署及权限账号建立:

  1、同步账号建立:

    在10.112.188.70上建立:

    mysql> grant replication slave on *.* to
‘rep1’@’10.112.188.71’ identified by ‘rep1’;

    在10.112.188.71

    mysql> grant replication slave on *.* to
‘rep1’@’10.112.188.70’ identified by ‘rep1’;

  2、守护进程账号建立:

    在DB1和DB2上均建立守护进程状态查询权限,只授予select:

    mysql> GRANT select ON *.* TO ‘check’@’10.112.188.70’
identified by ‘check’;

    mysql> GRANT select ON *.* TO ‘check’@’10.112.188.71’
identified by ‘check’;

  3、同步操作以下在DB2上操作:

    1)在DB1和DB2库配置文件中my.cnf开启log-bin

      server-id       = 1

      # Uncomment the following if you want to log updates

      log-bin=/iddbsdata/mysql-bin

    2)mysql> show variables like ‘log%’;  #查看主库的binlog开关是否生效(ON状态)

       +———————————+———————————————+

      | Variable_name     | Value
                                      |

      +———————————+———————————————+

      | log               | OFF
                                       |

      | log_bin           | ON
                                        |

    3)查看binlog节点位置。

      mysql> show master status;

      +——————+———-+————–+——————+

      | File             | Position | Binlog_Do_DB |
Binlog_Ignore_DB |

      +——————+———-+————–+——————+

      | mysql-bin.000002 |      1160  |              |
                 |

      +——————+———-+————–+——————+

      1 row in set (0.00 sec)

    4)同步(以10.112.188.71同步10.112.188.70为例,及DB2同步DB1):

      mysql>CHANGE MASTER TO

      >MASTER_HOST=’10.112.188.71′,

      >MASTER_PORT=3307,

      >MASTER_USER=’rep1′,

      >MASTER_PASSWORD=’rep1′,

      >MASTER_LOG_FILE=’mysql-bin.000002′,

      >MASTER_LOG_POS=1160;

  4、在DB1上重复进行3操作。

  5、注意,由于DB1和DB2属于是新库(新安装无数据),在同步之前省去了mysqldump,如果是有数据的数据库必须进行mysqldump操作才能进行数据同步。

 三、keepalive部署(需在root用户下)

  1、安装部署:

    yum install -y openssl openssl-devel

    gunzip keepalived-1.2.12.tar.gz

    tar -xvf keepalived-1.2.12.tar

    cd keepalived-1.2.12

    ./configure –prefix=/usr/local/keepalived

    make

    make install

    ls -lart /usr/local/keepalived/

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

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

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

    mkdir /etc/keepalived

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

    cd /etc

    cp keepalived.conf keepalived.conf.bak

  2、配置文件

   

必发bf88 3必发bf88 4

[root@xqcldb001 /etc/keepalived]#cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
#   notification_email_from Alexandre.Cassen@firewall.loc
#   smtp_server 192.168.200.1
#   smtp_connect_timeout 30
   router_id mysql
}

vrrp_instance VI_1 {
    state BACKUP
    interface bond0
    virtual_router_id 60
    priority 100
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
#10.208.218.99  dev eth0 label eth0:1
10.112.188.69/26
}
}

virtual_server 10.112.188.69 3307 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    net_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP

    real_server 10.112.188.70 3307 {
        weight 1
    # notify_down /iddbs/app/check_mysql.sh
     TCP_CHECK {
         connect_timeout 10
         nb_get_retry 3
         connect_port 8066
         }            
   }
}

DB1

 

必发bf88 5必发bf88 6

[root@xqcldb002 /etc/keepalived]#cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
#   notification_email_from Alexandre.Cassen@firewall.loc
#   smtp_server 192.168.200.1
#   smtp_connect_timeout 30
   router_id mysql
}

vrrp_instance VI_1 {
    state BACKUP
    interface bond0
    virtual_router_id 60
    priority 100
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
#10.208.218.99  dev eth0 label eth0:1
10.112.188.69/26
}
}

virtual_server 10.112.188.69 3307 {
    delay_loop 6
    lb_algo rr
    lb_kind NAT
    net_mask 255.255.255.0
    persistence_timeout 50
    protocol TCP

    real_server 10.112.188.71 3307 {
        weight 1
    # notify_down /iddbs/app/check_mysql.sh
     TCP_CHECK {
         connect_timeout 10
         nb_get_retry 3
         connect_port 8066
         }            
   }
}

DB2

  备注经测试VRRP state
BACKUP两台必须均为BACKUP,否则一旦重启keepalive会导致网卡IP错误。

 四、keepalive守护进程

必发bf88 7必发bf88 8

#!/bin/sh
while true
do

/iddbs/bin/mysql -uroot -p'Siina@678' -S /iddbsdata/mysql.sock -e "select version();">/dev/null 2>&1
if [ $? -eq 0  ]

        then
                /etc/init.d/keepalived start >/dev/null 2>&1
        else
                /etc/init.d/keepalived stop >/dev/null 2>&1

fi
sleep 1

/iddbs/bin/mysql -h10.112.188.69 -ucheck -p'check1'  -P 3307 -e "select version();">/dev/null 2>&1

if [ $? -eq 0 ]
        then
                /etc/init.d/keepalived stop >/dev/null 2>&1
        else
                /etc/init.d/keepalived start >/dev/null 2>&1
fi
sleep 2
done

DB1守护进程

必发bf88 9必发bf88 10

#!/bin/sh
while true
do

/iddbs/bin/mysql -h10.112.188.70 -ucheck -p'check1'  -P 3307 -e "select version();">/dev/null 2>&1

if [ $? -eq 0 ]
        then
                /etc/init.d/keepalived stop >/dev/null 2>&1
                sleep 2
        else
                /etc/init.d/keepalived start >/dev/null 2>&1

fi
sleep 3
done

DB2守护进程

  守护进程启动:[root@xqcldb001 ~]#sh check_mysql.sh &

五、双主同步及模拟数据库异常keepalive自动切换浮动IP

1、正常情况下:

正常情况在备用服务器上bond0网卡上只有一个地址。

xqcldb002:/iddbs>ip a

10: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500
qdisc noqueue state UP

    link/ether 70:79:90:a1:0b:48 brd ff:ff:ff:ff:ff:ff

    inet 10.112.188.71/26 brd 10.112.188.127 scope global bond0

    inet6 fe80::7279:90ff:fea1:b48/64 scope link

       valid_lft forever preferred_lft forever

 

 正常情况在主服务器上bond0网卡上有一个地址和一个虚拟IP地址

xqcldb001:/iddbs>ip a

10: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500
qdisc noqueue state UP

    link/ether 70:79:90:7d:bf:3e brd ff:ff:ff:ff:ff:ff

    inet 10.112.188.70/26 brd 10.112.188.127 scope global bond0

    inet 10.112.188.69/26 scope global
secondary bond0

    inet6 fe80::7279:90ff:fe7d:bf3e/64 scope link

       valid_lft forever preferred_lft forever

2、正常情况下:10.112.188.70数据库异常(模拟宕机):

  此种情况下70(DB1)bond0网卡上有一个真实地址,已经无浮动IP地址10.112.188.69

xqcldb001:/iddbs>sh mysql_start stop

 stop Mysql. [  OK  ]

 xqcldb001:/iddbs>ip a

10: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500
qdisc noqueue state UP

    link/ether 70:79:90:7d:bf:3e brd ff:ff:ff:ff:ff:ff

    inet 10.112.188.70/26 brd 10.112.188.127 scope global bond0

    inet6 fe80::7279:90ff:fe7d:bf3e/64 scope lin

  71(DB2)bond0网卡上有一个真实地址,浮动IP地址10.112.188.69

xqcldb002:/iddbs>ip a

10: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500
qdisc noqueue state UP

    link/ether 70:79:90:a1:0b:48 brd ff:ff:ff:ff:ff:ff

    inet 10.112.188.71/26 brd 10.112.188.127 scope global bond0

    inet 10.112.188.69/26 scope global
secondary bond0

    inet6 fe80::7279:90ff:fea1:b48/64 scope link

       valid_lft forever preferred_lft forever

 

3、数据库恢复:

 xqcldb001:/iddbs>sh mysql_start start

starting Mysql…. [  OK  ]

xqcldb001:/iddbs>

xqcldb001:/iddbs>

xqcldb001:/iddbs>ps -ef | grep mysql

root      2182 24200  0 11:30 pts/2    00:00:00 sh check_mysql.sh    
#守护进程

iddbs     4485     1  0 11:35 pts/2    00:00:00 /bin/sh
/iddbs/bin/mysqld_safe –defaults-file=/iddbs/my.cnf

iddbs     4890  4485 93 11:35 pts/2    00:00:06 /iddbs/bin/mysqld
–defaults-file=/iddbs/my.cnf –basedir=/iddbs –datadir=/iddbsdata
–plugin-dir=/iddbs/lib/plugin –log-error=/iddbsdata/mysql-error.log
–pid-file=/iddbsdata/mysql.pid –socket=/iddbsdata/mysql.sock
–port=3307

iddbs     4949  3619  0 11:36 pts/2    00:00:00 grep mysql

地址已经自动切换至DB1:

xqcldb001:/iddbs>ip a

10: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500
qdisc noqueue state UP

    link/ether 70:79:90:7d:bf:3e brd ff:ff:ff:ff:ff:ff

    inet 10.112.188.70/26 brd 10.112.188.127 scope global bond0

    inet 10.112.188.69/26 scope global
secondary bond0

    inet6 fe80::7279:90ff:fe7d:bf3e/64 scope link

       valid_lft forever preferred_lft forever

DB2只有真实地址而无虚拟浮动地址:

xqcldb002:/iddbs>ip a

10: bond0: <BROADCAST,MULTICAST,MASTER,UP,LOWER_UP> mtu 1500
qdisc noqueue state UP

    link/ether 70:79:90:a1:0b:48 brd ff:ff:ff:ff:ff:ff

    inet 10.112.188.71/26 brd 10.112.188.127 scope global bond0

    inet6 fe80::7279:90ff:fea1:b48/64 scope link

       valid_lft forever preferred_lft forever

admin

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注