標籤:des style blog http color io os 使用 ar
概述
由於單台redis伺服器的記憶體管理能力有限,使用過大記憶體redis伺服器的效能急劇下降,且伺服器發生故障將直接影響大面積業務。為了擷取更好的緩衝效能及擴充型,我們將需要搭建redis叢集來滿足需求。因redis 3.0 beta支援的叢集功能不適合生產環境的使用,所以我們採用twitter正在使用的twemproxy來搭建redis快取服務器叢集,目前使用者包括Pinterest、Tumblr、Twitter、Vine、Kiip、Wuaki.tv、Wanelo、Kontera、Wikimedia、Bright、56.com、Snapchat、Digg、Gawkermedia、3scale.net等。
Twemproxy是memcached和redis協議的Proxy 伺服器,並能有效減少大量串連對redis伺服器的效能影響,它提供的主要特性如下:
叢集架構
安裝Redis
有三台伺服器,一台COS1安裝twemproxy,另外兩台COS2,COS3安裝redis。
- 下載最新安裝包:redis-2.8.9.tar.gz , tcl-8.5.7-6.el6.x86_64.rpm ,nutcracker-0.3.0.tar.gz
- 安裝必要組件rpm:
[[email protected] redis-2.8.9]# yum install gcc[[email protected] src]# rpm -ivh tcl-8.5.7-6.el6.x86_64.rpm
- 安裝Redis:
[[email protected] src]# tar xvf redis-2.8.9.tar.gz[[email protected] src]# cd redis-2.8.9[[email protected] redis-2.8.9]# make…Hint: To run ‘make test‘ is a good idea ;)make[1]: Leaving directory `/usr/local/src/redis-2.8.9/src‘[root@COS2 redis-2.8.9]# make testAll tests passed without errors!Cleanup: may take some time... OKmake[1]: Leaving directory `/usr/local/src/redis-2.8.9/src‘[root@COS2 redis-2.8.9]# make install[[email protected] redis-2.8.9]# cd /usr/local/bin/[[email protected] bin]# lltotal 13908-rwxr-xr-x. 1 root root 4170264 Apr 26 11:51 redis-benchmark-rwxr-xr-x. 1 root root 22185 Apr 26 11:51 redis-check-aof-rwxr-xr-x. 1 root root 45419 Apr 26 11:51 redis-check-dump-rwxr-xr-x. 1 root root 4263471 Apr 26 11:51 redis-cli-rwxr-xr-x. 1 root root 5726791 Apr 26 11:51 redis-server
- 編輯redis設定檔:
[[email protected] redis-2.8.9]# cp redis.conf /etc/[[email protected] redis-2.8.9]# vim /etc/redredhat-release redis.conf [root@COS2 redis-2.8.9]# vim /etc/redis.conf把裡面的daemonize no 修改成 daemonize yes
- 啟動redis服務:
[[email protected] redis-2.8.9]# redis-server /etc/redis.conf
- 測試redis服務:
[[email protected] redis-2.8.9]# redis-cli 127.0.0.1:6379> set kin kinOK127.0.0.1:6379> get kin
- 同樣的步驟安裝其他redis伺服器。
安裝twemproxy
- 安裝twemproxy:
[[email protected] src]# tar xvf nutcracker-0.3.0.tar.gz[[email protected] nutcracker-0.3.0]# cd nutcracker-0.3.0[[email protected] src]#./configure [[email protected] nutcracker-0.3.0]# make && make install
- 編輯設定檔:
[[email protected] conf]# cd /usr/local/src/nutcracker-0.3.0/conf[[email protected] conf]# cp nutcracker.yml /etc/[[email protected] conf]# vim /etc/nutcracker.ymlalpha: listen: 0.0.0.0:22121 hash: fnv1a_64 distribution: ketama auto_eject_hosts: true redis: true server_retry_timeout: 2000 server_failure_limit: 1 servers: --兩台redis伺服器的地址和連接埠 - 10.23.22.240:6379:1 - 10.23.22.241:6379:1
- 測試組態檔案:
[[email protected] nutcracker-0.3.0]# nutcracker -t /etc/nutcracker.yml nutcracker: configuration file ‘conf/nutcracker.yml‘ syntax is ok
- 啟動twemproxy:
[[email protected] nutcracker-0.3.0]# nutcracker --helpThis is nutcracker-0.3.0Usage: nutcracker [-?hVdDt] [-v verbosity level] [-o output file] [-c conf file] [-s stats port] [-a stats addr] [-i stats interval] [-p pid file] [-m mbuf size]Options: -h, --help : this help -V, --version : show version and exit -t, --test-conf : test configuration for syntax errors and exit -d, --daemonize : run as a daemon -D, --describe-stats : print stats description and exit -v, --verbosity=N : set logging level (default: 5, min: 0, max: 11) -o, --output=S : set logging file (default: stderr) -c, --conf-file=S : set configuration file (default: conf/nutcracker.yml) #配置 -s, --stats-port=N : set stats monitoring port (default: 22222) -a, --stats-addr=S : set stats monitoring ip (default: 0.0.0.0) -i, --stats-interval=N : set stats aggregation interval in msec (default: 30000 msec) -p, --pid-file=S : set pid file (default: off) -m, --mbuf-size=N : set size of mbuf chunk in bytes (default: 16384 bytes)[root@COS1 nutcracker-0.3.0]# nutcracker -d -c /etc/nutcracker.yml[[email protected] nutcracker-0.3.0]# ps -ef|grep nutcrackerroot 15358 1 0 02:40 ? 00:00:00 nutcracker -d -c /etc/nutcracker.yml
- 測試twemproxy:
[[email protected] ~]# redis-cli -p 22121127.0.0.1:22121> get kin"kin"127.0.0.1:22121> set kin kingOK127.0.0.1:22121> get kin"king"
效能測試
這裡使用redis內建的redis-benchmark進行簡單的效能測試,測試結果如下:
- Set測試:
- 通過twemproxy測試:
[[email protected] src]# redis-benchmark -h 10.23.22.240 -p 22121 -c 100 -t set -d 100 -l –q
SET: 38167.94 requests per second
- 直接對後端redis測試:
[[email protected] ~]# redis-benchmark -h 10.23.22.241 -p 6379 -c 100 -t set -d 100 -l –q
SET: 53191.49 requests per second
- Get測試:
- 通過twemproxy測試:
[[email protected] src]# redis-benchmark -h 10.23.22.240 -p 22121 -c 100 -t get -d 100 -l -qGET: 37453.18 requests per second
- 直接對後端redis測試:
[[email protected] ~]# redis-benchmark -h 10.23.22.241 -p 6379 -c 100 -t get -d 100 -l -qGET: 62111.80 requests per second
- 查看索引值分布:
[[email protected] ~]# redis-cli info|grep db0db0:keys=51483,expires=0,avg_ttl=0[root@COS3 ~]# redis-cli info|grep db0db0:keys=48525,expires=0,avg_ttl=0
測試結果:以基本的set get命令通過twemproxy效能有所下降;通過twemproxy分布基本平均。測試資料以業務測試為準。
基於Twemproxy的Redis叢集方案