mysql HA方案:双向主从复制
环境
ip | 服务 |
---|---|
192.168.101.15 | mysql |
192.168.101.16 | mysql |
双节点安装配置
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum -y install mysql-server
主节点安装配置
vim /etc/my.cnf
[mysqld]
log-bin = mysql-bin
binlog_format = ROW
log_slave_updates = 1
expire_logs_days = 7
max_binlog_size = 100M
server-id = 1
max_connections=200
mysql-1>mysql -u root -p
mysql-1>GRANT REPLICATION SLAVE ON *.* TO 'rep'@'%' identified by 'Rep@rep123';
mysql-1> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000028 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql-2>change master to master_host='192.168.101.15',master_user='rep',master_password='Rep@rep123',master_log_file='mysql-bin.000028',master_log_pos=120;
start slave;
show slave status\G;
备节点安装配置
vim /etc/my.cnf
[mysqld]
log-bin = mysql-bin
binlog_format = ROW
log_slave_updates = 1
expire_logs_days = 7
max_binlog_size = 100M
server-id = 2
max_connections=200
mysql-2> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 707647 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql-1>change master to master_host='192.168.101.16',master_user='rep',master_password='Rep@rep123',master_log_file='mysql-bin.000001',master_log_pos=1422;
start slave;
show slave status\G;
管理节点HA方案:cloudstack-mangment+keepalived
环境
ip | 服务 |
---|---|
192.168.101.15 | cloudstack-mangment+keepalived |
192.168.101.16 | cloudstack-mangment+keepalived |
192.168.101.21 | vip |
setenforce 0
systemctl disable firewalld
双节点安装配置
cat /etc/yum.repos.d/cloudstack.repo
[cloudstack]
name=cloudstack
baseurl=http://download.cloudstack.org/centos/$releasever/4.15/
enabled=1
gpgcheck=0
yum -y install cloudstack-management
#数据库初始化,以root创建密码为password的数据库用户cloud
cloudstack-setup-databases cloud:password@localhost --deploy-as=root
#CloudStack has successfully initialized database, you can check your database configuration in /etc/cloudstack/management/db.properties
#初始化服务
cloudstack-setup-management
#Starting to configure CloudStack Management Server:
#Configure CloudStack Management Server ...[OK]
#CloudStack Management Server setup is Done!
#Please ensure the following ports are open for the management server to function properly : 8080 8250 8443 9090
systemctl enable cloudstack-management
主节点安装配置
cat /etc/cloudstack/management/db.properties
cluster.node.IP=192.168.101.15
cluster.servlet.port=9090
db.ha.enabled=true
db.cloud.replicas=192.168.101.16
cat /etc/keepalived/keepalived.conf
# 配置(Master)
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id DRBD_HA_MASTER
}
vrrp_script chk_management {
script "/etc/keepalived/check_cloudstack_management.sh"
interval 5
}
vrrp_instance VI_1 {
state MASTER
interface enp0s3
virtual_router_id 101
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nfs
}
notify_master /etc/keepalived/notify_master.sh
virtual_ipaddress {
192.168.101.21/24
}
}
其中
cat check_cloudstack_management.sh
#!/bin/bash
systemctl status cloudstack-management &>/dev/null
if [ $? -ne 0 ];then
systemctl restart cloudstack-management
sleep 3
systemctl status cloudstack-management &>/dev/null
if [ $? -ne 0 ];then
systemctl stop keepalived
fi
fi
cat notify_master.sh
#!/bin/sh
systemctl restart nfs
备节点安装配置
cat /etc/cloudstack/management/db.properties
cluster.node.IP=192.168.101.16
cluster.servlet.port=9090
db.ha.enabled=true
db.cloud.replicas=192.168.101.15
cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id DRBD_HA_BACKUP
}
vrrp_instance VI_1 {
state BACKUP
interface enp0s3
virtual_router_id 101
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
notify_master /etc/keepalived/notify_master.sh
virtual_ipaddress {
192.168.101.21/24
}
}
其中
cat /etc/keepalived/notify_master.sh
#!/bin/sh
systemctl restart cloudstack-management
nfs HA方案:nfs+drbd+keepalived
环境
ip | 服务 |
---|---|
192.168.101.13 | nfs+drbd+keepalived |
192.168.101.14 | nfs+drbd+keepalived |
192.168.101.20 | vip |
parted /dev/sdb
mklabel gpt
mkpart primary 0 2048
mkpart primary 2049 100%
p
quit
mkfs.ext4 /dev/sdb1
双节点安装配置
yum install https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm
yum install -y drbd84-utils kmod-drbd84
echo "modprobe drbd" >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
systemctl enable drbd && systemctl start drbd
cat /etc/drbd.d/r0.res
resource r0 {
net {
protocol C;
cram-hmac-alg "sha1";
shared-secret "c4f9375f9834b4e7f0a528cc65c055702bf5f24a";
}
device /dev/drbd0;
disk /dev/sdb1;
meta-disk /dev/sdb2[0];
on test1 {
address 192.168.101.13:7788;
}
on test2 {
address 192.168.101.14:7788;
}
}
#初始化
drbdadm create-md r0
#启用资源
drbdadm up r0
#查看状态
cat /proc/drbd
#提升主
drbdadm primary r0
mkfs.ext4 /dev/drbd0
mount /dev/drbd0 /export/primary/
yum install -y rpcbind nfs-utils
cat /etc/exports
/export/secondary *(rw,async,no_root_squash,no_subtree_check)
/export/primary *(rw,async,no_root_squash,no_subtree_check)
systemctl enable nfs && systemctl start nfs
systemctl enable rpcbind && systemctl start rpcbind
wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
yum install -y heartbeat
systemctl enable keepalived
主节点安装配置
cat /etc/keepalived/keepalived.conf
#Master
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id DRBD_HA_MASTER
}
vrrp_script chk_nfs {
script "/etc/keepalived/check_nfs.sh"
interval 5
}
vrrp_instance VI_1 {
state MASTER
interface enp0s3
virtual_router_id 101
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nfs
}
notify_master /etc/keepalived/notify_master.sh
notify_stop /etc/keepalived/notify_stop.sh
virtual_ipaddress {
192.168.101.200/24
}
}
其中
chmod 744 /etc/keepalived/*.sh
cat /etc/keepalived/check_nfs.sh
#!/bin/bash
/usr/bin/systemctl status nfs &>/dev/null
if [ $? -ne 0 ];then
/usr/bin/systemctl restart nfs
sleep 3
/usr/bin/systemctl status nfs &>/dev/null
if [ $? -ne 0 ];then
umount /dev/drbd0
drbdadm secondary r0
systemctl stop keepalived
fi
fi
cat /etc/keepalived/notify_master.sh
#!/bin/sh
drbdadm primary r0
mount /dev/drbd0 /export/primary
systemctl restart nfs
cat /etc/keepalived/notify_stop.sh
#!/bin/sh
umount /dev/drbd0
drbdadm secondary r0
备节点安装配置
cat /etc/keepalived/keepalived.conf
#slave
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id DRBD_HA_BACKUP
}
vrrp_instance VI_1 {
state BACKUP
interface enp0s3
virtual_router_id 101
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
notify_master /etc/keepalived/notify_master.sh
notify_backup /etc/keepalived/notify_backup.sh
virtual_ipaddress {
192.168.101.200/24
}
}
其中
cat /etc/keepalived/notify_master.sh
#!/bin/sh
drbdadm primary r0
mount /dev/drbd0 /export/primary
systemctl restart nfs
cat /etc/keepalived/notify_backup.sh
#!/bin/sh
umount /dev/drbd0
drbdadm secondray r0
测试
systemctl start keepalived
#vip在主节点上
ip addr
#主显示primary/secondary,备显示secondary/primary
cat /proc/drbd
echo "test123" >> /export/primary/12345.txt
#将主节点关机,vip迁移至备节点,备显示primary/secondary,同时cat /export/primary/12345.txt
#主节点开机,相关服务启动,状态自动为master,备由master切换为slave,vip以及drbd状态复原