1、单节点Redis服务器带来的问题
2、解决方法
说明:支持热添加和热删除,在运行状态下可以添加和删除
‘主从服务器的部署都是一样的,在此仅展示主服务器的部署’
[root@master1 ~]# yum install gcc gcc-c++ make -y '//安装环境组件编译器'
[root@master1 redis]# tar zxvf redis-5.0.7.tar.gz -C /opt 'redis源码包可以直接到官网下载'
[root@master1 redis]# cd /opt/redis-5.0.7/
[root@master1 redis-5.0.7]# make '直接进行make'
[root@master1 redis-5.0.7]# make PREFIX=/usr/local/redis/ install '指定redis目录并安装'
[root@master1 redis-5.0.7]# cd utils/'回到redis源码包解压目录'
[root@master1 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] '选择redis默认接口,直接回车'
Selecting default: 6379
Please select the redis config file name [/etc/redis/6379.conf] '选择redis默认配置文件名称,直接回车'
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log] '选择默认redis日志文件名称,直接回车'
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/redis/bin/redis-server '选择redis可执行文件路径,需要手动输入此路径:/usr/local/redis/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/redis/bin/redis-server
Cli Executable : /usr/local/redis/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@master1 utils]# ln -s /usr/local/redis/bin/* /usr/local/bin '将redis命令创建软连接,便于系统识别'
[root@master1 utils]# netstat -ntap |grep 6379
tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN
[root@master1 utils]# vim /etc/redis/6379.conf '修改主配置文件'
#bind 127.0.0.1 '注释第70行的监听127地址,表示监听所有地址'
protected-mode no '去掉第89行注释关闭安全保护'
port 6379 '去掉第93行注释,开启端口6379'
daemonize yes '去掉第137行注释,以独立进程启动'
cluster-enabled yes '去掉第833行注释,开启群集功能'
cluster-config-file nodes-6379.conf '去掉第841行注释,群集名称文件设置'
cluster-node-timeout 15000 '去掉第847行注释,群集超时时间设置'
appendonly yes '去掉第700行注释,开启aof持久化'
[root@master1 utils]# cd /var/lib/redis/6379/
[root@master1 6379]# /etc/init.d/redis_6379 restart '重启redis服务'
[root@master1 6379]# ls
appendonly.aof dump.rdb nodes-6379.conf
'生成了三个文件,appendonly.aof是AOF持久化文件,dump.rdb是RDB快照文件,nodes-6379.conf是节点首次启动生成的配置文件'
[root@master1 6379]# gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 '导入key文件,若出现error,重新导入一次即可'
[root@master1 6379]# curl -sSL https://get.rvm.io | bash -s stable '安装rvm,这边是需要的才能够获取'
[root@master1 redis]# tar zxvf rvm-master.tar.gz -C /opt
[root@master1 redis]# cd /opt/rvm-master/
[root@master1 rvm-master]# ./install
[root@master1 rvm-master]# source /etc/profile.d/rvm.sh '执行环境变量'
[root@master1 rvm-master]# rvm list known '列出ruby可以安装的版本'
[root@master1 rvm-master]# rvm install 2.4.10 '安装ruby2.4.10版本,安装版本的事件会比较长'
[root@master1 rvm-master]# ruby -v '查看当前ruby版本'
[root@master1 rvm-master]# gem install redis '再次安装redis'
主服务器添加两块网卡模式为NAT
启动服务关闭防火墙
[root@master1 rvm-master]# systemctl restart network '重启网卡'
[root@master1 rvm-master]# systemctl stop firewalld.service '关闭防火墙'
[root@master1 rvm-master]# setenforce 0
从服务器也添加两块网卡模式为NAT
启动服务关闭防火墙
[root@master1 rvm-master]# systemctl restart network '重启网卡'
[root@master1 rvm-master]# systemctl stop firewalld.service '关闭防火墙'
[root@master1 rvm-master]# setenforce 0
[root@master1 rvm-master]# redis-cli --cluster create 20.0.0.52:6379 20.0.0.51:6379 20.0.0.47:6379 20.0.0.48:6379 20.0.0.55:6379 20.0.0.60:6379 --cluster-replicas 1
'//反馈的信息输入yes回车即可,以下为反馈信息'
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 20.0.0.55:6379 to 20.0.0.52:6379
Adding replica 20.0.0.60:6379 to 20.0.0.51:6379
Adding replica 20.0.0.48:6379 to 20.0.0.47:6379
M: dacbafd23b78accca1ecb7a37a926dd7c711af0d 20.0.0.52:6379 '133,129,130位主'
slots:[0-5460] (5461 slots) master
M: dacbafd23b78accca1ecb7a37a926dd7c711af0d 20.0.0.51:6379
slots:[5461-10922] (5462 slots) master
M: dacbafd23b78accca1ecb7a37a926dd7c711af0d 20.0.0.47:6379
slots:[10923-16383] (5461 slots) master
S: f3a25427b5b2bd0af5b194d0d90c87882a09e4cd 20.0.0.48:6379 '134,131,132为从'
replicates dacbafd23b78accca1ecb7a37a926dd7c711af0d
S: f3a25427b5b2bd0af5b194d0d90c87882a09e4cd 20.0.0.55:6379
replicates dacbafd23b78accca1ecb7a37a926dd7c711af0d
S: f3a25427b5b2bd0af5b194d0d90c87882a09e4cd 20.0.0.60:6379
replicates dacbafd23b78accca1ecb7a37a926dd7c711af0d
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 20.0.0.52:6379)
M: dacbafd23b78accca1ecb7a37a926dd7c711af0d 20.0.0.52:6379
slots:[0-16383] (16384 slots) master
1 additional replica(s)
S: f3a25427b5b2bd0af5b194d0d90c87882a09e4cd 20.0.0.55:6379
slots: (0 slots) slave
replicates dacbafd23b78accca1ecb7a37a926dd7c711af0d
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
'一主一从的绑定关系是随机的'
主从数据验证
[root@master1 rvm-master]# redis-cli -h 20.0.0.52 -p 6379 '登陆到133服务器数据库'
20.0.0.52:6379> keys *
(empty list or set)
20.0.0.52:6379> set user lisi '创建一个键值'
OK
20.0.0.52:6379> keys * '查看所有键'
1) "user"
20.0.0.52:6379> get user '查看user键的值'
"lisi"
20.0.0.52:6379> exit '退出'
[root@master1 rvm-master]# redis-cli -h 20.0.0.55 -p 6379 '登陆到131服务器数据库'
20.0.0.55:6379> keys *
1) "user"
20.0.0.55:6379> get user '//查看user键的值'
(error) MOVED 5474 20.0.0.51:6379 '//提示移动到129节点上'
20.0.0.55:6379> exit
[root@master1 rvm-master]# redis-cli -h 20.0.0.51 -p 6379 '登陆到129服务器的数据库'
20.0.0.51:6379> keys *
1) "user"
20.0.0.51:6379> get user
"lisi"
20.0.0.51:6379> exit
hash方式存储的值验证与设置键的自动删除时间
[root@master1 rvm-master]# redis-cli -h 20.0.0.52 -p 6379
20.0.0.52:6379> hset person name zhangsan '使用hash方式创建键值对'
(integer) 1
20.0.0.52:6379> keys *
1) "person"
2) "user"
20.0.0.52:6379> hget person name
"zhangsan"
20.0.0.52:6379> expire user 10 '设置user键的自动删除时间10秒'
(integer) 1
'//等待10秒'
20.0.0.52:6379> keys *
1) "person" '成功自动删除'
20.0.0.52:6379> exit