redis三主三从搭建(两台虚拟机)

先准备环境centos7
ip:192.168.20.181 192.168.20.182
1,安装依赖

yum -y install gcc gcc-c++ wget

2,创建目录

mkdir -p /redis/data

3,进入目录,下载redis安装包并解压和编译安装

cd /redis/data/
wget http://download.redis.io/releases/redis-5.0.2.tar.gz
tar zxf redis-5.0.2.tar.gz
make && make install

4,另一台机器上一样的操作,安装redis
开始集群搭建
我们这里搭建的是redis集群 三主三从,所以需要有3个master节点和3个slave节点,共需6个节点。
1 分别在两台服务器下创建cluster目录

mkdir -p /redis/cluster
cd /redis/cluster/
mkdir 7001 7002 7003
ls
7001  7002  7003

5,在另一台服务器上同样操作,创建7004,7005,7006
2,修改配置文件

daemonize yes
port 7001(分别对每个机器的端口号进行设置)
dir /redis/cluster/7001/(指定数据文件存放位置,必须要指定不同的目录位置,不然会丢失数据)
cluster-enabled yes(启动集群模式)
cluster-config-file nodes-7001.conf(集群节点信息文件,这里700x最好和port对应上)
cluster-node-timeout 5000
bind 192.168.20.181
protected-mode no (关闭保护模式)
appendonly yes
dbfilename dump_7002.rdb

3,将修改好的配置文件分别拷贝至7001-7003,修改响应的端口,文件名

cp /redis/data/redis-5.0.2/redis.conf /redis/data/redis-cluster/7001/
cp /redis/data/redis-5.0.2/redis.conf /redis/data/redis-cluster/7002/
cp /redis/data/redis-5.0.2/redis.conf /redis/data/redis-cluster/7003/

4,分别启动redis

cd /redis/redis-5.0.2/src/
./redis-server /redis/cluster/7001/redis.conf 
./redis-server /redis/cluster/7002/redis.conf 
./redis-server /redis/cluster/7003/redis.conf
ps -ef | grep redis
root      65904      1  0 11:50 ?        00:00:00 ./redis-server 192.168.20.181:7001 [cluster]
root      65909      1  0 11:51 ?        00:00:00 ./redis-server 192.168.20.181:7002 [cluster]
root      65915      1  0 11:51 ?        00:00:00 ./redis-server 192.168.20.181:7003 [cluster]
root      65928   5474  0 11:51 pts/0    00:00:00 grep --color=auto redis

5,关闭防火墙和selinux

systemctl stop firewalld.service 
setenforce 0

6,将181上的配置文件拷贝到182上

scp /redis/cluster/7001/redis.conf root@192.168.20.182:/redis/cluster/7004
scp /redis/cluster/7002/redis.conf root@192.168.20.182:/redis/cluster/7005
scp /redis/cluster/7003/redis.conf root@192.168.20.182:/redis/cluster/7006

7,安装ruby

[root@bogon ~]# yum -y install ruby
[root@bogon ~]# gem install redis
Fetching: redis-4.1.4.gem (100%)
ERROR:  Error installing redis:
	redis requires Ruby version >= 2.3.0

8,创建集群

[root@bogon ~]# /redis/redis-5.0.2/src/redis-cli --cluster create 192.168.20.181:7001 192.168.20.181:7002 192.168.20.181:7003 192.168.20.182:7004 192.168.20.182:7005 192.168.20.182:7006 --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.20.182:7005 to 192.168.20.181:7001
Adding replica 192.168.20.181:7003 to 192.168.20.182:7004
Adding replica 192.168.20.182:7006 to 192.168.20.181:7002
M: 8190ed94b58c8c0320ec8584ebba0ae6503199fb 192.168.20.181:7001
   slots:[0-5460] (5461 slots) master
M: c11233b8a36dc32b0a4de35a4f500acbddd99d08 192.168.20.181:7002
   slots:[10923-16383] (5461 slots) master
S: e09d9147ab92d73fb24cd9ad22c5e9a608594134 192.168.20.181:7003
   replicates d144cdc1c9c27c0b5935a1b3652ea6e9b78a93c3
M: d144cdc1c9c27c0b5935a1b3652ea6e9b78a93c3 192.168.20.182:7004
   slots:[5461-10922] (5462 slots) master
S: eefe3ac899aef116a01cc09d6eeda33ee4d92bd0 192.168.20.182:7005
   replicates 8190ed94b58c8c0320ec8584ebba0ae6503199fb
S: 764595d022ebfb64a8f46d05440053f34e91b508 192.168.20.182:7006
   replicates c11233b8a36dc32b0a4de35a4f500acbddd99d08
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.20.181:7001)
M: 8190ed94b58c8c0320ec8584ebba0ae6503199fb 192.168.20.181:7001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: c11233b8a36dc32b0a4de35a4f500acbddd99d08 192.168.20.181:7002
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: 764595d022ebfb64a8f46d05440053f34e91b508 192.168.20.182:7006
   slots: (0 slots) slave
   replicates c11233b8a36dc32b0a4de35a4f500acbddd99d08
M: d144cdc1c9c27c0b5935a1b3652ea6e9b78a93c3 192.168.20.182:7004
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: e09d9147ab92d73fb24cd9ad22c5e9a608594134 192.168.20.181:7003
   slots: (0 slots) slave
   replicates d144cdc1c9c27c0b5935a1b3652ea6e9b78a93c3
S: eefe3ac899aef116a01cc09d6eeda33ee4d92bd0 192.168.20.182:7005
   slots: (0 slots) slave
   replicates 8190ed94b58c8c0320ec8584ebba0ae6503199fb
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

9,测试

[root@bogon ~]# /redis/redis-5.0.2/src/redis-cli -h 192.168.20.181 -p 7001
192.168.20.181:7001> set bb 22
OK
192.168.20.181:7001> get bb
"22"
192.168.20.181:7001> 
[root@bogon src]# /redis/redis-5.0.2/src/redis-cli -c -h 192.168.20.182 -p 7006
192.168.20.182:7006> get bb
-> Redirected to slot [1180] located at 192.168.20.181:7001
"22"
192.168.20.181:7001> 

你可能感兴趣的:(redis三主三从搭建(两台虚拟机))