前言
一组Redis Cluster是由多个Redis实例组成,官方推荐使用6实例,其中3个为主节点,3个为从节点。一旦有主节点发生故障的时候,Redis Cluster可以选举出对应的从结点成为新的主节点,继续对外服务,从而保证服务的高可用性,本文使用两台虚拟机添加6块网卡,充当3主3从。
实验环境
IP地址分配如下
主机名 | IP地址 |
---|---|
主服务器 | 192.168.7.128,192.168.7.130,192.168.7.131 |
从服务器 | 192.168.7.129,192.168.7.132,192.168.7.133 |
实验过程
1、安装redis
(1)安装环境包
[root@localhost ~]# yum install gcc gcc-c++ make -y
(2)编译安装redis
[root@localhost redis]# tar zxvf redis-5.0.7.tar.gz -C /opt
[root@localhost redis]# cd /opt/redis-5.0.7/
[root@localhost redis-5.0.7]# make
[root@localhost redis-5.0.7]# make PREFIX=/usr/local/redis/ install
(3)配置redis
[root@localhost redis-5.0.7]# cd utils/
[root@localhost utils]# ./install_server.sh
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/redis/bin/redis-server
[root@localhost utils]# ln -s /usr/local/redis/bin/* /usr/local/bin/
2、redis集群配置
(1)在所有节点上配置
注释掉 bind项,redis中bind选项默认监听所有网卡
//关闭保护模式
protected-mode no
port 6379
#以独立进程启动
daemonize yes
#开启群集功能
cluster-enabled yes
#群集名称文件设置
cluster-config-file nodes-6379.conf
#群集超时间设置
cluster-node-timeout 15000
#开启aof持久化
appendonly yes
#重启服务
[root@localhost ~]# /etc/init.d/redis_6379 restart
正常启动后,在/var/lib/redis/6379/目录下会多出两个文件
appendonly.aof:持久化文件
node-6379.conf:节点首次启动生成的配置文件
(2)在master上配置(此过程使用的是线网安装,需要时间较长)
#导入key文件
[root@localhost ~]# gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
#安装rvm
[root@localhost ~]# curl -sSL https://get.rvm.io | bash -s stable
#打开下面网址,将脚本复制到/opt目录下,执行
https://raw.githubusercontent.com/rvm/rvm/master/binscripts/rvm-installer
#执行环境变量
[root@localhost ~]# source /etc/profile.d/rvm.sh
#列出Ruby可安装的版本
[root@localhost ~]# rvm list known
#安装Ruby2.4.1版本
[root@localhost ~]# rvm install 2.4.1
#使用Ruby2.4.1版本
[root@localhost ~]# rvm use 2.4.1
#查看当前Ruby2.4.1版本
[root@localhost ~]# ruby -v
#再次安装redis
[root@localhost ~]# gem install redis
(3)创建集群
[root@localhost ~]# redis-cli --cluster create 192.168.7.128:6379 192.168.7.130:6379 192.168.7.131:6379 192.168.7.129:6379 192.168.7.132:6379 192.168.7.133:6379 --cluster-replicas 1
在群集上,写入的数据会随机保存到另一个地址节点上,数据删除也会同步删除
在从服务器上,无法创建和查看数据具体内容
在主服务器上可随意创建
[root@localhost ~]# redis-cli -h 192.168.7.128 -p 6379
192.168.7.128:6379> set people lisi
OK
192.168.7.128:6379> quit
[root@localhost ~]# redis-cli -h 192.168.7.129 -p 6379
192.168.7.129:6379> get people
(error) MOVED 6247 192.168.7.128:6379
192.168.7.129:6379> quit
[root@localhost ~]# redis-cli -h 192.168.7.130 -p 6379
192.168.7.130:6379> get people
"lisi"