1.首先安装Redis,进入到usr/local目录下,下载redis压缩包
cd /usr/local
wget https://github.com/antirez/redis/archive/5.0-rc3.tar.gz
2.解压
tar -zxvf 5.0-rc3.tar.gz
将 redis-trib.rb 复制到 /usr/local目录下
cp usr/local/redis-5.0-rc3/src/redis-trib.rb /usr/local/
3.安装Development Tools
yum update
yum groupinstall 'Development Tools'
4.进入redis-5.0-rc3/deps目录执行以下命令
cd usr/local/redis-5.0-rc3/deps
make hiredis lua jemalloc linenoise
5.进入到redis-5.0-rc3/src目录下面进行安装
cd usr/llocal/redis-5.0-rc3/src
make install
出现如下说明安装成功
6.修改redis.conf配置文件
vim /usr/local/redis-5.0-rc3/redis.conf
7.修改以下几个参数
cluster-enabled yes //开启集群 把注释#去掉
cluster-node-timeout 15000 //请求超时 默认15秒,可自行设置
bind 127.0.0.1----->>>#bind 127.0.0.1 #注释掉
protected-mode yes --->>>protected-mode no #关闭保护模式
daemonize no --->>daemonize yes # 守护进程模式开启
appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志
-------------------------------------------------------------------------------------------------------------------------------
因为redis集群最少要3个master节点,每个节点最少要一个slave,所以要复制6份
cp -r usr/local/redis-5.0-rc3 / usr/local/redis1
cp -r usr/local/redis-5.0-rc3 / usr/local/redis2
cp -r usr/local/redis-5.0-rc3 / usr/local/redis3
cp -r usr/local/redis-5.0-rc3 / usr/local/redis4
cp -r usr/local/redis-5.0-rc3 / usr/local/redis5
cp -r usr/local/redis-5.0-rc3 / usr/local/redis6
分别修改对应redis1-6里的redis.conf
port 6379---->>port 7001 #7001-7006
pidfile /var/run/redis_7001.pid #7001-7006
cluster-config-file nodes_7001.conf #7001-7006集群的配置
然后我们分别启动redis1-6
/usr/local/redis1/src/redis-server /usr/local/redis1/redis.conf
/usr/local/redis2/src/redis-server /usr/local/redis2/redis.conf
/usr/local/redis3/src/redis-server /usr/local/redis3/redis.conf
/usr/local/redis4/src/redis-server /usr/local/redis4/redis.conf
/usr/local/redis5/src/redis-server /usr/local/redis5/redis.conf
/usr/local/redis6/src/redis-server /usr/local/redis6/redis.conf
9.检查端口命令
ps -ef | grep redis
有端口监听说明启动成功。
然后我们要搭建集群需要安装ruby
yum install centos-release-scl-rh
yum install rh-ruby24 -y
scl enable rh-ruby24 bash
gem install redis
ruby -v
Redis 官方提供了 redis-trib.rb 这个工具,就在解压目录的 src 目录中,前面已将它复制到 /usr/local/ 目录中了,在usr/local目录下执行
./redis-trib.rb create --replicas 1 10.255.0.58:7001 10.255.0.58:7002 10.255.0.58:7003 10.255.0.58:7004 10.255.0.58:7005 10.255.0.58:7006
输入 yes 即可,然后出现如下内容,说明安装成功
集群验证
redis-cli -h 10.255.0.58 -c -p 7002
set hello world
key *
ctrl +c 再连接另一个redis
redis-cli -h 10.255.0.58 -c -p 7003
get hello
出现world说明集群成功