转自: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
添加如下内容:
- global_defs {
- router_id HaMySQL_1
- }
- vrrp_sync_group VGM {
- group {
- VI_MYSQL
- }
- }
- vrrp_instance VI_MYSQL {
- state MASTER
- interface eth1
- virtual_router_id 100
- priority 100
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 172.100.100.251/24 etho label eth0:1
- }
- }
- virtual_server 172.100.100.251 3306 {
- delay_loop 6
- lb_algo rr
- lb_kind DR
- # nat_mask 255.255.0.0
- #persistence_timeout 50
- protocol TCP
- real_server 172.16.48.206 3306 {
- weight 3
- TCP_CHECK {
- connect_timeout 3
- nb_get_retry 3
- delay_before_retry 3
- connect_port 3306
- }
- }
- real_server 172.16.48.207 3306 {
- weight 3
- TCP_CHECK {
- connect_timeout 3
- nb_get_retry 3
- delay_before_retry 3
- connect_port 3306
- }
- }
- real_server 172.16.48.208 3306 {
- weight 3
- TCP_CHECK {
- connect_timeout 3
- nb_get_retry 3
- delay_before_retry 3
- connect_port 3306
- }
- }
- real_server 172.16.48.211 3306 {
- weight 3
- TCP_CHECK {
- connect_timeout 3
- nb_get_retry 3
- delay_before_retry 3
- connect_port 3306
- }
- }
- }
4.sql服务节点的配置
vi /etc/rc.d/init.d/realserver.sh 添加如下配置:
- #!/bin/bash
- # description: Config realserver lo and apply noarp
- SNS_VIP=172.100.100.251
- /etc/rc.d/init.d/functions
- case "$1" in
- start)
- ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
- /sbin/route add -host $SNS_VIP dev lo:0
- echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
- echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
- echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
- echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
- sysctl -p >/dev/null 2>&1
- echo "RealServer Start OK"
- ;;
- stop)
- ifconfig lo:0 down
- route del $SNS_VIP >/dev/null 2>&1
- echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
- echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
- echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
- echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
- echo "RealServer Stoped"
- ;;
- *)
- echo "Usage: $0 {start|stop}"
- exit 1
- esac
- 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 进行查看
若看到如下提示,则表示配置成功。
- [root@sg204 mysql-cluster]# tail -f /var/log/messages
- May 29 17:21:01 sg204 Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'.
- May 29 17:21:01 sg204 Keepalived_vrrp: Configuration is using : 63921 Bytes
- May 29 17:21:01 sg204 Keepalived_healthcheckers: Using LinkWatch kernel netlink reflector...
- May 29 17:21:01 sg204 Keepalived_vrrp: Using LinkWatch kernel netlink reflector...
- May 29 17:21:01 sg204 Keepalived_healthcheckers: Activating healtchecker for service [172.16.48.206:3306]
- May 29 17:21:01 sg204 Keepalived_healthcheckers: Activating healtchecker for service [172.16.48.207:3306]
- May 29 17:21:01 sg204 Keepalived_healthcheckers: Activating healtchecker for service [172.16.48.208:3306]
- May 29 17:21:01 sg204 Keepalived_healthcheckers: Activating healtchecker for service [172.16.48.211:3306]
- May 29 17:21:01 sg204 Keepalived_vrrp: VRRP sockpool: [ifindex(3), proto(112), fd(10,11)]
- May 29 17:21:02 sg204 Keepalived_vrrp: VRRP_Instance(VI_MYSQL) Transition to MASTER STATE
- May 29 17:21:03 sg204 Keepalived_vrrp: VRRP_Instance(VI_MYSQL) Entering MASTER STATE
- May 29 17:21:03 sg204 Keepalived_vrrp: VRRP_Instance(VI_MYSQL) setting protocol VIPs.
- May 29 17:21:03 sg204 Keepalived_healthcheckers: Netlink reflector reports IP 172.100.100.251 added
- May 29 17:21:03 sg204 Keepalived_vrrp: VRRP_Instance(VI_MYSQL) Sending gratuitous ARPs on eth1 for 172.100.100.251
- May 29 17:21:03 sg204 Keepalived_vrrp: VRRP_Group(VGM) Syncing instances to MASTER state
- May 29 17:21:03 sg204 avahi-daemon[4768]: Registering new address record for 172.100.100.251 on eth1.
- May 29 17:21:03 sg204 Keepalived_vrrp: Netlink reflector reports IP 172.100.100.251 added
- May 29 17:21:08 sg204 Keepalived_vrrp: VRRP_Instance(VI_MYSQL) Sending gratuitous ARPs on eth1 for 172.100.100.251
在调度器上用ipvsadm命令查看,可以看到如下信息
- [root@sg204 mysql-cluster]# ipvsadm
- IP Virtual Server version 1.2.1 (size=4096)
- Prot LocalAddress:Port Scheduler Flags
- -> RemoteAddress:Port Forward Weight ActiveConn InActConn
- TCP 172.100.100.251:mysql rr
- -> 172.16.48.211:mysql Route 3 0 0
- -> 172.16.48.208:mysql Route 3 0 0
- -> 172.16.48.207:mysql Route 3 0 0
- -> 172.16.48.206:mysql Route 3 0 0
七、远程应用通过vip访问mysql数据库
要远程应用程序访问mysql数据库,还需要一个远程访问的账号和密码,在mysql刚创建后,mysql是没有远程访问账号的,
在sql节点上增加远程认证客户:
grant 权限1,权限2,…权限n on 数据库名称.表名称 to 用户名@用户地址 identified by ‘连接口令’
- Mysql>:select user,host,password from mysql.user;
- Mysql>: grant all privileges on *.* to admin@"%" identified by '111111' with grant option;
八、测试
在其他 节点上(调度器和sql节点之外的电脑),ping 172.100.100.251发现可以ping通
这里选用的是172.16.48.201节点:
- [root@sg201 ~]# ping 172.100.100.251
- PING 172.100.100.251 (172.100.100.251) 56(84) bytes of data.
- 64 bytes from 172.100.100.251: icmp_seq=1 ttl=64 time=0.093 ms
- 64 bytes from 172.100.100.251: icmp_seq=2 ttl=64 time=0.164 ms
- 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了
- May 30 11:04:11 sg204 Keepalived_healthcheckers: TCP connection to [172.16.48.208:3306] failed !!!
- 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服务启动起来,这时
- May 30 11:06:53 sg204 Keepalived_healthcheckers: TCP connection to [172.16.48.208:3306] success.
- May 30 11:06:53 sg204 Keepalived_healthcheckers: Adding service [172.16.48.208:3306] to VS [172.100.100.251:3306]
哈哈!成功搞定
相关推荐
基于mysql的负载均衡有很多种方式,如haproxy,前面一篇博客有介绍,还可以用更高效lvs做负载均衡,下面是基于percona xtradb cluster的三个节点的多主复制+atlas的lvs负载均衡,其实这里是不需要用atlas的,因为...
- LVS是Linux内核中的负载均衡技术,它可以在不修改应用代码的情况下,实现对服务请求的负载均衡,提高服务的可用性和响应速度。 - 在此方案中,LVS使用DR(Direct Routing)模式,这意味着客户端的连接请求直接...
**MySQL数据库集群+负载均衡(LVS)** 这个主题涵盖了构建一个高可用性、高性能的MySQL集群,并通过负载均衡技术来实现对集群中多个MySQL实例请求的智能分发。这种架构能够显著提高系统的稳定性和响应速度,适用于大...
在实验研究中,作者通过部署MySQL Cluster和Keepalived的组合,验证了这种架构可以有效实现数据同步和故障转移。在设置合理的集群参数和Keepalived策略后,系统在主服务器故障时能快速恢复服务,同时保持数据的完整...
第26讲 配置mysql+lvs+keeplived实现mysql读操作的负载均衡 课程目标: 熟练掌握mysql+lvs+keeplived的部署方式 keeplived简介 通过配置lvs+keeplived实现mysql读操作的负载均衡 测试mysql+lvs+keeplived负载均衡...
【财华社项目部署安装文档】是一份详细指导在8台服务器上搭建LVS+Keepalived+Nginx+Tomcat+FastDFS+Mysql-Cluster+Redis的实施方案。该文档旨在实现高可用性和负载均衡的系统架构,以确保服务的稳定运行。 首先,...
此外,为了实现高可用性和负载均衡,通常还会结合其他工具,如Keepalived和LVS(Linux Virtual Server)。Keepalived可以监控MySQL服务的状态,当主节点失效时,自动将流量切换到备用节点。LVS则负责在集群内的...
LVS(Linux Virtual Server)和keepalived用于设置高可用的负载均衡系统。 5.1 LVS-master配置:配置LVS主节点,负责流量调度和转发。 5.2 安装keepalived:keepalived是LVS的互补工具,提供心跳检测和故障转移...
1. 写入瓶颈:可以使用多 Master、Cluster、MySQL Sharing、DBProxy(LVS+Keepalived)等方案来解决写入瓶颈问题。 2. 写入瓶颈层面解决:可以使用消息队列来解决写入瓶颈问题。 3. 备份问题:可以使用多级备份、...
由国内著名技术社区联合推荐的2012年IT技术力作:《高性能Linux...14.4.2 利用LVS+Keepalived搭建高可用MySQL Slave集群 14.4.3 高可用Slave集群的一些注意点 14.5 部署MySQL集群要考虑的问题 14.6 本章小结
书中会涵盖高可用性(HA)集群、负载均衡器(Load Balancer)的配置,如Keepalived、HAProxy、LVS等,以及分布式文件系统如GlusterFS和Ceph的使用。此外,还会讲解如何实现数据库集群(如MySQL的InnoDB Cluster)和应用...
【IT运维中间件全面学习文档】涵盖了众多中间件技术,如MySQL、Redis、Tomcat、Nginx、Zabbix、Ansible、Docker、LVS+Keepalived、JDK、Kafka、MongoDB、Zookeeper、Kubernetes(K8s)、ELK、HBase、HDFS、Elastic...