memcached 双机热备

分类:CentOS运维 阅读:78940 次

一、需求背景

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

二、环境:

  1. CentOSrelease6.4(Final)
 
  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转发网段)
 
  1. 调度机(master):
  2. echo'201eth1'>>/etc/iproute2/rt_tables
  3. echo'defaulttableeth1via172.28.26.1deveth1'>/etc/sysconfig/network-scripts/route-eth1
  4. echo'from172.28.26.101/255.255.255.255tableeth1'>/etc/sysconfig/network-scripts/rule-eth1
  5. echo'from172.28.26.100/255.255.255.255tableeth1'>>/etc/sysconfig/network-scripts/rule-eth1
  6. echo'from172.28.16.101/32tableZW_LVS_LAN'>/etc/sysconfig/network-scripts/rule-eth2
  7. memched节点1:
  8. echo'defaulttableZW_LVS_LANvia172.28.16.100'>/etc/sysconfig/network-scripts/route-eth2
  9. echo'from172.28.16.0/24tableZW_LVS_LAN'>/etc/sysconfig/network-scripts/rule-eth2
  10. echo'202ZW_LVS_LAN'>>/etc/iproute2/rt_tables
  11. memched节点2:
  12. echo'defaulttableZW_LVS_LANvia172.28.16.100'>/etc/sysconfig/network-scripts/route-eth2
  13. echo'from172.28.16.0/24tableZW_LVS_LAN'>/etc/sysconfig/network-scripts/rule-eth2
  14. echo'202ZW_LVS_LAN'>>/etc/iproute2/rt_tables
 
  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=102465000
  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=94500000915000000927000000
  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=4096873804194304
  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=4096163844194304
  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. yum-yinstallipvsadmkeepalivedsendmail

  1. tar-zxvflibevent-1.4.13-stable.tar.gz
  2. cdlibevent-1.4.13-stable
  3. ./configure--prefix=/usr/local
  4. make
  5. makeinstall
  6. tar-zxvfmemcached-1.4.15.tar.gz
  7. cdmemcached-1.4.15
  8. ./configure--with-libevent=/usr/local
  9. make
  10. makeinstall
  11. tar-zxvfmemcached-1.2.8-repcached-2.2.tar.gz
  12. cdmemcached-1.2.8-repcached-2.2
  13. ./configure--enable-replication
  14. make
  15. makeinstall

五、启动服务

  1. /etc/init.d/keepalivedstart
 
  1. /usr/local/bin/memcached-d-v-l0.0.0.0-p11233-X11244-uroot(11244是数据同步端口)
 
  1. /usr/local/bin/memcached-d-v-l0.0.0.0-p11234-uroot-x172.28.16.103-X11244(11234是服务监听端口,11244是监听数据同步端口;-x指定masterIP,-X指定master数据同步监听端口)
 
  1. PS:
  2. 1、如果启动的时候报错如下:
  3. [root@yw_memcached_slave~]#/usr/local/bin/memcached-d-v-l0.0.0.0-p11233-X11244-uroot
  4. /usr/local/bin/memcached:errorwhileloadingsharedlibraries:libevent-1.4.so.2:cannotopensharedobjectfile:Nosuchfileordirectory
  5. 请yum安装libeven
  6. [root@yw_memcached_slave~]#yum-yinstalllibeven
  7. 2、repcached是日本人开发的实现memcached复制功能,它是一个单master单slave的方案,但它的master/slave都是可读写的,而且可以相互同步,如果master坏掉,slave侦测到连接断了,它会自动listen而成为master;而如果slave坏掉,master也会侦测到连接断,它就会重新listen等待新的slave加入;master没有抢占功能,如果master挂掉再起来只能是从了,并且永远只能是从。

六、测试

1、写一个php测试文件

本文出自 “为了梦想奋斗” 博客,转载请与作者联系!
原文地址:http://navyaijm.blog.51cto.com/4647068/1177508