分类: Mysql/postgreSQL
2008-04-29 07:53:05
来源:赛迪网 作者:Eric |
loadb1.test.com / loadb2.test.com:
cd /tmp
wget
tar xvfz DBD-mysql-3.0002.tar.gz
cd DBD-mysql-3.0002
perl Makefile.PL
make
make install
我们必须先启用packet forwarding:
loadb1.example.com / loadb2.example.com:
vi /etc/sysctl.conf
# Enables packet forwardingnet.ipv4.ip_forward = 1
6.2 配置heartbeat(心跳)
我们通过创建三个文件(loadb1.test.com 和 loadb2.test.com的文件必须完全一样)来配置heartbeat:
loadb1.test.com / loadb2.test.com:
vi /etc/ha.d/ha.cf
logfacility local0
bcast eth0
mcast eth0 225.0.0.1 694 1 0
auto_failback off
node loadb1
node loadb2
respawn hacluster /usr/lib/heartbeat/ipfail
apiauth ipfail gid=haclient uid=hacluster
请记住:你必须列出节点名称(本例中是loadb1 和 loadb2)
uname -n
除了这些,我们不需要对这个文件做任何修改。
vi /etc/ha.d/haresources
loadb1 \
ldirectord::ldirectord.cf \
LVSSyncDaemonSwap::master \
IPaddr2::192.168.0.105/24/eth0/192.168.0.255
你必须列举其中一个负载均衡节点名称(这里是:loadb1),并且列出
虚拟IP地址(192.168.0.105)和广播地址(192.168.0.255).
vi /etc/ha.d/authkeys
auth 3
3 md5 somerandomstring
somerandomstring是loadb1 和 loadb2 用来相互认证的两个heartbeat守护进程的密码。
这里设置成你自己的密码。你可以选择3种加密方式。我一般使用md5来加密。
/etc/ha.d/authkeys应该是root帐号只读,这里我们这样做:
loadb1.test.com / loadb2.test.com:
chmod 600 /etc/ha.d/authkeys
6.3 配置 ldirectord
现在我们为ldirectord创建配置文件, 负载均衡器:
loadb1.example.com / loadb2.example.com:
vi /etc/ha.d/ldirectord.cf
# Global Directives
checktimeout=10
checkinterval=2
autoreload=no
logfile="local0"
quiescent=yes
virtual = 192.168.0.105:3306
service = mysql
real = 192.168.0.101:3306 gate
real = 192.168.0.102:3306 gate
checktype = negotiate
login = "ldirector"
passwd = "ldirectorpassword"
database = "ldirectordb"
request = "SELECT * FROM connectioncheck"
scheduler = wrr
请填入正确的虚拟IP地址(192.168.0.105)和正确的MySQL群集节点的IP地址(192.168.0.101 和 192.168.0.102)。
3306是MySQL运行时默认的端口。我们也指定了一个MySQL用户(ldirector)和密码(ldirectorpassword),一个数据库(ldirectordb)和
一条SQL查询。ldirectord 使用这些信息来测试MySQL群集节点,以此来检查它们的是否一直可用。下一步我们将用ldirector用户来
创建ldirectordb数据库。
现在我们为heartbeat创建系统必需的启动连接,并移除ldirectord(因为ldirectord将由heartbeat来启动):
loadb1.test.com / loadb2.test.com:
update-rc.d -f heartbeat remove
update-rc.d heartbeat start 75 2 3 4 5 . stop 05 0 1 6 .
update-rc.d -f ldirectord remove
6.4 创建数据库ldirector
下面我们在MySQL群集节点sql1.test.com 和 sql2.test.com上创建ldirector数据库。这个数据库将会用于我们的负载均衡器来检查
MySQL群集节点的可用性。
sql1.test.com:
mysql -u root -p
GRANT ALL ON ldirectordb.* TO 'ldirector'@'%' IDENTIFIED BY 'ldirectorpassword';
FLUSH PRIVILEGES;
CREATE DATABASE ldirectordb;
USE ldirectordb;
CREATE TABLE connectioncheck (i INT) ENGINE=NDBCLUSTER;
INSERT INTO connectioncheck () VALUES (1);
quit;
sql2.test.com:
mysql -u root -p
GRANT ALL ON ldirectordb.* TO 'ldirector'@'%' IDENTIFIED BY 'ldirectorpassword';
FLUSH PRIVILEGES;
CREATE DATABASE ldirectordb;
quit;
6.4 为负载均衡准备MySQL群集节点
最后我们必须配置MySQL群集节点sql1.test.com 和 sql2.test.com来接受虚拟IP地址192.168.0.105。
sql1.test.com / sql2.test.com:
apt-get install iproute
在/etc/sysctl.conf里面添加下面的内容:
vi /etc/sysctl.conf
# Enable configuration of arp_ignore option
net.ipv4.conf.all.arp_ignore = 1
# When an arp request is received on eth0, only respond if that address is
# configured on eth0. In particular, do not respond if the address is
# configured on lo
net.ipv4.conf.eth0.arp_ignore = 1
# Ditto for eth1, add for all ARPing interfaces
#net.ipv4.conf.eth1.arp_ignore = 1
# Enable configuration of arp_announce option
net.ipv4.conf.all.arp_announce = 2
# When making an ARP request sent through eth0 Always use an address that
# is configured on eth0 as the source address of the ARP request. If this
# is not set, and packets are being sent out eth0 for an address that is on
# lo, and an arp request is required, then the address on lo will be used.
# As the source IP address of arp requests is entered into the ARP cache on
# the destination, it has the effect of announcing this address. This is
# not desirable in this case as adresses on lo on the real-servers should
# be announced only by the linux-director.
net.ipv4.conf.eth0.arp_announce = 2
# Ditto for eth1, add for all ARPing interfaces
#net.ipv4.conf.eth1.arp_announce = 2
sysctl -p
将下面这段加入到/etc/network/interfaces:
sql1.test.com / sql2.test.com:
vi /etc/network/interfaces
auto lo:0
iface lo:0 inet static
address 192.168.0.105
netmask 255.255.255.255
pre-up sysctl -p > /dev/null
ifup lo:0
七.启动负载均衡器并测试
现在我们启动两个负载均衡管理器:
loadb1.test.com / loadb2.test.com:
/etc/init.d/ldirectord stop
/etc/init.d/heartbeat start
如果你没有看见错误,你需要现在重启每个负载均衡器:
loadb1.test.com / loadb2.test.com:
shutdown -r now
重启之后我们可以检查是否两个负载均衡器象期望一样工作:
loadb1.test.com / loadb2.test.com:
ip addr sh eth0
主动的负载均衡器应该列出了虚拟IP地址(192.168.0.105):
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:16:3e:45:fc:f8 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.103/24 brd 192.168.0.255 scope global eth0
inet 192.168.0.105/24 brd 192.168.0.255 scope global secondary eth0
被动(热备)的负载均衡器应该显示如下:
2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:16:3e:16:c1:4e brd ff:ff:ff:ff:ff:ff
inet 192.168.0.104/24 brd 192.168.0.255 scope global eth0
loadb1.test.com / loadb2.test.com:
ldirectord ldirectord.cf status
主动负载均衡器上的输出:
ldirectord for /etc/ha.d/ldirectord.cf is running with pid: 1603
被动负载均衡器上的输出:
ldirectord is stopped for /etc/ha.d/ldirectord.cf
loadb1.example.com / loadb2.example.com:
ipvsadm -L -n
主动负载均衡器上的输出:
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.0.105:3306 wrr
-> 192.168.0.101:3306 Route 1 0 0
-> 192.168.0.102:3306 Route 1 0 0
被动负载均衡器上的输出:
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
loadb1.test.com / loadb2.test.com:
/etc/ha.d/resource.d/LVSSyncDaemonSwap master status
主动负载均衡器上的输出:
master running
(ipvs_syncmaster pid: 1766)
被动负载均衡器上的输出:
master stopped
(ipvs_syncbackup pid: 1440)
如果你的测试都是正常的,你现在可以从相同网络(192.168.0.x)中的其它服务器用虚拟IP地址192.168.0.105来访问MySQL数据库:
mysql -h 192.168.0.105 -u ldirector -p
(请记住:你的MySQL客户端必须至少是4.1版本的;旧的版本不能运行MySQL5。)
你现在可以关掉一个MySQL群集节点来测试;你应该仍然可以连接到MySQL数据库。
八 注解
运行一个MySQL群集时,这里有一些重要的东西需要记住:
–所有的数据都是存在内存! 因此你的群集节点上需要大容量的内存。这里有每个节点需要内存
容量的计算公式:
(数据库大小SizeofDatabase * 复制数量NumberOfReplicas * 1.1 ) / 数据节点数量NumberOfDataNodes
所以如果你有一个数据库的大小是1GB,你应该为每个节点配备1.1GB内存!
–群集管理节点在监听1186端口上的任意连接。所以这意味着不是很安全,所以你应当在一个独立的网络上运行MySQL群集。 |