redis集群的安装部署

1.下载redis的稳定版本

wget https://download.redis.io/redis-stable.tar.gz

2..安装redis软件

tar -xzvf redis-stable.tar.gz
cd redis-stable
make
make install

编译过程中提示报错,需要安装gcc

3.编译配置文件

7001服务

port 7001                          #修改端口     
bind 10.3.10.20                    #修改为对应主机IP地址
daemonize yes
pidfile /var/run/redis_7001.pid    #修改PID文件名
dir /opt/redis-cluster/7001/data   #定义存储文件夹  
cluster-enabled yes
cluster-config-file nodes-7001.conf  #修改集群文件名
cluster-node-timeout 5000
appendonly yes

7002服务

port 7002                         #修改端口     
bind 10.3.10.20                    #修改为对应主机IP地址
daemonize yes
pidfile /var/run/redis_7002.pid    #修改PID文件名
dir /opt/redis-cluster/7002/data   #定义存储文件夹  
cluster-enabled yes
cluster-config-file nodes-7002.conf  #修改集群文件名
cluster-node-timeout 5000
appendonly yes

7003服务

port 7003                         #修改端口     
bind 10.3.10.20                    #修改为对应主机IP地址
daemonize yes
pidfile /var/run/redis_7003.pid    #修改PID文件名
dir /opt/redis-cluster/7003/data   #定义存储文件夹  
cluster-enabled yes
cluster-config-file nodes-7003.conf  #修改集群文件名
cluster-node-timeout 5000
appendonly yes

7004服务

port 7004                         #修改端口     
bind 10.3.10.20                    #修改为对应主机IP地址
daemonize yes
pidfile /var/run/redis_7004.pid    #修改PID文件名
dir /opt/redis-cluster/7004/data   #定义存储文件夹  
cluster-enabled yes
cluster-config-file nodes-7004.conf  #修改集群文件名
cluster-node-timeout 5000
appendonly yes

7005服务

port 7005                        #修改端口     
bind 10.3.10.20                    #修改为对应主机IP地址
daemonize yes
pidfile /var/run/redis_7005.pid    #修改PID文件名
dir /opt/redis-cluster/7005/data   #定义存储文件夹  
cluster-enabled yes
cluster-config-file nodes-7005.conf  #修改集群文件名
cluster-node-timeout 5000
appendonly yes

7006服务

port 7006                        #修改端口     
bind 10.3.10.20                    #修改为对应主机IP地址
daemonize yes
pidfile /var/run/redis_7006.pid    #修改PID文件名
dir /opt/redis-cluster/7006/data   #定义存储文件夹  
cluster-enabled yes
cluster-config-file nodes-7006.conf  #修改集群文件名
cluster-node-timeout 5000
appendonly yes

4.启动服务

1.拷贝redis-server到服务目录
cd /opt/redis-cluster

cp /opt/redis-cluster/redis-stable/src/redis-server  7001

cp /opt/redis-cluster/redis-stable/src/redis-server  7002

cp /opt/redis-cluster/redis-stable/src/redis-server  7003

cp /opt/redis-cluster/redis-stable/src/redis-server  7004

cp /opt/redis-cluster/redis-stable/src/redis-server  7005

cp /opt/redis-cluster/redis-stable/src/redis-server  7006

2.启动服务

./redis-server redis-7001.conf
./redis-server redis-7002.conf 
./redis-server redis-7003.conf 
./redis-server redis-7004.conf 
./redis-server redis-7005.conf 
./redis-server redis-7006.conf 

5.安装集群所需的软件

yum install ruby rubygems -y

gem install redis 

升级rubygems gem

curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
curl -sSL https://get.rvm.io | bash -s stable
source /etc/profile.d/rvm.sh
rvm requirements
source /usr/local/rvm/scripts/rvm

rvm install 2.4.0
rvm use 2.4.0

gem install redis 



以上安装防范,安装的rubygems gem版本太低,可以考虑源码安装,如下:
下载软件包
wget https://cache.ruby-lang.org/pub/ruby/3.1/ruby-3.1.2.tar.gz
解压缩
tar zxvf ruby-3.1.2.tar.gz
安装
cd ruby-3.1.2
./configure  --prefix=/opt/ruby
 make && make install
ln -s /opt/ruby/bin/ruby /usr/bin/ruby
ln -s /opt/ruby/bin/gem /usr/bin/gem 
查看版本
ruby -v 
安装rubygem redis依赖:
wget http://rubygems.org/downloads/redis-4.1.0.gem
gem install -l redis-4.1.0.gem
上一步中已经把ruby工具所需要的运行环境和ruby包安装好了,接下来需要把这个ruby脚本工具复制到usr/local/redis-cluster目录下。那么这个ruby脚本工具在哪里呢?之前提到过,在redis源码解压文件的源代码里,即redis/src目录下的redis-trib.rb文件。
cd redis/src
cp redis-trib.rb /usr/local/redis-cluster






6.组建集群

cd /opt/redis-cluster/redis-stable/src
./redis-cli --cluster create 10.3.10.20:7001 10.3.10.20:7002 10.3.10.20:7003 10.3.10.20:7004 10.3.10.20:7005 10.3.10.20:7006 --cluster-replicas 1



集群输出如下

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 10.3.10.20:7005 to 10.3.10.20:7001
Adding replica 10.3.10.20:7006 to 10.3.10.20:7002
Adding replica 10.3.10.20:7004 to 10.3.10.20:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: cf656ddf619b566a48d15b7f076e6d773d646895 10.3.10.20:7001
   slots:[0-5460] (5461 slots) master
M: acb4146d7e15c2799cc1c9bb0d34d02e568bde13 10.3.10.20:7002
   slots:[5461-10922] (5462 slots) master
M: 419bfdab0a4cfcfa3ee78ef8bfaad59f968cbee3 10.3.10.20:7003
   slots:[10923-16383] (5461 slots) master
S: 8345ec77e8795dcce9dde5c61bf10939150c290a 10.3.10.20:7004
   replicates acb4146d7e15c2799cc1c9bb0d34d02e568bde13
S: 66c43b50c6e8fc8101502d7466379899305df17f 10.3.10.20:7005
   replicates 419bfdab0a4cfcfa3ee78ef8bfaad59f968cbee3
S: ab8f6e8c1ffcf48d6903b15fbe58e6ce4e2ad117 10.3.10.20:7006
   replicates cf656ddf619b566a48d15b7f076e6d773d646895
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.3.10.20:7001)
M: cf656ddf619b566a48d15b7f076e6d773d646895 10.3.10.20:7001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 8345ec77e8795dcce9dde5c61bf10939150c290a 10.3.10.20:7004
   slots: (0 slots) slave
   replicates acb4146d7e15c2799cc1c9bb0d34d02e568bde13
M: acb4146d7e15c2799cc1c9bb0d34d02e568bde13 10.3.10.20:7002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 419bfdab0a4cfcfa3ee78ef8bfaad59f968cbee3 10.3.10.20:7003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: ab8f6e8c1ffcf48d6903b15fbe58e6ce4e2ad117 10.3.10.20:7006
   slots: (0 slots) slave
   replicates cf656ddf619b566a48d15b7f076e6d773d646895
S: 66c43b50c6e8fc8101502d7466379899305df17f 10.3.10.20:7005
   slots: (0 slots) slave
   replicates 419bfdab0a4cfcfa3ee78ef8bfaad59f968cbee3
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

7.组建集群报错

[ERR] Node 10.3.10.20:7001 DENIED Redis is running in protected mode because protected mode is enabled and no password is set for the default user. In this mode connections are only accepted from the loopback interface. If you want to connect from exters to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SUREt publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and thg the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a an authentication password for the default user. NOTE: You only need to do one of the above things in order for the servaccepting connections from the outside.

解决方式

在配置文件增加

masterauth "redis"
requirepass "redis"

重启redis进程

之后使用如下命令创建集群

./redis-cli --cluster create 10.3.10.20:7001 10.3.10.20:7002 10.3.10.20:7003 10.3.10.20:7004 10.3.10.20:7005 10.3.10.20:7006 --cluster-replicas 1 -a htjs 

8.集群验证

./redis-cli -c -h 10.3.10.20 -p 7001 -a htjs

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.3.10.20:7001> 
10.3.10.20:7001> 
10.3.10.20:7001> 
10.3.10.20:7001> 
10.3.10.20:7001> 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:480
cluster_stats_messages_pong_sent:471
cluster_stats_messages_sent:951
cluster_stats_messages_ping_received:466
cluster_stats_messages_pong_received:480
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:951
total_cluster_links_buffer_limit_exceeded:0

10.3.10.20:7001> cluster nodes
8345ec77e8795dcce9dde5c61bf10939150c290a 10.3.10.20:7004@17004 slave acb4146d7e15c2799cc1c9bb0d34d02e568bde13 0 1658303013066 2 connected
acb4146d7e15c2799cc1c9bb0d34d02e568bde13 10.3.10.20:7002@17002 master - 0 1658303013568 2 connected 5461-10922
419bfdab0a4cfcfa3ee78ef8bfaad59f968cbee3 10.3.10.20:7003@17003 master - 0 1658303014572 3 connected 10923-16383
ab8f6e8c1ffcf48d6903b15fbe58e6ce4e2ad117 10.3.10.20:7006@17006 slave cf656ddf619b566a48d15b7f076e6d773d646895 0 1658303012564 1 connected
66c43b50c6e8fc8101502d7466379899305df17f 10.3.10.20:7005@17005 slave 419bfdab0a4cfcfa3ee78ef8bfaad59f968cbee3 0 1658303014069 3 connected
cf656ddf619b566a48d15b7f076e6d773d646895 10.3.10.20:7001@17001 myself,master - 0 1658303014000 1 connected 0-5460

10.3.10.20:7001> 
 

二.手工创建redis-cluster集群

(1)redis3.0版本之前只支持单例模式,在3.0版本及以后才支持集群,我这里用的是redis5.05版本;

(2)redis集群采用P2P模式,是完全去中心化的,不存在中心节点或者代理节点;

(3)Redis 集群会将用户数据分散保存至各个节点中,突破单机 Redis 内存最大存储容量。集群引入了 哈希槽slot的概念,其搭建完成后会生16384个哈希槽slot,同时会根据节点的数量大致均等的将16384个哈希槽映射到不同的节点上。当用户存储key-value时,集群会先对key进行CRC16校验然后对16384取模来决定key-value放置哪个槽,从而实现自动分割数据到不同的节点上。故每个Redis集群理论上最多可以有16384个节点。

(4)Redis 集群支持主从复制和故障恢复。集群使用了主从复制模型,每个主节点master应至少有一个从节点slave。假设某个主节点故障,其所有子节点会广播一个数据包给集群里的其他主节点来请求选票,一旦某个从节点收到了大多数主节点的回应,那么它就赢得了选举,被推选为主节点,负责处理之前旧的主节点负责的哈希槽。
具体操作

1.启动redis服务

6379 节点启动成功,第一次启动时如果没有集群配置文件,它会自动创 建一份,文件名称采用cluster-config-file 参数项控制,建议采用 node-{port}.conf格式定义,通过使用端口号区分不同节点,防止同一机器下多个节点彼此覆盖,造成集群信息异常。如果启动时存在集群配置文件,节点会使用配置文件内容初始化集群信息。启动过程如图

集群模式的 Redis 除了原有的配置文件之外又加了一份集群配置文件。当集群内节点信息发生变化,如添加节点、节点下线、故障转移等。节点会自动保存集群状态到配置文件中。需要注意的是,Redis 自动维护集群配置文件,不要手动修改,防止节点重启时产生集群信息错乱。
./redis-server redis-7001.conf
./redis-server redis-7002.conf 
./redis-server redis-7003.conf 
./redis-server redis-7004.conf 
./redis-server redis-7005.conf 
./redis-server redis-7006.conf 

2.节点握手
节点握手是指一批运行在集群模式下的节点通过 Gossip 协议彼此通信,达到感知对方的过程。节点握手是集群彼此通信的第一步,由客户端发起命 令:cluster meet{ip}{port}。
图中执行的命令是: cluster meet127.0.0.16380 让节点 6379 6380 节点进 行握手通信。cluster meet 命令是一个异步命令,执行之后立刻返回。内部发起与目标节点进行握手通信
1 )节点 6379 本地创建 6380 节点信息对象,并发送 meet 消息。
2 )节点 6380 接受到 meet 消息后,保存 6379 节点信息并回复 pong 消息。
3 )之后节点 6379 6380 彼此定期通过 ping/pong 消息进行正常的节点通
信。
这里的 meet ping pong 消息是 Gossip 协议通信的载体。
./redis-cli -h 10.3.10.20 -a htjs -p 7001

10.3.10.20:7001> cluster meet 10.3.10.20 7002

10.3.10.20:7001> cluster meet 10.3.10.20 7003

10.3.10.20:7001> cluster meet 10.3.10.20 7004

10.3.10.20:7001> cluster meet 10.3.10.20 7005

10.3.10.20:7001> cluster meet 10.3.10.20 7006
节点建立握手之后集群还不能正常工作,这时集群处于下线状态,所有的数据读写都被禁止

3.分配槽位

Redis 集群把所有的数据映射到 16384 个槽中。每个 key 会映射为一个固定的槽,只有当节点分配了槽,才能响应和这些槽关联的键命令。通过cluster addslots命令为节点分配槽,只在主节点操作,从节点不操作。
redis-cli -h 10.3.10.20 -p 7001 -a htjs cluster addslots {0...5461} 
redis-cli -h 10.3.10.20 -p 7002 -a htjscluster addslots {5462...10922} 
redis-cli -h 10.3.10.20 -p 7003 -a htjs cluster addslots {10923...16383}
16384 slot 平均分配给 7001 、7002 、7003 三个节点。执行cluster info查看集群状态。
所有的槽都已经分配给节点,执行cluster nodes 命令可以看到节点和槽的分配关系。
4.分配从节点,主从复制

Redis 集群成功上线之后,要给主节点指定从节点,这样实现集群的高可用。使用 cluster replicate {nodeId} 命令手动给从节点配置主节点。其中命令执行必须在对应的 从节点上执行,nodeId是要复制主节点的节点ID。

./redis-cli -h 10.3.10.20 -a htjs -p 7004

10.3.10.20:7004>cluster replicate cf656ddf619b566a48d15b7f076e6d773d646895

10.3.10.20:7005>cluster replicate acb4146d7e15c2799cc1c9bb0d34d02e568bde13

10.3.10.20:7006>cluster replicate 419bfdab0a4cfcfa3ee78ef8bfaad59f968cbee3 
以上依照 Redis 协议手动建立一个集群。它由 6 个节点构成,3个主节点负责处理槽和相关数据, 3 个从节点负责故障转移。
公司生产环境的redis配置文件
bind 10.10.19.84
protected-mode yes
port 5000
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes
supervised no
pidfile redis.pid
loglevel notice
logfile ""
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 ./
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
 maxclients 200000
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-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 yes
lua-time-limit 5000
 cluster-enabled yes
 cluster-config-file nodes.conf
 cluster-node-timeout 15000
 cluster-replica-no-failover no
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
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

忍冬行者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值