試驗(yàn)環(huán)境: LVS1:192.168.9.27 LVS2:192.168.9.28 keepalived 的VIP:192.168.9.230 要分發(fā)的mysql1: 192.168.9.29 要分發(fā)的mysql2:192.168.9.30 具體安裝步驟: #下載ipvsadm (也就是LVS程序包) wget http://www./software/kernel-2.6/ipvsadm-1.24.tar.gz #解壓 [root@lvs-a ~]# tar -zxf ipvsadm-1.24.tar.gz #進(jìn)入該ipvsadm目錄 [root@lvs-a ~]# cd /usr/local/ipvsadm-1.24 #安裝開發(fā)包及庫(kù)文件 [root@lvs-a ipvsadm-1.24]# # yum install zlib-devel gcc gcc-c++ openssl-devel pcre-devel libtool kernel-devel ncurses-devel -y #創(chuàng)建內(nèi)核連接 [root@lvs-a ipvsadm-1.24]# ln -sv /usr/src/kernels/2.6.18-194.el5-i686/ /usr/src/linux #(2.6.18-194.el5-i686)根據(jù)系統(tǒng)版本修改。 #編譯安裝 [root@lvs-a ipvsadm-1.24]# make;make install #進(jìn)入目錄 [root@lvs-a ~]# cd /usr/local/ #下載keepalived wget http://www./software/keepalived-1.1.17.tar.gz #解壓 [root@lvs-a ipvsadm-1.24]# tar -zxf keepalived-1.2.12.tar.gz #進(jìn)入該keepalived目錄 [root@lvs-a ~]cd keepalived-1.2.12 #編譯安裝 [root@lvs-a keepalived-1.2.12]#./configure --prefix=/usr/local/keepalived #這里編譯完后面顯示三個(gè)yes,說(shuō)明編譯成功,否則安裝不成功。 #以下為1.2.12版本./configure結(jié)果如下
#如果為1.1.17版本./configure結(jié)果如下
[root@lvs-a keepalived-1.2.12]#make [root@lvs-a keepalived-1.2.12]#make install #復(fù)制啟動(dòng)文件 [root@lvs-a keepalived-1.2.12]#cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ ##這樣才能用service keepalived 的命令。 #復(fù)制命令文件 [root@lvs-a keepalived-1.2.12]#cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ #復(fù)制【配置文件 [root@lvs-a keepalived-1.2.12]#cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ #新建主配置文件目錄 [root@lvs-a keepalived-1.2.12]#mkdir -p /etc/keepalived #編輯配置文件LVS1
[root@lvs-a sysconfig]#vim /etc/keepalived/keepalived.conf #192.168.9.231 #192.168.9.232
}
}virtual_server 192.168.9.230 3306 { ##定義虛擬服務(wù)器 delay_loop 6 #健康檢查時(shí)間,單位是秒。 lb_algo rr #負(fù)載調(diào)度算法,這里設(shè)置為rr,即輪詢算法 lb_kind DR #LVS實(shí)現(xiàn)負(fù)載均衡的機(jī)制,可以有NAT、TUN和DR三個(gè)模式可選 nat_mask 255.255.255.0 #可以不填寫這個(gè)參數(shù), # persistence_timeout 50 protocol TCP real_server 192.168.9.29 3306 { ##要分發(fā)的目標(biāo)服務(wù)器IP, weight 1 #設(shè)置權(quán)重,也就是分發(fā)的次數(shù),這里表示每個(gè)分發(fā)一次, TCP_CHECK { #通過(guò)tcpcheck判斷RealServer的健康狀態(tài) connect_timeout 3 ##連接超時(shí)時(shí)間
#nb_get_retry 3 #重連次數(shù) ,有默認(rèn)值 #配置完成重啟keepalived [root@lvs-a sysconfig]#service keepalived restart #添加自己的網(wǎng)關(guān) [root@lvs-a network-scripts]# route add -host 192.168.9.254 dev eth0
#安裝keepalived同lvs1相同 #安裝keepalived后配置LVS2,配置如下: [root@lvs-b ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
#配置完成重啟keepalived service keepalived restart #添加自己的網(wǎng)關(guān) route add -host 192.168.9.254 dev eth0 #查看 route -n #查看分發(fā)結(jié)果 Ipvsadm -ln #清除分發(fā) Ipvsadm -C #查看虛擬ip(VIP) Ip addr #配置 mysql-realserver1 在192.168.9.29上執(zhí)行: [root@lvs-a ~]# ifconfig lo:0 192.168.9.230 netmask 255.255.255.0 broadcast 192.168.9.230 up ##設(shè)置的一個(gè)臨時(shí)的IP 設(shè)置到主機(jī)的路由 [root@lvs-b ~]# route add -host 192.168.9.230 dev lo:0 設(shè)置默認(rèn)路由 [root@lvs-b ~]#route add default gw 192.168.9.137 保證arp協(xié)議在連接過(guò)程中,路由器上,只知道網(wǎng)絡(luò)中192.168.9.230對(duì)應(yīng)的是分發(fā)器 [root@lvs-b ~]#echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore [root@lvs-b ~]#echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce #配置 mysql-realserver2 在192.168.9.30上執(zhí)行: [root@lvs-a ~]# ifconfig lo:0 192.168.9.230 netmask 255.255.255.0 broadcast 192.168.9.230 up設(shè)置到主機(jī)的路由 [root@lvs-a ~]#route add -host 192.168.9.230 dev lo:0 設(shè)置默認(rèn)路由 [root@lvs-a ~]#route add default gw 192.168.153.137
保證arp協(xié)議在連接過(guò)程中,路由器上,只知道網(wǎng)絡(luò)中192.168.9.230對(duì)應(yīng)的是分發(fā)器 [root@lvs-a ~]#echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore [root@lvs-a ~]#echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
Service mysqld start LVS分發(fā)測(cè)試: 1,在 mysql-realserver1(192.168.9.29) 創(chuàng)建一個(gè)名字叫做slave1的數(shù)據(jù)庫(kù): mysql> create database slave1; Query OK, 1 row affected (0.00 sec) 2,在mysql-realserver2 (192.168.9.30)上創(chuàng)建一個(gè)名字叫做slave2的數(shù)據(jù)庫(kù): mysql> create database slave2 ; Query OK, 1 row affected (0.00 sec) 3,在一臺(tái)能ping通mysql-realserver1和mysql-realserver2的機(jī)器上執(zhí)行(注意不要在正在用著的lvs機(jī)器上執(zhí)行,例如我現(xiàn)在lvs用著[root@lvs-a ~]#這個(gè)機(jī)器,如果你在這里執(zhí)行他會(huì)報(bào)錯(cuò)
[root@lvs-a ~]# mysql -uroot -pliuwenhe -h192.168.9.230 -e'show databases'; [root@lvs-b ~]# mysql -uroot -pliuwenhe -h192.168.9.230 -e'show databases';
+--------------------+
說(shuō)明lvs分發(fā)成功。
|
|