Openstack 中 MySQL主主互备结合keepalived实现高可用

实验环境 服务器test1(主) 192.168.106.156 服务器test2(主) 192.168.106.158 Mysql版本:5.1.73 VM System OS:CentOS 6 X64

一 安装配置mysql主主互备 1.安装Mysql: 需要关闭防火墙、SELINUX,两台机子上要安装同样版本的mysql数据库。

yum install mysql-server

2.创建同步用户:

这里test1和test2互为主从,所以都要分别建立一个同步用户。 在test1、test2两台机子上分别执行:

mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO sync@'%' IDENTIFIED BY '123456';
mysql> flush privileges;

3.修改配置文件:

3.1 test1上mysql的配置文件:

[mysqld]
#log-bin=mysql-bin
server-id=156

log_bin = /var/lib/mysql/mysql-binlog
binlog-ignore-db=mysql
binlog-ignore-db=information_schema

replicate-ignore-db=mysql,information_schema
log-slave-updates
sync_binlog=1
auto_increment_offset=1
auto_increment_increment=2

3.2 test2上mysql的配置文件:

[mysqld]
#log-bin=mysql-bin
server-id=158

auto_increment_offset=2
auto_increment_increment=2
log-slave-updates
sync_binlog=1
log_bin = /var/lib/mysql/mysql-binlog
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
replicate-ignore-db=mysql,information_schema

4.然后,分别重启mysql服务器。

service mysqld restart

5.分别在test1、test2上查看主服务器状态:

  test1上:
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)

mysql> show master status\G
*************************** 1. row ***************************
            File: mysql-binlog.000001
        Position: 106
Binlog_Ignore_DB: mysql,information_schema
1 row in set (0.00 sec)

mysql> 
mysql> unlock tables;
  test2上:
  
mysql> stop slave;
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)

mysql> show master status\G
*************************** 1. row ***************************
            File: mysql-binlog.000001
        Position: 106
Binlog_Ignore_DB: mysql,information_schema
1 row in set (0.00 sec)

mysql> unlock tables;

注:这里锁表的目的是为了生产环境中不让进新的数据,好让从服务器定位同步位置。初次同步完成后,记得解锁。

6.分别在test1、test2上用change master语句指定同步位置:

6.1 test1:
mysql> change master to master_host='192.168.106.158', master_user='sync', master_password='123456', master_log_file='mysql-binlog.000001', master_log_pos=106;
Query OK, 0 rows affected (0.12 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
6.2 test2:
mysql> change master to master_host='192.168.106.156', master_user='sync', master_password='123456', master_log_file='mysql-binlog.000001', master_log_pos=106;
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
  1. 分别在test1、test2上查看从服务器状态:
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.106.158
                  Master_User: repli
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-binlog.000001
          Read_Master_Log_Pos: 106
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 254
        Relay_Master_Log_File: mysql-binlog.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:  
          Replicate_Ignore_DB: mysql,information_schema
… …

查看以上两项的值, Slave_IO_Running: Yes Slave_SQL_Running: Yes 均为Yes则表示状态正常。

8.测试: 双向测试,在test1上创建数据库db1,从test2上查看信息;同样,在test2上创建数据库db2后,从test1上查看信息。

二 安装和配置keepalived实现MySQL双主高可用 1.安装 keepalived

yum install keepalived

2.配置keepalived

2.1 test1 192.168.106.156
# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
 
global_defs {
   notification_email {
     admin@tcloudsoft.com
     zhuzy@tcloudsoft.com
   }
   notification_email_from keepalived@localhost  
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
 
vrrp_instance HA_1 {
    state BACKUP                #test1和test2都配置为BACKUP
    interface eth0              #指定HA检测的网络接口
    virtual_router_id 80        #虚拟路由标识,主备相同
    priority 100                #定义优先级,test2设置90
    advert_int 1                #设定test1和test2之间同步检查的时间间隔
    nopreempt                   #不抢占模式。只在优先级高的机器上设置即可
    authentication {
        auth_type PASS
        auth_pass 1111
    }
 
    virtual_ipaddress {                 #设置虚拟IP,可以设置多个,每行一个
        192.168.106.200/24 dev eth0       #MySQL对外服务的IP,即VIP
    }
}
 
virtual_server 192.168.106.200 3306 {
    delay_loop 2                    #每隔2秒查询real server状态
    lb_algo wrr                     #lvs 算法
    lb_kinf DR                      #LVS模式(Direct Route)
    persistence_timeout 50
    protocol TCP
 
    real_server 192.168.106.156 3306 {    #监听本机的IP
        weight 1
        notify_down /etc/keepalived/mysql.sh
        TCP_CHECK {
        connect_timeout 10         #10秒无响应超时
        bingto 192.168.106.200
        nb_get_retry 3
        delay_before_retry 3
        connect_port 3306
        }
    }
 
}

2.2 keepalived检测脚本,当其中一台MySQL服务出现故障down掉时,实现自动切换到正常的MySQL服务器继续提供服务

vi /etc/keepalived/mysql.sh
#!/bin/bash
pkill keepalived
2.3 test2 192.168.106.158
# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
 
global_defs {
   notification_email {
     root@tcloudsoft.com
     zhuzy@tcloudsoft.com
   }
   notification_email_from keepalived@localhost
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
 
vrrp_instance HA_1 {
    state BACKUP                #test1和test2都配置为BACKUP
    interface eth0              #指定HA检测的网络接口
    virtual_router_id 80        #虚拟路由标识,主备相同
    priority 90                #定义优先级,test2设置90
    advert_int 1                #设定test1和test2之间同步检查的时间间隔
    authentication {
        auth_type PASS
        auth_pass 1111
    }
 
    virtual_ipaddress {                 #设置虚拟IP,可以设置多个,每行一个
        192.168.106.200/24 dev eth0       #MySQL对外服务的IP,即VIP
    }
}
 
virtual_server 192.168.106.200 3306 {
    delay_loop 2
    lb_algo wrr
    lb_kinf DR
    persistence_timeout 50
    protocol TCP
 
    real_server 192.168.106.158 3306 {    #监听本机的IP
        weight 1
        notify_down /etc/keepalived/mysql.sh
        TCP_CHECK {
        connect_timeout 10
        bingto 192.168.106.200            
        nb_get_retry 3
        delay_before_retry 3
        connect_port 3306
        }
    }
 
}

3.授权VIP的root用户权限 授权远程主机可以通过VIP登录MySQL,并测试数据复制功能

mysql> grant all on *.* to root@'192.168.6.44' identified by '123456';
mysql> grant all on *.* to root@'192.168.106.200' identified by '123456';
mysql> flush privileges;

4.测试keepalived高可用功能 在OpenStack中,测试前需要先完成第三部分的设置 4.1远程主机登录通过VIP 192.168.106.200 登录MySQL,查看MySQL连接状态

mysql> show variables like 'hostname%';
+---------------+--------+
| Variable_name | Value  |
+---------------+--------+
| hostname      | test1 |
+---------------+--------+
1 row in set (0.00 sec)

4.2故障测试,停止test1的MySQL服务,再次查看是否转移至test2服务器上

mysql> show variables like 'hostname%';

三 OpenStack VM单网卡多IP 实现VIP切换的方法 1.使用port_security_enabled属性,port_security_enabled 是在kilo版后加入的Ml2扩展驱动,可能针对整个网络更改,也可针对某个端口更改,默认为true。

neutron port-list
neutron port-update --no-security-groups 413b58fe-44c0-4df2-b588-332d5b6030e9
neutron port-update 413b58fe-44c0-4df2-b588-332d5b6030e9  --port_security_enabled=False

2.使用allowed-address-pairs 来扩展端口属性,允许手动指定端口的mac_address和ip 地址对的流量通过。

neutron port-list
neutron port-show 413b58fe-44c0-4df2-b588-332d5b6030e9
neutron port-update 413b58fe-44c0-4df2-b588-332d5b6030e9 --allowed-address-pairs type=dict list=true ip_address=192.168.106.200
neutron port-update 413b58fe-44c0-4df2-b588-332d5b6030e9 --allowed-address-pairs action=clear

转载于:https://my.oschina.net/ccc4b/blog/843720

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值