Redis全称Remote Dictionary Server 远程数据字典服务,官网https://redis.io/,copy一段官网的介绍:Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.
一般我们用它做缓存使用的,毕竟在分布式环境,每次都缓存在本机不太现实,而且占内存,除了做缓存使用,另外用的比较多的就是用它的setnx方法设置分布锁,有个开源的Redisson,后面抽空再写篇文章介绍一下,本文记录实际环境搭建Redis集群的步骤,供大家参考。本文没有使用Docker,采用Redis源码编译后直接运行在linux服务器。
1.检查是否安装gcc(The GNU Compiler Collection 编译c或者c++等等的编译器,redis c写的)
gcc -v
如果没有安装,安装一下
yum install gcc-c++
2.创建目录编译安装
cd /usr/local
mkdir redis-cluster
cd redis-cluster
mkdir redis-source
cd redis-source
如果你们服务器能连外网可以在线下载
wget http://download.redis.io/releases/redis-5.0.5.tar.gz
我们服务器无法上外网,只能先下载好,再上传到服务器。
解压缩
tar zxf redis-5.0.5.tar.gz
cd redis-5.0.5
编译
make
设置路径,安装集群第一个节点
make install PREFIX=/usr/local/redis-cluster/redis01
配置文件也一起拷贝过去
cp redis.conf /usr/local/redis-cluster/redis01/bin/redis.conf
编辑配置文件
vim redis.conf
改成后台启动
daemonize yes
端口改为7001
port 7001
允许集群打开
cluster-enabled yes
保护模式关闭(重要,否则其他ip客户端或节点无法访问)
protected-mode no
bind ip访问 注释,否则其他Ip客户端无法访问
#bind 127.0.0.1
# bind 192.168.1.100 10.0.0.1 表示就192.168.1.100 10.0.0.1能访问该redis
密码打开设123456为密码,最好强度设大一点,简单测试就设123456了
requirepass 123456
以上6项改为保存一下
:wq
运行一下看看
[root@master bin]# ./redis-server redis.conf
29888:C 03 Jul 2020 10:01:53.935 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=29888, just started
说明能启动了,然后复制另外5个节点
[root@master redis-cluster]# cp -r redis01 redis02
[root@master redis-cluster]# cp -r redis01 redis03
[root@master redis-cluster]# cp -r redis01 redis04
[root@master redis-cluster]# cp -r redis01 redis05
[root@master redis-cluster]# cp -r redis01 redis06
将02~06节点的配置文件的port分别改成7002, 7003,7004,7005,7006
创建启动所有的redis节点的脚本
[root@master redis-cluster]# touch start-all.sh
vim start-all.sh
cd redis01
./bin/redis-server ./bin/redis.conf
cd ..
cd redis02
./bin/redis-server ./bin/redis.conf
cd ..
cd redis03
./bin/redis-server ./bin/redis.conf
cd ..
cd redis04
./bin/redis-server ./bin/redis.conf
cd ..
cd redis05
./bin/redis-server ./bin/redis.conf
cd ..
cd redis06
./bin/redis-server ./bin/redis.conf
cd ..
保存:wq
添加执行权限
chmod +x start-all.sh
启动所有节点
[root@master redis-cluster]# ./start-all.sh
3836:C 03 Jul 2020 10:34:32.673 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3836:C 03 Jul 2020 10:34:32.673 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=3836, just started
3836:C 03 Jul 2020 10:34:32.673 # Configuration loaded
3838:C 03 Jul 2020 10:34:32.677 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3838:C 03 Jul 2020 10:34:32.677 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=3838, just started
3838:C 03 Jul 2020 10:34:32.677 # Configuration loaded
3840:C 03 Jul 2020 10:34:32.682 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3840:C 03 Jul 2020 10:34:32.682 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=3840, just started
3840:C 03 Jul 2020 10:34:32.682 # Configuration loaded
3845:C 03 Jul 2020 10:34:32.686 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3845:C 03 Jul 2020 10:34:32.686 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=3845, just started
3845:C 03 Jul 2020 10:34:32.686 # Configuration loaded
3850:C 03 Jul 2020 10:34:32.691 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3850:C 03 Jul 2020 10:34:32.691 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=3850, just started
3850:C 03 Jul 2020 10:34:32.691 # Configuration loaded
3852:C 03 Jul 2020 10:34:32.702 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3852:C 03 Jul 2020 10:34:32.702 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=3852, just started
3852:C 03 Jul 2020 10:34:32.702 # Configuration loaded
3.所有节点已经启动,现在开始搭建集群
写个集群脚本
touch makecluster.sh
vim makecluster.sh
./redis01/bin/redis-cli -h 131.252.95.108 -p 7001 -a 123456
--cluster create --cluster-replicas 1 131.252.95.108:7001
131.252.95.108:7002 131.252.95.108:7003 131.252.95.108:7004 131.252.95.108:7005 131.252.95.108:7006
注意:如果之前有创建过但是没创建成功,这时需要把dump.rdb和nodes.conf先删除,否则集群无法搭建
[root@master redis-cluster]# cd redis01 //每个里面都需要删除
[root@master redis01]# ls
bin dump.rdb nodes.conf
[root@master redis01]# rm -rf dump.rdb
[root@master redis01]# rm -rf nodes.conf
chmod +x makecluster.sh
./makecluster.sh执行后
[root@master redis-cluster]# ./makecluster.sh
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 127.0.0.1:7005 to 127.0.0.1:7001
Adding replica 127.0.0.1:7006 to 127.0.0.1:7002
Adding replica 127.0.0.1:7004 to 127.0.0.1:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: fba1a25e4e792ea33efc702f7474f073c93d242c 127.0.0.1:7001
slots:[0-5460] (5461 slots) master
M: c432199c9db762d76a262b1a270f3ebe1b9e2080 127.0.0.1:7002
slots:[5461-10922] (5462 slots) master
M: 787ac566d7d227107a2353c0d273d78ecfe02be0 127.0.0.1:7003
slots:[10923-16383] (5461 slots) master
S: 86c89cce406e94c1007693b1689b197bae164cdb 127.0.0.1:7004
replicates 787ac566d7d227107a2353c0d273d78ecfe02be0
S: 6f2698350ae2b7d26245ac902ef1211d847377a6 127.0.0.1:7005
replicates fba1a25e4e792ea33efc702f7474f073c93d242c
S: 6d534bd66f04c823fed568be7d2bfc4336daa94b 127.0.0.1:7006
replicates c432199c9db762d76a262b1a270f3ebe1b9e2080
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 127.0.0.1:7001)
M: fba1a25e4e792ea33efc702f7474f073c93d242c 127.0.0.1:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 6f2698350ae2b7d26245ac902ef1211d847377a6 127.0.0.1:7005
slots: (0 slots) slave
replicates fba1a25e4e792ea33efc702f7474f073c93d242c
M: c432199c9db762d76a262b1a270f3ebe1b9e2080 127.0.0.1:7002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 86c89cce406e94c1007693b1689b197bae164cdb 127.0.0.1:7004
slots: (0 slots) slave
replicates 787ac566d7d227107a2353c0d273d78ecfe02be0
S: 6d534bd66f04c823fed568be7d2bfc4336daa94b 127.0.0.1:7006
slots: (0 slots) slave
replicates c432199c9db762d76a262b1a270f3ebe1b9e2080
M: 787ac566d7d227107a2353c0d273d78ecfe02be0 127.0.0.1:7003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
因为是本机测试的,所以使用127.0.0.1了,如果使用其他服务器ip换一下就可以了,一般最少拿3台服务器来做负载
4.通过客户端访问集群
客户端连接上其中任意一台,就可以进行set,get操作了,切记要加上-c,这样才能访问集群中的其他机器
./redis-cli -c -a 123456 -h 131.252.95.108 -p 7001
[root@master local]# cd redis-cluster/
[root@master redis-cluster]# ls
makecluster.sh redis01 redis02 redis03 redis04 redis05 redis06 redis-source start-all.sh stop-all.sh
[root@master redis-cluster]# cd redis01
[root@master redis01]# ls
bin nodes.conf
[root@master redis01]# cd bin
[root@master bin]# ls
nodes.conf redis-benchmark redis-check-aof redis-check-rdb redis-cli redis.conf redis-sentinel redis-server
[root@master bin]# ./redis-cli -a 123456 -c -h 127.0.0.1 -p 7001
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:7001> set key1 hello,world
-> Redirected to slot [9189] located at 127.0.0.1:7002
OK
127.0.0.1:7002> get key1
"hello,world"
127.0.0.1:7002> quit
5.查看集群信息
cluster info
127.0.0.1:7002> 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:2
cluster_stats_messages_ping_sent:444
cluster_stats_messages_pong_sent:453
cluster_stats_messages_meet_sent:5
cluster_stats_messages_sent:902
cluster_stats_messages_ping_received:449
cluster_stats_messages_pong_received:449
cluster_stats_messages_meet_received:4
cluster_stats_messages_received:902
6.关闭集群
写个脚本
touch stop-all.sh
chmod 777 stop-all.sh
vim stop-all.sh
#!/bin/bash
pid_name="redis"
echo "杀死的进程名称:" $pid_name
pid=$(ps -ef|grep $pid_name|grep -v grep|awk '{print $2}')
echo "pid列表:" $pid
for item in $pid
do
echo "杀死进程pid=" $item
kill -9 $item
done
pid=$(ps -ef|grep $pid_name|grep -v grep|awk '{print $2}')
echo "执行完成后,pid列表:" $pid
执行脚本关闭redis集群
./stop-all.sh