CentOS 7 x64 Codis 群集 以及 单机   docker



# 要安装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



以下为群集配置

-------------------------------------------------------

tickTime=2000

initLimit=5

syncLimit=2

dataDir=/opt/local/zookeeper/data

dataLogDir=/opt/local/zookeeper/logs

clientPort=2181

server.1=0.0.0.0:2888:3888

server.2=172.16.32.33:2888:3888

server.3=172.16.32.40:2888:3888

#maxClientCnxns=60

#autopurge.snapRetainCount=3

#autopurge.purgeInterval=1

-------------------------------------------------------


echo 1 > /opt/local/zookeeper/data/myid

echo 2 > /opt/local/zookeeper/data/myid

echo 3 > /opt/local/zookeeper/data/myid


启动 zookeeper


/opt/local/zookeeper/bin/zkServer.sh start


查看启动状态

/opt/local/zookeeper/bin/zkServer.sh status






# 首先安装 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 端口  多proxy 配置VIP地址,但只能启动一个dashboard ,程序掉线再启动其他


dashboard_addr


( zk  配置 zookeeper 地址,单机配置一个,群集配置多个~已,号隔开 )


proxy_id=codis_proxy_1  配置为唯一


product=     # 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"   #日志文件

maxmemory 20gb                #最大内存设置

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/



------------------------------------------------------------------------------------

dashboard  禁止使用 kill -9 结束进程


如果使用kill -9 或者 意外关机导致进程关闭,需要将zookeeper 节点信息删除才能启动。


或者修改 config.ini 中的 product  选项,填写不相同的节点。


登陆zkCli.sh

ls /zk/codis/     查找相应节点


删除对应节点名称下的 dashboard 信息 db_codis 为 product 配置的名称

delete /zk/codis/db_codis/dashboard


------------------------------------------------------------------------------------



# 添加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





------------------------------------------------------------------------------

codis-server 主从切换配置


codis-ha 是一个通过 是一个通过 codis开放的 api 实现自动切换主从 


配置如下:


go get github.com/ngaut/codis-ha

cd /usr/local/codis/src/github.com/ngaut/codis-ha

go build

cp codis-ha /usr/local/codis/bin

启动:

cd /usr/local/codis/bin 

./codis-ha --codis-config="127.0.0.1:18087" --productName="mycodis"


------------------------------------------------------------------------------








docker 


172.16.1.52


docker run --net=none -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 172.16.1.26:5000/mx-codis





需要开放的端口  -p 2181:2181 -p 19000:19000 -p 18087:18087 -p 11000:11000


----------------------------------------------------------------------------------------

群集版 codis_proxy 


docker 需要配置 固定IP模式,否则 ip addr 读取的为 docker 容器的网卡ip 


----------------------------------------------------------------------------------------







 


你可能感兴趣的:(codis)