于三台虚拟机模拟真机部署三主三从redis集群全过程实录

第一步:首先准备三台CentOS虚拟机

192.168.3.101 192.168.3.102 192.168.3.103
端口:6379 端口:6380 端口:6379 端口:6380 端口:6379 端口:6380

第二步:其次编写两套redis.conf配置文件,分别服务于主节点和从节点

主:从: 内容如下所示:

主节点配置 从节点配置
port 6379
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
protected-mode no
port 6380
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
daemonize yes
protected-mode no

第三步:准备环境

1、准备redis   redis-4.0.10 下载地址:http://download.redis.io/releases/redis-4.0.10.tar.gz    我为了统一,将所有的tar包拷贝至三台机器的usr/redis_cluster目录下,该目录为我自己建的,可以按照个人喜好建立自己的文件夹以示区分。由于一台机器上主从节点要启动两个redis实例,所以建议单独建立两个文件夹存储主从redis程序。如下所示

192.168.3.101 192.168.3.102 192.168.3.103

将redis-4.0.10.tar.gz复制到每一个文件夹中,运行命令进行解压。以redis_101_01为例

  1.  
    [root@localhost redis_101_01]# tar -xzvf redis-4.0.10.tar.gz
  2.  
    [root@localhost redis_101_01]# cd redis-4.0.10/src/
  3.  
    [root@localhost src]# make MALLOC=libc

将刚才的配置文件“redis_conf”分别按照1为主 2为从的规划放置在文件夹中

2、准备Ruby环境,安装RVM

  1.  
    $ gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
  2.  
    $ curl -sSL https://get.rvm.io | bash -s stable
  3.  
    $ source ~/.bashrc
  4.  
    $ source ~/.bash_profile

列出已知的 Ruby 版本

rvm list known

安装一个 Ruby 版本

rvm install 2.5.1 --disable-binary

3、安装redis插件

gem install redis

看到如下提示则成功

第四步 : 启动redis实例

在不同的机器上分别执行下面命令 101机器就执行 101_01 101_02的命令

  1.  
    [root@localhost redis-cluster]# ./redis_101_01/redis-4.0.10/src/redis-server ./redis_101_01/redis.conf
  2.  
    [root@localhost redis-cluster]# ./redis_101_02/redis-4.0.10/src/redis-server ./redis_101_02/redis.conf
  3.  
    [root@localhost redis-cluster]# ./redis_102_01/redis-4.0.10/src/redis-server ./redis_102_01/redis.conf
  4.  
    [root@localhost redis-cluster]# ./redis_102_02/redis-4.0.10/src/redis-server ./redis_102_02/redis.conf
  5.  
    [root@localhost redis-cluster]# ./redis_103_01/redis-4.0.10/src/redis-server ./redis_103_01/redis.conf
  6.  
    [root@localhost redis-cluster]# ./redis_103_02/redis-4.0.10/src/redis-server ./redis_103_02/redis.conf

第五步: 关闭linux防火墙

  1.  
    systemctl stop firewalld.service #停止firewall
  2.  
    systemctl disable firewalld.service #禁止firewall开机启动

第六步 : 启动redis集群

./redis-trib.rb create --replicas 1 192.168.3.101:6379 192.168.3.101:6380 192.168.3.102:6379 192.168.3.102:6380 192.168.3.103:6379 192.168.3.103:6380

测试一下

102机器上

这个时候如果在其他机器上去获取这个key 会报错

这是因为这个值已经放在102槽中,

怎么办,我们可以使用 ./redis-cli -c方式查找

可以看到该值位于102

原文地址:https://www.cnblogs.com/michaelwang2018/p/9606187.html