前端用两台server做lvs+keepalived负载调度器,中间用apache+php作为web应用服务器,后端用两台做mysql高可用,用nfs、mfs或作为web文件共享服务器

 
系统环境:
LVS调度主服务器:192.168.8.11
LVS调度从服务器:192.168.8.12
LVS VIP:192.168.8.10
apache服务器:192.168.8.20  192.168.8.21
MySQL主服务器:192.168.8.31
MySQL从服务器:192.168.8.32
MySQL VIP:192.168.8.30
 
 
由于工作时间比较紧,同时还要培训,架构图后期补上!!
 
用到软件准备:
所有服务器软件包都放在/opt 目录下,下面是下载地址:
  1. wget http://www.keepalived.org/software/keepalived-1.1.20.tar.gz 
  2. wget http://downloads.mysql.com/archives/mysql-5.5/mysql-5.5.24-linux2.6-x86_64.tar.gz  #64位 
  3. wget http://downloads.mysql.com/archives/mysql-5.5/mysql-5.5.24-linux2.6-i686.tar.gz    #32位 
一、LVS+Keepalived
1.安装ipvsadm
yum -y install ipvsadm kernel-devel
ln -sv /usr/src/kernels/2.6.18-308.8.2.el5-x86_64/ /usr/src/linux
 
2.安装keepalived
  1. cd /opt/ 
  2. tar zxf keepalived-1.1.20.tar.gz 
  3. cd keepalived-1.1.20 
  4. ./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.18-308.11.1.el5-x86_64/ 
  5.  
  6. 出现如下信息内核加载成功: 
  7. Keepalived configuration 
  8. ------------------------ 
  9. Keepalived version       : 1.1.20 
  10. Compiler                 : gcc 
  11. Compiler flags           : -g -O2 
  12. Extra Lib                : -lpopt -lssl -lcrypto  
  13. Use IPVS Framework       : Yes 
  14. IPVS sync daemon support : Yes 
  15. Use VRRP Framework       : Yes 
  16. Use Debug flags          : No 
  17. make;make install 
  18. cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d 
  19. cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ 
  20. mkdir /etc/keepalived 
  21. chkconfig keepalived on 

3.创建配置文件
  1. cat /etc/keepalived/keepalived.conf 
  2.  
  3. ! Configuration File for keepalived 
  4.   
  5. global_defs { 
  6.    notification_email { 
  7.       250621008@qq.com 
  8.    } 
  9.    notification_email_from 250621008@qq.com 
  10.    smtp_server smtp.163.com 
  11.   #smtp_connect_timeout 30 
  12.    router_id LVS_DEVEL 
  13.  
  14. vrrp_instance VI_1 { 
  15.     state MASTER             #备份服务器上将MASTER改为BACKUP    
  16.     interface eth0 
  17.     lvs_sync_daemon_inteface eth0 
  18.     virtual_router_id 51 
  19.     priority 100        #备份服务上将100改为低于100的数值 
  20.     advert_int 5 
  21.     authentication { 
  22.         auth_type PASS 
  23.         auth_pass 1111 
  24.     } 
  25.     virtual_ipaddress { 
  26.         192.168.8.10 
  27.     } 
  28.   
  29. virtual_server 192.168.8.10 80 { 
  30.     delay_loop 6                  #(每隔10秒查询realserver状态) 
  31.     lb_algo wlc                   #(lvs 算法) 
  32.     lb_kind DR                    #(Direct Route) 
  33.     persistence_timeout 60        #(同一IP的连接60秒内被分配到同一台realserver) 
  34.     protocol TCP                  #(用TCP协议检查realserver状态) 
  35.   
  36.     real_server 192.168.8.11 80 { 
  37.         weight 100                #(权重) 
  38.         TCP_CHECK { 
  39.         connect_timeout 10        #(10秒无响应超时) 
  40.         nb_get_retry 3 
  41.         delay_before_retry 3 
  42.         connect_port 80 
  43.         } 
  44.     } 
  45.     real_server 192.168.8.12 80 { 
  46.         weight 100 
  47.         TCP_CHECK { 
  48.         connect_timeout 10 
  49.         nb_get_retry 3 
  50.         delay_before_retry 3 
  51.         connect_port 80 
  52.         } 
  53.      } 
PS :从配置文件参考以上主的配置文件,不同的地方是红色标注两处!
 
二、WEB集群
1、这里可以选择lamp、lnmp、lanmp等web架构,至于搭建略!
2、在各web节点上创建realserver脚本
  1. cat /root/sh/lvs_real.sh 
  2. #!/bin/bash 
  3. #Description: Config realserver script 
  4. #Written by : opsren----http://linuxops.blog.51cto.com 
  5. SNS_VIP=192.168.8.10 
  6. /etc/rc.d/init.d/functions 
  7.   
  8. case "$1" in 
  9. start) 
  10.        /sbin/ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP 
  11.        /sbin/route add -host $SNS_VIP dev lo:0 
  12.        echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore 
  13.        echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce 
  14.        echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore 
  15.        echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce 
  16.        sysctl -p >/dev/null 2>&1 
  17.        echo "RealServer Start OK" 
  18.   
  19.        ;; 
  20. stop) 
  21.        /sbin/ifconfig lo:0 down 
  22.        /sbin/route del $SNS_VIP >/dev/null 2>&1 
  23.        echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore 
  24.        echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce 
  25.        echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore 
  26.        echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce 
  27.        echo "RealServer Stoped" 
  28.        ;; 
  29. *) 
  30.        echo "Usage: $0 {start|stop}" 
  31.        exit 1 
  32. esac 
  33.   
  34. exit 0 
  35.  
  36. 给予可执行权限: 
  37. chmod +755 /roo/sh/lvs_real.sh 
  38.  
  39. 启动脚本: 
  40. /root/sh/lvs_real.sh start 

三、MySQL高可用(双主)部署
1.MySQL安装与配置
这里采用mysql5.5.24二进制tar包
tar zxf mysql-5.5.24-linux2.6-x86_64.tar.gz -C /usr/local/mysql5.5
设置配置文件:
  1. cat >> /etc/my.cnf << EOF 
  2. [client] 
  3. port            = 3306 
  4. socket          = /tmp/mysql.sock 
  5. #character-set-server = utf8 
  6.  
  7. [mysqld] 
  8. connect_timeout = 20 
  9. basedir         = /usr/local/mysql 
  10. datadir         = /data/mysql-5.5/data 
  11. user            = mysql 
  12.  
  13. port            = 3306 
  14. socket          = /tmp/mysql.sock 
  15. pid-file        = /data/mysql-5.5/data/mysql.pid 
  16.  
  17. ##init_connect    = 'SET AUTOCOMMIT=1' 
  18. character-set-server = utf8 
  19.  
  20. open_files_limit = 10240 
  21. table_open_cache      = 6144 
  22. table_definition_cache  = 4096 
  23. table_cache = 512 
  24. max_connections         = 1100 
  25. max_connect_errors      = 1000 
  26. back_log        = 500 
  27. event_scheduler = ON 
  28.  
  29. external-locking = FALSE 
  30. skip-name-resolve 
  31. #default_table_type = MyISAM 
  32.  
  33. server-id = 10 
  34.  
  35. #master-host = 192.168.8.31 
  36. #master-user = repl 
  37. #master-password = repl 
  38. #replicate-ignore-db=mysql 
  39. slave-net-timeout=35 
  40. #master-connect-retry=30 
  41. #relay-log = relay-bin 
  42. #max-relay-log-size = 128M 
  43. #skip-slave-start 
  44. #replicate-same-server-id=0 
  45. #log_slave_update 
  46. #replicate-do-db=WATCDB01 
  47. #replicate-do-db=WATCDB02 
  48. #replicate-do-db=WATCDB03 
  49. #replicate-do-db=WATCDB04 
  50.  
  51.  
  52. wait_timeout=100 
  53. interactive_timeout = 100 
  54. ##Add for debug,record all the sql 
  55. ##log                   = /usr/local/mysql/fetiondata/data/g_log.log 
  56. ##innodb_status_file    = 1 
  57. # 
  58. #tmpdir                  = /tmp 
  59. tmpdir                  = /data/mysql-5.5/tmp 
  60.  
  61.  
  62. tmp_table_size          = 32M 
  63. max_heap_table_size     = 64M 
  64.  
  65. thread_cache_size       = 64 
  66. thread_concurrency      = 32 
  67. thread_stack            = 192K 
  68.  
  69. max_allowed_packet      = 16M 
  70.  
  71. sort_buffer_size        = 512k 
  72. join_buffer_size        = 512k 
  73. query_cache_size        = 0 
  74. query_cache_limit       = 2M 
  75. key_buffer_size         = 64M 
  76. read_buffer_size        = 512k 
  77. read_rnd_buffer_size    = 512k 
  78. bulk_insert_buffer_size = 32M 
  79.  
  80. binlog_format           = mixed 
  81. #binlog_cache_size       = 1M 
  82. #log-bin                 = /data/mysql-5.5/logs/mysql-bin 
  83. max_binlog_size         = 128M 
  84.  
  85. #log_long_format 
  86. slow-query-log 
  87. slow-query-log-file = mysql.slow 
  88.  
  89. #log_queries_not_using_indexes   = 0 
  90. long_query_time                 = 1 
  91.  
  92. myisam_sort_buffer_size         = 32M 
  93. myisam_max_sort_file_size       = 10G 
  94. #myisam_max_extra_sort_file_size = 10G 
  95. myisam_repair_threads           = 1 
  96. myisam_recover 
  97.  
  98. log_error               = /data/mysql-5.5/logs/error.log 
  99.  
  100.  
  101. # READ-UNCOMMITTED, READ-COMMITTED, REPEATABLE-READ, SERIALIZABLE 
  102. #default#transaction_isolation = REPEATABLE-READ 
  103. transaction_isolation = READ-COMMITTED 
  104.  
  105. #innodb_checksums        = 0 
  106. #innodb_doublewrite      = 0 
  107. innodb_flush_method     = O_DIRECT 
  108.  
  109. #96G  
  110. innodb_buffer_pool_size         = 48G 
  111. #innodb_buffer_pool_size        = 60G 
  112. #innodb_buffer_pool_instances   = 10 
  113. innodb_buffer_pool_instances    = 4 
  114.  
  115. #innodb_buffer_pool_size         = 33G 
  116. innodb_additional_mem_pool_size = 16M 
  117.  
  118. innodb_data_home_dir    =  
  119. #innodb_data_file_path = ibdata1:20G;ibdata2:20G;ibdata3:20G;ibdata4:20G;ibdata5:20G;ibdata6:20G;ibdata7:20G;ibdata8:100M:autoextend 
  120. innodb_data_file_path  = ibdata1:2048M:autoextend 
  121.  
  122. innodb_log_group_home_dir  = /data/mysql-5.5/logs 
  123. innodb_log_buffer_size    = 16M 
  124. innodb_log_file_size      = 1024M 
  125. innodb_log_files_in_group = 3 
  126.  
  127. innodb_thread_concurrency = 288 
  128. innodb_thread_sleep_delay = 500 
  129. #innodb_file_io_threads    = 8 
  130. innodb_concurrency_tickets = 1000 
  131.  
  132. innodb_sync_spin_loops=40 
  133.  
  134. ##innodb_force_recovery is only used for recovery mode; 
  135. ##innodb_force_recovery   = 3  
  136.  
  137.  
  138. innodb_flush_log_at_trx_commit  = 0 
  139. innodb_max_dirty_pages_pct      = 90 
  140. #innodb_lock_wait_timeout        = 120 
  141. innodb_lock_wait_timeout        = 10 
  142. innodb_support_xa               = 0 
  143.  
  144. #Added to allow create function 
  145. log_bin_trust_function_creators = 1 
  146.  
  147.  
  148. innodb_file_per_table = 1 
  149.  
  150. #turn  
  151. innodb_stats_on_metadata=0 
  152. innodb_old_blocks_pct=20 
  153. innodb_change_buffering=all 
  154. #innodb_max_purge_lag  
  155. innodb_purge_threads=1 
  156. innodb_purge_batch_size = 32 
  157.  
  158.  
  159.  
  160. innodb_read_io_threads = 8 
  161. innodb_write_io_threads = 8 
  162. innodb_io_capacity = 2000 
  163.  
  164. innodb_file_format = Barracuda 
  165.  
  166. #manage 
  167. performance_schema 
  168. performance_schema_events_waits_history_size=100 
  169. performance_schema_events_waits_history_long_size=30000 
  170.  
  171. #innodb_strict_mode=1 
  172.  
  173. [mysqldump] 
  174. quick 
  175. max_allowed_packet = 128M 
  176.  
  177. [mysql] 
  178. no-auto-rehash 
  179.  
  180. [mysqlhotcopy] 
  181. interactive-timeout 
  182.  
  183. [mysqld_safe] 
  184. open-files-limit = 28192 
  185. EOF 

注意两点:
1).在两台MySQL服务器上开启bin-log日志,默认是开启的!
2).另外一台服务器节点的server-id设置为20,本来设置为10
 
2.将31服务器设置32的主服务器
在31服务器上建立授权用户!
  1. mysql> grant replication slave on *.* to 'rep'@'%' identified by "rep"
  2. Query OK, 0 rows affected (0.09 sec) 
  3.  
  4. mysql> flush privileges; 
  5. Query OK, 0 rows affected (0.09 sec) 
  6.  
  7. mysql> show master status; 
  8. +---------------+----------+--------------+------------------+ 
  9. | File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | 
  10. +---------------+----------+--------------+------------------+ 
  11. | binlog.000004 |   138637 |              |                  |  
  12. +---------------+----------+--------------+------------------+ 
  13. 1 row in set (0.03 sec) 
  14.  
  15. 在32服务器上把31服务器设为自己的主服务器,mysql客户端连接到32服务器: 
  16. mysql> change master to  master_host='192.168.8.31',master_user='rep',master_password='rep',master_log_file='binlog.000004',master_log_pos=138637; 
  17. Query OK, 0 rows affected (0.01 sec) 
  18.  
  19. mysql> start slave; 
  20. Query OK, 0 rows affected (0.00 sec) 
  21.  
  22. mysql> show slave status\G 
  23. *************************** 1. row *************************** 
  24.                Slave_IO_State: Waiting for master to send event 
  25.                   Master_Host: 192.168.8.31 
  26.                   Master_User: rep 
  27.                   Master_Port: 3306 
  28.                 Connect_Retry: 60 
  29.               Master_Log_File: binlog.000004 
  30.           Read_Master_Log_Pos: 138637 
  31.                Relay_Log_File: relaylog.000002 
  32.                 Relay_Log_Pos: 250 
  33.         Relay_Master_Log_File: binlog.000004 
  34.              Slave_IO_Running: Yes 
  35.             Slave_SQL_Running: Yes 
  36.               Replicate_Do_DB:  
  37.           Replicate_Ignore_DB: mysql,test,information_schema 
  38.            Replicate_Do_Table:  
  39.        Replicate_Ignore_Table:  
  40.       Replicate_Wild_Do_Table:  
  41.   Replicate_Wild_Ignore_Table:  
  42.                    Last_Errno: 0 
  43.                    Last_Error:  
  44.                  Skip_Counter: 0 
  45.           Exec_Master_Log_Pos: 138637 
  46.               Relay_Log_Space: 399 
  47.               Until_Condition: None 
  48.                Until_Log_File:  
  49.                 Until_Log_Pos: 0 
  50.            Master_SSL_Allowed: No 
  51.            Master_SSL_CA_File:  
  52.            Master_SSL_CA_Path:  
  53.               Master_SSL_Cert:  
  54.             Master_SSL_Cipher:  
  55.                Master_SSL_Key:  
  56.         Seconds_Behind_Master: 0 
  57. Master_SSL_Verify_Server_Cert: No 
  58.                 Last_IO_Errno: 0 
  59.                 Last_IO_Error:  
  60.                Last_SQL_Errno: 0 
  61.                Last_SQL_Error:  
  62.   Replicate_Ignore_Server_Ids:  
  63.              Master_Server_Id: 1 
  64. 1 row in set (0.00 sec) 

PS:
   Slave_IO_Running: Yes
   Slave_SQL_Running: Yes
 两处都为YES,表示OK!
 
3.将32服务器设置31的主服务器
在32服务器上建立授权用户:
 
  1. mysql> grant replication slave on *.* to 'rep'@'%' identified by "rep"
  2. Query OK, 0 rows affected (0.03 sec) 
  3.  
  4. mysql> flush privileges; 
  5. Query OK, 0 rows affected (0.00 sec) 
  6.  
  7. mysql> show master status;  
  8. +---------------+----------+--------------+------------------+ 
  9. | File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | 
  10. +---------------+----------+--------------+------------------+ 
  11. | binlog.000004 |      320 |              |                  | 
  12. +---------------+----------+--------------+------------------+ 
  13. 1 row in set (0.00 sec) 
  14.  
  15. 在31服务器上把32服务器设为自己的主服务器,mysql客户端连接到31服务器: 
  16. mysql> change master to  master_host='192.168.8.32',master_user='rep',master_password='rep',master_log_file='binlog.000004',master_log_pos=320; 
  17. ERROR 2006 (HY000): MySQL server has gone away 
  18. No connection. Trying to reconnect... 
  19. Connection id:    74 
  20. Current database: *** NONE *** 
  21.  
  22. Query OK, 0 rows affected (0.07 sec) 
  23.  
  24. mysql> start slave; 
  25. Query OK, 0 rows affected (0.03 sec) 
  26.  
  27. mysql> show slave status\G 
  28. *************************** 1. row *************************** 
  29.                Slave_IO_State: Waiting for master to send event 
  30.                   Master_Host: 192.168.8.32 
  31.                   Master_User: rep 
  32.                   Master_Port: 3306 
  33.                 Connect_Retry: 60 
  34.               Master_Log_File: binlog.000004 
  35.           Read_Master_Log_Pos: 320 
  36.                Relay_Log_File: relaylog.000002 
  37.                 Relay_Log_Pos: 250 
  38.         Relay_Master_Log_File: binlog.000004 
  39.              Slave_IO_Running: Yes 
  40.             Slave_SQL_Running: Yes 
  41.               Replicate_Do_DB:  
  42.           Replicate_Ignore_DB: mysql,test,information_schema 
  43.            Replicate_Do_Table:  
  44.        Replicate_Ignore_Table:  
  45.       Replicate_Wild_Do_Table:  
  46.   Replicate_Wild_Ignore_Table:  
  47.                    Last_Errno: 0 
  48.                    Last_Error:  
  49.                  Skip_Counter: 0 
  50.           Exec_Master_Log_Pos: 320 
  51.               Relay_Log_Space: 399 
  52.               Until_Condition: None 
  53.                Until_Log_File:  
  54.                 Until_Log_Pos: 0 
  55.            Master_SSL_Allowed: No 
  56.            Master_SSL_CA_File:  
  57.            Master_SSL_CA_Path:  
  58.               Master_SSL_Cert:  
  59.             Master_SSL_Cipher:  
  60.                Master_SSL_Key:  
  61.         Seconds_Behind_Master: 0 
  62. Master_SSL_Verify_Server_Cert: No 
  63.                 Last_IO_Errno: 0 
  64.                 Last_IO_Error:  
  65.                Last_SQL_Errno: 0 
  66.                Last_SQL_Error:  
  67.   Replicate_Ignore_Server_Ids:  
  68.              Master_Server_Id: 2 
  69. 1 row in set (0.00 sec) 

4.主主同步测试
先在31服务器上建个库:
  1. mysql> create database t_test; 
  2. Query OK, 1 row affected (0.03 sec) 
  3.  
  4. mysql> show databases; 
  5. +--------------------+ 
  6. | Database           | 
  7. +--------------------+ 
  8. | information_schema | 
  9. | mysql              | 
  10. | performance_schema | 
  11. | t_test             | 
  12. | test               | 
  13. +--------------------+ 
  14. 5 rows in set (0.00 sec) 
  15.  
  16. 再到32服务器查询是否同步过来这个库: 
  17. mysql> show databases; 
  18. +--------------------+ 
  19. | Database           | 
  20. +--------------------+ 
  21. | information_schema |  
  22. | bbs                |  
  23. | mysql              |  
  24. | performance_schema |  
  25. | t_test             |  
  26. | test               |  
  27. +--------------------+ 
  28. 6 rows in set (0.00 sec) 
  29.  
  30. 可以看到同步过来了! 
  31. 反过来一样!这步略! 
  32.  
  33. 5.keepalived安装配置 
  34. yum -y install kernel-devel ipvsadm 
  35. ln -sv /usr/src/kernels/2.6.18-308.8.2.el5-x86_64/ /usr/src/linux 
  36. cd /opt 
  37. tar zxf keepalived-1.1.20.tar.gz 
  38. cd keepalived-1.1.20 
  39. ./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.18-308.11.1.el5-x86_64/ 
  40. make;make install 
  41. cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d 
  42. cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ 
  43. mkdir /etc/keepalived 
  44. chkconfig keepalived on 
  45.  
  46. 创建配置文件: 
  47. 主服务器: 
  48. cat /etc/keepalived/keepalived.conf 
  49.  
  50. ! Configuration File for keepalived 
  51.  
  52. global_defs { 
  53.    notification_email { 
  54.      250621008@qq.com 
  55.    } 
  56.    notification_email_from 250621008@qq.com 
  57.    smtp_server 127.0.0.1 
  58.    smtp_connect_timeout 30 
  59.    router_id mysql-ha 
  60.  
  61. vrrp_instance VI_1 { 
  62.     state BACKUP 
  63.     interface eth0 
  64.     virtual_router_id 51 
  65.     priority 100 
  66.     advert_int 1 
  67.     nopreempt   #不抢占,只在priority高的节点上设置 
  68.     authentication { 
  69.         auth_type PASS 
  70.         auth_pass 1111 
  71.     } 
  72.     virtual_ipaddress { 
  73.         192.168.8.30 
  74.     } 
  75.  
  76. virtual_server 192.168.8.30 3306 { 
  77.     delay_loop 2 
  78.     lb_algo wrr 
  79.     lb_kind DR 
  80.     persistence_timeout 60 
  81.     protocol TCP 
  82.  
  83.     real_server 192.168.8.31 3306 
  84.         weight 3 
  85.         notify_down /root/sh/mysql.sh 
  86.             TCP_CHECK { 
  87.             connect_timeout 10 
  88.             nb_get_retry 3 
  89.             delay_before_retry 3 
  90.             connect_port 3306 
  91.         } 
  92.     } 
  93.  
  94. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
  95.  
  96. 从服务器: 
  97. cat /etc/keepalived/keepalived.conf 
  98.  
  99. ! Configuration File for keepalived 
  100.  
  101. global_defs { 
  102.    notification_email { 
  103.      250621008@qq.com 
  104.    } 
  105.    notification_email_from 250621008@qq.com 
  106.    smtp_server 127.0.0.1 
  107.    smtp_connect_timeout 30 
  108.    router_id mysql-ha 
  109.  
  110. vrrp_instance VI_1 { 
  111.     state BACKUP 
  112.     interface eth0 
  113.     virtual_router_id 51 
  114.     priority 90 
  115.     advert_int 1 
  116.     authentication { 
  117.         auth_type PASS 
  118.         auth_pass 1111 
  119.     } 
  120.     virtual_ipaddress { 
  121.         192.168.8.30 
  122.     } 
  123.  
  124. virtual_server 192.168.8.30 3306 { 
  125.     delay_loop 2 
  126.     lb_algo wrr 
  127.     lb_kind DR 
  128.     persistence_timeout 60 
  129.     protocol TCP 
  130.  
  131.     real_server 192.168.8.32 3306 
  132.         weight 3 
  133.         notify_down /root/sh/mysql.sh 
  134.             TCP_CHECK { 
  135.             connect_timeout 10 
  136.             nb_get_retry 3 
  137.             delay_before_retry 3 
  138.             connect_port 3306 
  139.         } 
  140.     } 

主备配置文件注意几点:
1).router_id 两边必须相同
2).state 两边都为BACKUP
3).virtual_router_id 两边必须相同
4).priority 主节点的值必须大于从节点的值
5).nopreempt 不抢占,只在priority高的节点(即主节点)上设置
6).real_server 只需要本机的IP,不需要其它节点的!
7).notify_down 作用是监测到当mysql停止工作时自动关闭本机的keepalived的脚本,实现故障转移!
 
在主从上都创建当mysql停止工作时自动关闭本机的keepalived的脚本:
  1. cat /root/sh/mysql.sh 
  2. #!/bin/bash 
  3.   
  4. MYSQL=/usr/local/mysql/bin/mysql 
  5. MYSQL_HOST=localhost 
  6. MYSQL_USER=root 
  7. MYSQL_PASSWORD="admin" 
  8.   
  9. CHECK_TIME=3 
  10.   
  11. #mysql is working MYSQL_OK is 0 , mysql down MYSQL_OK is 1 
  12. MYSQL_OK=1 
  13.   
  14. function check_mysql_helth (){ 
  15. $MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p $MYSQL_PASSWORD -e "show status;" > /dev/null 2>&1 
  16. if [ $? = 0 ] ;then 
  17. MYSQL_OK=0 
  18. else 
  19. MYSQL_OK=1 
  20. fi 
  21. return $MYSQL_OK 
  22.   
  23. while [ $CHECK_TIME -ne 0 ] 
  24. do 
  25. let "CHECK_TIME -= 1" 
  26.   
  27. check_mysql_helth 
  28.   
  29. if [ $MYSQL_OK = 0 ] ; then 
  30. CHECK_TIME=0 
  31. exit 0 
  32. fi 
  33.   
  34. if [ $MYSQL_OK -eq 1 ] && [ $CHECK_TIME -eq 1 ] 
  35. then 
  36.    /etc/init.d/keepalived stop 
  37. exit 1 
  38. fi 
  39.   
  40. sleep 1 
  41.   
  42. done 

6.故障转移测试
分别启动主备节点的keepalived服务,然后在主节点查看VIP有没有启动:
  1. 31服务器: 
  2. [root@mysql1 ~]# ip a |grep eth0 
  3. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 
  4.     inet 192.168.8.31/24 brd 192.168.8.255 scope global eth0 
  5.     inet 192.168.8.30/32 scope global eth0 
  6.  
  7. 32服务器: 
  8. [root@mysql2 keepalived-1.1.19]# ip a |grep eth0 
  9. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 
  10.     inet 192.168.8.32/24 brd 192.168.8.255 scope global eth0 
  11.  
  12. 现在把31服务器的mysql服务停掉: 
  13. [root@mysql1 keepalived]# ip a 
  14. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue  
  15.     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 
  16.     inet 127.0.0.1/8 scope host lo 
  17. 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000 
  18.     link/ether 00:0c:29:d4:fd:b3 brd ff:ff:ff:ff:ff:ff 
  19.     inet 192.168.8.31/24 brd 192.168.8.255 scope global eth0 
  20.     inet 192.168.8.30/32 scope global eth0 
  21. [root@mysql1 keepalived]# service mysqld stop 
  22. Shutting down MySQL.                                       [  OK  ] 
  23. [root@mysql1 keepalived]# service keepalived status 
  24. keepalived is stopped 
  25. [root@mysql1 keepalived]# 

可以看到mysql服务停掉后,keepalived服务也马上停掉了!
再到32节点上查看下VIP有没有接管:
[root@mysql2 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue 
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 00:0c:29:e3:dd:5a brd ff:ff:ff:ff:ff:ff
    inet 192.168.8.32/24 brd 192.168.8.255 scope global eth0
    inet 192.168.8.30/32 scope global eth0
可以看到接管过来了!
 
四、存储部分
请参考笔者的相关文章:
http://linuxops.blog.51cto.com/2238445/895945  --mfs分布式存储(这里修正了几个错误!!!)
 
http://linuxops.blog.51cto.com/2238445/899652  --第五部分:NFS高可用web存储部署

 

本文出自 “->” 博客,请务必保留此出处http://qiuzhijun.blog.51cto.com/2238445/932673