redis 应用于web前端,做缓存和数据存取的速度是挺可观的,最近看了一些资料,手痒了,就弄了一个测试环境,两台方案,试用一下。
##Redis 集群部署##
一,方案调研:
参考博客:
http://jolestar.com/redis-ha/
http://www.luocs.com/archives/tag/redis
https://github.com/wandoulabs/codis/blob/master/doc/tutorial_zh.md
https://github.com/twitter/twemproxy
二,部署架构
三,方案简介:
需求:
缓存:定时任务临时写入和读取的数据量大概在10G左右
存储:日常存储量大概在40G左右
需要7*24小时不间断提供数据服务,所以要求redis集群要稳定,在有故障时可自动切换和数据即时恢复,并在业务增长或下降时,集群可伸缩或扩展(手动),并不影响在线业务,故调研测试这两种方案,以codis 为代理的codis server redis的集群,以twemproxy为代理的redis集群
codis 集群技术原理参考 https://github.com/wandoulabs/codis/blob/master/doc/tutorial_zh.md
twemproxy 集群技术原理参考:
https://github.com/twitter/twemproxy
http://blog.jpush.cn/redis-twemproxy-benchmark/
四,部署架构规划
服务器操作系统: centos 6.x 注意:centos 5版本的内核支持不了codis 安装编译
服务器部署规划:
192.168.5.14 keepalived ,codis相关服务
192.168.5.15 keepalived,codis相关服务
192.168.5.16 twemporxy,redis,haproxy
192.168.5.44 twemporxy,redis,haproxy
五,部署安装
1,codis 方案安装
1.1安装go 环境:
首先按照golang,下载地址:https://golang.org/dl/,最新的1.4.2版本。
如果被墙使用golang中国下载http://golangtc.com/download。
cd /usr/loca/
wget https://storage.googleapis.com/golang/go1.4.2.linux-amd64.tar.gz
tar -zxvf go1.4.2.linux-amd64.tar.gz
vim /etc/profile
最后追加
#set go & codis environment
GOROOT=/usr/local/go
PATH=$PATH:$GOROOT/bin
GOPATH=/usr/local/codis
export GOROOT PATH GOPATH
source /etc/profile
1.2 安装配置codis 相关配置
cd /usr/local
git clone https://github.com/wandoulabs/codis.git
#将codis的pkg包拷贝到$GOPATH目录。
mkdir -p /usr/local/codis/src/github.com/wandoulabs/codis
cp /usr/local/codis/pkg/ /usr/local/codis/cmd/ /usr/local/codis/src/github.com/wandoulabs/codis -R
cd /usr/local/codis/ ; sh bootstrap.sh #安装编译需要些时间,过程略过...... 大概3分钟左右,我的是这样,你的环境,哈哈,你懂的,
1.3配置脚本:
github上原码中有示例,可以参考试用 https://github.com/wandoulabs/codis
在$path/sample 目录下:
cat startall.sh
./start_dashboard.sh
sleep 3
./start_redis.sh
./add_group.sh
./initslot.sh
./start_proxy.sh
./set_proxy_online.sh
cat start_dashboard.sh
#!/bin/sh
nohup ../bin/codis-config -c config.ini -L ./log/dashboard.log dashboard --addr=:18087 --http-log=./log/requests.log &>/dev/null &
启动前注意一下redis的配置文件./redis_conf/*.conf
cat start_redis.sh
#!/bin/sh
nohup ../bin/codis-server ./redis_conf/6381.conf &> ./log/redis_6381.log &
nohup ../bin/codis-server ./redis_conf/6382.conf &> ./log/redis_6382.log &
echo "sleep 3s"
sleep 3
tail -n 30 ./log/redis_6381.log
tail -n 30 ./log/redis_6382.log
cat add_group.sh
#!/bin/sh
echo "add group 1 with a master(localhost:6381), Notice: do not use localhost when in produciton"
../bin/codis-config -c config.ini -L ./log/cconfig.log server add 5 192.168.5.44:6381 master
echo "add group 2 with a master(localhost:6382), Notice: do not use localhost when in produciton"
../bin/codis-config -c config.ini -L ./log/cconfig.log server add 6 192.168.5.44:6382 master
cat ./initslot.sh
#!/bin/sh
echo "slots initializing..."
#../bin/codis-config -c config.ini slot init -f
echo "done"
echo "set slot ranges to server groups..."
../bin/codis-config -c config.ini slot range-set 0 511 5 online
../bin/codis-config -c config.ini slot range-set 512 1023 6 online
echo "done"
cat ./start_proxy.sh
#!/bin/sh
echo "shut down proxy_g5_g6.."
../bin/codis-config -c config.ini proxy offline proxy_g5_g6
echo "done"
echo "start new proxy..."
nohup ../bin/codis-proxy --log-level info -c config.ini -L ./log/proxy.log --cpu=8 --addr=0.0.0.0:19000 --http-addr=0.0.0.0:11000 &
echo "done"
echo "sleep 3s"
sleep 3
tail -n 30 ./log/proxy.log
cat ./set_proxy_online.sh
#!/bin/sh
echo "set proxy_1 online"
../bin/codis-config -c config.ini proxy online proxy_g5_g6
echo "done"
1.4有自已编写的一个类上面所有脚本的python脚本:在测试中,
#!/usr/bin/env python
# coding:utf8
#author:shantuwqk@163.com
import os,sys,commands,time
from subprocess import Popen,PIPE
from mako.template import Template
codis_config = "config.ini"
codis_root = "/data/setup/codis/data"
def codis_dashboard(opt):
if opt == "start":
#os.chdir(codis_root)
exec_cmd = "cd %s; nohup ../bin/codis-config -c %s -L ./log/dashboard.log dashboard --addr=:18087 --http-log=./log/http.log &>/dev/null &" %(codis_root,codis_config)
s = os.system(exec_cmd)
if s == 0:
print "