实验用主机:
提供iscsi磁盘:172.16.103.1,提供iscsi磁盘
高可用主机:172.16.103.2,172.16.103.3
实验拓扑:
实验步骤:
一、配置172.16.103.1,输出iscsi磁盘,创建的磁盘分区为/dev/sda3,在实际的使用环境中的通常为整个磁盘而非分区。
# yum install -y scsi-target-utils
# service tgtd start
# tgtadm --lld iscsi --mode target --op new --tid 1 --targetname iqn.2014-09.com.iscsi:t1 创建target
# tgtadm --lld iscsi --mode target --op show #查看创建的target
# tgtadm --lld iscsi --mode logicalunit --op new --tid 1 --lun 1 --backing-store /dev/sda3
# tgtadm --lld iscsi --mode target --op show #查看lun创建的结果
# tgtadm --lld iscsi --mode target --op bind --tid 1 --initiator-address 172.16.0.0/16 #基于IP地址做访问控制,授权给172.16.0.0/16网段的主机访问。
二、配置前端的高可用节点,发现并使用该磁盘设备:
# yum install -y iscsi-initiator-utils
# iscsiadm -m discovery -t st -p 172.16.103.1
# iscsiadm -m node -T iqn.2014-09.com.iscsi:t1 -p 172.16.103.1 -l
此时在磁盘信息中可以查看到新的磁盘信息,显示的新磁盘为未创建分区的新磁盘:
[root@node1 ~]# fdisk -l /dev/sd[a-z]
Disk /dev/sda: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000652c0
Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 7859 62914560 8e Linux LVM
/dev/sda3 7859 7990 1059291 83 Linux
/dev/sda4 7991 15665 61649437+ 5 Extended
/dev/sda5 7991 8122 1060258+ 83 Linux
Disk /dev/sdb: 2154 MB, 2154000384 bytes
67 heads, 62 sectors/track, 1012 cylinders
Units = cylinders of 4154 * 512 = 2126848 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
将该磁盘分区创建为/dev/sdb1,所有的容量都分配给该磁盘分区,执行格式化操作:
# mke2fs -t ext4 /dev/sdb1
# mkdir /mydata/data #作为数据库存储数据的目录
# groupadd -g 3306 mysql
# useradd -u 3306 -g mysql -s /sbin/nologin -M mysql
# chown -R mysql.mysql /mydata/data
# mount /dev/sdb1 /mydata/data
三、使用二进制格式的MariaDB安装mysql
# tar xf mariadb-5.5.39-linux-x86_64.tar.gz -C /usr/local
# ln -sv mariadb-5.5.39-linux-x86_64/ mysql
# cd mysql
# chown -R root.mysql ./
# scripts/mysql_install_db --user=mysql --datadir=/mydata/data
# mkdir /etc/mysql
# cp support-files/my-large.cnf /etc/mysql/my.cnf
# vim /etc/mysql/my.cnf
datadir = /mydata/data
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
# chmod +x /etc/rc.d/init.d/mysqld
# mount /dev/sdb1 /mydata/data
# service mysqld start
# /usr/local/mysql/bin/mysql #连接数据库测试
> CREATE DATABASE tdb; #创建一个数据库用于验证其他节点挂载同一磁盘时是否可以使用相同的数据
另一个节点的数据库安装方式相同,就不在重复了。
四、使用ansible安装corosync+pacemaker+crmsh:
在配置高可用集群之前一定要先确定数据库和iscsi磁盘未挂载!
使用crmsh配置时的具体步骤为:
crm(live)configure# property stonith-enabled=false
crm(live)configure# property no-quorum-policy=ignore
crm(live)configure# primitive myip ocf:heartbeat:IPaddr params ip=172.16.103.25
crm(live)configure# primitive mystore ocf:heartbeat:Filesystem params device=/dev/sdb1 directory=/mydata/data fstype=ext4 op start timeout=60s op stop timeout=60s
crm(live)configure# primitive mysql lsb:mysqld
crm(live)configure# group myservice myip mystore mysql
crm(live)configure# order myip_before_mystore_before_mysql mandatory: myip mystore mysql
crm(live)configure# verify
crm(live)configure# commit
配置完成后查看资源启动的状态:
crm(live)# status
Last updated: Sun Sep 7 18:09:00 2014
Last change: Sun Sep 7 18:08:47 2014 via crm_attribute on node2.cluster.com
Stack: classic openais (with plugin)
Current DC: node1.cluster.com - partition with quorum
Version: 1.1.10-14.el6-368c726
2 Nodes configured, 2 expected votes
3 Resources configured
Node node2.cluster.com: standby
Online: [ node1.cluster.com ]
Resource Group: myservice
myip(ocf::heartbeat:IPaddr):Started node1.cluster.com
mystore(ocf::heartbeat:Filesystem):Started node1.cluster.com
mysql(lsb:mysqld):Started node1.cluster.com
当前资源运行在node1上,使用ifconfig命令可以查看ip地址配置的结果以及测试连接MySQL测试数据库是否可用。
[root@node1 ~]# ip addr show
1: lo: mtu 16436 qdisc noqueue state UNKNOWN
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: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:e1:37:51 brd ff:ff:ff:ff:ff:ff
inet 172.16.103.2/16 brd 172.16.255.255 scope global eth0
inet 172.16.103.25/16 brd 172.16.255.255 scope global secondary eth0
inet6 fe80::20c:29ff:fee1:3751/64 scope link
valid_lft forever preferred_lft forever
[root@node1 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.71 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
可以看到IP地址172.16.103.25配置在eth0上,mysql数据库连接也正常。如果切换node1为standby模式,资源是否会转移,测试结果为:
crm(live)# status
Last updated: Sun Sep 7 18:14:33 2014
Last change: Sun Sep 7 18:14:27 2014 via crm_attribute on node1.cluster.com
Stack: classic openais (with plugin)
Current DC: node1.cluster.com - partition with quorum
Version: 1.1.10-14.el6-368c726
2 Nodes configured, 2 expected votes
3 Resources configured
Node node1.cluster.com: standby
Online: [ node2.cluster.com ]
Resource Group: myservice
myip(ocf::heartbeat:IPaddr):Started node2.cluster.com
mystore(ocf::heartbeat:Filesystem):Started node2.cluster.com
mysql(lsb:mysqld):Started node2.cluster.com
可以看到资源都运行在了node2上。