文章目录

  • 前言
  • 一:环境
  • 1.1:环境
  • 1.2:实验目的
  • 二:实验过程
  • 2.1:主从服务器部署Redis软件
  • 2.2 redis 集群,所有节点参数配置
  • 2.3 重启服务,正常启动下会多3个文件
  • 2.4 在一台maser 上 安装ruby 软件
  • 2.5 具体实操如下:
  • 2.6 . 创建群集
  • 2.7 主从对应关系,前面为从,后面为主
  • 2.8 下面开始验证群集功能


前言

一:环境

1.1:环境

Redis群集 java Redis群集setn_Redis群集 java

VMware软件
Redis官网建议使用6台服务器搭建群集,3台master,3台slave
我们用6台服务器
主服务器master1:IP地址:192.168100.20
主服务器master2:IP地址:192.168.100.4
主服务器master3:IP地址:192.168.100.3
从服务器slave1:IP地址:192.168.100.5
从服务器slave2:IP地址:192.168.100.6
从服务器slave3:IP地址:192.168.100.12
Redis其中一台master 需要安装rvm和Ruby控制集群软件

1.2:实验目的

通过实验实现以下目标
部署Redis服务
构建Redis群集
验证Redis群集功能

二:实验过程

2.1:主从服务器部署Redis软件

/关闭防火墙
systemctl stop firewalld
setenforce 0

//安装
yum install -y gcc gcc-c++ make

//解压安装包,make编译
tar zxvf redis-5.0.7.tar.gz -C /opt/
cd /opt/redis-5.0.7/
make
make PREFIX=/usr/local/redis/ install

[root@promote redis-5.0.7]# cd utils/     #一直回车到最后只修改路径
[root@localhost utils]# ./install_server.sh 

..............................................................省略...................................................
Please select the redis executable path [] /usr/local/redis/bin/redis-server //可执行文件路径,需要自行定义
...............................................................省略.....................................................................
//查看服务状态,服务已经开启
[root@promote utils]# netstat -natp | grep 6379
tcp        0      0 127.0.0.1:6379          0.0.0.0:*               LISTEN      22999/redis-server  

//创建软链接,优化服务
ln -s /usr/local/redis/bin/* /usr/local/bin/

2.2 redis 集群,所有节点参数配置

vim /etc/redis/6379.conf
注释掉bind项    //redis中bind选项默认监听所有网卡
70 #bind 127.0.0.1         注释掉 监听网卡     !
89/ protected-mode no      //关闭保护模式  !
93/ port 6379
137/ daemonize yes     //以独立进程启动
833/ cluster-enabled yes    /开启群集功能  !
841/ cluster-config-file nodes-6379.conf    //群集名称文件设置 !
847/ cluster-node-timeout 15000      /群集超时时间设置  !
700/ appendonly yes    /开启aof持久化

2.3 重启服务,正常启动下会多3个文件

/etc/init.d/redis_6379 restart

核对下面有3个文件生成
[root@pc-2 utils]# cd /var/lib/redis/6379/
[root@pc-2 6379]# ls
appendonly.aof  dump.rdb  nodes-6379.conf

2.4 在一台maser 上 安装ruby 软件

2.4.1 导入KEY文件

gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3

2.4.2 访问网页下载脚本文件

curi -ssL https://get.rvm.io | bash -s stable    //访问,下载脚本

将脚本保存下来,放到服务器上 rvm-installer.sh
2.4.3 执行脚本

./rvm-installer.sh

2.4.4 执行环境变量

source /etc/profile.d/rvm.sh

2.4.5 列出 Ruby可安装的版本

rvm list known

2.4.6 安装Ruby2.4.1版本

rvm install 2.4.1

2.4.7 使用Ruby2.4.1版本

rvm use 2.4.1

2.4.7 查看当前Ruby2.4.1版本

ruby -v

2.4.8 显示可用版本

[root@pc-2 profile.d]# rvm list known

2.4.9 安装 版本2.4.1

[root@pc-2 profile.d]# rvm install 2.4.1  安装 版本2.4.1

2.5 具体实操如下:

[root@pc-2 opt]# ls
mongodb-linux-x86_64-3.2.1      redis-5.0.7         rh
mongodb-linux-x86_64-3.2.1.tgz  redis-5.0.7.tar.gz  rvm-installer.sh
[root@pc-2 opt]# chmod +x rvm-installer.sh
[root@pc-2 opt]# ./rvm-installer.sh
Downloading https://github.com/rvm/rvm/archive/master.tar.gz
Creating group 'rvm'
Installing RVM to /usr/local/rvm/
Installation of RVM in /usr/local/rvm/ is almost complete:

  * First you need to add all users that will be using rvm to 'rvm' group,
    and logout - login again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`.

  * To start using RVM you need to run `source /etc/profile.d/rvm.sh`
    in all your open shell windows, in rare cases you need to reopen all shell windows.
  * Please do NOT forget to add your users to the rvm group.
     The installer no longer auto-adds root or users to the rvm group. Admins must do this.
     Also, please note that group memberships are ONLY evaluated at login time.
     This means that users must log out then back in before group membership takes effect!
Thanks for installing RVM 
Please consider donating to our open collective to help us maintain RVM.

  Donate: https://opencollective.com/rvm/donate

[root@pc-2 opt]# cd /etc/profile.d/
[root@pc-2 profile.d]# ls
256term.csh                   colorgrep.sh  lang.csh       rvm.sh    which2.csh
256term.sh                    colorls.csh   lang.sh        sh.local  which2.sh
abrt-console-notification.sh  colorls.sh    less.csh       vim.csh
bash_completion.sh            csh.local     less.sh        vim.sh
colorgrep.csh                 flatpak.sh    PackageKit.sh  vte.sh
[root@pc-2 profile.d]# source /etc/profile.d/rvm.sh
[root@pc-2 profile.d]# rvm list known
[root@pc-2 profile.d]# rvm install 2.4.1
[root@pc-2 profile.d]# rvm use 2.4.1   使用版本
Using /usr/local/rvm/gems/ruby-2.4.1     
[root@pc-2 profile.d]# ruby -v  查看当前版本
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]
[root@pc-2 profile.d]# gem install redis     再次安装redis
Successfully installed redis-4.2.2
Parsing documentation for redis-4.2.2
Installing ri documentation for redis-4.2.2
Done installing documentation for redis after 1 seconds
1 gem installed

2.6 . 创建群集

创建群集
[root@pc-2 6379]# redis-cli --cluster create  192.168.100.20:6379  192.168.100.3:6379192.168.100.4:6379  192.168.100.5:6379  192.168.100.12:6379  192.168.100.6:6379   --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.100.12:6379 to 192.168.100.20:6379
Adding replica 192.168.100.6:6379 to 192.168.100.3:6379
Adding replica 192.168.100.5:6379 to 192.168.100.4:6379
M: dd9a45612ab4aa16aa7c0070e001f62b81a1f156 192.168.100.20:6379
   slots:[0-5460] (5461 slots) master
M: 729184b33cae9a1509ae8457d965081ed9fffdb1 192.168.100.3:6379
   slots:[5461-10922] (5462 slots) master
M: a50110d7be2ddfb2295c5a19051f5e0ee3efa714 192.168.100.4:6379
   slots:[10923-16383] (5461 slots) master
S: 513bcc390744e262aad96d99c618e37480fca384 192.168.100.5:6379
   replicates a50110d7be2ddfb2295c5a19051f5e0ee3efa714
S: 77faad547a60348b1dd25e99fb29c0754b054ddf 192.168.100.12:6379
   replicates dd9a45612ab4aa16aa7c0070e001f62b81a1f156
S: d4efac1c688844b9bf86f6b9e91c38ab5e8b2513 192.168.100.6:6379
   replicates 729184b33cae9a1509ae8457d965081ed9fffdb1
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.100.20:6379)
M: dd9a45612ab4aa16aa7c0070e001f62b81a1f156 192.168.100.20:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: a50110d7be2ddfb2295c5a19051f5e0ee3efa714 192.168.100.4:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 513bcc390744e262aad96d99c618e37480fca384 192.168.100.5:6379
   slots: (0 slots) slave
   replicates a50110d7be2ddfb2295c5a19051f5e0ee3efa714
S: 77faad547a60348b1dd25e99fb29c0754b054ddf 192.168.100.12:6379
   slots: (0 slots) slave
   replicates dd9a45612ab4aa16aa7c0070e001f62b81a1f156
M: 729184b33cae9a1509ae8457d965081ed9fffdb1 192.168.100.3:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: d4efac1c688844b9bf86f6b9e91c38ab5e8b2513 192.168.100.6:6379
   slots: (0 slots) slave
   replicates 729184b33cae9a1509ae8457d965081ed9fffdb1
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

2.7 主从对应关系,前面为从,后面为主

Adding replica 192.168.100.12:6379 to 192.168.100.20:6379
Adding replica 192.168.100.6:6379 to 192.168.100.3:6379
Adding replica 192.168.100.5:6379 to 192.168.100.4:6379

2.8 下面开始验证群集功能

登录数据库

[root@pc-2 6379]# redis-cli -c -h 192.168.100.20
192.168.100.20:6379> set name zhangsan
-> Redirected to slot [5798] located at 192.168.100.3:6379
OK
192.168.100.3:6379> keys *
1) "name"
192.168.100.3:6379> quit
[root@pc-2 6379]# redis-cli -c -h 192.168.100.20
192.168.100.20:6379> keys *
1) "age"

创建文件,发现文件自动被分配创建到了另外一台master 上

192.168.100.20:6379> set stu 77   // 
-> Redirected to slot [11255] located at 192.168.100.4:6379
OK
192.168.100.4:6379> keys *
1) "stu"
192.168.100.4:6379>

查看 master 192.168.100.4 的从服务器 192.168.100.5,发现已经自动同步了键值

[root@pc-5 utils]# redis-cli -c -h 192.168.100.5
192.168.100.5:6379> keys *
(empty list or set)
192.168.100.5:6379> redis-cli -c -h 192.168.100.5

192.168.100.5:6379> keys *
1) "stu"
192.168.100.5:6379>

下面将master 192.168.100.20 down 掉,测试其余的群集功能

Redis群集 java Redis群集setn_运维_02


在另外一台master 上创建 键名,发现又被分配写入到了 192.168.100.4 master 上

[root@pc-3 6379]# redis-cli -c -h 192.168.100.3
192.168.100.3:6379> set new naz
-> Redirected to slot [15045] located at 192.168.100.4:6379
OK
192.168.100.4:6379>

在 192,168,100.4 的从服务器上查看,同步正常,看来并无影响

192.168.100.5:6379> keys *
1) "new"
2) "stu"
192.168.100.5:6379> get new
-> Redirected to slot [15045] located at 192.168.100.4:6379
"naz"
192.168.100.4:6379>

设置10S自动消失

先在master 上进行设置 键名  new   10S自动消失
 [root@pc-4 ~]# redis-cli -c -h 192.168.100.4
192.168.100.4:6379> keys *
1) "new"
2) "stu"
192.168.100.4:6379> expire new 10
(integer) 1
10S之后查看,果然消失了

192.168.100.4:6379> keys *
1) "stu"
192.168.100.4:6379>

在master 副本上查看同步情况,果然也没有了
[root@pc-5 ~]# redis-cli -c -h 192.168.100.5
192.168.100.5:6379> keys *
1) "stu"
192.168.100.5:6379>

在之前的从上192.168.100.12上创建键名,发现自动跳转到另外一台master 上去了,说明自动切换成master了


[root@sever ~]# redis-cli -c -h 192.168.100.12
 192.168.100.12:6379> set new4 888
 -> Redirected to slot [11274] located at 192.168.100.4:6379
 OK
 192.168.100.4:6379> keys *1. “stu”
2. “new4”

在另外一台master 上创建文件,发现自动跳到 192.168.100.12 上了

192.168.100.3:6379> set new6 666
 -> Redirected to slot [3144] located at 192.168.100.12:6379
 OK
 192.168.100.12:6379> quit


查看成功

[root@sever ~]# redis-cli -c -h 192.168.100.12
 192.168.100.12:6379> keys 8
 (empty list or set)
 192.168.100.12:6379> keys *1. “new6”
2. “age”
3. “new2”
 192.168.100.12:6379> get new6
 “666”