一、需求背景

发现公司好多项目都用到memched。并且都是单点,memched服务器 挂了就会影响业务,于是百度之,发现memched不可以集群,也没有看到有什么高可用方案,但是发现了repcached(memched主从复制),结合LVS的NAT模式不同端口转发实现memched双机HA,本文章记录本人的部署过程,从属实战,没有文字性的说明,欢迎各位指点。

二、环境:

1、系统环境:
 
 
   
  1. CentOS release 6.4 (Final) 
2、网络环境
 
 
   
  1. 调度机(master):  
  2. vip:172.28.26.100  
  3. vip1:172.28.16.100(lvs转发网关   )  
  4. eth1:172.28.26.101 (内网)  
  5. eth2:172.28.16.101(lvs转发网段)  
  6. 调度机(backup):  
  7. eth1:172.28.26.99 (内网)  
  8. eth2:172.28.16.99(lvs转发网段)  
  9. memched备节点:  
  10. eth1:172.28.26.102 (内网)  
  11. eth2:172.28.16.102(lvs转发网段)   
  12. memched主节点:  
  13. eth1:172.28.26.103 (内网)  
  14. eth2:172.28.16.103(lvs转发网段) 
3、路由策略
 
 
   
  1. 调度机(master):  
  2. echo '201      eth1' >> /etc/iproute2/rt_tables  
  3. echo 'default table eth1 via 172.28.26.1 dev eth1' > /etc/sysconfig/network-scripts/route-eth1  
  4. echo 'from 172.28.26.101/255.255.255.255 table eth1' > /etc/sysconfig/network-scripts/rule-eth1  
  5. echo 'from 172.28.26.100/255.255.255.255 table eth1' >> /etc/sysconfig/network-scripts/rule-eth1  
  6. echo 'from 172.28.16.101/32 table ZW_LVS_LAN' > /etc/sysconfig/network-scripts/rule-eth2  
  7. memched节点1:  
  8. echo 'default table ZW_LVS_LAN via 172.28.16.100' > /etc/sysconfig/network-scripts/route-eth2  
  9. echo 'from 172.28.16.0/24 table ZW_LVS_LAN' > /etc/sysconfig/network-scripts/rule-eth2  
  10. echo '202       ZW_LVS_LAN' >> /etc/iproute2/rt_tables  
  11. memched节点2:  
  12. echo 'default table ZW_LVS_LAN via 172.28.16.100' > /etc/sysconfig/network-scripts/route-eth2  
  13. echo 'from 172.28.16.0/24 table ZW_LVS_LAN' > /etc/sysconfig/network-scripts/rule-eth2  
  14. echo '202       ZW_LVS_LAN' >> /etc/iproute2/rt_tables 
4、内核参数
 
 
   
  1. fs.file-max = 1000000  
  2. kernel.core_uses_pid = 1  
  3. kernel.msgmax = 1048560  
  4. kernel.msgmnb = 1073741824  
  5. kernel.shmall = 4294967296  
  6. kernel.shmmax = 68719476736  
  7. kernel.sysrq = 0  
  8. net.core.netdev_max_backlog = 1048576  
  9. net.core.rmem_default = 2097152  
  10. net.core.rmem_max = 16777216  
  11. net.core.somaxconn = 1048576  
  12. net.core.wmem_default = 2097152  
  13. net.core.wmem_max = 16777216  
  14. net.ipv4.conf.default.accept_source_route = 0  
  15. net.ipv4.conf.default.rp_filter = 1  
  16. net.ipv4.ip_forward = 1  
  17. net.ipv4.ip_local_port_range = 1024    65000  
  18. net.ipv4.neigh.default.gc_thresh1 = 10240  
  19. net.ipv4.neigh.default.gc_thresh2 = 40960  
  20. net.ipv4.neigh.default.gc_thresh3 = 81920  
  21. net.ipv4.tcp_fin_timeout = 1  
  22. net.ipv4.tcp_keepalive_intvl = 15  
  23. net.ipv4.tcp_keepalive_probes = 5  
  24. net.ipv4.tcp_keepalive_time = 30  
  25. net.ipv4.tcp_max_orphans = 3276800  
  26. net.ipv4.tcp_max_syn_backlog = 1048576  
  27. net.ipv4.tcp_max_tw_buckets = 50000  
  28. net.ipv4.tcp_mem = 94500000 915000000 927000000  
  29. net.ipv4.tcp_orphan_retries = 3  
  30. net.ipv4.tcp_reordering = 5  
  31. net.ipv4.tcp_retrans_collapse = 0  
  32. net.ipv4.tcp_retries2 = 5  
  33. net.ipv4.tcp_rmem = 4096        87380   4194304  
  34. net.ipv4.tcp_sack = 1  
  35. net.ipv4.tcp_synack_retries = 1  
  36. net.ipv4.tcp_syncookies = 0  
  37. net.ipv4.tcp_syn_retries = 1  
  38. net.ipv4.tcp_timestamps = 1  
  39. net.ipv4.tcp_tw_recycle = 1  
  40. net.ipv4.tcp_tw_reuse = 1  
  41. net.ipv4.tcp_window_scaling = 1  
  42. net.ipv4.tcp_wmem = 4096        16384   4194304  
  43. net.ipv6.conf.all.disable_ipv6 = 1  
  44. net.ipv6.conf.default.disable_ipv6 = 1  
  45. net.ipv4.conf.eth0.rp_filter = 1 #memched节点不需要  
  46. net.ipv4.conf.eth1.rp_filter = 1 #memched节点不需要  
  47. net.ipv4.conf.eth2.rp_filter = 1 #memched节点不需要  
  48. net.ipv4.conf.all.rp_filter = 0    #memched节点不需要 
三、服务安装
1、调度机(master、backup)
 
 
   
  1. yum -y install ipvsadm keepalived sendmail 
2、memched节点1、memched节点2


 
   
  1. wget http://memcached.googlecode.com/files/memcached-1.4.15.tar.gz  
  2. wget http://www.monkey.org/~provos/libevent-1.4.13-stable.tar.gz  
  3. wget http://downloads.sourceforge.net/repcached/memcached-1.2.8-repcached-2.2.tar.gz  

 

 
  
  1. tar -zxvf libevent-1.4.13-stable.tar.gz  
  2. cd libevent-1.4.13-stable  
  3. ./configure --prefix=/usr/local  
  4. make  
  5. make install  
  6.  
  7. tar -zxvf memcached-1.4.15.tar.gz  
  8. cd memcached-1.4.15  
  9. ./configure --with-libevent=/usr/local  
  10. make  
  11. make install  
  12.  
  13. tar -zxvf memcached-1.2.8-repcached-2.2.tar.gz  
  14. cd memcached-1.2.8-repcached-2.2  
  15. ./configure  --enable-replication  
  16. make  
  17. make install  
四、keepalived配置(master、backup)
[root@LVS1 keepalived]# cat /etc/keepalived/gobal_module
 
  
  1. global configure file  
  2.  
  3. global_defs {   
  4.    notification_email {  
  5.      navyaijm@qq.com  
  6.    }  
  7.    notification_email_from navyaijm@qq.com  
  8.    smtp_server 127.0.0.1  
  9.    smtp_connect_timeout 30  
  10.    router_id ZH_DG_LVS1  
  11. }  
  12.  
  13. [root@LVS1 keepalived]# cat /etc/keepalived/keepalived.conf  
  14. ! Keepalived main configure file  
  15. include /etc/keepalived/gobal_module  
  16. include /etc/keepalived/vrrpd_module  
  17. include /etc/keepalived/lvs_module  
  18.  
[root@LVS1 keepalived]# cat /etc/keepalived/vrrpd_module
 
  
  1. vrrp_sync_group ZH_DG_Memcached1 {  
  2.     group {  
  3.         ZH_DG_WAN1  
  4.         ZH_DG_LAN1  
  5.     }  
  6.     smtp_alter  
  7. }  
  8.  
  9. vrrp_instance ZH_DG_WAN1 {   
  10.     state MASTER    (从上为:backup)         
  11.     interface eth1     
  12.     virtual_router_id 10  (从为:9)   
  13.     priority 100    
  14.     advert_int 1   
  15.     authentication {  
  16.         auth_type PASS   
  17.         auth_pass 08756CD0  
  18.     }  
  19.     virtual_ipaddress {  
  20.         172.28.26.100  # vip  
  21.     }  
  22. }  
  23.  
  24.  
  25. vrrp_instance ZH_DG_LAN2 {  
  26.     state MASTER (从上为:backup)  
  27.     interface eth2  
  28.     virtual_router_id 10 (从为:9)  
  29.     priority 100  
  30.     advert_int 1  
  31.     authentication {  
  32.         auth_type PASS  
  33.         auth_pass 6F8DBC2E  
  34.     }  
  35.     virtual_ipaddress {  
  36.         172.28.16.100 # Memcached 虚拟网关  
  37.     }  

[root@LVS1 keepalived]# cat /etc/keepalived/lvs_module

 
   
  1. virtual_server 172.28.26.100 11211 {  
  2.     delay_loop 6  
  3.     lb_algo wrr  
  4.     lb_kind NAT  
  5.     persistence_timeout 60  
  6.     protocol TCP  
  7.  
  8.     include /etc/keepalived/realserver/172.28.26.102_11234.conf  
  9.     include /etc/keepalived/realserver/172.28.26.103_11233.conf  
  10. }  
  11.  
  12.  
  13. [root@LVS1 keepalived]# cat /etc/keepalived/realserver/172.28.26.102_11234.conf   
  14. real_server 172.28.16.102 11234 {  
  15.         weight 1                        #权重  
  16.         inhibit_on_failure              #故障会修改权重为零  
  17.         TCP_CHECK {  
  18.                 connect_timeout 10      #10秒无响应超时  
  19.                 nb_get_retry 3          #重连次数  
  20.                 delay_before_retry 3    #重连间隔,单位为秒  
  21.                 connect_port 11234         #检测端口  
  22.         }  
  23. }  
  24.  
  25.  
  26. [root@LVS1 keepalived]# cat /etc/keepalived/realserver/172.28.26.103_11233.conf   
  27. real_server 172.28.16.103 11233 {  
  28.         weight 1                        #权重  
  29.         inhibit_on_failure              #故障会修改权重为零  
  30.         TCP_CHECK {  
  31.                 connect_timeout 10      #10秒无响应超时  
  32.                 nb_get_retry 3          #重连次数  
  33.                 delay_before_retry 3    #重连间隔,单位为秒  
  34.                 connect_port 11233         #检测端口  
  35.         }  

五、启动服务

1、keepalived启动(master和backup)
 
 
   
  1. /etc/init.d/keepalived start 
memcached的启动
主节点:
 
 
   
  1. /usr/local/bin/memcached -d -v -l 0.0.0.0 -p 11233 -X 11244 -uroot (11244是数据同步端口) 
备节点:
 
 
   
  1. /usr/local/bin/memcached -d -v -l 0.0.0.0 -p 11234 -uroot -x 172.28.16.103 -X 11244(11234是服务监听端口,11244是监听数据同步端口;-x 指定masterIP,-X指定master数据同步监听端口) 
 
  
  1. PS:  
  2. 1、如果启动的时候报错如下:  
  3. [root@yw_memcached_slave ~]#/usr/local/bin/memcached -d -v -l 0.0.0.0 -p 11233 -X 11244 -uroot  
  4. /usr/local/bin/memcached: error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory  
  5. 请yum安装libeven  
  6. [root@yw_memcached_slave ~]# yum -y install libeven  
  7. 2、repcached是日本人开发的实现memcached复制功能,它是一个单 master单 slave的方案,但它的 master/slave都是可读写的,而且可以相互同步,如果 master坏掉, slave侦测到连接断了,它会自动 listen而成为 master;而如果 slave坏掉, master也会侦测到连接断,它就会重新 listen等待新的 slave加入 ;master没有抢占功能,如果master挂掉再起来只能是从了,并且永远只能是从。  

六、测试

1、写一个php测试文件

vi session.php
 
      
  1. <?php  
  2. ini_set('session.save_handler''memcached');  
  3. ini_set('session.save_path'"172.28.26.100:11211");  
  4. session_start();  
  5. $_SESSION['aa'] = "bb";  
  6.  
  7. echo session_id();  
  8. ?>  
2、用php执行这个文件会得到一个字符串,telnet 172.28.26.100 11211 或者telnet 172.28.26.103 11233再或者telnet 172.28.26.103 11234,如果能得到值说明session已经写入memched,down到任何一台memched数据不会丢。