Codis: Redis 集群解决方案
Codis 由四部分组成:
Codis Proxy (codis-proxy)
Codis Manager (codis-config)
Codis Redis (codis-server)
ZooKeeper
# 要安装zookeeper
wget http://mirrors.cnnic.cn/apache/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gz
tar zxvf zookeeper-3.4.6.tar.gz
mv zookeeper-3.4.6 /opt/local/zookeeper
mkdir /opt/local/zookeeper/logs
cd /opt/local/zookeeper/conf/
cp zoo_sample.cfg zoo.cfg
vi zoo.cfg 修改配置文件
修改 dataDir
dataDir=/opt/local/zookeeper/data
并增加 日志
dataLogDir=/opt/local/zookeeper/logs
启动 zookeeper
/opt/local/zookeeper/bin/zkServer.sh start
# 首先安装 go 语言
wget https://storage.googleapis.com/golang/go1.4.2.linux-amd64.tar.gz
tar zxvf go1.4.2.linux-amd64.tar.gz
mv go /usr/local/
# 配置环境变量
vi /etc/profile
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
export GOPATH=/usr/local/codis
source /etc/profile
执行
go version
查看版本
安装codis
yum install -y git
go get -u -d github.com/wandoulabs/codis #这个需要几分钟下载共30M文件
package github.com/wandoulabs/codis
imports github.com/wandoulabs/codis
imports github.com/wandoulabs/codis: no buildable Go source
files in /usr/local/codis/src/github.com/wandoulabs/codis
cd /usr/local/codis/src/github.com/wandoulabs/codis
# 安装配置
./bootstrap.sh #这个需要十几分钟共下载50M文件
make gotest
# 将编译好后,把bin目录和一些脚本复制过去/usr/local/codis目录下:
mkdir -p /usr/local/codis/{logs,conf,scripts}
mkdir -p /opt/local/codis_server/{logs,conf,data}
cp -rf bin /usr/local/codis/
cp sample/config.ini /usr/local/codis/conf/
cp sample/redis_conf/6381.conf /opt/local/codis_server/conf/
cp -rf /usr/local/codis/src/github.com/wandoulabs/codis/sample/*.sh /usr/local/codis/scripts/
cp -rf /usr/local/codis/src/github.com/wandoulabs/codis/sample/usage.md /usr/local/codis/scripts/
cp /usr/local/codis/src/github.com/wandoulabs/codis/extern/redis-2.8.21/src/redis-cli /usr/local/codis/bin/redis-cli-2.8.21
ln -s /usr/local/codis/bin/redis-cli-2.8.21 /usr/local/codis/bin/redis-cli
# 修改配置文件
cd /usr/local/codis/conf
vi config.ini
# 修改IP,配置WEB管理 默认 18087 端口
dashboard_addr
( zk 配置 zookeeper 地址,单机配置一个,群集配置多个~已,号隔开 )
cd /opt/local/codis_server/conf/
# 修改redis 配置文件
6381.conf
cp 两份 分别为 6379.conf 6380.conf 为 主与从配置
# 修改里面的配置端口与配置 需要修改的配置如下:
daemonize yes #后台模式运行
pidfile var/run/redis_6379.pid #pid 文件
port 6379 #运行端口
timeout 50 #请求超时时间,默认0
logfile "/opt/local/codis_server/logs/codis_6379.log" #日志文件
save 900 1 #打开保存快照的条件( 第一个*表示多长时间 , 第三个*表示执行多少次写操作 )
save 300 10
save 60 10000
dbfilename 6379.rdb #数据快照保存的名字
dir /opt/local/codis_server/data #数据快照的保存目录
appendfilename "6379_appendonly.aof" #Redis更加高效的数据库备份及灾难恢复方式。
appendfsync everysec # ( always: always表示每次有写操作都进行同步. everysec: 表示对写操作进行累积,每秒同步一次 )
# 启动codis-server服务
/usr/local/codis/bin/codis-server /opt/local/codis_server/conf/6379.conf
/usr/local/codis/bin/codis-server /opt/local/codis_server/conf/6380.conf
# 修改 dashboard 启动脚本
vi /usr/local/codis/scripts/start_dashboard.sh
# 修改为如下路径:
nohup /usr/local/codis/bin/codis-config -c /usr/local/codis/conf/config.ini -L /opt/local/codis_server/logs/dashboard.log dashboard --addr=:18087 --http-log=/opt/local/codis_server/logs/requests.log &>/dev/
null &
# 启动
/usr/local/codis/scripts/start_dashboard.sh
# 访问dashboard
http://localhost:18087/admin/
# 添加redis组
/usr/local/codis/scripts/
vi /usr/local/codis/scripts/add_group.sh
-------------------------------------------------------------------------------------------------------------------------------------------
#!/bin/sh
let group=0
for port in 638{0..3}; do
let group="1+group"
echo "add group $group with a master(localhost:$port), Notice: do not use localhost when in produciton"
/usr/local/codis/bin/codis-config -c /usr/local/codis/conf/config.ini -L /opt/local/codis_server/logs/cconfig.log server add $group localhost:$port master
done
-------------------------------------------------------------------------------------------------------------------------------------------
运行 /usr/local/codis/scripts/add_group.sh 自动添加
# Server Groups -- > New Server Group -- > Add New Redis Instance
#通过管理页面添加组ID,为组添加主从实例,一个组里只能有一个redis-master:
第一个主一个从,默认每个组里面第一个实例是主
group_1
127.0.0.1:6379 master
127.0.0.1:6380 slave
group_2
..............
group_3
..............
group_4
..............
必须有4组或以上,否则报错
# 修改脚本,初始化槽
vi /usr/local/codis/scripts/initslot.sh
---------------------------------------------------------------------------------------------------------------
#!/bin/sh
echo "slots initializing..."
/usr/local/codis/bin/codis-config -c /usr/local/codis/conf/config.ini slot init -f
echo "done"
echo "set slot ranges to server groups..."
for ((i=0;i<4;i++)); do
let beg="256*i"
let end="256*i + 255"
let group="1+i"
/usr/local/codis/bin/codis-config -c /usr/local/codis/conf/config.ini slot range-set $beg $end $group online
done
echo "done"
----------------------------------------------------------------------------------------------------------------
# 启动脚本 ( 初始化solt是在group设置好之后 )
/usr/local/codis/scripts/initslot.sh
# 修改start_proxy.sh,启动codis-proxy服务
vi /usr/local/codis/scripts/start_proxy.sh
----------------------------------------------------------------------------------------------------------------------------------------
#!/bin/sh
echo "shut down proxy_1..."
/usr/local/codis/bin/codis-config -c /usr/local/codis/conf/config.ini proxy offline proxy_1
echo "done"
echo "start new proxy..."
nohup /usr/local/codis/bin/codis-proxy --log-level info -c /usr/local/codis/conf/config.ini -L /opt/local/codis_server/logs/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 /opt/local/codis_server/logs/proxy.log
------------------------------------------------------------------------------------------------------------------------------------------
vi /usr/local/codis/scripts/set_proxy_online.sh
------------------------------------------------------------------------------------------------------------------------------------------
#!/bin/sh
echo "set proxy_1 online"
/usr/local/codis/bin/codis-config -c /usr/local/codis/conf/config.ini proxy online proxy_1
echo "done"
------------------------------------------------------------------------------------------------------------------------------------------
# 启动proxy
/usr/local/codis/scripts/start_proxy.sh
# 上线proxy
/usr/local/codis/scripts/set_proxy_online.sh
docker
172.16.1.52
docker run -h="Codis" --name Codis -d -v /opt/upload/zookeeper:/opt/local/zookeeper/data -v /opt/upload/codis/data:/opt/local/codis_server/data -v /opt/upload/codis/logs:/opt/local/codis_server/logs -p 8002:22 -p 6379:6379 -p 6380:6380 -p 19000:19000 -p 18087:18087 -p 11000:11000 172.16.1.26:5000/mx-codis