codis集群介绍


一、CC Codis使用介绍

简单一句话,在RedisCluster集群方案没出现时,业界只有twemproxy支持Sharding方案, 但是Twemproxy对集群的扩缩容,resharding处理得不那么优雅,Codis方案应运而生。当然不仅仅是上面的优缺点,还有更多大家自己发掘,本文是站在一个运维人员,也就是administrator的角度,针对codis集群运维做以简介,不讨论业务层面相关知识。

二、Codis 3.0.x集群介绍

2.1 参考

https://github.com/Littlegump/codis/tree/release3.0
https://github.com/Littlegump/codis/blob/release3.0/doc/tutorial_zh.md

2.2 codis环境初始化

自己搭建的环境,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"]

2.2 集群架构&&组件

  1. Codis-Server(n >=1, group >=1):
    基于2.8.21开发的redis实例,用于组建group(一个replication集群,master-r1-r2)
  2. Codis proxy(n>=1)
    客户端入口,用于实现redis协议,由codis-dashboard保证状态保证其状态同步
  3. Codis Dashboard(n = 0 || n = 1):
    集群管理page:
    • codis-proxy的增删
    • codis-server的增删
    • 数据迁移
    • 维护codis-proxy数据一致性[TODO]
  4. Codis Admin(集群管理的命令行工具):

    • 创建group
      codis_admin --create-group --gid $g
    • 将codis-server加入到group
      codis_admin --group-add --gid $g -x 127.0.0.1:${p}
    • 将codis-proxy加入到codis集群dashboard管理范围内
      codis_admin --create-proxy -x 127.0.0.1:${p1}
      codis_admin --slot-action --interval=100 # 暂时未学习
      codis_admin --rebalance --confirm # 暂时未学习
  5. Codis FE:
    展示页面
  6. Codis HA
    • 3.0版本中,codis-ha依赖codis-dashboard实例,可选组件,它自动获取各个组件状态
    • 根据集群状态自动生成主从切换策略(原理[TODO]), 主从切换的执行人是codis-dashboard
  7. Codis Storage
    不同集群会按照配置各个组件的"product_name"配置来进行区分,也就是说,一个codis-dashboard可以管理多个codis集群,对codis集群做逻辑划分

2.3 启动

  1. zookeeper方式
    附录2 startEtcdCodis.sh
  2. etcd方式
    附录3 startZkCodis.sh

三、zookeeper

  1. 参考
    中文教程 https://www.w3cschool.cn/zookeeper/zookeeper_overview.html
    集群管理: https://zookeeper.apache.org/doc/current/zookeeperAdmin.html
    一个zoo集群,由一个leader和n个followers组成
  2. 安装&&准备
    # 需要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
  3. 集群搭建: 见DockerFile
    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选举时的投票通信
  4. 集群管理tutorial点击这里
  5. 常用命令

    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

  1. 安装

    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
  2. 启动
    nohup etcd --name="codisInstance" &> etcd.log &

五、附录

  • initSoftware.sh
#!/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
  1. 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 < /data/zookeeper/zoo2.cfg < /data/zookeeper/zoo3.cfg < proxy1.toml < proxy2.toml < proxy3.toml < redis1.conf < redis2.conf < redis3.conf < redis4.conf < redis5.conf < redis6.conf < codis.json <dashboard.toml < 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
  2. 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 < proxy2.toml < proxy3.toml < redis1.conf < redis2.conf < redis3.conf < redis4.conf < redis5.conf < redis6.conf < codis.json <dashboard.toml < 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