Redis集群部署

1.准备工作

准备三台redis机器,每台配置不同的节点及监听的端口。

192.168.134.165上有7001和7002两节点。

192.168.134.166上有7003和7004两节点。

192.168.134.163上有7005和7006两节点。

2.集群部署

192.168.134.165配置:
[root@server01 ~]# mkdir /data/application/{7001,7002}/data  -p
[root@server01 ~]# cd /data/application/
[root@server01 application]# vim 7001/redis.conf
port 7001
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
bind 0.0.0.0
pidfile  /data/application/7001/redis_7001.pid
dir  /data/application/7001/data
                                 
[root@server01 application]# vim 7002/redis.conf
port 7002
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
bind 0.0.0.0
pidfile  /data/application/7002/redis_7002.pid
dir  /data/application/7002/data
                                  

启动两个节点并查看端口:

[root@server01 ~]# /usr/local/redis/src/redis-server  /data/application/7001/redis.conf 
1590:C 07 Nov 2023 11:44:12.781 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1590:C 07 Nov 2023 11:44:12.781 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=1590, just started
1590:C 07 Nov 2023 11:44:12.781 # Configuration loaded
[root@server01 ~]# /usr/local/redis/src/redis-server  /data/application/7002/redis.conf 
1592:C 07 Nov 2023 11:44:21.952 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1592:C 07 Nov 2023 11:44:21.952 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=1592, just started
1592:C 07 Nov 2023 11:44:21.952 # Configuration loaded

 

 192.168.134.166配置:
[root@server02 ~]# mkdir /data/application/{7003,7004}/data  -p
[root@server02 ~]# cd /data/application/
[root@server02 application]# vim 7003/redis.conf
port 7003
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
bind 0.0.0.0
pidfile  /data/application/7003/redis_7003.pid
dir  /data/application/7003/data
                                 
[root@server01 application]# vim 7004/redis.conf
port 7004
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
bind 0.0.0.0
pidfile  /data/application/7004/redis_7004.pid
dir  /data/application/7004/data
                                  

启动两个节点并查看端口:

[root@localhost ~]# /usr/local/redis/src/redis-server  /data/application/7003/redis.conf
1518:C 07 Nov 2023 11:49:42.398 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1518:C 07 Nov 2023 11:49:42.398 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=1518, just started
1518:C 07 Nov 2023 11:49:42.398 # Configuration loaded
[root@localhost ~]# /usr/local/redis/src/redis-server  /data/application/7004/redis.conf
1520:C 07 Nov 2023 11:49:45.593 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1520:C 07 Nov 2023 11:49:45.593 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=1520, just started
1520:C 07 Nov 2023 11:49:45.593 # Configuration loaded

 

 192.168.134.166配置:
[root@server03 ~]# mkdir /data/application/{7005,7006}/data  -p
[root@server03 ~]# cd /data/application/
[root@server03 application]# vim 7005/redis.conf
port 7005
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
bind 0.0.0.0
pidfile  /data/application/7005/redis_7005.pid
dir  /data/application/7005/data
                                 
[root@server01 application]# vim 7006/redis.conf
port 7006
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
bind 0.0.0.0
pidfile  /data/application/7006/redis_7004.pid
dir  /data/application/7006/data
                                  

启动两个节点并查看端口:

[root@server03 ~]# /usr/local/redis/src/redis-server  /data/application/7005/redis.conf
1454:C 07 Nov 2023 11:52:40.941 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1454:C 07 Nov 2023 11:52:40.941 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=1454, just started
1454:C 07 Nov 2023 11:52:40.941 # Configuration loaded
[root@server03 ~]# /usr/local/redis/src/redis-server  /data/application/7006/redis.conf
1456:C 07 Nov 2023 11:52:45.042 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1456:C 07 Nov 2023 11:52:45.042 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=1456, just started
1456:C 07 Nov 2023 11:52:45.042 # Configuration loaded

 3.创建集群

[root@server01 ~]#  /usr/local/redis/src/redis-cli  --cluster create  192.168.134.165:7001 192.168.134.165:7002 192.168.134.166:7003 192.168.134.166:7004 192.168.134.163:7005 192.168.134.163:7006 --cluster-replicas 1  

[root@server01 ~]# /usr/local/redis/src/redis-cli  --cluster create  192.168.134.165:7001 192.168.134.165:7002 192.168.134.166:7003 192.168.134.166:7004 192.168.134.163:7005 192.168.134.163:7006 --cluster-replicas 1
>>> 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.134.166:7004 to 192.168.134.165:7001
Adding replica 192.168.134.163:7006 to 192.168.134.166:7003
Adding replica 192.168.134.165:7002 to 192.168.134.163:7005
M: 1afda1f8c95d1513534df876ec740481435ddea7 192.168.134.165:7001
   slots:[0-5460] (5461 slots) master
S: e8173faa6bb97802c42a1593e0630db2e65a070d 192.168.134.165:7002
   replicates d291a53e3dec8214f62479e8dcc0b6bbb36a33e8
M: 0f3941f9970f76a7d72acdee52273f1cde297811 192.168.134.166:7003
   slots:[5461-10922] (5462 slots) master
S: 75203ef95af4a0ba7efbf6fca958c6417e175bc9 192.168.134.166:7004
   replicates 1afda1f8c95d1513534df876ec740481435ddea7
M: d291a53e3dec8214f62479e8dcc0b6bbb36a33e8 192.168.134.163:7005
   slots:[10923-16383] (5461 slots) master
S: 085fac233f57702b3830f5ea2bbc500e2b511101 192.168.134.163:7006
   replicates 0f3941f9970f76a7d72acdee52273f1cde297811
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.134.165:7001)
M: 1afda1f8c95d1513534df876ec740481435ddea7 192.168.134.165:7001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: e8173faa6bb97802c42a1593e0630db2e65a070d 192.168.134.165:7002
   slots: (0 slots) slave
   replicates d291a53e3dec8214f62479e8dcc0b6bbb36a33e8
M: 0f3941f9970f76a7d72acdee52273f1cde297811 192.168.134.166:7003
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: d291a53e3dec8214f62479e8dcc0b6bbb36a33e8 192.168.134.163:7005
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 085fac233f57702b3830f5ea2bbc500e2b511101 192.168.134.163:7006
   slots: (0 slots) slave
   replicates 0f3941f9970f76a7d72acdee52273f1cde297811
S: 75203ef95af4a0ba7efbf6fca958c6417e175bc9 192.168.134.166:7004
   slots: (0 slots) slave
   replicates 1afda1f8c95d1513534df876ec740481435ddea7
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

Redis集群部署_第1张图片

4集群测试

  • 集群客户端连接方式 redis-cli  -c  

客户端测试:

[root@server01 ~]# /usr/local/redis/src/redis-cli  -h 192.168.134.165 -p 7001 -c
192.168.134.165:7001> set name aren 
-> Redirected to slot [5798] located at 192.168.134.166:7003
OK
192.168.134.166:7003> set age 22
-> Redirected to slot [741] located at 192.168.134.166:7004
OK
192.168.134.166:7004> set gender female
-> Redirected to slot [15355] located at 192.168.134.163:7005
OK
192.168.134.163:7005> get name
-> Redirected to slot [5798] located at 192.168.134.166:7003
"aren"
192.168.134.166:7003> get age
-> Redirected to slot [741] located at 192.168.134.166:7004
"22"
192.168.134.166:7004> get gender
-> Redirected to slot [15355] located at 192.168.134.163:7005
"female"
192.168.134.163:7005> 
  • 查看所有节点信息.

Redis集群部署_第2张图片

  • 宕机掉一个master并重启可以发现其中一个从节点代替了master,被宕机的master变成了slave。 

这里宕机7004后重启。

[root@server02 ~]# kill -9 1459
[root@server02 ~]# /usr/local/redis/src/redis-server  /data/application/7004/redis.conf 
1858:C 07 Nov 2023 15:04:05.905 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1858:C 07 Nov 2023 15:04:05.905 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=1858, just started
1858:C 07 Nov 2023 15:04:05.905 # Configuration loaded
  •  重启7004节点后查看所有节点,发现7004节点由master变为了slave。

Redis集群部署_第3张图片

你可能感兴趣的:(redis,数据库,linux)