企业级NoSql数据库 --- Redis集群

avatar
作者
筋斗云
阅读量:0

什么是 redis ?

Redis是一个开源的、遵循BSD协议的、基于内存的而且目前比较流行的键值数据库(key-value database),是一个非关系型数据库,redis 提供将内存通过网络远程共享的一种服务,提供类似功能的还有memcached,但相比memcached,redis还提供了易扩展、高性能、具备数据持久性等功能。

Redis 在高并发、低延迟环境要求比较高的环境使用量非常广泛。

环境准备:

母盘:RHEL9 ---> 1G,1内核

克隆:redis-node1(172.25.254.10)、redis-node2(172.25.254.20)、redis-node3(172.25.254.30)

vmset.sh eth0 172.25.254.10 redis-node1.test.org

vmset.sh eth0 172.25.254.20 redis-node1.test.org

vmset.sh eth0 172.25.254.30 redis-node1.test.org

redis安装

[root@redis-node1 ~]# tar zxf redis-7.4.0.tar.gz  [root@redis-node1 ~]# cd redis-7.4.0/  [root@redis-node1 redis-7.4.0]# dnf install make -y [root@redis-node1 redis-7.4.0]# dnf install initscripts -y  [root@redis-node1 redis-7.4.0]# ll /etc/init.d/ [root@redis-node1 redis-7.4.0]# dnf install gcc -y [root@redis-node1 redis-7.4.0]# make && make install   [root@redis-node1 redis-7.4.0]# cd utils/ [root@redis-node1 utils]# vim install_server.sh  [root@redis-node1 utils]# ./install_server.sh  #一路回车
[root@redis-node1 utils]# vim /etc/redis/6379.conf [root@redis-node1 utils]# /etc/init.d/redis_6379 restart

[root@redis-node1 utils]# cd [root@redis-node1 ~]# scp -r redis-7.4.0 root@172.25.254.20:/root [root@redis-node1 ~]# scp -r redis-7.4.0 root@172.25.254.30:/root  [root@redis-node1 ~]# cd /usr/local/bin/  #10~30:都需要安装rsync [root@redis-node1 bin]# dnf install rsync -y  [root@redis-node1 bin]# rsync -al * root@172.25.254.20:/usr/local/bin [root@redis-node1 bin]# rsync -al * root@172.25.254.30:/usr/local/bin
[root@redis-node2 ~]# dnf install initscripts -y [root@redis-node2 ~]# dnf install rsync -y   [root@redis-node2 ~]# cd redis-7.4.0/ [root@redis-node2 redis-7.4.0]# cd utils/ [root@redis-node2 utils]# ./install_server.sh  Welcome to the redis service installer This script will help you easily set up a running redis server  Please select the redis port for this instance: [6379]  Selecting default: 6379 Please select the redis config file name [/etc/redis/6379.conf]  Selected default - /etc/redis/6379.conf Please select the redis log file name [/var/log/redis_6379.log]  Selected default - /var/log/redis_6379.log Please select the data directory for this instance [/var/lib/redis/6379]  Selected default - /var/lib/redis/6379 Please select the redis executable path [/usr/local/bin/redis-server]  Selected config: Port           : 6379 Config file    : /etc/redis/6379.conf Log file       : /var/log/redis_6379.log Data dir       : /var/lib/redis/6379 Executable     : /usr/local/bin/redis-server Cli Executable : /usr/local/bin/redis-cli Is this ok? Then press ENTER to go on or Ctrl-C to abort. Copied /tmp/6379.conf => /etc/init.d/redis_6379 Installing service... Successfully added to chkconfig! Successfully added to runlevels 345! Starting Redis server... Installation successful! [root@redis-node2 utils]#  [root@redis-node2 utils]# vim /etc/redis/6379.conf  [root@redis-node2 utils]# /etc/init.d/redis_6379 restart Stopping ... Redis stopped Starting Redis server... 
[root@redis-node3 ~]# dnf install initscripts -y [root@redis-node3 ~]# dnf install rsync -y   [root@redis-node3 ~]# cd redis-7.4.0/ [root@redis-node3 redis-7.4.0]# cd utils/ [root@redis-node3 utils]# ./install_server.sh  Welcome to the redis service installer This script will help you easily set up a running redis server  Please select the redis port for this instance: [6379]  Selecting default: 6379 Please select the redis config file name [/etc/redis/6379.conf]  Selected default - /etc/redis/6379.conf Please select the redis log file name [/var/log/redis_6379.log]  Selected default - /var/log/redis_6379.log Please select the data directory for this instance [/var/lib/redis/6379]  Selected default - /var/lib/redis/6379 Please select the redis executable path [/usr/local/bin/redis-server]  Selected config: Port           : 6379 Config file    : /etc/redis/6379.conf Log file       : /var/log/redis_6379.log Data dir       : /var/lib/redis/6379 Executable     : /usr/local/bin/redis-server Cli Executable : /usr/local/bin/redis-cli Is this ok? Then press ENTER to go on or Ctrl-C to abort. Copied /tmp/6379.conf => /etc/init.d/redis_6379 Installing service... Successfully added to chkconfig! Successfully added to runlevels 345! Starting Redis server... Installation successful! [root@redis-node3 utils]#  [root@redis-node3 utils]# vim /etc/redis/6379.conf [root@redis-node3 utils]# /etc/init.d/redis_6379 restart Stopping ... Redis stopped Starting Redis server... 

redis的主从复制

[root@redis-node2 utils]# vim /etc/redis/6379.conf [root@redis-node2 utils]# /etc/init.d/redis_6379 restart Stopping ... Redis stopped Starting Redis server... [root@redis-node2 utils]#  [root@redis-node2 utils]# redis-cli  127.0.0.1:6379> get name "lee"
[root@redis-node3 utils]# vim /etc/redis/6379.conf  [root@redis-node3 utils]# /etc/init.d/redis_6379 restart Stopping ... Redis stopped Starting Redis server... [root@redis-node3 utils]#  [root@redis-node3 utils]# redis-cli  127.0.0.1:6379> get name "lee"

redis哨兵

[root@redis-node1 ~]# ls anaconda-ks.cfg  redis-7.4.0  redis-7.4.0.tar.gz [root@redis-node1 ~]# cd redis-7.4.0/ [root@redis-node1 redis-7.4.0]# ls 00-RELEASENOTES     INSTALL      redis.conf              runtest-sentinel  TLS.md BUGS                LICENSE.txt  REDISCONTRIBUTIONS.txt  SECURITY.md       utils CODE_OF_CONDUCT.md  Makefile     runtest                 sentinel.conf CONTRIBUTING.md     MANIFESTO    runtest-cluster         src deps                README.md    runtest-moduleapi       tests [root@redis-node1 redis-7.4.0]# cp sentinel.conf /etc/redis/ [root@redis-node1 redis-7.4.0]# vim /etc/redis/sentinel.conf [root@redis-node1 redis-7.4.0]# scp /etc/redis/sentinel.conf root@172.25.254.20:/etc/redis/sentinel.conf root@172.25.254.20's password:  sentinel.conf                                    100%   14KB  10.6MB/s   00:00     [root@redis-node1 redis-7.4.0]# scp /etc/redis/sentinel.conf root@172.25.254.30:/etc/redis/sentinel.conf root@172.25.254.30's password:  sentinel.conf                                    100%   14KB  10.5MB/s   00:00     [root@redis-node1 redis-7.4.0]#  [root@redis-node1 redis-7.4.0]# cd /etc/redis/ [root@redis-node1 redis]# ls 6379.conf  sentinel.conf [root@redis-node1 redis]# cp sentinel.conf sentinel.conf.bak  [root@redis-node1 redis]# redis-sentinel /etc/redis/sentinel.conf
[root@redis-node2 ~]# cd /etc/redis/ [root@redis-node2 redis]# ls 6379.conf  sentinel.conf [root@redis-node2 redis]# cp sentinel.conf sentinel.conf.bak  [root@redis-node2 redis]# redis-sentinel /etc/redis/sentinel.conf
[root@redis-node3 ~]# cd /etc/redis/ [root@redis-node3 redis]# ls 6379.conf  sentinel.conf [root@redis-node3 redis]# cp sentinel.conf sentinel.conf.bak  [root@redis-node3 redis]# redis-sentinel /etc/redis/sentinel.conf

测试:

redis cluster

克隆:redis-node110、redis-node120、redis-node130

vmset.sh eth0 172.25.254.110 redis-node110.test.org

vmset.sh eth0 172.25.254.120 redis-node120.test.org

vmset.sh eth0 172.25.254.130 redis-node130.test.org

[root@redis-node1 redis]# cd /root/redis-7.4.0/ [root@redis-node1 redis-7.4.0]# /etc/init.d/redis_6379 stop Stopping ... Redis stopped [root@redis-node1 redis-7.4.0]# ps aux | grep redis root        7666  0.0  0.3   6408  2296 pts/1    S+   18:42   0:00 grep --color=auto redis [root@redis-node1 redis-7.4.0]# make uninstall cd src && make uninstall make[1]: Entering directory '/root/redis-7.4.0/src' rm -f /usr/local/bin/{redis-server,redis-benchmark,redis-cli,redis-check-rdb,redis-check-aof,redis-sentinel} make[1]: Leaving directory '/root/redis-7.4.0/src' [root@redis-node1 redis-7.4.0]#  [root@redis-node1 redis-7.4.0]# cd [root@redis-node1 ~]# dnf install redis -y   [root@redis-node1 ~]# vim /etc/redis/redis.conf [root@redis-node1 ~]# systemctl enable --now redis Created symlink /etc/systemd/system/multi-user.target.wants/redis.service → /usr/lib/systemd/system/redis.service. [root@redis-node1 ~]# systemctl restart redis.service [root@redis-node1 ~]# netstat -antlupe | grep redis tcp        0      0 0.0.0.0:16379           0.0.0.0:*               LISTEN      991        40761      7919/redis-server *  tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      991        40759      7919/redis-server *  tcp6       0      0 :::16379                :::*                    LISTEN      991        40762      7919/redis-server *  tcp6       0      0 :::6379                 :::*                    LISTEN      991        40760      7919/redis-server *  [root@redis-node1 ~]#  [root@redis-node1 ~]# redis-cli  -bash: /usr/local/bin/redis-cli: No such file or directory [root@redis-node1 ~]# exit   [root@redis-node1 ~]# redis-cli  127.0.0.1:6379> auth 123456 OK 127.0.0.1:6379> keys * (empty array)    [root@redis-node1 ~]# for i in 20 30 110 120 130 > do > scp /etc/redis/redis.conf root@172.25.254.$i:/etc/redis/redis.conf > done 
[root@redis-node2 redis-7.4.0]# cd [root@redis-node2 ~]# dnf install redis -y   [root@redis-node2 ~]# vim /etc/hosts  [root@redis-node2 ~]# systemctl enable --now redis  [root@redis-node2 ~]# exit [root@redis-node2 ~]# redis-cli -a 123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:6379> set name lee OK 
[root@redis-node3 redis-7.4.0]# cd [root@redis-node3 ~]# dnf install redis -y   [root@redis-node3 ~]# vim /etc/hosts  [root@redis-node3 ~]# systemctl enable --now redis  [root@redis-node3 ~]# exit [root@redis-node3 ~]# redis-cli -a 123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:6379> get name (error) MOVED 5798 172.25.254.20:6379 
[root@redis-node110 ~]# dnf install redis -y [root@redis-node110 ~]# vim /etc/hosts  [root@redis-node110 ~]# systemctl enable --now redis
[root@redis-node120 ~]# dnf install redis -y [root@redis-node120 ~]# vim /etc/hosts  [root@redis-node120 ~]# systemctl enable --now redis
[root@redis-node130 ~]# dnf install redis -y [root@redis-node130 ~]# vim /etc/hosts  [root@redis-node130 ~]# systemctl enable --now redis
[root@redis-node1 ~]# redis-cli --cluster create -a 123456 \ > 172.25.254.10:6379 172.25.254.20:6379 172.25.254.30:6379 \ > 172.25.254.110:6379 172.25.254.120:6379 172.25.254.130:6379 \ > --cluster-replicas 1 ... >>> Performing Cluster Check (using node 172.25.254.10:6379) M: 51942ff68767525da5b43516d5a1ac6d3a4236c0 172.25.254.10:6379    slots:[0-5460] (5461 slots) master    1 additional replica(s) S: f775d5a125e9056a699f6ef324566e3024c8eba4 172.25.254.110:6379    slots: (0 slots) slave    replicates 53eaf5b37b8961c67beed67abe67a4fdb568ba06 S: 2a8f1dfd906a1638fdcfc771dc24f00a21b5e8bf 172.25.254.120:6379    slots: (0 slots) slave    replicates 51942ff68767525da5b43516d5a1ac6d3a4236c0 M: bd12ca08ea95bdc5abb1f52e35d6bb825611ba72 172.25.254.20:6379    slots:[5461-10922] (5462 slots) master    1 additional replica(s) M: 53eaf5b37b8961c67beed67abe67a4fdb568ba06 172.25.254.30:6379    slots:[10923-16383] (5461 slots) master    1 additional replica(s) S: aafeaea3cf23aa110960219c8b000946d94dd8a8 172.25.254.130:6379    slots: (0 slots) slave    replicates bd12ca08ea95bdc5abb1f52e35d6bb825611ba72 ...   #20被推选,所以10不能创建 [root@redis-node1 ~]# redis-cli -a 123456 Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:6379> set name lee (error) MOVED 5798 172.25.254.20:6379

集群扩容

克隆:redis50、redis150 #都是1G 1内核

vmset.sh eth0 172.25.254.50 redis50.silingchuan.org

vmset.sh eth0 172.25.254.150 redis150.silingchuan.org

50 150:

dnf install redis -y

[root@redis-node2 ~]# scp /etc/redis/redis.conf root@172.25.254.50:/etc/redis/redis.conf  [root@redis-node2 ~]# scp /etc/redis/redis.conf root@172.25.254.150:/etc/redis/redis.conf
[root@redis-node50 ~]# dnf install redis -y [root@redis-node50 ~]# systemctl start redis
[root@redis-node150 ~]# dnf install redis -y [root@redis-node150 ~]# systemctl start redis

新增50master

[root@redis-node2 ~]# redis-cli -a 123456 --cluster add-node 172.25.254.50:6379 172.25.254.20:6379  [root@redis-node2 ~]# redis-cli -a 123456 --cluster check 172.25.254.10:6379

共享槽位

[root@redis-node2 ~]# redis-cli -a 123456 --cluster reshard 172.25.254.20:6379

(4096 50-ID all yes)

新增150slave

[root@redis-node2 ~]# redis-cli -a 123456 --cluster add-node 172.25.254.150:6379 172.25.254.20:6379 --cluster-slave --cluster-master-id 2ce4bedb48252ca1c7030586d45d1b271490176a

移除150:

[root@redis-node2 ~]# redis-cli -a 123456 --cluster del-node  172.25.254.150:6379 6ab4d6ec8aab005c3934f6bfa272a6be77545cbb

清空槽位:

[root@redis-node2 ~]# redis-cli -a 123456 --cluster  reshard 172.25.254.20:6379

(4096 20-ID 50-ID done yes)

移除50:

[root@redis-node2 ~]# redis-cli -a 123456 --cluster del-node  172.25.254.50:6379 2ce4bedb48252ca1c7030586d45d1b271490176a

    广告一刻

    为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!