redis2.0一般使用哨兵做集群(中小企业够用),3.0以后使用rediscluter模式(高可用,高并发),本地搭建了cluster模式,记录一下。
1.下载redis-3.2.6
cd /usr/local/src/
wget http://download.redis.io/releases/redis-3.2.6.tar.gz
2.编译
tar zxvf redis-3.2.6.tar.gz -C/usr/local/soft/
cd /usr/local/soft/
cd redis-3.2.6/
make
3.创建rediscluster目录
cd /usr/local/soft/
mkdir redis_cluster
cd redis_cluster/
mkdir 8001 8002 8003 8004 80058006
4 修改redis-conf 文件
daemonize yes //redis后台运行
pidfile /var/run/redis_8001.pid //pidfile文件port 8001
cluster-enabled yes //开启集群 把注释#去掉
cluster-config-file nodes_8001.conf //集群的配置 配置文件首次启动自动生成
cluster-node-timeout 5000 //请求超时 设置5秒够了
appendonly yes
bind 本机ip地址
5 copy redis-conf 文件到 8001/8002/8003/8004/8005/8006
cp/usr/local/soft/redis-3.2.6/redis.conf /usr/local/soft/redis_cluster/8001
cp/usr/local/soft/redis-3.2.6/redis.conf /usr/local/soft/redis_cluster/8002
cp/usr/local/soft/redis-3.2.6/redis.conf /usr/local/soft/redis_cluster/8003
cp/usr/local/soft/redis-3.2.6/redis.conf /usr/local/soft/redis_cluster/8004
cp/usr/local/soft/redis-3.2.6/redis.conf /usr/local/soft/redis_cluster/8005
cp/usr/local/soft/redis-3.2.6/redis.conf /usr/local/soft/redis_cluster/8006
批量修改 :%s/8001/800*/g 依次替换所有的redis-conf文件
并启动每一个节点的redis(启动集群前,需要把每个节点先启动)
cd /usr/local/bin/
redis-server/usr/local/redis-cluster/8001/redis.conf
redis-server/usr/local/redis-cluster/8002/redis.conf
redis-server/usr/local/redis-cluster/8003/redis.conf
redis-server/usr/local/redis-cluster/8004/redis.conf
redis-server/usr/local/redis-cluster/8005/redis.conf
redis-server/usr/local/redis-cluster/8006/redis.conf
7 如果没有安装ruby,则安装(我的系统是centos6默认安装ruby版本是2.0.0,如果集成redis接口,需要最低版本2.2.2,解决方法:先安装rvm,再把ruby版本提升,可以参考https://blog.youkuaiyun.com/qq_37595946/article/details/77800147)
cd/usr/local/soft/redis-3.2.6/src/
yum -y install ruby ruby-develrubygems rpm-build
gem install redis
8 查看ruby安装是否成功
./redis-trib.rb
Usage: redis-trib <command> <options><arguments ...>
rebalance host:port
--threshold <arg>
--simulate
--weight <arg>
--timeout <arg>
--auto-weights
--pipeline <arg>
--use-empty-masters
info host:port
check host:port
fix host:port
--timeout <arg>
add-node new_host:new_port existing_host:existing_port
--master-id <arg>
--slave
import host:port
--replace
--copy
--from <arg>
reshard host:port
--to <arg>
--from <arg>
--yes
--timeout <arg>
--slots <arg>
--pipeline <arg>
help (show this help)
call host:port command arg arg .. arg
set-timeout host:portmilliseconds
del-node host:portnode_id
create host1:port1 ... hostN:portN
--replicas <arg>
For check, fix, reshard, del-node, set-timeout you canspecify the host and port of any working node in the cluster.
如果正常提示,启动cluster集群(执行创建集群命令)
cd/usr/local/soft/redis-3.2.6/src/
./redis-trib.rb create --replicas1 192.168.181.1:8001 192.168.181.1:8002 192.168.181.1:8003 192.168.181.1:8004192.168.181.1:8005 192.168.181.1:8006
redis-cli -c -h 192.168.181.1 -p8001
redis-cli -c -h 192.168.181.1 -p8002
redis-cli -c -h 192.168.181.1 -p8003