准备两台服务器,两台服务器可以互相ping通,可以新建两个虚拟机,然后配置网络,此处不在演示
准备两台虚拟机之后,两台虚拟机安装redis,gcc编译之后我们开始配置redis集群
服务器1:linux-master
服务器2:linux-slave01
linux-master服务器的redis.conf文件都放到redis/bin下
redis.conf文件:vim redis.conf
bind改为本机ip地址

linux-slave01配置文件新加一些东西,用来指定主节点和端口号(6379)


可以看到redis成功启动


我们搭建的是三个主节点,三个从节点的集群
主节点:
从节点:
同主从复制,先有两个服务器
[root@localhost redis-6.2.5]# mkdir redis_cluster
redis.conf文件复制到该文件夹下[root@localhost redis-6.2.5]# cp redis.conf /usr/local/redis-6.2.5/redis_cluster/redis-7001.conf
[root@localhost redis-6.2.5]# cp redis.conf /usr/local/redis-6.2.5/redis_cluster/redis-7002.conf
[root@localhost redis-6.2.5]# cp redis.conf /usr/local/redis-6.2.5/redis_cluster/redis-7003.conf
redis-7001.conf文件port 7004
daemonize yes
bind 192.168.230.1 #本机ip 集群配置不用指定主节点
pidfile /var/run/redis_7001.pid
cluster-enabled yes
cluster-config-file nodes-7005.conf
同理,7002、7003、7004、7005、7006一样的配置,无非改一下ip地址和端口号之类的
#主节点的机器
[root@localhost bin]# ./redis-server ../redis_cluster/redis-7001.conf
[root@localhost bin]# ./redis-server ../redis_cluster/redis-7002.conf
[root@localhost bin]# ./redis-server ../redis_cluster/redis-7003.conf
#从节点的机器
[root@localhost bin]# ./redis-server ../redis_cluster/redis-7004.conf
[root@localhost bin]# ./redis-server ../redis_cluster/redis-7005.conf
[root@localhost bin]# ./redis-server ../redis_cluster/redis-7006.conf
ps -ef | grep redis


[root@localhost bin]# ./redis-cli --cluster create 192.168.230.101:7001 192.168.230.101:7002 192.168.230.101:7003 --cluster-replicas 0
开启成功之后:

[root@localhost bin]#./redis-cli --cluster add-node 192.168.230.1:7004 192.168.230.101:7001 --cluster-slave --cluster-master-id 966a7429e95b40c38c96f651582a790ca4fb3817
192.168.230.1:7004从节点192.168.230.101:7001从节点指定的主节点,7001->7004966a7429e95b40c38c96f651582a790ca4fb3817主节点id同理,开启其他两个节点,对应关系为7002->7005,7003->7006
全部开启成功之后:

./redis-cli -h 192.168.230.101 -p 7001 -c
10.10.41.111:7001> cluster info #查看集群信息

10.10.41.111:7000> cluster nodes #查看节点信息

可以看到节点连接状况
[root@localhost bin]#./redis-cli --cluster create 1 192.168.230.101:7001 192.168.230.101:7002 192.168.230.101:7003 192.168.230.1:7004 192.168.230.1:7005 192.168.230.1:7006 --cluster-replicas 1
该命令会随机分配主节点和从节点


主节点写入:

可以看到,根据redis内置的算法,不同的键值被分到不同的槽位,有的是7004,有的7006有的7002,不过都是从节点的


集群搭建完毕