Memcached HA架構探索

magent是一款開源的Memcached代理服務器軟件,可以用它做一些高可用架構嘗試。目前magent已更新到0.6,我在centos 6.0 64bit機器上面未編譯通過,所以我在這裏用0.5的源碼來測試;

google項目地址:http://code.google.com/p/memagent/

 

一、安裝步驟:
1、編譯安裝libevent:

  1. wget https://github.com/downloads/libevent/libevent/libevent-2.0.16-stable.tar.gz 
  2. tar zxvf libevent-2.0.16-stable.tar.gz 
  3. cd libevent-2.0.16-stable/ 
  4. ./configure --prefix=/usr 
  5. make && make install 
  6. cd ../ 

2、編譯安裝Memcached:

  1. wget http://memcached.googlecode.com/files/memcached-1.4.14.tar.gz 
  2. tar zxvf memcached-1.4.14.tar.gz 
  3. cd memcached-1.4.14/ 
  4. ./configure --prefix=/usr --with-libevent=/usr 
  5. make && make install 
  6. cd ../ 

3、編譯安裝magent:

  1. mkdir magent 
  2. cd magent/ 
  3. wget http://memagent.googlecode.com/files/magent-0.5.tar.gz 
  4. tar zxvf magent-0.5.tar.gz 
  5. /sbin/ldconfig 
  6. vim Makefile 
  7. CFLAGS = -Wall -O2 -g 
  8. 改爲: 
  9. CFLAGS = -lrt -Wall -O2 -g 
  10. sed -i "s#LIBS = -levent#LIBS = -levent -lm#g" Makefile 
  11. vim ketama.h 
  12. 加入 
  13. #ifndef SSIZE_MAX 
  14. #define SSIZE_MAX     32767 
  15. #endif  
  16. make 
  17. cp magent /usr/bin/magent 
  18. cd ../ 
安裝中常遇到的問題

  1. gcc -lrt -Wall -g -O2 -I/usr/local/include -m64 -c -o magent.o magent.c 
  2.  
  3. magent.c: In function ‘writev_list’: 
  4.  
  5. magent.c:729: error: ‘SSIZE_MAX’ undeclared (first use in this function
  6.  
  7. magent.c:729: error: (Each undeclared identifier is reported only once 
  8.  
  9. magent.c:729: error: for each function it appears in.) 
  10.  
  11. make: *** [magent.o] Error 1 
解決辦法:

  1. vim ketama.h 
  2.  
  3. 加入 
  4.  
  5. #ifndef SSIZE_MAX 
  6. #define SSIZE_MAX     32767 
  7. #endif  
二、高可用網絡架構
此架構生產環境需要負載均衡器做負載,具體使用什麼軟件或硬件就要看你的具體情況來定了。                             
生產環境每臺服務器分別啓動一個memcached進程和magen進程做冗餘
  1. memcached -d -m 256 -l 192.168.10.11 -p 11211 -u www -c 10240 -P /tmp/memcached.pid  -vv >/home/logs/memcached.txt 2>&1 
  2. memcached -d -m 128 -l 192.168.10.12 -p 11211 -u www -c 10240 -P /tmp/memcached.pid  -vv >/home/logs/memcached.txt 2>&1 
  3. memcached -d -m 128 -l 192.168.10.13 -p 11211 -u www -c 10240 -P /tmp/memcached.pid  -vv >/home/logs/memcached.txt 2>&1 
  4.  
  5. magent -u www -n 51200 -l 192.168.10.11 -p 12000 -s 192.168.10.12:11211 -s 192.168.10.13:11211 -b 192.168.10.11:11211  
  6. magent -u www -n 51200 -l 192.168.10.12 -p 12000 -s 192.168.10.12:11211 -s 192.168.10.13:11211 -b 192.168.10.11:11211  
  7. magent -u www -n 51200 -l 192.168.10.13 -p 12000 -s 192.168.10.12:11211 -s 192.168.10.13:11211 -b 192.168.10.11:11211  
參數說明:
-s 爲要寫入的memcached, 
-b 爲備份用的memcached。
說明:測試環境用magent和memached的不同端口來實現,在生產環境中可以將magent和memached作爲一組放到多臺服務器上。
看到了吧這樣的架構最好做負載了,用一個VIP分別映射三臺magent的12000端口即可。
#實驗環境測試過程:

  1. [root@odb ~]# telnet 127.0.0.1 12000 
  2. Trying 127.0.0.1… 
  3. Connected to localhost.localdomain (127.0.0.1). 
  4. Escape character is ‘^]’. 
  5. set key 0 0 8                       <—在10000端口設置key的值 
  6. 88888888 
  7. STORED 
  8. quit 
  9. Connection closed by foreign hos 
  10. [root@odb ~]# telnet 127.0.0.1 11211 
  11. Trying 127.0.0.1… 
  12. Connected to localhost.localdomain (127.0.0.1). 
  13. Escape character is ‘^]’. 
  14. get key                     <—在11211端口獲取key的值成功 
  15. VALUE key 0 8 
  16. 88888888 
  17. END 
  18. quit 
  19. Connection closed by foreign host. 
  20. [root@odb ~]# telnet 127.0.0.1 11211 
  21. Trying 127.0.0.1… 
  22. Connected to localhost.localdomain (127.0.0.1). 
  23. Escape character is ‘^]’. 
  24. get key                     <—在11211端口獲取key的值成功 
  25. VALUE key 0 8 
  26. 88888888 
  27. END 
  28. quit 
  29. Connection closed by foreign host. 
  30.  
  31. 高可用性測試: 
  32. [root@odb ~]# ps aux |grep -v grep |grep memcached 
  33. root     23455  0.0  0.0  5012 1796 ?        Ss   09:22   0:00 memcached -m 1 -u root -d -l 127.0.0.1 -p 11212 
  34. root     24950  0.0  0.0  4120 1800 ?        Ss   10:58   0:00 memcached -m 1 -u root -d -l 127.0.0.1 -p 11211 
  35. [root@odb ~]# ps aux |grep -v grep |grep ‘magent -u’ 
  36. root     25919  0.0  0.0  2176  484 ?        Ss   12:00   0:00 magent -u root -n 51200 -l 127.0.0.1 -p 10000 -s 127.0.0.1:11211 -b 127.0.0.1:11212 
  37. root     25925  0.0  0.0  3004  484 ?        Ss   12:00   0:00 magent -u root -n 51200 -l 127.0.0.1 -p 11000 -s 127.0.0.1:11212 -b 127.0.0.1:11211 
  38. [root@odb ~]# telnet 127.0.0.1 10000 
  39. Trying 127.0.0.1… 
  40. Connected to localhost.localdomain (127.0.0.1). 
  41. Escape character is ‘^]’. 
  42. set stone 0 0 6                      <—在10000端口設置stone的值 
  43. 123456 
  44. STORED 
  45. quit 
  46. Connection closed by foreign host. 
  47. [root@odb ~]# telnet 127.0.0.1 11000 
  48. Trying 127.0.0.1… 
  49. Connected to localhost.localdomain (127.0.0.1). 
  50. Escape character is ‘^]’. 
  51. set shidl 0 0 6                      <—在11000端口設置shidl的值 
  52. 666666 
  53. STORED 
  54. get stone                      <—在11000端口獲取stone的值成功 
  55. VALUE stone 0 6 
  56. 123456 
  57. END 
  58. incr stone 2                      <—在11000端口修改stone的值成功 
  59. 123458 
  60. get stone 
  61. VALUE stone 0 6                     <—在11000端口驗證stone的值,證明上面的修改成功 
  62. 123458 
  63. END 
  64. get shidl                      <—在11000端口獲取shidl的值成功 
  65. VALUE shidl 0 6 
  66. 666666 
  67. END 
  68. quit                     <—退出11000端口 
  69. Connection closed by foreign host. 
  70. [root@odb ~]# telnet 127.0.0.1 10000 
  71. Trying 127.0.0.1… 
  72. Connected to localhost.localdomain (127.0.0.1). 
  73. Escape character is ‘^]’. 
  74. get stone                      <—在10000端口獲取stone的值,已被修改 
  75. VALUE stone 0 6 
  76. 123458 
  77. END 
  78. get shidl                      <—在10000端口獲取shidl的值成功 
  79. VALUE shidl 0 6 
  80. 666666 
  81. END 
  82. delete shidl                      <—在10000端口刪除shidl 
  83. DELETED 
  84. get shidl                      <—在10000端口刪除shidl生效 
  85. END 
  86. quit 
  87. Connection closed by foreign host. 
  88. [root@odb ~]# telnet 127.0.0.1 11000 
  89. Trying 127.0.0.1… 
  90. Connected to localhost.localdomain (127.0.0.1). 
  91. Escape character is ‘^]’. 
  92. get shidl                      <—在11000端口驗證刪除shidl生效 
  93. END 
  94. get stone                      <—在11000端口獲取stone的值成功 
  95. VALUE stone 0 6 
  96. 123458 
  97. END 
  98. quit 
  99. Connection closed by foreign host. 
Down機模擬測試:
Down掉11211端口的memcached:

  1. [root@odb ~]# kill -9 24950 
  2. [root@odb ~]# telnet 127.0.0.1 10000 
  3. Trying 127.0.0.1… 
  4. Connected to localhost.localdomain (127.0.0.1). 
  5. Escape character is ‘^]’. 
  6. get stone                      <—在10000依然可以獲取stone的值 
  7. VALUE stone 0 6 
  8. 123458 
  9. END 
  10. quit 
  11. Connection closed by foreign host. 
  12. [root@odb ~]# telnet 127.0.0.1 11000 
  13. Trying 127.0.0.1… 
  14. Connected to localhost.localdomain (127.0.0.1). 
  15. Escape character is ‘^]’. 
  16. get stone                      <—在11000依然可以獲取stone的值 
  17. VALUE stone 0 6 
  18. 123458 
  19. END 
  20. quit 
  21. Connection closed by foreign host. 
  22.  
  23. Down掉11000端口的magent: 
  24.  
  25. [root@odb ~]# kill -9 25925 
  26. [root@odb ~]# telnet 127.0.0.1 10000 
  27. Trying 127.0.0.1… 
  28. Connected to localhost.localdomain (127.0.0.1). 
  29. Escape character is ‘^]’. 
  30. get stone                      <—在10000依然可以獲取stone的值 
  31. VALUE stone 0 6 
  32. 123458 
  33. END 
  34. quit 
  35. Connection closed by foreign host. 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章