Redis数据库集群部署!

Posted handsomeboy-东

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis数据库集群部署!相关的知识,希望对你有一定的参考价值。

Redis

Redis主从复制部署

设备准备:三台Redis服务器,master(192.168.118.100)、slave1(192.168.118.200)、slave2(192.168.118.50)

  • 三台服务器同步操作
yum install -y gcc gcc-c++ make
ntpdate ntp1.aliyun.com
cd /opt/redis-5.0.7/
make
make prefix=/usr/local/redis install
cd utils/
./install_server.sh
  • master
[root@master utils]# vim /etc/redis/6379.conf 


[root@master utils]# /etc/init.d/redis_6379 restart			#重启服务
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
Starting Redis server...
  • 两台slave同步操作
[root@slave1 utils]# vim /etc/redis/6379.conf 			#修改配置文件


[root@slave1 ~]# /etc/init.d/redis_6379 restart					#重启服务
Stopping ...
Waiting for Redis to shutdown ...
Redis stopped
Starting Redis server...
  • 在master上查看日志文件
[root@master ~]# cat /var/log/redis_6379.log 
………………………………
81072:M 08 Aug 2021 23:00:47.259 * Background saving terminated with success
81072:M 08 Aug 2021 23:00:47.259 * Synchronization with replica 192.168.118.50:6379 succeeded
81072:M 08 Aug 2021 23:00:47.259 * Synchronization with replica 192.168.118.200:6379 succeeded
[root@master ~]# redis-cli info replication				#验证主从复制效果
# Replication
role:master
connected_slaves:2
##master启动时生成40位16进制的随机字符串,用来标识master节点
slave0:ip=192.168.118.50,port=6379,state=online,offset=546,lag=0
slave1:ip=192.168.118.200,port=6379,state=online,offset=546,lag=1
master_replid:4e5fe691b45ddcbc614fae909211ffa2538dcb54
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:546
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:546
[root@master ~]# redis-cli									#进入主节点,设置一个key,然后在从节点查看
127.0.0.1:6379> keys *
(empty list or set)
127.0.0.1:6379> set lisi www
OK
[root@slave1 ~]# redis-cli
127.0.0.1:6379> keys * 
1) "lisi"

Redis哨兵模式部署

设备准备,这里用上面三台服务器来做(先删除主从复制的配置)

  • 三台服务器同时配置
vim /opt/redis-5.0.7/sentinel.conf 				#修改哨兵配置文件






  • 启动哨兵节点,先启动主节点,再启动两个从节点
[root@master ~]# cd /opt/redis-5.0.7/
[root@master redis-5.0.7]# redis-sentinel sentinel.conf &
[root@master redis-5.0.7]# redis-cli -p 26379 info sentinel				#查看哨兵信息
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.118.100:6379,slaves=2,sentinels=3

cluster集群搭建

设备三主三从,这里为了方便我们用一台设备做实验,三主节点:112/113/114 、三从节点:115/116/117

[root@cluster ~]# cd /etc/redis
[root@cluster redis]# mkdir -p redis-cluster/redis112..7			#创建redis六个端口的工作目录
[root@cluster redis]# vim /opt/redis.sh								#设置启动脚本
#!/bin/bash
for i in 2..7
do
        cp /opt/redis-5.0.7/redis.conf /etc/redis/redis-cluster/redis11$i
        cp /opt/redis-5.0.7/src/redis-cli /opt/redis-5.0.7/src/redis-server /etc/redis/redis-cluster/redis11$i
done
[root@cluster redis]# sh -x /opt/redis.sh							#执行脚本
  • 修改配置文件
[root@cluster redis]# cd /etc/redis/redis-cluster/redis112			#首先修改节点111的
[root@cluster redis112]# vim redis.conf




  • 将修改的111节点配置文件分别复制到各个节点,然后修改其端口和名称
[root@cluster redis112]# cp redis.conf ../redis117/
cp: overwrite ‘../redis112/redis.conf’? y
[root@cluster redis112]# cp redis.conf ../redis113/
cp: overwrite ‘../redis113/redis.conf’? y
[root@cluster redis112]# cp redis.conf ../redis114/
cp: overwrite ‘../redis114/redis.conf’? y
[root@cluster redis112]# cp redis.conf ../redis115/
cp: overwrite ‘../redis115/redis.conf’? y
[root@cluster redis112]# cp redis.conf ../redis116/
cp: overwrite ‘../redis116/redis.conf’? y


  • 更改端口和文件名称,后面依次类推,保存退出设置启动脚本
[root@cluster redis116]# vim /opt/redis-start.sh
#!/bin/bash
for i in 2..7
do
        cd /etc/redis/redis-cluster/redis11$i
        redis-server redis.conf
done
ps -ef | grep redis
[root@cluster redis116]# sh -x /opt/redis-start.sh 
root      24540      1  0 21:18 ?        00:00:00 /usr/local/bin/redis-server 127.0.0.1:6379
root      24711  20063  0 21:27 pts/1    00:00:00 sh -x /opt/redis-start.sh
root      24713      1  0 21:27 ?        00:00:00 redis-server 127.0.0.1:112 [cluster]
root      24715      1  0 21:27 ?        00:00:00 redis-server 127.0.0.1:113 [cluster]
root      24720      1  0 21:27 ?        00:00:00 redis-server 127.0.0.1:114 [cluster]
root      24725      1  0 21:27 ?        00:00:00 redis-server 127.0.0.1:115 [cluster]
root      24727      1  0 21:27 ?        00:00:00 redis-server 127.0.0.1:116 [cluster]
root      24738      1  0 21:27 ?        00:00:00 redis-server 127.0.0.1:117 [cluster]
root      24740  24711  0 21:27 pts/1    00:00:00 grep redis
  • 加入集群然后测试
[root@localhost redis117]# redis-cli --cluster create 127.0.0.1:112 127.0.0.1:113 127.0.0.1:114 127.0.0.1:115 127.0.0.1:116 127.0.0.1:117 --cluster-replicas 1
##六个节点,三主三从,每个主对应一个从节点,后面的交互输入yes即可
>>> Performing Cluster Check (using node 127.0.0.1:112)
M: 397888ebf5109561da23e32d00b54d7d1ef2127f 127.0.0.1:112
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: c9aeec32e6fefd7a6e0465cd81eab676f4eb5709 127.0.0.1:113
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: f1b9dac943f8d003e063cb1b2c56d60b701ac537 127.0.0.1:117
   slots: (0 slots) slave
   replicates c9aeec32e6fefd7a6e0465cd81eab676f4eb5709
M: 1b9411e364e5273846d9857c08ed867e946b9e37 127.0.0.1:114
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: cf2f2204207bf37ad8b94b7ea18cd562fe6bfa15 127.0.0.1:116
   slots: (0 slots) slave
   replicates 397888ebf5109561da23e32d00b54d7d1ef2127f
S: 8d7916590a49bd2b2e534e2e543830b51997921d 127.0.0.1:115
   slots: (0 slots) slave
   replicates 1b9411e364e5273846d9857c08ed867e946b9e37
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
  • 查看各节点对应的哈希槽
[root@localhost redis117]# redis-cli -p 112 -c		#进入第一个节点,-c表示节点之间可以切换
127.0.0.1:112> cluster slots						#查看各节点对应的哈希槽
1) 1) (integer) 5461
   2) (integer) 10922
   3) 1) "127.0.0.1"
      2) (integer) 113
      3) "c9aeec32e6fefd7a6e0465cd81eab676f4eb5709"
   4) 1) "127.0.0.1"
      2) (integer) 117
      3) "f1b9dac943f8d003e063cb1b2c56d60b701ac537"
2) 1) (integer) 0
   2) (integer) 5460
   3) 1) "127.0.0.1"
      2) (integer) 112
      3) "397888ebf5109561da23e32d00b54d7d1ef2127f"
   4) 1) "127.0.0.1"
      2) (integer) 116
      3) "cf2f2204207bf37ad8b94b7ea18cd562fe6bfa15"
3) 1) (integer) 10923
   2) (integer) 16383
   3) 1) "127.0.0.1"
      2) (integer) 114
      3) "1b9411e364e5273846d9857c08ed867e946b9e37"
   4) 1) "127.0.0.1"
      2) (integer) 115
      3) "8d7916590a49bd2b2e534e2e543830b51997921d"
  • 测试集群
127.0.0.1:112> set name whd						#创建一个key键whd,发现他对应哈希槽位5798
-> Redirected to slot [5798] located at 127.0.0.1:113
OK
127.0.0.1:113> cluster keyslot name				#查看键的哈希槽,对应发现在113节点中
(integer) 5798

以上是关于Redis数据库集群部署!的主要内容,如果未能解决你的问题,请参考以下文章

在Kubernetes上部署Redis集群

[NoSQL数据库] Redis集群部署

docker硬核知识-容器部署redis集群

02.使用ansible自动化部署redis集群

Redis哨兵模式集群部署

Redis(主从复制哨兵模式集群)概述及部署