简单一句话,在RedisCluster集群方案没出现时,业界只有twemproxy支持Sharding方案, 但是Twemproxy对集群的扩缩容,resharding处理得不那么优雅,Codis方案应运而生。当然不仅仅是上面的优缺点,还有更多大家自己发掘,本文是站在一个运维人员,也就是administrator的角度,针对codis集群运维做以简介,不讨论业务层面相关知识。
https://github.com/Littlegump/codis/tree/release3.0
https://github.com/Littlegump/codis/blob/release3.0/doc/tutorial_zh.md
自己搭建的环境,docker run 直接可用
FROM debian:8.9
WORKDIR /data
ADD . /data
RUN apt-get update && apt-get install -y python supervisor curl vim psmisc wget man-db python-pip git gcc make net-tools netcat default-jdk dirmngr gnupg && apt-get clean && rm -rf /var/lib/apt/lists/*
RUN bash initSoftware.sh ##内容参见附录
EXPOSE 8080 18080 2181 11080 19000
CMD ["sleep","infinity"]
Codis Admin(集群管理的命令行工具):
codis_admin --create-group --gid $g
codis_admin --group-add --gid $g -x 127.0.0.1:${p}
codis_admin --create-proxy -x 127.0.0.1:${p1}
codis_admin --slot-action --interval=100
# 暂时未学习codis_admin --rebalance --confirm
# 暂时未学习# 需要java环境
ZKPKG=zookeeper-3.4.14
wget -O $ZKPKG.tar.gz https://downloads.apache.org/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz
tar zxf $ZKPKG.tar.gz -C /usr/local
ln -s /usr/local/$ZKPKG /usr/local/zookeeper
cat zoo1.cfg
tickTime=2000 # hearbeat Detect
initLimit=5 // leader和其他zoo成员通信相关的,暂未了解
syncLimit=2 // 同上
clientPort=2181 # zoo 客户端端口
dataDir=/data/zoo/01
dataLogDir= /data/zoo/01
server.1=127.0.0.1:2888:3888 // 下述三行列举了本zookeeper集群的所有成员,
server.2=127.0.0.1:2889:3889 // server.x = [hostname]:[port1]:[port2]
server.3=127.0.0.1:2890:3890 // x表示myid, port1用于L和F的通信,port2用于L选举时的投票通信
常用命令
zkCli.sh --server 127.0.0.1:2181
[zk: 127.0.0.1:2182(CONNECTED) 8] help // 查看帮助
[...] create /zk_test my_data # 创建一个znode,并与"my_data"关联
[...] ls / # 用于查看当前zk集群管理的znode
[codis3, zookeeper, zk_test]
[...] get /zk_test 验证数据是否有与znode("/zk_test")关联的字符串
my_data
cZxid = 0x100001c21
ctime = Wed Apr 08 07:11:17 UTC 2020
mZxid = 0x100001c21
mtime = Wed Apr 08 07:11:17 UTC 2020
pZxid = 0x100001c21
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0
安装
ETCD_VERSION=${ETCD_VERSION:-v3.3.1}
curl -L https://github.com/coreos/etcd/releases/download/$ETCD_VERSION/etcd-$ETCD_VERSION-linux-amd64.tar.gz -o etcd-$ETCD_VERSION-linux-amd64.tar.gz
tar xzvf etcd-$ETCD_VERSION-linux-amd64.tar.gz
rm etcd-$ETCD_VERSION-linux-amd64.tar.gz
cd etcd-$ETCD_VERSION-linux-amd64
cp etcd /usr/local/bin/
cp etcdctl /usr/local/bin/
rm -rf etcd-$ETCD_VERSION-linux-amd64
etcdctl --version
nohup etcd --name="codisInstance" &> etcd.log &
#!/bin/bash
wget https://dl.google.com/go/go1.5.2.linux-amd64.tar.gz && tar -C /usr/local -xzf go1.5.2.linux-amd64.tar.gz
echo "export PATH=$PATH:/usr/local/go/bin:/root/go/bin" >> ~/.bashrc |~
echo """export GOPATH="$HOME/go"""" >> ~/.bashrc
source ~/.bashrc
go get -u github.com/tools/godep
# install redis
wget http://download.redis.io/releases/redis-5.0.8.tar.gz && tar xzf redis-5.0.8.tar.gz && cd redis-5.0.8 && make
ln -sv /data/redis-5.0.8/src/redis* /usr/bin/
# install codis
mkdir -p $GOPATH/src/github.com/CodisLabs
cd $_ && git clone https://github.com/CodisLabs/codis.git -b release3.0
cd $GOPATH/src/github.com/CodisLabs/codis && make
# install etcd
ETCD_VERSION=${ETCD_VERSION:-v3.3.1}
curl -L https://github.com/coreos/etcd/releases/download/$ETCD_VERSION/etcd-$ETCD_VERSION-linux-amd64.tar.gz -o etcd-$ETCD_VERSION-linux-amd64.tar.gz
tar xzvf etcd-$ETCD_VERSION-linux-amd64.tar.gz
rm etcd-$ETCD_VERSION-linux-amd64.tar.gz
cd etcd-$ETCD_VERSION-linux-amd64
cp etcd /usr/local/bin/
cp etcdctl /usr/local/bin/
rm -rf etcd-$ETCD_VERSION-linux-amd64
etcdctl --version
# install zookeeper
ZKPKG=zookeeper-3.4.14
wget -O $ZKPKG.tar.gz https://downloads.apache.org/zookeeper/zookeeper-3.4.14/zookeeper-3.4.14.tar.gz
tar zxf $ZKPKG.tar.gz -C /usr/local
ln -s /usr/local/$ZKPKG /usr/local/zookeeper
startZkCodis.sh
#!/bin/bash
PATH=$PATH:/root/go/src/github.com/CodisLabs/codis/bin
# start ZooKeeper
rm -rf /data/zookeeper/ && mkdir -p /data/zookeeper/{01,02,03}
echo 1 > /data/zookeeper/01/myid
echo 2 > /data/zookeeper/02/myid
echo 3 > /data/zookeeper/03/myid
cat > /data/zookeeper/zoo1.cfg <<EOF
tickTime=2000
dataDir=/data/zookeeper/01
dataLogDir=/data/zookeeper/01
clientPort=2181
initLimit=5
syncLimit=2
server.1=0.0.0.0:2888:3888
server.2=0.0.0.0:2889:3889
server.3=0.0.0.0:2890:3890
EOF
cat > /data/zookeeper/zoo2.cfg <<EOF
tickTime=2000
dataDir=/data/zookeeper/02
dataLogDir=/data/zookeeper/02
clientPort=2182
initLimit=5
syncLimit=2
server.1=0.0.0.0:2888:3888
server.2=0.0.0.0:2889:3889
server.3=0.0.0.0:2890:3890
EOF
cat > /data/zookeeper/zoo3.cfg <<EOF
tickTime=2000
dataDir=/data/zookeeper/03
dataLogDir=/data/zookeeper/03
clientPort=2183
initLimit=5
syncLimit=2
server.1=0.0.0.0:2888:3888
server.2=0.0.0.0:2889:3889
server.3=0.0.0.0:2890:3890
EOF
pushd /usr/local/zookeeper
./bin/zkServer.sh start /data/zookeeper/zoo1.cfg
./bin/zkServer.sh start /data/zookeeper/zoo2.cfg
./bin/zkServer.sh start /data/zookeeper/zoo3.cfg
rm -rf /data/tmp; mkdir -p /data/tmp && pushd /data/tmp
cat > proxy1.toml <<EOF
product_name="codisInstance"
product_auth=""
proto_type="tcp4"
admin_addr="0.0.0.0:11080"
proxy_addr="0.0.0.0:19000"
EOF
cat > proxy2.toml <<EOF
product_name="codisInstance"
product_auth=""
proto_type="tcp4"
admin_addr="0.0.0.0:11081"
proxy_addr="0.0.0.0:19001"
EOF
cat > proxy3.toml <<EOF
product_name="codisInstance"
product_auth=""
proto_type="tcp4"
admin_addr="0.0.0.0:11082"
proxy_addr="0.0.0.0:19002"
EOF
cat > redis1.conf <<EOF
port 6379
dbfilename dump1.rdb
EOF
cat > redis2.conf <<EOF
port 6380
dbfilename dump2.rdb
EOF
cat > redis3.conf <<EOF
port 6381
dbfilename dump3.rdb
EOF
cat > redis4.conf <<EOF
port 6382
dbfilename dump4.rdb
EOF
cat > redis5.conf <<EOF
port 6383
dbfilename dump5.rdb
EOF
cat > redis6.conf <<EOF
port 6384
dbfilename dump6.rdb
EOF
cat > codis.json <<EOF
[
{
"name": "codisInstance",
"dashboard": "127.0.0.1:18080"
}
]
EOF
cat >dashboard.toml <<EOF
coordinator_name = "zookeeper"
coordinator_addr = "127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183"
product_name = "codisInstance"
product_auth = ""
admin_addr = "0.0.0.0:18080"
EOF
nohup etcd --name="codisInstance" &> etcd.log &
if [ $? -ne 0 ];then
echo "codis-etcd start failed"
exit 1
fi
nohup codis-server redis1.conf &> redis-6379.log &
nohup codis-server redis2.conf &> redis-6380.log &
nohup codis-server redis3.conf &> redis-6381.log &
nohup codis-server redis4.conf &> redis-6382.log &
nohup codis-server redis5.conf &> redis-6383.log &
nohup codis-server redis6.conf &> redis-6384.log &
if [ $? -ne 0 ];then
echo "codis-server start failed"
exit 1
fi
nohup codis-proxy -c proxy1.toml &>proxy1.log &
nohup codis-proxy -c proxy2.toml &>proxy2.log &
nohup codis-proxy -c proxy3.toml &>proxy3.log &
if [ $? -ne 0 ];then
echo "codis-proxy start failed"
exit 1
fi
nohup codis-dashboard -c dashboard.toml &> dashboard.log &
if [ $? -ne 0 ];then
echo "codis-Dashboard start failed"
exit 1
fi
nohup /root/go/src/github.com/CodisLabs/codis/bin/codis-fe -d codis.json --listen 0.0.0.0:8080 &> fe.log &
if [ $? -ne 0 ];then
echo "codis-FE start failed"
exit 1
fi
nohup codis-ha --dashboard=127.0.0.1:18080 &> codis_ha.log &
if [ $? -ne 0 ];then
echo "codis-HA start failed"
exit 1
fi
codis-admin --dashboard=127.0.0.1:18080 --create-group --gid 1
codis-admin --dashboard=127.0.0.1:18080 --create-group --gid 2
codis-admin --dashboard=127.0.0.1:18080 --create-group --gid 3
codis-admin --dashboard=127.0.0.1:18080 --group-add --gid 1 -x 127.0.0.1:6379
codis-admin --dashboard=127.0.0.1:18080 --group-add --gid 1 -x 127.0.0.1:6380
codis-admin --dashboard=127.0.0.1:18080 --group-add --gid 2 -x 127.0.0.1:6381
codis-admin --dashboard=127.0.0.1:18080 --group-add --gid 2 -x 127.0.0.1:6382
codis-admin --dashboard=127.0.0.1:18080 --group-add --gid 3 -x 127.0.0.1:6383
codis-admin --dashboard=127.0.0.1:18080 --group-add --gid 3 -x 127.0.0.1:6384
codis-admin --dashboard=127.0.0.1:18080 --create-proxy -x 127.0.0.1:11080
codis-admin --dashboard=127.0.0.1:18080 --create-proxy -x 127.0.0.1:11081
codis-admin --dashboard=127.0.0.1:18080 --create-proxy -x 127.0.0.1:11082
codis-admin --dashboard=127.0.0.1:18080 --slot-action --interval=100
codis-admin --dashboard=127.0.0.1:18080 --rebalance --confirm
while true; do
date
sleep 60
done
startEtcdCodis.sh
#!/bin/bash
PATH=$PATH:/root/go/src/github.com/CodisLabs/codis/bin
rm -rf tmp; mkdir -p tmp && pushd tmp
cat > proxy1.toml <<EOF
product_name="codisInstance"
product_auth=""
proto_type="tcp4"
admin_addr="0.0.0.0:11080"
proxy_addr="0.0.0.0:19000"
EOF
cat > proxy2.toml <<EOF
product_name="codisInstance"
product_auth=""
proto_type="tcp4"
admin_addr="0.0.0.0:11081"
proxy_addr="0.0.0.0:19001"
EOF
cat > proxy3.toml <<EOF
product_name="codisInstance"
product_auth=""
proto_type="tcp4"
admin_addr="0.0.0.0:11082"
proxy_addr="0.0.0.0:19002"
EOF
cat > redis1.conf <<EOF
port 6379
dbfilename dump1.rdb
EOF
cat > redis2.conf <<EOF
port 6380
dbfilename dump2.rdb
EOF
cat > redis3.conf <<EOF
port 6381
dbfilename dump3.rdb
EOF
cat > redis4.conf <<EOF
port 6382
dbfilename dump4.rdb
EOF
cat > redis5.conf <<EOF
port 6383
dbfilename dump5.rdb
EOF
cat > redis6.conf <<EOF
port 6384
dbfilename dump6.rdb
EOF
cat > codis.json <<EOF
[
{
"name": "codisInstance",
"dashboard": "127.0.0.1:18080"
}
]
EOF
cat >dashboard.toml <<EOF
coordinator_name = "etcd"
coordinator_addr = "127.0.0.1:2379"
product_name = "codisInstance"
product_auth = ""
admin_addr = "0.0.0.0:18080"
EOF
nohup etcd --name="codisInstance" &> etcd.log &
nohup codis-server redis1.conf &> redis-6379.log &
nohup codis-server redis2.conf &> redis-6380.log &
nohup codis-server redis3.conf &> redis-6381.log &
nohup codis-server redis4.conf &> redis-6382.log &
nohup codis-server redis5.conf &> redis-6383.log &
nohup codis-server redis6.conf &> redis-6384.log &
nohup codis-proxy -c proxy1.toml &>proxy1.log &
nohup codis-proxy -c proxy2.toml &>proxy2.log &
nohup codis-proxy -c proxy3.toml &>proxy3.log &
nohup codis-dashboard -c dashboard.toml &> dashboard.log &
nohup /root/go/src/github.com/CodisLabs/codis/bin/codis-fe -d codis.json --listen 0.0.0.0:8080 &> fe.log &
nohup codis-ha --dashboard=127.0.0.1:18080 &> codis_ha.log &
codis-admin --dashboard=127.0.0.1:18080 --create-group --gid 1
codis-admin --dashboard=127.0.0.1:18080 --create-group --gid 2
codis-admin --dashboard=127.0.0.1:18080 --create-group --gid 3
codis-admin --dashboard=127.0.0.1:18080 --group-add --gid 1 -x 127.0.0.1:6379
codis-admin --dashboard=127.0.0.1:18080 --group-add --gid 1 -x 127.0.0.1:6380
codis-admin --dashboard=127.0.0.1:18080 --group-add --gid 2 -x 127.0.0.1:6381
codis-admin --dashboard=127.0.0.1:18080 --group-add --gid 2 -x 127.0.0.1:6382
codis-admin --dashboard=127.0.0.1:18080 --group-add --gid 3 -x 127.0.0.1:6383
codis-admin --dashboard=127.0.0.1:18080 --group-add --gid 3 -x 127.0.0.1:6384
codis-admin --dashboard=127.0.0.1:18080 --create-proxy -x 127.0.0.1:11080
codis-admin --dashboard=127.0.0.1:18080 --create-proxy -x 127.0.0.1:11081
codis-admin --dashboard=127.0.0.1:18080 --create-proxy -x 127.0.0.1:11082
codis-admin --dashboard=127.0.0.1:18080 --slot-action --interval=100
codis-admin --dashboard=127.0.0.1:18080 --rebalance --confirm
sleep 3
while true; do
date
sleep 60
done
原文:https://blog.51cto.com/gumpping/2491359