千家信息网

NoSQL之Redis群集配置(实践篇)

发表于:2025-01-23 作者:千家信息网编辑
千家信息网最后更新 2025年01月23日,实验环境用两台服务器模拟6台服务器(添加网卡)IP节点主服务器192.168.144.144192.168.144.159192.168.144.154从服务器192.168.144.141192.1
千家信息网最后更新 2025年01月23日NoSQL之Redis群集配置(实践篇)

实验环境

用两台服务器模拟6台服务器(添加网卡)

  • IP节点
    • 主服务器
    • 192.168.144.144
    • 192.168.144.159
    • 192.168.144.154
    • 从服务器
    • 192.168.144.141
    • 192.168.144.155
    • 192.168.144.160

在主服务器与从服务器上分别添加两张网卡

[root@master ruby-2.4.1]# service network restart        //重启网卡[root@master ruby-2.4.1]# systemctl stop firewalld.service      //关闭防火墙[root@master ruby-2.4.1]# setenforce 0
[root@slave utils]# service network restart         //重启网卡[root@slave utils]# systemctl stop firewalld.service   //关闭防火墙[root@slave utils]# setenforce 0

在两台服务器上都安装Redis

[root@localhost utils]# vim /etc/redis/6379.conf #bind 127.0.0.1                 //注释第70行的监听127地址,已监听所有地址protected-mode no               //开启关闭安全保护port 6379                       //开启端口6379daemonize yes                   //开启以独立进程启动cluster-enabled yes             //开启群集功能cluster-config-file nodes-6379.conf  //群集名称文件设置cluster-node-timeout 15000           //群集超时时间设置appendonly yes                       //开启aof持久化[root@localhost utils]# /etc/init.d/redis_6379 restart   //重启服务Stopping ...Redis stoppedStarting Redis server...[root@localhost utils]# cd /var/lib/redis/6379/[root@localhost 6379]# lsappendonly.aof  dump.rdb  nodes-6379.conf        //生成aof、rdb和节点文件

在主服务器上安装rvm、Ruby控制群集软件

[root@master 6379]# gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3        //导入key文件[root@master 6379]# curl -sSL https://get.rvm.io | bash -s stable      //安装rvm[root@localhost utils]# source /etc/profile.d/rvm.sh       //执行环境变量[root@localhost utils]# rvm list known          //列出ruby可以安装的版本[root@localhost utils]# rvm install 2.4.1        //安装2.4.1 版本[root@localhost utils]# rvm use 2.4.1             //使用rubyruby2.4.1版本Using /usr/local/rvm/gems/ruby-2.4.1[root@localhost utils]# ruby -v                   //查看当前版本ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux][root@localhost utils]# gem install redis               //再次安装Redis

在master服务器上创建集群

[root@master ruby-2.4.1]# redis-cli --cluster create 192.168.144.144:6379 192.168.144.159:6379 192.168.144.154:6379 192.168.144.141:6379 192.168.144.155:6379 192.168.144.160:6379 --cluster-replicas 1     //创建群集,每组一主一从>>> Performing hash slots allocation on 6 nodes...Master[0] -> Slots 0 - 5460Master[1] -> Slots 5461 - 10922Master[2] -> Slots 10923 - 16383Adding replica 192.168.144.155:6379 to 192.168.144.144:6379Adding replica 192.168.144.160:6379 to 192.168.144.159:6379Adding replica 192.168.144.141:6379 to 192.168.144.154:6379M: d2aef8bb466d29891e051edd1c9c35d760c452e8 192.168.144.144:6379   slots:[0-5460] (5461 slots) masterM: d2aef8bb466d29891e051edd1c9c35d760c452e8 192.168.144.159:6379   slots:[5461-10922] (5462 slots) masterM: d2aef8bb466d29891e051edd1c9c35d760c452e8 192.168.144.154:6379   slots:[10923-16383] (5461 slots) masterS: 984482d225d614b2b2b084f5c54bf197202065a0 192.168.144.141:6379   replicates d2aef8bb466d29891e051edd1c9c35d760c452e8S: 984482d225d614b2b2b084f5c54bf197202065a0 192.168.144.155:6379   replicates d2aef8bb466d29891e051edd1c9c35d760c452e8S: 984482d225d614b2b2b084f5c54bf197202065a0 192.168.144.160:6379   replicates d2aef8bb466d29891e051edd1c9c35d760c452e8Can I set the above configuration? (type 'yes' to accept): yes    //输入yes>>> Nodes configuration updated>>> Assign a different config epoch to each node>>> Sending CLUSTER MEET messages to join the clusterWaiting for the cluster to join>>> Performing Cluster Check (using node 192.168.144.144:6379)M: d2aef8bb466d29891e051edd1c9c35d760c452e8 192.168.144.144:6379   slots:[0-16383] (16384 slots) master   1 additional replica(s)S: 984482d225d614b2b2b084f5c54bf197202065a0 192.168.144.155:6379   slots: (0 slots) slave   replicates d2aef8bb466d29891e051edd1c9c35d760c452e8[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.

验证群集读写原理

root@master opt]# redis-cli -h 192.168.144.144 -p 6379      //主服务器1192.168.144.144:6379> set name zhangsan        //创建键值对OK 192.168.144.144:6379> keys *1) "name"192.168.144.144:6379> get name"zhangsan"192.168.144.144:6379> exit[root@master opt]# redis-cli -h 192.168.144.155 -p 6379       //从服务器192.168.144.155:6379> keys *           //查看从上也有1) "name"192.168.144.155:6379> get name"zhangsan"[root@master opt]# redis-cli -h 192.168.144.144 -p 6379 192.168.144.144:6379> hset person age 20       //用hash方式建立键值对(integer) 1192.168.144.144:6379> hset person name lisi(integer) 1192.168.144.144:6379> keys *1) "person"192.168.144.144:6379> hget person age        //获取键的值"20"192.168.144.144:6379> expire person 5       //设置键的删除时间5s(integer) 1192.168.144.144:6379> keys *1) "person"192.168.144.144:6379> keys *(empty list or set)
0