之前写过一篇基于redis-3.2.4版本的安装日记,这篇是基于redis-6.2.6改动不少,故再次记录一下
两台电脑10.2.5.147,10.2.5.148,都是centos7.5
本次搭建4主4从集群
1.单机安装
1.1.下载安装包
直接从redis官网下载安装包,官网地址:https://redis.io/download
直接使用命令下载:wget http://download.redis.io/releases/redis-6.2.6.tar.gz
1.2.安装编译
将安装包直接上传到了/opt目录
解压 tar -zxvf redis-6.2.6.tar.gz 之后,得到一个redis-6.2.6的文件夹
个人比较喜欢将文件信息放在/usr/local/redis目录下,所以cp /opt/redis-6.2.6/* /usr/local/redis
接下来就是编译了
进入到/usr/local/redis目录,执行make命令
可能会报错缺少基础依赖类库,例如gcc,使用命令下载安装即可,总之缺少什么就安装什么
make[3]: 进入目录“/usr/local/redis/deps/hiredis” cc -std=c99 -pedantic -c -O3 -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings -Wno-missing-field-initializers -g -ggdb alloc.c make[3]: cc:命令未找到 make[3]: *** [alloc.o] 错误 127 make[3]: 离开目录“/usr/local/redis/deps/hiredis” make[2]: *** [hiredis] 错误 2 make[2]: 离开目录“/usr/local/redis/deps” make[1]: [persist-settings] 错误 2 (忽略) CC adlist.o /bin/sh: cc: 未找到命令 make[1]: *** [adlist.o] 错误 127 make[1]: 离开目录“/usr/local/redis/src”
安装更新gcc:yum isntall gcc-c++
执行完之后,make test
在接下来安装redis
执行make install命令,最后安装结果如下
[root@localhost redis]# make install cd src && make install make[1]: 进入目录“/usr/local/redis/src” Hint: It's a good idea to run 'make test' ;) INSTALL redis-server INSTALL redis-benchmark INSTALL redis-cli make[1]: 离开目录“/usr/local/redis/src”
安装成功!
1.3 试运行
安装完成,需要修改如下配置,找到/usr/local/redis目录下的redis.conf文件
打开文件,修改如下参数
允许远程连接,将下一行注释
#bind 127.0.0.1 -::1
redis默认是前台线程,改为后台启动,修改参数
daemonize yes
设置redis登录密码,找到参数设置
requirepass 你的密码
启动redis:redis-server /usr/local/redis/redis.conf
启动之后查看对应redis线程已启动,redis已可以使用
2.搭建集群
4主4从集群分布:
10.2.5.147 服务器,7010 7011 7012 7013四个节点
10.2.5.148 服务器,7015 7016 7017 7018四个节点
2.1.节点文件拷贝
在/usr/local/redis目录下新建集群节点目录redis_cluster文件夹
mkdir -p /usr/local/redis/redis_cluster
在目录下创建4个节点文件夹
mkdir -p /usr/local/redis/redis_cluster/node01_7010 mkdir -p /usr/local/redis/redis_cluster/node02_7011 mkdir -p /usr/local/redis/redis_cluster/node03_7012 mkdir -p /usr/local/redis/redis_cluster/node04_7013
将redis文件夹中的redis.conf文件分别拷贝至上述4个文件夹中,并且依次修改每个文件,内容如下
#以node01_7010节点为例 port 7010 #每个节点配置自己的端口号 pidfile /var/run/redis_7010.pid #pid文件 cluster-enabled yes #启用集群 cluster-config-file nodes_7010.conf #集群配置文件首次启动自动生成 cluster-node-timeout 15000 #超时时间 appendonly yes #aof日志
其他节点端口号依次为7011 7012 7013对应修改配置文件即可
修改完成后,启用4个节点
#启动7010节点 redis-server /usr/local/redis/redis_cluster/node01_7010/redis.conf #启动7011节点 redis-server /usr/local/redis/redis_cluster/node02_7011/redis.conf #启动7012节点 redis-server /usr/local/redis/redis_cluster/node03_7012/redis.conf #启动7013节点 redis-server /usr/local/redis/redis_cluster/node04_7013/redis.conf
在10.2.5.148服务器中,同样进行上述操作即可,占用端口分别为7015 7016 7017 7018
2.2.安装ruby
安装2.4版本的ruby
2.3.组件集群
在任意一台服务器中,进行集群组建
redis-cli --cluster create 10.2.5.147:7010 10.2.5.147:7011 10.2.5.147:7012 10.2.5.147:7013 10.2.5.148:7015 10.2.5.148:7016 10.2.5.148:7017 10.2.5.148:7018 --cluster-replicas 1
系统会随机选择主从关系
>>> Performing hash slots allocation on 8 nodes... Master[0] -> Slots 0 - 4095 Master[1] -> Slots 4096 - 8191 Master[2] -> Slots 8192 - 12287 Master[3] -> Slots 12288 - 16383 Adding replica 10.2.5.148:7017 to 10.2.5.147:7010 Adding replica 10.2.5.147:7013 to 10.2.5.148:7015 Adding replica 10.2.5.148:7018 to 10.2.5.147:7011 Adding replica 10.2.5.147:7012 to 10.2.5.148:7016 M: 988000d58f5b4c999c20172ef4ee1572ab24f22e 10.2.5.147:7010 slots:[0-4095] (4096 slots) master M: b0d35f7d14f5c9ef1c261a6ef4cff3a09f39bd75 10.2.5.147:7011 slots:[8192-12287] (4096 slots) master S: 738825958b81184e899b6f5a473a846392e6a9b6 10.2.5.147:7012 replicates 757e7f5f9d973acc182c9dadcd1d1f905571d43d S: 723a484f7570dad408b7b50e525af5125b56e05a 10.2.5.147:7013 replicates 0a8d20f140315deeb649bf100793018243771394 M: 0a8d20f140315deeb649bf100793018243771394 10.2.5.148:7015 slots:[4096-8191] (4096 slots) master M: 757e7f5f9d973acc182c9dadcd1d1f905571d43d 10.2.5.148:7016 slots:[12288-16383] (4096 slots) master S: 59b175c9eba3c9931f14ac5fd65ad3ca1a19c140 10.2.5.148:7017 replicates 988000d58f5b4c999c20172ef4ee1572ab24f22e S: c2927e8d368d4698b0accbc9e76216a3b5c67d12 10.2.5.148:7018 replicates b0d35f7d14f5c9ef1c261a6ef4cff3a09f39bd75 Can I set the above configuration? (type 'yes' to accept): yes >>> Nodes configuration updated >>> Assign a different config epoch to each node >>> Sending CLUSTER MEET messages to join the cluster Waiting for the cluster to join ... >>> Performing Cluster Check (using node 10.2.5.147:7010) M: 988000d58f5b4c999c20172ef4ee1572ab24f22e 10.2.5.147:7010 slots:[0-4095] (4096 slots) master 1 additional replica(s) S: c2927e8d368d4698b0accbc9e76216a3b5c67d12 10.2.5.148:7018 slots: (0 slots) slave replicates b0d35f7d14f5c9ef1c261a6ef4cff3a09f39bd75 M: 0a8d20f140315deeb649bf100793018243771394 10.2.5.148:7015 slots:[4096-8191] (4096 slots) master 1 additional replica(s) S: 59b175c9eba3c9931f14ac5fd65ad3ca1a19c140 10.2.5.148:7017 slots: (0 slots) slave replicates 988000d58f5b4c999c20172ef4ee1572ab24f22e S: 723a484f7570dad408b7b50e525af5125b56e05a 10.2.5.147:7013 slots: (0 slots) slave replicates 0a8d20f140315deeb649bf100793018243771394 M: b0d35f7d14f5c9ef1c261a6ef4cff3a09f39bd75 10.2.5.147:7011 slots:[8192-12287] (4096 slots) master 1 additional replica(s) S: 738825958b81184e899b6f5a473a846392e6a9b6 10.2.5.147:7012 slots: (0 slots) slave replicates 757e7f5f9d973acc182c9dadcd1d1f905571d43d M: 757e7f5f9d973acc182c9dadcd1d1f905571d43d 10.2.5.148:7016 slots:[12288-16383] (4096 slots) master 1 additional replica(s) [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
至此集群已经组建起来了,可以试试是否可用
在10.2.5.147服务器中推送消息、存储数据至10.2.5.148的7017节点
[root@localhost redis]# redis-cli -h 10.2.5.148 -c -p 7017 10.2.5.148:7017> PUBLISH testchannel "hello!" (integer) 0 10.2.5.148:7017> PUBLISH testchannel "hello!" (integer) 0 10.2.5.148:7017> SET accessToken 111111111111111111111111111 -> Redirected to slot [2275] located at 10.2.5.147:7010 OK
在10.2.5.148服务器中登录10.2.5.147的监听队列,获取数据
[root@localhost redis]# redis-cli -h 10.2.5.147 -c -p 7010 10.2.5.147:7010> SUBSCRIBE testchannel Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "testchannel" 3) (integer) 1 1) "message" 2) "testchannel" 3) "hello!" [root@localhost redis]# redis-cli -h 10.2.5.147 -c -p 7010 10.2.5.147:7010> get accessToken "111111111111111111111111111"
完成