Chinaunix首页 | 论坛 | 博客
  • 博客访问: 228453
  • 博文数量: 24
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 1007
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-19 12:10
个人简介

人若不能在内心找到安宁,在哪里也无济于事。

文章分类

全部博文(24)

文章存档

2013年(24)

我的朋友

分类: 系统运维

2013-08-13 22:44:11

lpvsadm
IPVS(IP Virtual Server)是整个负载均衡的基础,如果没有这个基础,故障隔离与失败切换就毫无意义了。
IPVS 具体实现是由ipvsadm这个程序来完成。ipvsadm只要安装好就可以,所有配置由keepalived.conf这个文件来实现。
keepalived
在这里主要作用是Realserver的健康状态检查及故障隔离;负载均衡Master机和Backup主机之间失败切换的实现(failover)。
mysql
这里的两个mysql没有做主从,仅是单纯的实现负载均衡;适用于多个从数据库的轮询,就是将这些从mysql承担的查询压力
分散开来,并且实现这些节点的故障隔离及节点控制端的高可用。

一、准备工作
1.server准备
virtual ip              192.168.1.130    由master提供,master出现故障时由backup提供
server01 (master)       192.168.1.120    提供vip,并实时监测mysql01、mysql02的健康状态
server02 (backup)       192.168.1.121    备用机,master出现故障时接替其工作
mysql01 (realserver01)  192.168.1.122    真实地提供mysql服务,与mysql02做轮询
mysql02 (realserver02)  192.168.1.123    真实地提供mysql服务,与mysql01做轮询
2.所需软件
system:      Redhat 2.6.32-279.el6.x86_64
ipvsadm:    
keepalived:  
二、安装配置
1.安装ipvsadm (master、backup都要安装)
  1. # ln -s /usr/src/kernels/2.6.32-279.el6.x86_64/ /usr/src/linux/
  2. # tar zxvf ipvsadm-1.26.tar.gz
  3. # cd ipvsadm-1.26
  4. # make && make install
  5. # ipvsadm -ln
  6. IP Virtual Server version 1.2.1 (size=4096)
  7. Prot LocalAddress:Port Scheduler Flags
  8.   -> RemoteAddress:Port Forward Weight ActiveConn InActConn
2.安装keepalived (master、backup都要安装配置,注意配置文件的区别,其它都相同)
  1. # tar zxvf keepalived-1.2.0.tar.gz
  2. # cd keepalived-1.2.0
  3. # ./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.32-279.el6.x86_64/
  4. # make && make install
  5. # cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived
  6. # cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
  7. # mkdir /etc/keepalived
  8. # cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
  9. # cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
  10. master配置文件:
  11. # vi /etc/keepalived/keepalived.conf
  12. ! Configuration File for keepalived
  13. global_defs {
  14.    notification_email {
  15.         root@localhost
  16.    }
  17.    notification_email_from root@localhost
  18.    smtp_server 127.0.0.1
  19.    smtp_connect_timeout 30
  20.    router_id LVS_MASTER
  21. }

  22. vrrp_instance VI_1 {
  23.     state MASTER --backup此处需要修改为BACKUP
  24.     interface eth0
  25.     virtual_router_id 51
  26.     priority 100 --backup此处值应略小一些,如90
  27.     advert_int 1
  28.     authentication {
  29.         auth_type PASS
  30.         auth_pass 1111
  31.     }
  32.     virtual_ipaddress {
  33.         192.168.1.130
  34.     }
  35. }

  36. virtual_server 192.168.1.130 3306 {
  37.     delay_loop 6
  38.     lb_algo wrr
  39.     lb_kind DR
  40. # persistence_timeout 50 --表示同一ip50秒内的连接都发送到同一realserver,为了测试轮询,先注释
  41.     protocol TCP

  42.     real_server 192.168.1.122 3306 {
  43.         weight 3
  44.         TCP_CHECK {
  45.             connect_timeout 10
  46.             nb_get_retry 3
  47.             delay_before_retry 3
  48.             connect_port 3306
  49.         }
  50.     }
  51.     real_server 192.168.1.123 3306 {
  52.         weight 3
  53.         TCP_CHECK {
  54.             connect_timeout 10
  55.             nb_get_retry 3
  56.             delay_before_retry 3
  57.             connect_port 3306
  58.         }
  59.     }
  60. }

  61. backup配置文件:
  62. 与master相同,仅两处需要修改为:
  63. state MASTER --> state BACKUP
  64. priority 100 --> priority 90

  65. # chkconfig keepalived on
  66. # service keepalived start
  67. # service keepalived status
  68. keepalived (pid 1344) 正在运行...
3.realserver.sh脚本(mysql01 mysql02 都要部署)
  1. vi /root/bin/realserver.sh
  2. --------------------------------------------------------------
  3. #!/bin/bash
  4. vip=192.168.1.130
  5. . /etc/rc.d/init.d/functions
  6. case "$1" in
  7.     start)
  8.         /sbin/ifconfig lo:0 $vip broadcast $vip netmask 255.255.255.255 up
  9.         /sbin/route add -host $vip dev lo:0
  10.         echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
  11.         echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
  12.         echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
  13.         echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
  14.         sysctl -p >/dev/null 2>&1
  15.         echo "RealServer Start OK" ;;
  16.     stop)
  17.         /sbin/ifconfig lo:0 down
  18.         /sbin/route del $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.      *) echo "Usage: $0 {start|stop}"
  25.         exit 1;;
  26. esac
  27. exit 0
  28. --------------------------------------------------------------
  29. # chmod +x /root/bin/realserver.sh
  30. # sh /root/bin/realserver.sh start
  31. # echo "sh /root/bin/realserver.sh start" >> /etc/rc.local
4.启动mysql01、mysql02上的mysql服务,为了更好区分,分别执行:
  1. mysql> grant all on *.* to 'mstsc'@'192.168.1.%' identified by 'mstsc';
  2. mysql> flush privileges;
  3. mysql> create database mysql_122;

  4. mysql> grant all on *.* to 'mstsc'@'192.168.1.%' identified by 'mstsc';
  5. mysql> flush privileges;
  6. mysql> create database mysql_123
三、测试
1.查看状态,并测试页面是否正常轮询 (此时master、backup上都启动keepalived,mysql01、mysql02都启动mysql)
  1. # ip a|grep 192.168
  2.     inet 192.168.1.120/24 brd 192.168.1.255 scope global eth0
  3.     inet 192.168.1.130/32 scope global eth0
  4. # ipvsadm -ln
  5. IP Virtual Server version 1.2.1 (size=4096)
  6. Prot LocalAddress:Port Scheduler Flags
  7.   -> RemoteAddress:Port Forward Weight ActiveConn InActConn
  8. TCP 192.168.1.130:3306 rr
  9.   -> 192.168.1.122:3306 Route 3 1 0
  10.   -> 192.168.1.123:3306 Route 3 0 1
  11. # mysql -h192.168.1.130 -umstsc -p
  12. Enter password:
  13. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  14. mysql> show databases;
  15. +--------------------+
  16. | Database |
  17. +--------------------+
  18. | information_schema |
  19. | mysql |
  20. | mysql_123 |
  21. | test |
  22. +--------------------+
  23. 4 rows in set (0.01 sec)
  24. mysql> quit

  25. [root@server02 keepalived]# mysql -h192.168.1.130 -umstsc -p
  26. Enter password:
  27. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  28. mysql> show databases;
  29. +--------------------+
  30. | Database |
  31. +--------------------+
  32. | information_schema |
  33. | mysql |
  34. | mysql_122 |
  35. | test |
  36. +--------------------+
  37. 4 rows in set (0.00 sec)
  38. 状态及轮询测试正常
2.停止master上的keepalived,查看backup是否正常接管vip,再测试页面轮询是否正常
  1. master
  2. # service keepalived stop
  3. 停止 keepalived: [确定]
  4. # ip a|grep 192.168
  5.     inet 192.168.1.120/24 brd 192.168.1.255 scope global eth0 
  6. backup
  7. # ip a |grep 192.168
  8.     inet 192.168.1.121/24 brd 192.168.1.255 scope global eth0
  9.     inet 192.168.1.130/32 scope global eth0
  10. [root@server02 keepalived]# ipvsadm -ln
  11. IP Virtual Server version 1.2.1 (size=4096)
  12. Prot LocalAddress:Port Scheduler Flags
  13.   -> RemoteAddress:Port Forward Weight ActiveConn InActConn
  14. TCP 192.168.1.130:3306 rr
  15.   -> 192.168.1.122:3306 Route 3 0 0
  16.   -> 192.168.1.123:3306 Route 3 0 0
  17. backup正常接管
  18. master
  19. # mysql -h192.168.1.130 -umstsc -p
  20. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  21. mysql> show databases;
  22. +--------------------+
  23. | Database |
  24. +--------------------+
  25. | information_schema |
  26. | mysql |
  27. | mysql_123 |
  28. | test |
  29. +--------------------+
  30. 4 rows in set (0.01 sec)

  31. # mysql -h192.168.1.130 -umstsc -p
  32. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

  33. mysql> show databases;
  34. +--------------------+
  35. | Database |
  36. +--------------------+
  37. | information_schema |
  38. | mysql |
  39. | mysql_122 |
  40. | test |
  41. +--------------------+
  42. 4 rows in set (0.00 sec)
  43. 轮询正常
3.停止mysql01上的mysql,查看keepalived能否正常检测出并隔离故障机器
  1. mysql01
  2. # service mysqld stop
  3. backup
  4. # ipvsadm -ln
  5. IP Virtual Server version 1.2.1 (size=4096)
  6. Prot LocalAddress:Port Scheduler Flags
  7.   -> RemoteAddress:Port Forward Weight ActiveConn InActConn
  8. TCP 192.168.1.130:3306 rr
  9.   -> 192.168.1.123:3306 Route 3 0 0
  10. master
  11. # mysql -h192.168.1.130 -umstsc -p
  12. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  13. mysql> show databases;
  14. +--------------------+
  15. | Database |
  16. +--------------------+
  17. | information_schema |
  18. | mysql |
  19. | mysql_123 |
  20. | test |
  21. +--------------------+
  22. 4 rows in set (0.00 sec)

  23. # mysql -h192.168.1.130 -umstsc -p
  24. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  25. mysql> show databases;
  26. +--------------------+
  27. | Database |
  28. +--------------------+
  29. | information_schema |
  30. | mysql |
  31. | mysql_123 |
  32. | test |
  33. +--------------------+
  34. 4 rows in set (0.00 sec)
  35. mysql01节点故障被检测到并被隔离,master连续访问mysql,此时只能访问到mysql02这个real server提供的mysql服务。

阅读(3193) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~