本文最后更新于688 天前,其中的信息可能已经过时,如有错误请发送邮件到434658198@qq.com
1、介绍
当前项目中并未用到redis6以及任何版本的redis集群,在看redis官方文档介绍的时,按照上面的步骤介绍,在虚拟机中搭建一下,特此记录。
https://redis.io/topics/cluster-tutorial
官方文档:
2、下载Redis
我这里下载的6.0.9版本的
https://download.redis.io/releases/redis-6.0.9.tar.gz
放在 /opt/module目录
wget https://download.redis.io/releases/redis-6.0.9.tar.gz
3、解压
tar -zxvf redis-6.0.9.tar.gz
4、切入redis目录
cd redis-6.0.9
5、编译
make
#如果遇到问题,参考第六条后继续编译
6、编译过程中可能会出现的错误
1、cc: command not found
yum install gcc
2、server.c:xxxx:xx: error: ****** has no member named ******
gcc 版本低于 5.3 ,需要升级
查看当前gcc版本
[root@centos7 redis-6.0.9]# gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper Target: x86_64-redhat-linux Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threaable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languagesbj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/buiUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux Thread model: posix gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
升级gcc到9+
yum install centos-release-scl yum install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils // 临时有效,退出 shell 或重启会恢复原 gcc 版本 scl enable devtoolset-9 bash // 长期有效 echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile
重新进入shell,,查看gcc版本
[root@centos7 ~]# gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-9/root/usr/libexec/gcc/x86_64-redhat-linux/9/lto-wrapper Target: x86_64-redhat-linux Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-9/root/usr --mandir=/opt/rh/devtoolset-9/root/usr/share/man --infodir=/opt/rh/devtoolset-9/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-9.3.1-20200408/obj-x86_64-redhat-linux/isl-install --disable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux Thread model: posix gcc version 9.3.1 20200408 (Red Hat 9.3.1-2) (GCC)
7、编译测试
make test
编译成功后,会推荐我们进行编译测试,但是会遇到问题,解决下面问题后即可编译测试成功
\o/ All tests passed without errors!
Hint: It's a good idea to run 'make test' ;)
编译测试遇到新问题,需要高版本的tcl
[root@centos7 redis-6.0.9]# make test cd src && make test make[1]: Entering directory `/opt/module/redis-6.0.9/src' You need tcl 8.5 or newer in order to run the Redis test
安装新版本的tcl
[root@centos7 module]# wget https://nchc.dl.sourceforge.net/project/tcl/Tcl/8.6.5/tcl8.6.5-src.tar.gz [root@centos7 module]# tar -zxvf tcl8.6.5-src.tar.gz [root@centos7 module]# cd tcl8.6.5/unix [root@centos7 unix]# ./configure [root@centos7 unix]# make [root@centos7 unix]# make install
8、集群搭建
准备使用六个节点的redis集群7000-7005
复制一个节点,名字叫redis-7000[root@centos7 module]# cp -r redis-6.0.9 redis-7000
进入到redis-7000根目录,编辑redis.conf
#68行,把127.0.0.1改成0.0.0.0 bind 0.0.0.0 #91行 把默认端口6379改成7000 port 7000 #224行 把no改成yes,以守护进程运行 daemonize yes #247行 pidfile /var/run/redis_7000.pid #260行 logfile "redis.log" #365行 把当前目录./改成redis的绝对路径 dir /opt/module/redis-7000 #395行,此处和后面设置的requirepass的密码保持一致 masterauth 1234 #790行 设置访问密码 requirepass 1234 #1089 改为yes,启用追加备份 appendonly yes #1222行 启用集群模式 cluster-enabled yes #1230行 名字和端口号对应 cluster-config-file nodes-7000.conf #1236 超时时间设置 cluster-node-timeout 5000
9、copy其他节点并修改配置文件
[root@centos7 module]# cp -r redis-7000 redis-7001
[root@centos7 module]# cp -r redis-7000 redis-7002
[root@centos7 module]# cp -r redis-7000 redis-7003
[root@centos7 module]# cp -r redis-7000 redis-7004
[root@centos7 module]# cp -r redis-7000 redis-7005
# vim分别打开每个节点的配置文件,批量替换文件内容,对应各自的端口号
:%s/7000/7001/g
:%s/7000/7002/g
:%s/7000/7003/g
:%s/7000/7004/g
:%s/7000/7005/g
10、批量启动脚本和批量停止脚本
[root@centos7 module]# vim redis-cluster-start.sh
#!/bin/bash
/opt/module/redis-7000/src/redis-server /opt/module/redis-7000/redis.conf
/opt/module/redis-7001/src/redis-server /opt/module/redis-7001/redis.conf
/opt/module/redis-7002/src/redis-server /opt/module/redis-7002/redis.conf
/opt/module/redis-7003/src/redis-server /opt/module/redis-7003/redis.conf
/opt/module/redis-7004/src/redis-server /opt/module/redis-7004/redis.conf
/opt/module/redis-7005/src/redis-server /opt/module/redis-7005/redis.conf
#赋予可执行权限
[root@centos7 module]# chmod +x redis-cluster-start.sh
-rwxr-xr-x. 1 root root 456 Dec 26 15:41 redis-cluster-start.sh
[root@centos7 module]# vim redis-cluster-stop.sh
ps -aux|grep redis-700*|awk '{print $2}'|xargs kill -15
#赋予可执行权限
[root@centos7 module]# chmod +x redis-cluster-stop.sh
-rwxr-xr-x. 1 root root 456 Dec 26 15:44 redis-cluster-stop.sh
#批量启动redis
[root@centos7 module]# ./redis-cluster-start.sh
#查看启动情况
[root@centos7 module]# ps -aux | grep redis
root 82528 0.1 0.1 143944 5040 ? Ssl 15:43 0:00 /opt/module/redis-7000/src/redis-server 0.0.0.0:7000 [cluster]
root 82534 0.1 0.0 143944 3152 ? Ssl 15:43 0:00 /opt/module/redis-7001/src/redis-server 0.0.0.0:7001 [cluster]
root 82540 0.0 0.1 143944 5040 ? Ssl 15:43 0:00 /opt/module/redis-7002/src/redis-server 0.0.0.0:7002 [cluster]
root 82542 0.0 0.1 143944 5048 ? Ssl 15:43 0:00 /opt/module/redis-7003/src/redis-server 0.0.0.0:7003 [cluster]
root 82544 0.0 0.0 143944 3152 ? Ssl 15:43 0:00 /opt/module/redis-7004/src/redis-server 0.0.0.0:7004 [cluster]
root 82547 0.0 0.0 143944 3156 ? Ssl 15:43 0:00 /opt/module/redis-7005/src/redis-server 0.0.0.0:7005 [cluster]
11、执行集群相关命令
执行集群相关命令,进入到任意一个redis的src目录下,都可执行。出现下列信息后,一个3主3从的redis集群就搭建完成了。
[root@centos7 src]# ./redis-cli --cluster create 192.168.200.145:7000 192.168.200.145:7001 192.168.200.145:7002 192.168.200.145:7003 192.168.200.145:7004 192.168.200.145:7005 --cluster-replicas 1 -a 1234
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.200.145:7004 to 192.168.200.145:7000
Adding replica 192.168.200.145:7005 to 192.168.200.145:7001
Adding replica 192.168.200.145:7003 to 192.168.200.145:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 41383c5185f54580a7128e8024c9dfca28e0199a 192.168.200.145:7000
slots:[0-5460] (5461 slots) master
M: 7c7541b6b59a3a4d07fefc39e05481da62bb9f66 192.168.200.145:7001
slots:[5461-10922] (5462 slots) master
M: c5f101660f79e16be3ba929e3b89db473d4f0e1e 192.168.200.145:7002
slots:[10923-16383] (5461 slots) master
S: 1417e90909a9dd9f00e712490505fd5caa6976b1 192.168.200.145:7003
replicates c5f101660f79e16be3ba929e3b89db473d4f0e1e
S: fbc9ca8666f68ce05abd0983e2ee85a04abad5d3 192.168.200.145:7004
replicates 41383c5185f54580a7128e8024c9dfca28e0199a
S: fb1e9836d0ae118263af16e213adc5a042fbd1d5 192.168.200.145:7005
replicates 7c7541b6b59a3a4d07fefc39e05481da62bb9f66
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 192.168.200.145:7000)
M: 41383c5185f54580a7128e8024c9dfca28e0199a 192.168.200.145:7000
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 7c7541b6b59a3a4d07fefc39e05481da62bb9f66 192.168.200.145:7001
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: fb1e9836d0ae118263af16e213adc5a042fbd1d5 192.168.200.145:7005
slots: (0 slots) slave
replicates 7c7541b6b59a3a4d07fefc39e05481da62bb9f66
M: c5f101660f79e16be3ba929e3b89db473d4f0e1e 192.168.200.145:7002
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 1417e90909a9dd9f00e712490505fd5caa6976b1 192.168.200.145:7003
slots: (0 slots) slave
replicates c5f101660f79e16be3ba929e3b89db473d4f0e1e
S: fbc9ca8666f68ce05abd0983e2ee85a04abad5d3 192.168.200.145:7004
slots: (0 slots) slave
replicates 41383c5185f54580a7128e8024c9dfca28e0199a
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
12、查看集群信息
[root@centos7 src]# pwd
/opt/module/redis-7000/src
[root@centos7 src]# ./redis-cli -p 7000 -a 1234 -c
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# 集群信息
127.0.0.1:7000> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:1118
cluster_stats_messages_pong_sent:1098
cluster_stats_messages_sent:2216
cluster_stats_messages_ping_received:1093
cluster_stats_messages_pong_received:1118
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:2216
#节点信息
127.0.0.1:7000> cluster nodes
7c7541b6b59a3a4d07fefc39e05481da62bb9f66 192.168.200.145:7001@17001 master - 0 1608969568354 2 connected 5461-10922
fb1e9836d0ae118263af16e213adc5a042fbd1d5 192.168.200.145:7005@17005 slave 7c7541b6b59a3a4d07fefc39e05481da62bb9f66 0 1608969567851 2 connected
41383c5185f54580a7128e8024c9dfca28e0199a 192.168.200.145:7000@17000 myself,master - 0 1608969566000 1 connected 0-5460
c5f101660f79e16be3ba929e3b89db473d4f0e1e 192.168.200.145:7002@17002 master - 0 1608969569358 3 connected 10923-16383
1417e90909a9dd9f00e712490505fd5caa6976b1 192.168.200.145:7003@17003 slave c5f101660f79e16be3ba929e3b89db473d4f0e1e 0 1608969567000 3 connected
fbc9ca8666f68ce05abd0983e2ee85a04abad5d3 192.168.200.145:7004@17004 slave 41383c5185f54580a7128e8024c9dfca28e0199a 0 1608969568000 1 connected
127.0.0.1:7000>
13、配置开机自启动
#进入该目录
[root@centos7 system]# cd /usr/lib/systemd/system
[root@centos7 system]# vim redis-cluster.service
[Unit]
Description=Redis 6.0.9 Cluster Service
After=network.target
[Service]
Type=forking
ExecStart=/opt/module/redis-cluster-start.sh
ExecStop=/opt/module/redis-cluster-stop.sh
PrivateTmp=true
[Install]
WantedBy=default.target
#开机自启动
[root@centos7 ~]# systemctl enable redis-cluster
14、使用systemctl管理服务
# 查看状态
[root@centos7 ~]# systemctl status redis-cluster
● redis-cluster.service - Redis 6.0.9 Cluster Service
Loaded: loaded (/usr/lib/systemd/system/redis-cluster.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2020-12-26 16:59:54 CST; 50s ago
Process: 1076 ExecStart=/opt/module/redis-cluster-start.sh (code=exited, status=0/SUCCESS)
Tasks: 36
Memory: 85.2M
CGroup: /system.slice/redis-cluster.service
├─1154 /opt/module/redis-7000/src/redis-server 0.0.0.0:7000 [cluster]
├─1195 /opt/module/redis-7001/src/redis-server 0.0.0.0:7001 [cluster]
├─1255 /opt/module/redis-7002/src/redis-server 0.0.0.0:7002 [cluster]
├─1278 /opt/module/redis-7003/src/redis-server 0.0.0.0:7003 [cluster]
├─1378 /opt/module/redis-7004/src/redis-server 0.0.0.0:7004 [cluster]
└─1471 /opt/module/redis-7005/src/redis-server 0.0.0.0:7005 [cluster]
Dec 26 16:59:50 centos7 systemd[1]: Starting Redis 6.0.9 Cluster Service...
Dec 26 16:59:54 centos7 systemd[1]: Started Redis 6.0.9 Cluster Service.
# 停止服务
[root@centos7 ~]# systemctl stop redis-cluster
# 启动服务
[root@centos7 ~]# systemctl start redis-cluster