这篇文章主要介绍“集群的搭建步骤是什么”,在日常操作中,相信很多人在集群的搭建步骤是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”集群的搭建步骤是什么”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
下载redis压缩包,然后解压压缩文件,我们统一将Redis下载在/opt目录下
进入到解压缩后的redis文件目录(此时可以看到Makefile文件),编译redis源文件;
执行命令如下
$ cd /opt $ wget http://download.redis.io/releases/redis-4.0.9.tar.gz $ tar zxvf redis-4.0.9.tar.gz $ cd redis-4.0.9 $ make $ make install
把编译好的redis源文件安装到/usr/local/redis目录下,如果/local目录下没有redis目录,会自动新建redis目录;
然后修改该$ ./src/redis-server redis.conf文件->daemonize:no 改为daemonize:yes;
进入/usr/local/redis/bin目录,直接./redis-server启动redis(此时为前端启动redis);
注意,由于Redis的保护模式,只绑定了本机的127.0.0.1,从其他机器是不能访问的。所以我们需要添加本机的ip
192.168.xxx.xxx。
在/bin目录下通过./redis-server redis.conf启动redis(此时为后台启动)。
$ cd /opt/redis-4.0.9 $ ./src/redis-server .redis.conf
根据官方推荐,集群部署至少要 3 台以上的master节点,最好使用 3 主 3 从六个节点的模式。
咱们准备 6 个配置文件 ,端口 7001,7002,7003,7004,7005,7006
并在redis-cluster目录下创建6个节点的配置文件。分别为:
redis-7000.conf redis-7001.conf redis-7002.conf redis-7003.conf redis-7004.conf redis-7005.conf
后面的7000,7001等是redis启动的端口号。接下来编辑文件的内容:
#该集群阶段的端口 port 7000 #为每一个集群节点指定一个pid_file pidfile /var/run/redis_7000.pid #在bind指令后添加本机的ip bind 127.0.0.1 149.28.37.147 #找到Cluster配置的代码段,使得Redis支持集群 cluster-enabled yes #每一个集群节点都有一个配置文件,这个文件是不能手动编辑的。确保每一个集群节点的配置文件不通 cluster-config-file nodes-7000.conf #集群节点的超时时间,单位:ms,超时后集群会认为该节点失败 cluster-node-timeout 5000 #最后将appendonly改成yes appendonly yes
看以下启动情况
首先我们创建Redis的配置文件目录,如下:
$ cd /opt $ mkdir redis-cluster
这样一个节点的配置就完成,其他的5个节点也做同样的配置。并将6个节点的Redis实例启动:
$ nohup /opt/redis-4.0.9/src/redis-server /opt/redis-cluster/redis-7000.conf & $ nohup /opt/redis-4.0.9/src/redis-server /opt/redis-cluster/redis-7001.conf & $ nohup /opt/redis-4.0.9/src/redis-server /opt/redis-cluster/redis-7002.conf & $ nohup /opt/redis-4.0.9/src/redis-server /opt/redis-cluster/redis-7003.conf & $ nohup /opt/redis-4.0.9/src/redis-server /opt/redis-cluster/redis-7004.conf & $ nohup /opt/redis-4.0.9/src/redis-server /opt/redis-cluster/redis-7005.conf &
使用这6个节点创建集群:
$ /opt/redis-4.0.9/src/redis-trib.rb create --replicas 1 149.28.37.147:7000 149.28.37.147:7001 149.28.37.147:7002 149.28.37.147:7003 149.28.37.147:7004 149.28.37.147:7005
或
$ redis-cli --cluster create 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006 --cluste r-replicas 1
--replicas 1 表示我们希望为集群中的每个主节点创建一个从节点。
执行命令后会显示:
>>> Creating cluster >>> Performing hash slots allocation on 6 nodes... >>> Using 3 masters: >>> 149.28.37.147:7000 >>> 149.28.37.147:7001 >>> 149.28.37.147:7002 --------------------------------------------- # 执行成功结果如下 # 我们可以看到 7001,7002,7003 成为了 master 节点, # 分别占用了 slot [0-5460],[5461-10922],[10923-16383] >>> Performing hash slots allocation on 6 nodes... Master[0] -> Slots 0 - 5460 Master[1] -> Slots 5461 - 10922 Master[2] -> Slots 10923 - 16383 -------------------------------------------- >>> Adding replica 149.28.37.147:7004 to 149.28.37.147:7000 >>> Adding replica 149.28.37.147:7005 to 149.28.37.147:7001 >>> Adding replica 149.28.37.147:7003 to 149.28.37.147:7002 >>> Trying to optimize slaves allocation for anti-affinity >>> [WARNING] Some slaves are in the same host as their master >>> M: 65625091304b0fa2dd75a00f62b6aceac1701094 149.28.37.147:7000 >>> slots:0-5460 (5461 slots) master >>> M: 4da569bf8402e4f75ab6e0fe7076919c22e3f900 149.28.37.147:7001 >>> slots:5461-10922 (5462 slots) master >>> M: b977680e24f23f8fec96876d9014803ca752e2e2 149.28.37.147:7002 >>> slots:10923-16383 (5461 slots) master >>> S: 7183e47a64bca23157140229352455d1a1407dc2 149.28.37.147:7003 >>> replicates b977680e24f23f8fec96876d9014803ca752e2e2 >>> S: b2f916a643fefef1d43dbd1ef5d22f72c0ee43d6 149.28.37.147:7004 >>> replicates 65625091304b0fa2dd75a00f62b6aceac1701094 >>> S: e362d9aae5fe3e9c343d266a5ab952272e0e37b0 149.28.37.147:7005 >>> replicates 4da569bf8402e4f75ab6e0fe7076919c22e3f900 >>> 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 149.28.37.147:7000) >>> M: 65625091304b0fa2dd75a00f62b6aceac1701094 149.28.37.147:7000 >>> slots:0-5460 (5461 slots) master >>> 1 additional replica(s) >>> M: b977680e24f23f8fec96876d9014803ca752e2e2 149.28.37.147:7002 >>> slots:10923-16383 (5461 slots) master >>> 1 additional replica(s) >>> S: e362d9aae5fe3e9c343d266a5ab952272e0e37b0 149.28.37.147:7005 >>> slots: (0 slots) slave >>> replicates 4da569bf8402e4f75ab6e0fe7076919c22e3f900 >>> S: b2f916a643fefef1d43dbd1ef5d22f72c0ee43d6 149.28.37.147:7004 >>> slots: (0 slots) slave >>> replicates 65625091304b0fa2dd75a00f62b6aceac1701094 >>> M: 4da569bf8402e4f75ab6e0fe7076919c22e3f900 149.28.37.147:7001 >>> slots:5461-10922 (5462 slots) master >>> 1 additional replica(s) >>> S: 7183e47a64bca23157140229352455d1a1407dc2 149.28.37.147:7003 >>> slots: (0 slots) slave >>> replicates b977680e24f23f8fec96876d9014803ca752e2e2 >>> [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... >>> [OK] All 16384 slots covered. >>> 集群搭建完毕。我们可以使用Spring-Boot非常方便的去访问Redis集群了。
注意 集群模式下要带参数 -c,表示集群,否则不能正常存取数据!!!
[root@localhost redis-5.0.5]# redis-cli -p 7100 -c
127.0.0.1:7001> set k1 v1 -> Redirected to slot [12706] located at 127.0.0.1:7003 OK
127.0.0.1:7003> get k1 "v1"
[root@localhost redis-5.0.5]# redis-cli -p 7001 -c 127.0.0.1:7001> get k1 -> Redirected to slot [12706] located at 127.0.0.1:7003 "v1"
127.0.0.1:7003>
到此,关于“集群的搭建步骤是什么”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。