Redis-Cluster集群搭建(本次实验用了 两台虚拟机 进行的搭建Cluster集群)

								**Redis介绍**

Redis是完全开源免费的,遵守BSD协议 是一个高性能的key-value数据库

Redis与其他的key-value缓存产品有以下三个特点:
1:redis支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以在次加载使用
2:redis不仅仅支持简单的key-value类型的数据 同时还提供list set zset hash等数据架构存储。
3:redis支持数据的备份即master-salve模式的数据备份

废话不多说 开始部署
本次环境
主机ip 角色 主机名
192.168.200.107 Master 7000 7001 7002 Master
192.168.200.108 Slave 8000 8001 8002 Slave

两台都要做

[root@slave ~]# systemctl stop firewalld && systemctl disable firewalld && setenforce 0

Redis的基础编译部署和优化(两台)
[root@master ~]# yum -y install wget gcc gcc-c++ make tar openssl openssl-devel cmake
[root@master ~]# tar xf redis-4.0.10.tar.gz -C /usr/src/
[root@master ~]# cd /usr/src/redis-4.0.10/
[root@master redis-4.0.10]# make 
[root@master redis-4.0.10]# make MALLOC=jemalloc 
[root@master redis-4.0.10]# make PREFIX=/usr/local/redis install
[root@master redis-4.0.10]#cd /usr/local/redis/
[root@master redis]# mkdir -p /usr/local/redis/conf
[root@master redis]# cp /usr/src/redis-4.0.10/redis.conf /usr/local/redis/conf/ ##配置文件
[root@master redis]# cp /usr/src/redis-4.0.10/sentinel.conf /usr/local/redis/conf/ ##哨兵文件
[root@master redis]# cp /usr/src/redis-4.0.10/src/redis-trib.rb /usr/local/redis/bin/ ## 集群工具
[root@master redis]# cp conf/redis.conf{,.bak}
[root@master redis]# egrep -v "^$|^#" conf/redis.conf.bak > conf/redis.conf
[root@localhost ~]# ln -s /usr/local/redis/bin/* /usr/local/bin/
优化
[root@master redis]#  echo "* - nofile 10240" >> /etc/security/limits.conf
[root@master redis]# echo "net.core.somaxconn = 10240" >> /etc/sysctl.conf
[root@master redis]# echo "vm.overcommit_memory = 1" >> /etc/sysctl.conf
[root@master redis]# sysctl -p
net.core.somaxconn = 10240
vm.overcommit_memory = 1
[root@master redis]#  echo never > /sys/kernel/mm/transparent_hugepage/enabled
[root@master redis]# echo never > /sys/kernel/mm/transparent_hugepage/defrag
[root@master redis]#  echo 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' >> /etc/rc.local
[root@master redis]# echo 'echo never > /sys/kernel/mm/transparent_hugepage/defrag' >> /etc/rc.local
在master上操作
[root@master ~]# mkdir -p /data/redis-cluster
[root@master ~]# cd /data/redis-cluster
[root@master redis-cluster]# mkdir -p 7000 7001 7002
[root@master redis-cluster]# cp /usr/local/redis/conf/redis.conf /data/redis-cluster/7000/
[root@master redis-cluster]# cp /usr/local/redis/conf/redis.conf /data/redis-cluster/7001/
[root@master redis-cluster]# cp /usr/local/redis/conf/redis.conf /data/redis-cluster/7002/
在salve上操作
[root@slave redis]# mkdir -p /data/redis-cluster
[root@slave redis]# cd /data/redis-cluster
[root@slave redis-cluster]# mkdir -p 8000 8001 8002
[root@slave redis-cluster]# cp /usr/local/redis/conf/redis.conf /data/redis-cluster/8000/
[root@slave redis-cluster]# cp /usr/local/redis/conf/redis.conf /data/redis-cluster/8001/
[root@slave redis-cluster]# cp /usr/local/redis/conf/redis.conf /data/redis-cluster/8002/
Master上操作
[root@master ~]# cat /data/redis-cluster/7000/redis.conf 
bind 0.0.0.0
cluster-enabled yes
protected-mode yes
port 7000
tcp-backlog 511
timeout 0
tcp-keepalive 0
daemonize yes
supervised no
pidfile /data/redis-cluster/7000/redis_6379.pid
loglevel notice
logfile "/data/redis-cluster/7000/redis.log"
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data/redis-cluster/7000/
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
slave-lazy-flush no
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble no
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit slave 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
aof-rewrite-incremental-fsync yes

Master启动
[root@localhost ~]# redis-server /data/redis-cluster/7000/redis.conf 
[root@localhost ~]# redis-server /data/redis-cluster/7001/redis.conf 
[root@localhost ~]# redis-server /data/redis-cluster/7002/redis.conf 
[root@localhost ~]# netstat -anpt | grep redis
tcp        0      0 0.0.0.0:17000           0.0.0.0:*               LISTEN      6164/redis-server 0 
tcp        0      0 0.0.0.0:17001           0.0.0.0:*               LISTEN      6173/redis-server 0 
tcp        0      0 0.0.0.0:17002           0.0.0.0:*               LISTEN      6178/redis-server 0 
tcp        0      0 0.0.0.0:7000            0.0.0.0:*               LISTEN      6164/redis-server 0 
tcp        0      0 0.0.0.0:7001            0.0.0.0:*               LISTEN      6173/redis-server 0 
tcp        0      0 0.0.0.0:7002            0.0.0.0:*               LISTEN      6178/redis-server 0 



两台都要编译安装ruby要大于2.2 为什么要安装这个呐?(两台)
因为redis集群版本号在3.0以上 redis-cluster安装前需要安装ruby环境 搭建集群需要使用到官方提供的ruby脚本 
[root@localhost ~]# tar xf ruby-2.5.1.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/ruby-2.5.1/
[root@localhost ruby-2.5.1]# ./configure && make && make install
[root@localhost ~]# ruby --version
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
在线安装rubt的redis的扩展
[root@localhost ~]# /usr/local/bin/gem install redis
Fetching: redis-4.1.0.gem (100%)
Successfully installed redis-4.1.0
Parsing documentation for redis-4.1.0
Installing ri documentation for redis-4.1.0
Done installing documentation for redis after 1 seconds
1 gem installed
Master操作 
创建集群 使用redis自带工具redis-trib.rb工具创建
[root@localhost ~]# redis-trib.rb create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002

查看cluster集群信息
[root@localhost ~]# redis-cli -p 7000 cluster nodes
b5643c792d777f2f9e4de70fd0a26b8b069f5ae1 127.0.0.1:7000@17000 myself,master - 0 1556586192000 1 connected 0-5460
b5f3031e98348414ffb00cd39ad16bfb582f85c0 127.0.0.1:7001@17001 master - 0 1556586193378 2 connected 5461-10922
3fc7635f8a599282e7dcdd5c524e0f4aa8e1e5d5 127.0.0.1:7002@17002 master - 0 1556586194383 3 connected 10923-16383
去中心化

[root@localhost ~]# redis-cli -c -p 7001 set name1 yunjisuan
OK
[root@localhost ~]# redis-cli -c -p 7001 get name1
"yunjisuan"
[root@localhost ~]# redis-cli -c -p 7002 set name2 benet
OK
[root@localhost ~]# redis-cli -c -p 7001 get name2
"benet"
[root@localhost ~]# 

在slave操作

[root@localhost ~]# redis-cli -h 192.168.200.107 -c -p 7000 get name1
Could not connect to Redis at 127.0.0.1:7002: Connection refused
Could not connect to Redis at 127.0.0.1:7002: Connection refused
[root@localhost ~]# redis-cli -h 192.168.200.107 -c -p 7002 get name1
"yunjisuan"
为何以远程的方式访问 redis-cluster 时,被拒绝了呢?那是因为。我们创建集群的时候,是以 127.0.0.1 的本地 IP 方式创建的,因此只有本地访问时 cluster 集群才能发挥作用远程访问 7002 端口时,之所以能成功,那是因为,数据本来就是存在了监听 7002 端口的 redis 上。所以,我们不加-c 也能访问成功。

Cluster重建 master上
[root@localhost ~]# netstat -antp | grep redis
tcp        0      0 0.0.0.0:17000           0.0.0.0:*               LISTEN      79789/redis-server  
tcp        0      0 0.0.0.0:17001           0.0.0.0:*               LISTEN      79794/redis-server  
tcp        0      0 0.0.0.0:17002           0.0.0.0:*               LISTEN      79799/redis-server  
tcp        0      0 0.0.0.0:7000            0.0.0.0:*               LISTEN      79789/redis-server  
tcp        0      0 0.0.0.0:7001            0.0.0.0:*               LISTEN      79794/redis-server  
tcp        0      0 0.0.0.0:7002            0.0.0.0:*               LISTEN      79799/redis-server  
[root@localhost ~]# killall -9 redis-server				(直接杀掉)
[root@localhost ~]# netstat -antp | grep redis			
[root@localhost ~]# rm -rf /data/redis-cluster/7000/dump.rdb 	(删除dump.rdb和nodes.conf)
[root@localhost ~]# rm -rf /data/redis-cluster/7000/nodes.conf 
[root@localhost ~]# rm -rf /data/redis-cluster/7001/dump.rdb 
[root@localhost ~]# rm -rf /data/redis-cluster/7001/nodes.conf 
[root@localhost ~]# rm -rf /data/redis-cluster/7002/nodes.conf 
[root@localhost ~]# rm -rf /data/redis-cluster/7002/dump.rdb 
[root@localhost ~]# redis-server /data/redis-cluster/7000/redis.conf 
[root@localhost ~]# redis-server /data/redis-cluster/7001/redis.conf 
[root@localhost ~]# redis-server /data/redis-cluster/7002/redis.conf 
[root@localhost ~]# netstat -anpt | grep redis
tcp        0      0 0.0.0.0:17000           0.0.0.0:*               LISTEN      79874/redis-server  
tcp        0      0 0.0.0.0:17001           0.0.0.0:*               LISTEN      79879/redis-server  
tcp        0      0 0.0.0.0:17002           0.0.0.0:*               LISTEN      79884/redis-server  
tcp        0      0 0.0.0.0:7000            0.0.0.0:*               LISTEN      79874/redis-server  
tcp        0      0 0.0.0.0:7001            0.0.0.0:*               LISTEN      79879/redis-server  
tcp        0      0 0.0.0.0:7002            0.0.0.0:*               LISTEN      79884/redis-server  
[root@localhost ~]# redis-trib.rb create 192.168.200.107:7000 192.168.200.107:7001 192.168.200.107:7002					(重新创建)
>>> Creating cluster
>>> Performing hash slots allocation on 3 nodes...
Using 3 masters:
192.168.200.107:7000
192.168.200.107:7001
192.168.200.107:7002
M: 9d70bd2f8e62fe702ef4190ab6f7c3bf347dfe1b 192.168.200.107:7000
   slots:0-5460 (5461 slots) master
M: ecc5807dd075d67602be5fbc695bb4733b8bc8c0 192.168.200.107:7001
   slots:5461-10922 (5462 slots) master
M: 2ea10b8241ccb7bb6c990e493e6e98e1bbf25ec6 192.168.200.107:7002
   slots:10923-16383 (5461 slots) master
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.107:7000)
M: 9d70bd2f8e62fe702ef4190ab6f7c3bf347dfe1b 192.168.200.107:7000
   slots:0-5460 (5461 slots) master
   0 additional replica(s)
M: 2ea10b8241ccb7bb6c990e493e6e98e1bbf25ec6 192.168.200.107:7002
   slots:10923-16383 (5461 slots) master
   0 additional replica(s)
M: ecc5807dd075d67602be5fbc695bb4733b8bc8c0 192.168.200.107:7001
   slots:5461-10922 (5462 slots) master
   0 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

在slave进行测试看看远程

[root@localhost ~]# redis-cli  -h 192.168.200.107 -c -p 7000 set name yunjisuan
OK
[root@localhost ~]# redis-cli  -h 192.168.200.107 -c -p 7001 get name 
"yunjisuan"
[root@localhost ~]# redis-cli  -h 192.168.200.107 -c -p 7002 get name 
"yunjisuan"

批量导入数据查看key集群分布情况

 [root@localhost ~]#for line in `seq -w 10000`;do redis-cli -h 192.168.200.107 -p 7000 -c set key3_${line} vlaue_${line};done

分析cluster集群key节点情况

[root@localhost ~]# redis-cli  -h 192.168.200.107 -p 7000 info Keyspace
#keyspace
db0:keys=3337,expires=0,avg_ttl=0
[root@localhost ~]# redis-cli  -h 192.168.200.107 -p 7001 info Keyspace
#keyspace
db0:keys=3354,expires=0,avg_ttl=0
[root@localhost ~]# redis-cli  -h 192.168.200.107 -p 7002 info Keyspace
#keyspace
db0:keys=3310,expires=0,avg_ttl=0

Redis-cluster集群故障自动切换
将master的配置文件考到slave下面去

[root@localhost redis-cluster]# scp 7000/redis.conf 192.168.200.108:/data/redis-cluster/8000
root@192.168.200.108's password: 
redis.conf                                                                                    100% 1400    71.2KB/s   00:00   
[root@localhost redis-cluster]# scp 7000/redis.conf 192.168.200.108:/data/redis-cluster/8001
root@192.168.200.108's password: 
redis.conf                                                                                    100% 1400    71.2KB/s   00:00    
[root@localhost redis-cluster]# scp 7000/redis.conf 192.168.200.108:/data/redis-cluster/8002
root@192.168.200.108's password: 
redis.conf                                                                                    100% 1400    52.4KB/s   00:00    

去slave那些配置文件改一下 端口 这里忽略

[root@localhost redis-cluster]# vim 8000/redis.conf
[root@localhost redis-cluster]# vim 8001/redis.conf
[root@localhost redis-cluster]# vim 8002/redis.conf
[root@localhost redis-cluster]# cd
[root@localhost ~]# redis-server /data/redis-cluster/8000/redis.conf 
[root@localhost ~]# redis-server /data/redis-cluster/8001/redis.conf 
[root@localhost ~]# redis-server /data/redis-cluster/8002/redis.conf 
[root@localhost ~]# netstat -anpt | grep redis
tcp        0      0 0.0.0.0:18000           0.0.0.0:*               LISTEN      97441/redis-server  
tcp        0      0 0.0.0.0:18001           0.0.0.0:*               LISTEN      97446/redis-server  
tcp        0      0 0.0.0.0:18002           0.0.0.0:*               LISTEN      97453/redis-server  
tcp        0      0 0.0.0.0:8000            0.0.0.0:*               LISTEN      97441/redis-server  
tcp        0      0 0.0.0.0:8001            0.0.0.0:*               LISTEN      97446/redis-server  
tcp        0      0 0.0.0.0:8002            0.0.0.0:*               LISTEN      97453/redis-server  

Redis-cluster集群从库的添加
第一组

[root@localhost ~]#redis-trib.rb add-node --slave 192.168.200.108:8000 192.168.200.107:7000

第二组

[root@localhost ~]#redis-trib.rb add-node --slave 192.168.200.108:8001 192.168.200.107:7001

第三组

[root@localhost ~]#redis-trib.rb add-node --slave 192.168.200.108:8002 192.168.200.107:7002

查看集群信息

[root@localhost ~]# redis-cli -p 7000 cluster nodes
2ea10b8241ccb7bb6c990e493e6e98e1bbf25ec6 192.168.200.107:7002@17002 master - 0 1556590082941 3 connected 10923-16383
9d70bd2f8e62fe702ef4190ab6f7c3bf347dfe1b 192.168.200.107:7000@17000 myself,master - 0 1556590080000 1 connected 0-5460
ecc5807dd075d67602be5fbc695bb4733b8bc8c0 192.168.200.107:7001@17001 master - 0 1556590082000 2 connected 5461-10922
6efeaad171defb28ad682cdaa9141aacd0ad40f2 192.168.200.108:8000@18000 slave 9d70bd2f8e62fe702ef4190ab6f7c3bf347dfe1b 0 1556590081000 1 connected
fa67a6ae5c08aa2a458b9f10ad7020d82203a2b9 192.168.200.108:8001@18001 slave ecc5807dd075d67602be5fbc695bb4733b8bc8c0 0 1556590079000 2 connected
9c15d2914bb81c53cb47c98072217613141f8852 192.168.200.108:8002@18002 slave 2ea10b8241ccb7bb6c990e493e6e98e1bbf25ec6 0 1556590081925 3 connected

Redis-cluster集群slave从库的读写测试

[root@localhost ~]# redis-cli -h 192.168.200.108 -c -p 8000
192.168.200.108:8000> set name 111
-> Redirected to slot [5798] located at 192.168.200.107:7001
OK
192.168.200.107:7001> set name 1111
OK
192.168.200.107:7001> exit
[root@localhost ~]# redis-cli -h 192.168.200.108 -c -p 8000 get name 
"1111"
[root@localhost ~]# redis-cli -h 192.168.200.108 -c -p 8001 get name 
"1111"
[root@localhost ~]# redis-cli -h 192.168.200.108 -c -p 8002 get name 
"1111"
[root@localhost ~]# redis-cli -h 192.168.200.107 -c -p 7000 get name 
"1111"
[root@localhost ~]# redis-cli -h 192.168.200.107 -c -p 7001 get name 
"1111"
[root@localhost ~]# redis-cli -h 192.168.200.107 -c -p 7002 get name 
"1111"

通过测试 我们发现redis的cluster集群 不论是主库还是从库 都可以进行get和set 因此在使用中我们就没有必要都去主库了。

Redis集群的主从自动切换 主库挂掉后 从自动变为主

[root@localhost ~]# redis-cli -h 192.168.200.107 -p 7000 shutdown
[root@localhost ~]# redis-cli -h 192.168.200.108 -p 8000 cluster nodes
9d70bd2f8e62fe702ef4190ab6f7c3bf347dfe1b 192.168.200.107:7000@17000 master,fail - 1556592187900 1556592186694 1 disconnected
fa67a6ae5c08aa2a458b9f10ad7020d82203a2b9 192.168.200.108:8001@18001 slave ecc5807dd075d67602be5fbc695bb4733b8bc8c0 0 1556592289773 2 connected
6efeaad171defb28ad682cdaa9141aacd0ad40f2 192.168.200.108:8000@18000 myself,master - 0 1556592289000 4 connected 0-5460
9c15d2914bb81c53cb47c98072217613141f8852 192.168.200.108:8002@18002 slave 2ea10b8241ccb7bb6c990e493e6e98e1bbf25ec6 0 1556592288763 3 connected
ecc5807dd075d67602be5fbc695bb4733b8bc8c0 192.168.200.107:7001@17001 master - 0 1556592290783 2 connected 5461-10922
2ea10b8241ccb7bb6c990e493e6e98e1bbf25ec6 192.168.200.107:7002@17002 master - 0 1556592291793 3 connected 10923-16383

手动down掉了7000端口的redis-server 从信息得知 7000挂了 8000被切换成了master

重新启动7000端口的server再次查看

[root@localhost ~]# redis-server /data/redis-cluster/7000/redis.conf
[root@localhost ~]# redis-cli -h 192.168.200.108 -p 8000 cluster nodes
9d70bd2f8e62fe702ef4190ab6f7c3bf347dfe1b 192.168.200.107:7000@17000 slave 6efeaad171defb28ad682cdaa9141aacd0ad40f2 0 1556592666000 4 connected
fa67a6ae5c08aa2a458b9f10ad7020d82203a2b9 192.168.200.108:8001@18001 slave ecc5807dd075d67602be5fbc695bb4733b8bc8c0 0 1556592667264 2 connected
6efeaad171defb28ad682cdaa9141aacd0ad40f2 192.168.200.108:8000@18000 myself,master - 0 1556592665000 4 connected 0-5460
9c15d2914bb81c53cb47c98072217613141f8852 192.168.200.108:8002@18002 slave 2ea10b8241ccb7bb6c990e493e6e98e1bbf25ec6 0 1556592666256 3 connected
ecc5807dd075d67602be5fbc695bb4733b8bc8c0 192.168.200.107:7001@17001 master - 0 1556592665245 2 connected 5461-10922
2ea10b8241ccb7bb6c990e493e6e98e1bbf25ec6 192.168.200.107:7002@17002 master - 0 1556592665000 3 connected 10923-16383
从信息的只7000端口起来之后变成了slave现在我们手动把他变成主库
[root@localhost ~]# redis-cli -h 192.168.200.107 -c -p 7000 cluster failover
Ok
[root@localhost ~]# redis-cli -h 192.168.200.108 -p 8000 cluster nodes
9d70bd2f8e62fe702ef4190ab6f7c3bf347dfe1b 192.168.200.107:7000@17000 master - 0 1556592841427 5 connected 0-5460
fa67a6ae5c08aa2a458b9f10ad7020d82203a2b9 192.168.200.108:8001@18001 slave ecc5807dd075d67602be5fbc695bb4733b8bc8c0 0 1556592840417 2 connected
6efeaad171defb28ad682cdaa9141aacd0ad40f2 192.168.200.108:8000@18000 myself,slave 9d70bd2f8e62fe702ef4190ab6f7c3bf347dfe1b 0 1556592841000 4 connected
9c15d2914bb81c53cb47c98072217613141f8852 192.168.200.108:8002@18002 slave 2ea10b8241ccb7bb6c990e493e6e98e1bbf25ec6 0 1556592842435 3 connected
ecc5807dd075d67602be5fbc695bb4733b8bc8c0 192.168.200.107:7001@17001 master - 0 1556592837393 2 connected 5461-10922
2ea10b8241ccb7bb6c990e493e6e98e1bbf25ec6 192.168.200.107:7002@17002 master - 0 1556592838000 3 connected 10923-16383

版权声明:本文为博主原创文章,未经博主允许不得转载。
如有侵犯联系我qq:2695683956 经查询属实立马删除

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐