`

keepalived+lvs+mysql cluster架构实现负载均衡

阅读更多

转自:http://blog.csdn.net/chenxingzhen001/article/details/7615012

 

一、环境

用途 ip os memory
调度器 172.16.48.204 CentOS 5.5 64bit 32G
sql节点1 172.16.48.206 CentOS 5.5 64bit 32G
sql节点2 172.16.48.207 CentOS 5.5 64bit 32G
sql节点3 172.16.48.208 CentOS 5.5 64bit 32G
sql节点4 172.16.48.211 CentOS 5.5 64bit 32G

VIP设置为:172.100.100.251 (注:不能和调度器、sql节点在一个网段上)

在调度器172.16.48.204电脑上,用route add default dev eth1
命令添加路由,使得
172.16.48.204节点有对100网段的访问权限。

二、架构图


这里调度器只用了一个节点,没有设置Backup

三、软件下载

lvs:    http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz

keepalived:     http://www.keepalived.org/download.html

四、软件安装

编辑lvs的时候需要操作系统的内核,不然会报错

configure: error: 
!!! OpenSSL is not properly installed on your system. !!!
!!! Can not include OpenSSL headers files. !!!

解决方法:yum install openssl*

可能 还会报错:libipvs.c:253: 错误:提领指向不完全类型的指针

解决方法:yum -y install kernel-devel    可参考:http://692344.blog.51cto.com/blog/682344/804278

如果还是不行,可能是由于lvs keepalived的版本过高

1.在调度器172.16.48.204上安装lvs

ln -s /usr/src/kernels/2.6.18-164.el5-i686/ /usr/src/linux
tar zxvf ipvsadm-1.24.tar.gz
cd ipvsadm-1.24
make && make install

2.在调度器172.16.48.204上安装keepalived

tar zxvf keepalived-1.1.19.tar.gz
cd keepalived-1.1.19
./configure --prefix=/usr/local/keepalived
make 
make install
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
mkdir /etc/keepalived

3.配置keepalived.conf

vi /etc/keepalived/keepalived.conf
添加如下内容:

[plain] view plaincopy
 
  1. global_defs {  
  2.     router_id HaMySQL_1  
  3. }  
  4. vrrp_sync_group VGM {  
  5.     group {  
  6.         VI_MYSQL  
  7.     }  
  8. }  
  9. vrrp_instance VI_MYSQL {  
  10.     state MASTER  
  11.     interface eth1  
  12.     virtual_router_id 100  
  13.     priority 100  
  14.     advert_int 1  
  15.     authentication {  
  16.         auth_type PASS  
  17.         auth_pass 1111  
  18.     }  
  19.     virtual_ipaddress {  
  20.         172.100.100.251/24  etho label eth0:1  
  21.     }  
  22. }  
  23.   
  24. virtual_server 172.100.100.251 3306 {  
  25.     delay_loop 6  
  26.     lb_algo rr  
  27.     lb_kind DR  
  28.     # nat_mask 255.255.0.0  
  29. #persistence_timeout 50  
  30.     protocol TCP  
  31.   
  32.     real_server 172.16.48.206 3306 {  
  33.         weight 3  
  34.         TCP_CHECK {  
  35.         connect_timeout 3  
  36.         nb_get_retry 3  
  37.         delay_before_retry 3  
  38.         connect_port 3306  
  39.         }  
  40.     }  
  41.   
  42.     real_server 172.16.48.207 3306 {  
  43.         weight 3  
  44.         TCP_CHECK {  
  45.         connect_timeout 3  
  46.         nb_get_retry 3  
  47.         delay_before_retry 3  
  48.         connect_port 3306  
  49.         }  
  50.     }  
  51.     real_server 172.16.48.208 3306 {  
  52.         weight 3  
  53.         TCP_CHECK {  
  54.         connect_timeout 3  
  55.         nb_get_retry 3  
  56.         delay_before_retry 3  
  57.  connect_port 3306  
  58.         }  
  59.     }  
  60.     real_server 172.16.48.211 3306 {  
  61.         weight 3  
  62.         TCP_CHECK {  
  63.         connect_timeout 3  
  64.         nb_get_retry 3  
  65.         delay_before_retry 3  
  66.         connect_port 3306  
  67.         }  
  68.     }  
  69. }  


4.sql服务节点的配置

vi /etc/rc.d/init.d/realserver.sh 添加如下配置:

[plain] view plaincopy
 
  1. #!/bin/bash  
  2. # description: Config realserver lo and apply noarp  
  3. SNS_VIP=172.100.100.251  
  4. /etc/rc.d/init.d/functions  
  5. case "$1" in  
  6. start)  
  7.        ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP  
  8.        /sbin/route add -host $SNS_VIP dev lo:0  
  9.        echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore  
  10.        echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce  
  11.        echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore  
  12.        echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce  
  13.        sysctl -p >/dev/null 2>&1  
  14.        echo "RealServer Start OK"  
  15.        ;;  
  16. stop)  
  17.        ifconfig lo:0 down  
  18.        route del $SNS_VIP >/dev/null 2>&1  
  19.        echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore  
  20.        echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce  
  21.        echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore  
  22.        echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce  
  23.        echo "RealServer Stoped"  
  24.        ;;  
  25. *)  
  26.        echo "Usage: $0 {start|stop}"  
  27.        exit 1  
  28. esac  
  29. exit 0  

添加加可执行的权限
chmod +x /etc/rc.d/init.d/realserver.sh

注:四台sql节点都要进行此步操作

五、启动LVS节点(mysql cluster中的sql节点)

/etc/rc.d/init.d/realserver.sh start

六、启动调度器(keepalived的服务器端启动)
/etc/rc.d/init.d/keepalived start

启动后可以用命令 tail -f /var/log/messages 进行查看

若看到如下提示,则表示配置成功。

[plain] view plaincopy
 
  1. [root@sg204 mysql-cluster]# tail -f /var/log/messages  
  2. May 29 17:21:01 sg204 Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'.   
  3. May 29 17:21:01 sg204 Keepalived_vrrp: Configuration is using : 63921 Bytes  
  4. May 29 17:21:01 sg204 Keepalived_healthcheckers: Using LinkWatch kernel netlink reflector...  
  5. May 29 17:21:01 sg204 Keepalived_vrrp: Using LinkWatch kernel netlink reflector...  
  6. May 29 17:21:01 sg204 Keepalived_healthcheckers: Activating healtchecker for service [172.16.48.206:3306]  
  7. May 29 17:21:01 sg204 Keepalived_healthcheckers: Activating healtchecker for service [172.16.48.207:3306]  
  8. May 29 17:21:01 sg204 Keepalived_healthcheckers: Activating healtchecker for service [172.16.48.208:3306]  
  9. May 29 17:21:01 sg204 Keepalived_healthcheckers: Activating healtchecker for service [172.16.48.211:3306]  
  10. May 29 17:21:01 sg204 Keepalived_vrrp: VRRP sockpool: [ifindex(3), proto(112), fd(10,11)]  
  11. May 29 17:21:02 sg204 Keepalived_vrrp: VRRP_Instance(VI_MYSQL) Transition to MASTER STATE  
  12. May 29 17:21:03 sg204 Keepalived_vrrp: VRRP_Instance(VI_MYSQL) Entering MASTER STATE  
  13. May 29 17:21:03 sg204 Keepalived_vrrp: VRRP_Instance(VI_MYSQL) setting protocol VIPs.  
  14. May 29 17:21:03 sg204 Keepalived_healthcheckers: Netlink reflector reports IP 172.100.100.251 added  
  15. May 29 17:21:03 sg204 Keepalived_vrrp: VRRP_Instance(VI_MYSQL) Sending gratuitous ARPs on eth1 for 172.100.100.251  
  16. May 29 17:21:03 sg204 Keepalived_vrrp: VRRP_Group(VGM) Syncing instances to MASTER state  
  17. May 29 17:21:03 sg204 avahi-daemon[4768]: Registering new address record for 172.100.100.251 on eth1.  
  18. May 29 17:21:03 sg204 Keepalived_vrrp: Netlink reflector reports IP 172.100.100.251 added  
  19. May 29 17:21:08 sg204 Keepalived_vrrp: VRRP_Instance(VI_MYSQL) Sending gratuitous ARPs on eth1 for 172.100.100.251  


在调度器上用ipvsadm命令查看,可以看到如下信息

[plain] view plaincopy
 
  1. [root@sg204 mysql-cluster]# ipvsadm  
  2. IP Virtual Server version 1.2.1 (size=4096)  
  3. Prot LocalAddress:Port Scheduler Flags  
  4.   -> RemoteAddress:Port           Forward Weight ActiveConn InActConn  
  5. TCP  172.100.100.251:mysql rr  
  6.   -> 172.16.48.211:mysql          Route   3      0          0           
  7.   -> 172.16.48.208:mysql          Route   3      0          0           
  8.   -> 172.16.48.207:mysql          Route   3      0          0           
  9.   -> 172.16.48.206:mysql          Route   3      0          0   


七、远程应用通过vip访问mysql数据库


要远程应用程序访问mysql数据库,还需要一个远程访问的账号和密码,在mysql刚创建后,mysql是没有远程访问账号的,

在sql节点上增加远程认证客户:

grant 权限1,权限2,…权限n on 数据库名称.表名称 to 用户名@用户地址 identified by ‘连接口令’

[plain] view plaincopy
 
  1. Mysql>:select user,host,password from mysql.user;    
  2. Mysql>: grant all privileges on *.* to admin@"%"  identified by '111111' with grant option;  


八、测试

在其他 节点上(调度器和sql节点之外的电脑),ping 172.100.100.251发现可以ping通

这里选用的是172.16.48.201节点:

[plain] view plaincopy
 
  1. [root@sg201 ~]# ping 172.100.100.251  
  2. PING 172.100.100.251 (172.100.100.251) 56(84) bytes of data.  
  3. 64 bytes from 172.100.100.251: icmp_seq=1 ttl=64 time=0.093 ms  
  4. 64 bytes from 172.100.100.251: icmp_seq=2 ttl=64 time=0.164 ms  
  5. 64 bytes from 172.100.100.251: icmp_seq=3 ttl=64 time=0.162 ms  


在172.16.48.201上安装mysql客户端后,用命令 mysql -u admin -p -h 172.100.100.251测试发现可以 连接数据库,

将172.16.48.208上的mysql服务停掉后,这时在高度器上tail -f var/log/messages会看到调度器自动将无效sql节点移除lvs了

[plain] view plaincopy
 
  1. May 30 11:04:11 sg204 Keepalived_healthcheckers: TCP connection to [172.16.48.208:3306] failed !!!  
  2. May 30 11:04:11 sg204 Keepalived_healthcheckers: Removing service [172.16.48.208:3306] from VS [172.100.100.251:3306]  


再将172.16.48.208上的mysql服务启动起来,这时

[plain] view plaincopy
 
  1. May 30 11:06:53 sg204 Keepalived_healthcheckers: TCP connection to [172.16.48.208:3306] success.  
  2. May 30 11:06:53 sg204 Keepalived_healthcheckers: Adding service [172.16.48.208:3306] to VS [172.100.100.251:3306]  


哈哈!成功搞定

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics