redis 的部署

本文介绍了如何在Linux环境下部署Redis集群,包括无中心化的配置步骤,如何修改配置文件以允许外部通信,并展示了如何搭建一主三从的高可用环境。在Redis Cluster中,数据被分为16384个槽位,客户端可以自动发现节点,当节点故障时,其他slave将接管master,确保服务的连续性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

redis官网 

Download | RedisRedisYou can download the last Redis source files here. For additional options, see the Redis downloads section below.Stable (7.0)Redis 7.0 …https://redis.io/download/#stable-622中文文档

Redis命令中心(Redis commands) -- Redis中国用户组(CRUG)Redis命令大全,显示全部已知的redis命令,redis集群相关命令,近期也会翻译过来,Redis命令参考,也可以直接输入命令进行命令检索。http://redis.cn/commands.html

下载完安装包之后可以直接make  及 make  install

编辑文件redis-6.2.7/utils/install_server.sh    因为这个安装脚本在安装的时候会判断是否是systemd

要想跳过直接禁用此判断

#if [ "${_pid_1_exe##*/}" = systemd ]
#then
#       echo "This systems seems to use systemd."
#       echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!"
#       exit 1
#fi
#unset _pid_1_exe
[root@server6 utils]# netstat -antlp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      19022/redis-server  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      3184/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      3283/master         
tcp        0      0 172.25.31.6:22          172.25.31.250:50956     ESTABLISHED 3985/sshd: root@pts 
tcp6       0      0 ::1:6379                :::*                    LISTEN      19022/redis-server  
tcp6       0      0 :::22                   :::*                    LISTEN      3184/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      3283/master      

由于监听的是本机的回环接口的6379端口  不能与外部网络进行通信

所以需要更改配置文件/etc/redis/6379.conf

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 0.0.0.0

将监听接口设置为所有接口

用此命令来重启

[root@server6 ~]# /etc/init.d/redis_6379 restart
Stopping ...
Redis stopped
Starting Redis server...

要配置主从配置的话  将刚才编辑好的目录整个传输到新节点上

[root@server7 redis-6.2.7]# make install
[root@server7 redis-6.2.7]# cd utils/
[root@server7 utils]# ./install_server.sh 

编辑文件//etc/redis/6379.conf

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
bind 0.0.0.0


#
# replicaof <masterip> <masterport>
replicaof 172.25.31.6 6379

完成后重启即可生效

======================================================

高可用环境的搭建

配置一主三从环境

将此文件传输到制定目录下

cp redis-6.2.7/sentinel.conf /etc/redis/

编辑文件内容   更改以下内容

[root@server6 redis]# grep -v ^# sentinel.conf | uniq

port 26379

daemonize no

pidfile /var/run/redis-sentinel.pid

logfile ""

dir /tmp

sentinel monitor mymaster 172.25.31.6 6379 2

sentinel down-after-milliseconds mymaster 10000

acllog-max-len 128

sentinel parallel-syncs mymaster 1

sentinel failover-timeout mymaster 180000

sentinel deny-scripts-reconfig yes

SENTINEL resolve-hostnames no

SENTINEL announce-hostnames no

更改完成后将此文件同步到所有节点上

在三台节点上使用此命令可以看出后台master的

redis-sentinel /etc/redis/sentinel.conf

redis集群配置无中心化

集群部署至少要 3 台以上的master节点,最好使用 3 主 3 从六个节点的模式

集群由多个Redis主从组成,每一个节点代表一个主从,每个节点负责一部分数据,他们之间通过一种特殊的二进制协议交互集群信息。

Redis Cluster将所有数据分片,分成16384个槽位,Redis Cluster对key值使用crc16算法进行hash,然后用除留余数发模除16384得到具体的槽位,每个节点负责其中一部分槽位。

当客户端连接集群,会得到一份集群的槽位匹配信息,当客户端要查找key,可以直接定位到目标节点。

redis去中心化,由多个节点组成,客户端连接时可以只用一个节点的地址,其余节点可通过该节点自动发现,但如果该节点挂了,就必须手动更换地址,因此连接多个地址安全性更高。

redis内部拥有模拟运行的实例集群  可以在一台主机上自动运行多台实例

连接主机使用此命令  对接到本地回环指定端口  (开启的那6台任意一台即可)

每台master都会储存哈希槽            一共16384个哈希槽

[root@server7 create-cluster]# redis-cli --cluster check 127.0.0.1:30001
127.0.0.1:30001 (7aabe312...) -> 0 keys | 5461 slots | 1 slaves.
127.0.0.1:30002 (f8b7e1e5...) -> 0 keys | 5462 slots | 1 slaves.
127.0.0.1:30003 (dde3a5da...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 127.0.0.1:30001)
M: 7aabe312c28928d02676c1558b0ba5d61a171c95 127.0.0.1:30001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: f8b7e1e5f80afbdc87af9c2871105cf882090861 127.0.0.1:30002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: d3145f410c9c9d2a927827b8def1ff6177fa68b8 127.0.0.1:30005
   slots: (0 slots) slave
   replicates 7aabe312c28928d02676c1558b0ba5d61a171c95
M: dde3a5da318d400890435195336027495461d028 127.0.0.1:30003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 7b00bff5418898bab89b7c367968857c6692f18f 127.0.0.1:30004
   slots: (0 slots) slave
   replicates dde3a5da318d400890435195336027495461d028
S: 1cfddd7e0ce566472dabca453fd81358ebe2fc82 127.0.0.1:30006
   slots: (0 slots) slave
   replicates f8b7e1e5f80afbdc87af9c2871105cf882090861
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

进入主机斯用此命令进入

[root@server7 create-cluster]# redis-cli -c -p 30001
127.0.0.1:30001> 

使用 info 来查看详细信息

当访问任意一台节点都可以访问到你的数据  无中心化

127.0.0.1:30001> set name westos
#在输入westos时将数据定向到30002主机上  
-> Redirected to slot [5798] located at 127.0.0.1:30002
OK
#存储后会自动切换到30002
127.0.0.1:30002> get name
"westos"
127.0.0.1:30002> 

当关闭一台master实例后会有其他slave来接管master

127.0.0.1:30002> SHUTDOWN
not connected> 
not connected> 
[root@server7 create-cluster]# redis-cli --cluster check 127.0.0.1:30001
Could not connect to Redis at 127.0.0.1:30002: Connection refused
127.0.0.1:30001 (7aabe312...) -> 0 keys | 5461 slots | 1 slaves.
127.0.0.1:30003 (dde3a5da...) -> 0 keys | 5461 slots | 1 slaves.
127.0.0.1:30006 (1cfddd7e...) -> 1 keys | 5462 slots | 0 slaves.
[OK] 1 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 127.0.0.1:30001)
M: 7aabe312c28928d02676c1558b0ba5d61a171c95 127.0.0.1:30001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: d3145f410c9c9d2a927827b8def1ff6177fa68b8 127.0.0.1:30005
   slots: (0 slots) slave
   replicates 7aabe312c28928d02676c1558b0ba5d61a171c95
M: dde3a5da318d400890435195336027495461d028 127.0.0.1:30003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 7b00bff5418898bab89b7c367968857c6692f18f 127.0.0.1:30004
   slots: (0 slots) slave
   replicates dde3a5da318d400890435195336027495461d028
M: 1cfddd7e0ce566472dabca453fd81358ebe2fc82 127.0.0.1:30006
   slots:[5461-10922] (5462 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

当在关闭一台master机器后整个集群将不能使用

要重新启动的话直接使用这条命令

[root@server7 create-cluster]#  ./create-cluster start
Starting 30001
Starting 30002
Starting 30003
Starting 30004
Starting 30005
Starting 30006

添加额外的实例

编辑文件redis-6.2.7/utils/create-cluster/create-cluster

将实例开启的数量改为8 即可多开启两个实例

BIN_PATH="../../src/"
CLUSTER_HOST=127.0.0.1
PORT=30000
TIMEOUT=2000
#实例开启的数量
NODES=8
REPLICAS=1
PROTECTED_MODE=yes
ADDITIONAL_OPTIONS=""
[root@server7 create-cluster]# vim create-cluster 
[root@server7 create-cluster]# ./create-cluster start
Starting 30001
Starting 30002
Starting 30003
Starting 30004
Starting 30005
Starting 30006
Starting 30007
Starting 30008

创建完成后还要添加节点

[root@server7 create-cluster]# redis-cli --cluster add-node 127.0.0.7:30007 127.0.0.1:30001
>>> Adding node 127.0.0.7:30007 to cluster 127.0.0.1:30001
>>> Performing Cluster Check (using node 127.0.0.1:30001)
M: 7aabe312c28928d02676c1558b0ba5d61a171c95 127.0.0.1:30001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: f8b7e1e5f80afbdc87af9c2871105cf882090861 127.0.0.1:30002
   slots: (0 slots) slave
   replicates 1cfddd7e0ce566472dabca453fd81358ebe2fc82
S: d3145f410c9c9d2a927827b8def1ff6177fa68b8 127.0.0.1:30005
   slots: (0 slots) slave
   replicates 7aabe312c28928d02676c1558b0ba5d61a171c95
M: dde3a5da318d400890435195336027495461d028 127.0.0.1:30003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 7b00bff5418898bab89b7c367968857c6692f18f 127.0.0.1:30004
   slots: (0 slots) slave
   replicates dde3a5da318d400890435195336027495461d028
M: 1cfddd7e0ce566472dabca453fd81358ebe2fc82 127.0.0.1:30006
   slots:[5461-10922] (5462 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.
>>> Send CLUSTER MEET to node 127.0.0.7:30007 to make it join the cluster.
[OK] New node added correctly.

在添加一台实例来指定它为slave  指定他的master为30007

[root@server7 create-cluster]# redis-cli --cluster add-node 127.0.0.7:30008 127.0.0.1:30001 --cluster-slave --cluster-master-id ed8b9236d1b3dac6fc460e70f2d7d7a8f86604ee

此时实例建立完成后还没有哈希槽  迁移哈希槽使用以下操作

[root@server7 create-cluster]# redis-cli --cluster reshard 127.0.0.1:30001

#询问迁移多少哈希槽  迁移给谁  从那台master上迁移
How many slots do you want to move (from 1 to 16384)? 3000  
What is the receiving node ID? ed8b9236d1b3dac6fc460e70f2d7d7a8f86604ee
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1: all

至此无中心化redis-cluster服务完成

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值