用一个虚拟机在短时间内搭建一个伪Redis集群。

1.安装Redis不再过多赘述。
2.创建一个redis-cluster目录、在该目录下创建8001、8002、8003、8004、8005、8006目录用于存放redis的配置。

   mkdir -p redis-cluster/8001 redis-cluster/8002 redis-cluster/8003 redis-cluster/8004 redis-cluster/8005 redis-cluster/8006

3.移动Redis.conf 到8001下面,如果不知道自己的Redis配置在哪里,可以通过 sudo find / -name redis.conf查询其所在位置。

  sudo cp /etc/redis/redis.conf /var/www/2020/redis-cluster/8001/  

4.编辑Redis.conf文件

  sudo vi /var/www/2020/redis-cluster/8001/Redis.conf
  编辑以下几项内容:
  1.daemonize yes (Redis 开启守护线程的方式启动)
  2.port 8001 (分别对应没个机器的端口进行设置)
  3.bind 192.168.33.11(必须要绑定当前机器的IP,不绑定可能会出现循环查找集群节点机器的情况)
  4.dir /var/www/2020/redis-cluster/8001/ (指定数据要存放的地方,唯一性)
  5.cluster-enabled yes (启动集群模式)
  6.cluster-config-file nodes-8001.conf(这里800X最好和port对应)
  7.cluster-node-timeout 5000(集群通信超时设置)
  8.appendonly yes (开启日志)

5.把修改后的配置文件分贝copy到8002、8003、8004、8005、8006下面
注意2,4,6项的端口要和对应的目录一样,可以用批量替换。

sudo vi /var/www/2020/redis-cluster/8001/Redis.conf
:%s/源字符串/目的字符串/g
替换每一个配置文件

6.Redis -v3.0 之后提供了一个ruby命令创建集群,我们安装一下。

  sudo apt-get install ruby
  sudo apt-get install rubygems
  #安装reids和ruby的接口
  sudo gem install redis --version 3.0.0 (你的redis版本)
  

7.启动一下6个Redis实例,并验证时候启动成功

vagrant@nz2020:~$ sudo /usr/bin/redis-server /var/www/2020/redis-cluster/8001/redis.conf
vagrant@nz2020:~$ sudo /usr/bin/redis-server /var/www/2020/redis-cluster/8002/redis.conf
vagrant@nz2020:~$ sudo /usr/bin/redis-server /var/www/2020/redis-cluster/8003/redis.conf
vagrant@nz2020:~$ sudo /usr/bin/redis-server /var/www/2020/redis-cluster/8004/redis.conf
vagrant@nz2020:~$ sudo /usr/bin/redis-server /var/www/2020/redis-cluster/8005/redis.conf
vagrant@nz2020:~$ sudo /usr/bin/redis-server /var/www/2020/redis-cluster/8006/redis.conf
vagrant@nz2020:~$ ps -ef|grep redis
redis     1250     1  0 11:01 ?        00:00:00 /usr/bin/redis-server 127.0.0.1:6379
root      2109     1  0 11:02 ?        00:00:00 /usr/bin/redis-server 192.168.33.11:8001 [cluster]
root      2114     1  0 11:02 ?        00:00:00 /usr/bin/redis-server 192.168.33.11:8002 [cluster]
root      2119     1  0 11:02 ?        00:00:00 /usr/bin/redis-server 192.168.33.11:8003 [cluster]
root      2124     1  0 11:02 ?        00:00:00 /usr/bin/redis-server 192.168.33.11:8004 [cluster]
root      2129     1  0 11:02 ?        00:00:00 /usr/bin/redis-server 192.168.33.11:8005 [cluster]
root      2134     1  0 11:02 ?        00:00:00 /usr/bin/redis-server 192.168.33.11:8006 [cluster]
vagrant   2138  2063  0 11:02 pts/0    00:00:00 grep --color=auto redis

8.查找一下刚安装的redis-trib.rb脚本,我们进到该文件所对应的目录,创建集群。

vagrant@nz2020:~$ sudo find / -name redis-trib.rb
/usr/share/doc/redis-tools/examples/redis-trib.rb
vagrant@nz2020:~$ cd /usr/share/doc/redis-tools/examples/
#创建集群说明: 
./redis-trib.rb  create --replicas  (Masters/Slave的比) 集群的IP:端口  集群的IP:端口  集群的IP:端口  
比如我这里创建的是3个Masters、3个Slave,那么就是比例就是1。
而且按顺序前3个实例为主,后三个实例为从,第一个主对应第一个从,以此类推。
vagrant@nz2020:/usr/share/doc/redis-tools/examples$ ./redis-trib.rb create --replicas 1 192.168.33.11:8001 192.168.33.11:8002 192.168.33.11:8003 192.168.33.11:8004 192.168.33.11:8005 192.168.33.11:8006
./redis-trib.rb:1573: warning: key "threshold" is duplicated and overwritten on line 1573
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.33.11:8001
192.168.33.11:8002
192.168.33.11:8003
Adding replica 192.168.33.11:8004 to 192.168.33.11:8001
Adding replica 192.168.33.11:8005 to 192.168.33.11:8002
Adding replica 192.168.33.11:8006 to 192.168.33.11:8003
M: 9b577939acafde6a6efbebfea60e499fdecda596 192.168.33.11:8001
   slots:0-5460 (5461 slots) master
M: 67e2dc5ef3684b9ed8dff8e88d563be120fab11d 192.168.33.11:8002
   slots:5461-10922 (5462 slots) master
M: ee8f93ccb529ac5351aadeb8a3e7f6e12ac4293d 192.168.33.11:8003
   slots:10923-16383 (5461 slots) master
S: 109c1318a7a6f04d7dc372bf3e096bf569d7c97c 192.168.33.11:8004
   replicates 9b577939acafde6a6efbebfea60e499fdecda596
S: 4c984c2644f58838b1568a5be9893c56e2075ed4 192.168.33.11:8005
   replicates 67e2dc5ef3684b9ed8dff8e88d563be120fab11d
S: b55cd10c4114a61931617f021cf0a01ceddc549a 192.168.33.11:8006
   replicates ee8f93ccb529ac5351aadeb8a3e7f6e12ac4293d
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.33.11:8001)
M: 9b577939acafde6a6efbebfea60e499fdecda596 192.168.33.11:8001
   slots:0-5460 (5461 slots) master
M: 67e2dc5ef3684b9ed8dff8e88d563be120fab11d 192.168.33.11:8002
   slots:5461-10922 (5462 slots) master
M: ee8f93ccb529ac5351aadeb8a3e7f6e12ac4293d 192.168.33.11:8003
   slots:10923-16383 (5461 slots) master
M: 109c1318a7a6f04d7dc372bf3e096bf569d7c97c 192.168.33.11:8004
   slots: (0 slots) master
   replicates 9b577939acafde6a6efbebfea60e499fdecda596
M: 4c984c2644f58838b1568a5be9893c56e2075ed4 192.168.33.11:8005
   slots: (0 slots) master
   replicates 67e2dc5ef3684b9ed8dff8e88d563be120fab11d
M: b55cd10c4114a61931617f021cf0a01ceddc549a 192.168.33.11:8006
   slots: (0 slots) master
   replicates ee8f93ccb529ac5351aadeb8a3e7f6e12ac4293d
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

9.验证集群,我们只需要进入一个redis实例即可验证

  usr/share/doc/redis-tools/examples$ /usr/bin/redis-cli -c -h 192.168.33.11 -p 8001
  192.168.33.11:8001> cluster info(查看状态)
  cluster_state:ok
  cluster_slots_assigned:16384
  cluster_slots_ok:16384
  cluster_slots_pfail:0
  cluster_slots_fail:0
  cluster_known_nodes:6  (一共6个节点)
  cluster_size:3         (主节点三个)
  cluster_current_epoch:6
  cluster_my_epoch:1
  cluster_stats_messages_sent:1068
  cluster_stats_messages_received:1068
  #查看详细信息 一一对应
  192.168.33.11:8001> cluster nodes
  b55cd10c4114a61931617f021cf0a01ceddc549a 192.168.33.11:8006 slave ee8f93ccb529ac5351aadeb8a3e7f6e12ac4293d 0 1585225764477 6 connected
  ee8f93ccb529ac5351aadeb8a3e7f6e12ac4293d 192.168.33.11:8003 master - 0 1585225765485 3 connected 10923-16383
  109c1318a7a6f04d7dc372bf3e096bf569d7c97c 192.168.33.11:8004 slave 9b577939acafde6a6efbebfea60e499fdecda596 0 1585225765485 4 connected
  4c984c2644f58838b1568a5be9893c56e2075ed4 192.168.33.11:8005 slave 67e2dc5ef3684b9ed8dff8e88d563be120fab11d 0 1585225764477 5 connected
  67e2dc5ef3684b9ed8dff8e88d563be120fab11d 192.168.33.11:8002 master - 0 1585225764980 2 connected 5461-10922
  9b577939acafde6a6efbebfea60e499fdecda596 192.168.33.11:8001 myself,master - 0 0 1 connected 0-5460   (我当前连接的myself,master)
  # 验证结果
  192.168.33.11:8002> set name1 niuzheng2
  -> Redirected to slot [12933] located at 192.168.33.11:8003
  OK
  192.168.33.11:8003> set name2 niuzheng3
  -> Redirected to slot [742] located at 192.168.33.11:8001
  OK
  192.168.33.11:8001> get name2
  "niuzheng3"
  192.168.33.11:8001>
Last modification:March 27th, 2020 at 07:36 am
如果觉得我的文章对你有用,请随意赞赏