# 三台主机,IP地址分别为 192.168.60.57 192.168.60.91 192.168.60.94 # 很明显,三个主机是在同一个局域网络里面,当然你可以设置在不同的网络里面,只要能互相访问就行了
sudo apt-get install redis-server [email protected]:~$ ps -aux|grep redis redis 5960 0.0 0.4 44044 4500 ? Ssl 18:18 0:00 /usr/bin/redis-server 127.0.0.1:6379 hb2 6247 0.0 0.0 15940 924 pts/0 S+ 18:25 0:00 grep --color=auto redis # 三台设备都要安装好
配置 192.168.60.91 [email protected]:~$ sudo vim /etc/redis/redis.conf
这里也就是将bind 绑定为自己本机的IP地址(如果是同一台电脑上要修改端口)
,设置好主服务的IP地址
在192.168.60.91这台电脑上创建三个文件 7001.conf 7002.conf 7003.conf这个名字无所谓,随便写
port 7001 bind 192.168.60.91 daemonize yes pidfile 7001.pid cluster-enabled yes cluster-config-file 7001_node.conf cluster-node-timeout 15000 appendonly yes
启动三个服务 sudo redis-server ./7001.conf sudo redis-server ./7002.conf sudo redis-server ./7003.conf [email protected]:~/Desktop/conf$ ps -aux|grep redis hb2 33057 0.0 0.7 57364 7712 ? Ssl 00:36 0:01 redis-server 192.168.60.91:7001 [cluster] hb2 33069 0.0 0.7 57364 7716 ? Ssl 00:37 0:01 redis-server 192.168.60.91:7002 [cluster] hb2 33074 0.0 0.4 57364 4620 ? Ssl 00:37 0:01 redis-server 192.168.60.91:7003 [cluster] hb2 33139 0.0 0.0 15940 920 pts/23 S+ 01:06 0:00 grep --color=auto redis 同样,另外一台电脑也这样启动,使用ps -aux|grep redis看到服务已经启动就可以了
我们可以看到后面的大括号里面显示集群,同样另外一台电脑也要相同的配置好并运行起来
介绍 下面的命令就是分别将两台电脑上的cluster联系起来,形成一个整体的集群系统
redis-cli --cluster create 192.168.60.91:7002 192.168.60.91:7003 192.168.60.91:7001 192.168.60.94:7000 192.168.60.94:7001 192.168.60.94:7002
这个命令是最新,很久之前是用ruby,现在可以不用他了
>>> Performing hash slots allocation on 6 nodes... #这个是每个节点槽节点的范围,以后数据会根据一定条件存储在下面这些槽节点中 Master[0] -> Slots 0 - 2730 Master[1] -> Slots 2731 - 5460 Master[2] -> Slots 5461 - 8191 Master[3] -> Slots 8192 - 10922 Master[4] -> Slots 10923 - 13652 Master[5] -> Slots 13653 - 16383 M: 3ac33dd385d9c3293e298afd85813e649d6b4aa7 192.168.60.91:7002 slots:[0-2730] (2731 slots) master M: 360bc13a876ffee58e673f7a119135e3977511e8 192.168.60.91:7003 slots:[5461-8191] (2731 slots) master M: 2715502ab3b3b111f9538af8e856a03ceae32952 192.168.60.91:7001 slots:[10923-13652] (2730 slots) master M: 8cb62ce72e09dd28cc0027a788a23cd12375d6f3 192.168.60.94:7000 slots:[2731-5460] (2730 slots) master M: 3e94ccbed1a251059f328026300c79384fc940ab 192.168.60.94:7001 slots:[8192-10922] (2731 slots) master M: 8c953a9cb0ada80e24190ccd56a6477bf2b8cc50 192.168.60.94:7002 slots:[13653-16383] (2731 slots) master 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.60.91:7002) M: 3ac33dd385d9c3293e298afd85813e649d6b4aa7 192.168.60.91:7002 slots:[0-2730] (2731 slots) master M: 2715502ab3b3b111f9538af8e856a03ceae32952 192.168.60.91:7001 slots:[10923-13652] (2730 slots) master M: 3e94ccbed1a251059f328026300c79384fc940ab 192.168.60.94:7001 slots:[8192-10922] (2731 slots) master M: 8c953a9cb0ada80e24190ccd56a6477bf2b8cc50 192.168.60.94:7002 slots:[13653-16383] (2731 slots) master M: 8cb62ce72e09dd28cc0027a788a23cd12375d6f3 192.168.60.94:7000 slots:[2731-5460] (2730 slots) master M: 360bc13a876ffee58e673f7a119135e3977511e8 192.168.60.91:7003 slots:[5461-8191] (2731 slots) master [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
[email protected]:~/redis-5.0.3/src$ redis-cli -h 192.168.60.91 -c -p 7002 192.168.60.91:7002> set name "zhangsan" -> Redirected to slot [5798] located at 192.168.60.91:7003 OK 192.168.60.91:7003> set age 500 -> Redirected to slot [741] located at 192.168.60.91:7002 OK 192.168.60.91:7002> set ag 90000 # 如果经过计算不是这个槽结点,会跳转到另外一个节点当中 -> Redirected to slot [9306] located at 192.168.60.94:7001 OK 192.168.60.94:7001>