redis 5 集群实验
2019-03-25 本文已影响5人
Gswu
基础环境:
vmware虚拟机2台,每台开3个redis进程。模拟6个节点3主3备。
yum install -y net-tools wget gcc gcc-c++
cd /tmp
wget[http://120.221.34.83:6510/download.redis.io/releases/redis-5.0.4.tar.gz](http://120.221.34.83:6510/download.redis.io/releases/redis-5.0.4.tar.gz)
tar zxvf redis-5.0.4.tar.gz
cd redis.-5.0.4
make
make install
cd utils
./install_server.sh
image
开始配置
cd /etc/redis
cp 6379.conf 7000.conf
vi 7000.conf
#bind 127.0.0.1 # 不限定只监听本机
port 7000 #设置端口
protected-mode no #关闭保护模式,运行远程连接
daemonize yes #运行后台守护进程方式运行
pidfile /var/run/redis_7000.pid #一定要根据进程不同分开
logfile /var/log/redis_7000.log #最好也根据进程不同分开
dir /var/lib/redis/7000
cluster-enabled yes
cluster-config-file nodes_7000.conf
cluster-node-timeout 5000
appendonly yes
#依次建立7001.conf 和7002.conf
cd /var/lib/redis
mkdir 7000 7001 7002
#需要关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
然后复制一台相同的虚拟机,IP设置为不同
两台机器开机,分别开启三个redis-server,查看进程开启情况:
下面是一台的:
[root@192 ~]# redis-server /etc/redis/7000.conf
[root@192 ~]# redis-server /etc/redis/7001.conf
[root@192 ~]# redis-server /etc/redis/7002.conf
[root@192 ~]# ps aux | grep redis
root 6839 0.2 0.7 153880 7656 ? Ssl 10:51 0:00 /usr/local/bin/redis-server 127.0.0.1:6379
root 7066 0.2 0.8 156440 8208 ? Ssl 10:53 0:00 redis-server *:7000 [cluster]
root 7071 0.2 1.2 162584 12292 ? Ssl 10:53 0:00 redis-server *:7001 [cluster]
root 7076 0.2 0.8 156440 8212 ? Ssl 10:53 0:00 redis-server *:7002 [cluster]
root 7082 0.0 0.0 112708 980 pts/0 R+ 10:54 0:00 grep --color=auto redis
[root@192 ~]#
在一台上运行以下命令:(redis5.0 以上职称redis -cli直接建集群)
redis-cli --cluster create 192.168.3.131:7000 192.168.3.131:7001 \
192.168.3.131:7002 192.168.3.132:7000 192.168.3.132:7001 192.168.3.132:7002 \
--cluster-replicas 1
会出现以下信息,代表建立集群成功,且会显示集群中各node的角色:
[root@192 ~]# redis-cli --cluster create 192.168.3.131:7000 192.168.3.131:7001 \
> 192.168.3.131:7002 192.168.3.132:7000 192.168.3.132:7001 192.168.3.132:7002 \
> --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.3.132:7002 to 192.168.3.131:7000
Adding replica 192.168.3.131:7002 to 192.168.3.132:7000
Adding replica 192.168.3.132:7001 to 192.168.3.131:7001
M: d398ace1b958cc40e1b120c0bc76ebdbc0788e57 192.168.3.131:7000
slots:[0-5460] (5461 slots) master
M: aeb2caaa4d4be0e27079387f33681249f73d8ca7 192.168.3.131:7001
slots:[10923-16383] (5461 slots) master
S: 0236063723676cd0110536b430cef2dbd13462c5 192.168.3.131:7002
replicates 05bad1b8a58e3a321c6107e0418a61aff7d845d6
M: 05bad1b8a58e3a321c6107e0418a61aff7d845d6 192.168.3.132:7000
slots:[5461-10922] (5462 slots) master
S: 98ded94af7b8af77e2e1fa886ce89f422df1e7dd 192.168.3.132:7001
replicates aeb2caaa4d4be0e27079387f33681249f73d8ca7
S: 57ab71d60585efcbf802dbc7db965599a4930757 192.168.3.132:7002
replicates d398ace1b958cc40e1b120c0bc76ebdbc0788e57
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.3.131:7000)
M: d398ace1b958cc40e1b120c0bc76ebdbc0788e57 192.168.3.131:7000
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 98ded94af7b8af77e2e1fa886ce89f422df1e7dd 192.168.3.132:7001
slots: (0 slots) slave
replicates aeb2caaa4d4be0e27079387f33681249f73d8ca7
M: aeb2caaa4d4be0e27079387f33681249f73d8ca7 192.168.3.131:7001
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 57ab71d60585efcbf802dbc7db965599a4930757 192.168.3.132:7002
slots: (0 slots) slave
replicates d398ace1b958cc40e1b120c0bc76ebdbc0788e57
S: 0236063723676cd0110536b430cef2dbd13462c5 192.168.3.131:7002
slots: (0 slots) slave
replicates 05bad1b8a58e3a321c6107e0418a61aff7d845d6
M: 05bad1b8a58e3a321c6107e0418a61aff7d845d6 192.168.3.132:7000
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
连接验证
连接集群模式,使用redis-cli 加 -c 选项,要不然会报错:(error) MOVED 5798
[root@192 ~]# redis-cli -h 192.168.3.131 -p 7000
#没有加-c选项,提示错误!
192.168.3.131:7000> set name gswu001
(error) MOVED 5798 192.168.3.132:7000
192.168.3.131:7000> quit
[root@192 ~]# redis-cli -c -h 192.168.3.131 -p 7000
192.168.3.131:7000> set name gswu001
#添加一条数据
-> Redirected to slot [5798] located at 192.168.3.132:7000
OK
192.168.3.132:7000> quit
#连接另一个node验证,出现了redirected!!
[root@192 ~]# redis-cli -c -h 192.168.3.131 -p 7001
192.168.3.131:7001> get name
-> Redirected to slot [5798] located at 192.168.3.132:7000
"gswu001"
192.168.3.132:7000>
#再连接另一台虚拟机上的测试一下:
[root@192 ~]# redis-cli -c -h 192.168.3.132 -p 7002
192.168.3.132:7002> get name
-> Redirected to slot [5798] located at 192.168.3.132:7000
"gswu001"
192.168.3.132:7000>