redis集群配置详解(redis集群模式搭建)

redis集群模式搭建

我们在centos上的一台机器上搭建redis 集群模式

  1. 第一步,首先安装redis, 这里安装版本5.0.14

#下载源码到soft目录[root@VM-16-12-centos root]# cd soft[root@VM-16-12-centos soft]# wget https://download.redis.io/releases/redis-5.0.14.tar.gz[root@VM-16-12-centos soft]# ls -l red*-rw-r--r-- 1 root root 2000179 Oct 4 19:08 redis-5.0.14.tar.gz#解压[root@VM-16-12-centos soft]# tar -xvf redis-5.0.14.tar.gz #进行编译[root@VM-16-12-centos soft]# cd redis-5.0.14/[root@VM-16-12-centos redis-5.0.14]# make #安装[root@VM-16-12-centos redis-5.0.14]# make install#安装完成后,默认会安装在下面目录/usr/local/bin/,[root@VM-16-12-centos bin]# ls -l /usr/local/bin/redis*-rwxr-xr-x 1 root root 4367048 Nov 20 21:21 /usr/local/bin/redis-benchmark-rwxr-xr-x 1 root root 8153048 Nov 20 21:21 /usr/local/bin/redis-check-aof-rwxr-xr-x 1 root root 8153048 Nov 20 21:21 /usr/local/bin/redis-check-rdb-rwxr-xr-x 1 root root 4808280 Nov 20 21:21 /usr/local/bin/redis-clilrwxrwxrwx 1 root root 12 Nov 20 21:21 /usr/local/bin/redis-sentinel -> redis-server-rwxr-xr-x 1 root root 8153048 Nov 20 21:21 /usr/local/bin/redis-server#但是配置文件需要我们自己拷贝到一个目录,这里我们先拷贝 /usr/local/etc/redis下[root@VM-16-12-centos ~]# cd /usr/local/etc[root@VM-16-12-centos etc]# mkdir redis[root@VM-16-12-centos etc]# cd redis[root@VM-16-12-centos redis]# cp /root/soft/redis-5.0.14/redis.conf ./#至此安装完成,我们启动下redis-server,启动日志如下,默认当前为前台启动,CRT C后就自动退出[root@VM-16-12-centos ~]# redis-server /usr/local/etc/redis/redis.conf 下面说明集群模式搭建流程,搭建的集群架构如下,这里只画了4个节点,其实有6个节点

redis集群配置详解(redis集群模式搭建)(1)

  1. 集群模式会启动多个redis-server实例,要求mater只是三个节点,每个master需要一个备份节点,因此至少需要6个节点。 因此首先创建6个目录来存放每个节点的数据和配置文件

#进入到redis配置文件存放目录[root@VM-16-12-centos ~]# cd /usr/local/etc/redis#创建目录,node后面的数字表示服务端端口号[root@VM-16-12-centos redis]# mkdir node638{0,1,2,3,4,5}#拷贝redis.conf 到node6380目录[root@VM-16-12-centos redis]# cp redis.conf node6380[root@VM-16-12-centos redis]# cd node6380#修改配置文件的内容[root@VM-16-12-centos node6380]# vim redis.conf#需要修改如下配置,改完后如下:#----------------------------------------------#后台模式运行daemonize yes#服务端端口port 6380#保护模式关闭protected-mode no#注释bind 127.0.0.1,开启绑定所有IP,否则外部IP无法连接#bind 127.0.0.1#数据存储目录修改dir /usr/local/etc/redis/node6380#卡其集群模式cluster-enabled yes#集群配置文件,这个配置文件是由redis自动维护的,不是我们手动维护的cluster-config-file nodes-6380.conf#开启AOF持久化模式appendonly yes#设置密码验证,主从同步需要开启。密码需要移植requirepass 111111masterauth 111111

  1. 经过1步骤操作后,node6380 就配置好了,接下来配置其它节点配置文件

#复制6380下redis.conf到其它几个目录[root@VM-16-12-centos node6380]# cp redis.conf ../node6380[root@VM-16-12-centos node6380]# cp redis.conf ../node6381[root@VM-16-12-centos node6380]# cp redis.conf ../node6382[root@VM-16-12-centos node6380]# cp redis.conf ../node6383[root@VM-16-12-centos node6380]# cp redis.conf ../node6384[root@VM-16-12-centos node6380]# cp redis.conf ../node6385#接下来分别进入到其它几个目录,分别修改如下几个配置,以node6381修改为例,其它类似修改即可。#服务端端口port 6381#数据存储目录修改dir /usr/local/etc/redis/node6381#集群配置文件,这个配置文件是由redis自动维护的,不是我们手动维护的cluster-config-file nodes-6381.conf

  1. 配置文件已经全部配置好,然后分别启动6个实例。[root@VM-16-12-centos ~]

redis-server /usr/local/etc/redis/node6380/redis.conf[root@VM-16-12-centos ~]redis-server /usr/local/etc/redis/node6381/redis.conf[root@VM-16-12-centos ~]redis-server /usr/local/etc/redis/node6382/redis.conf[root@VM-16-12-centos ~]redis-server /usr/local/etc/redis/node6383/redis.conf[root@VM-16-12-centos ~]redis-server /usr/local/etc/redis/node6384/redis.conf[root@VM-16-12-centos ~]redis-server /usr/local/etc/redis/node6385/redis.conf[root@VM-16-12-centos redis]# ps -ef|grep redisroot 10859 1 0 13:23 ? 00:00:01 redis-server 127.0.0.1:6380 [cluster]root 10864 1 0 13:23 ? 00:00:01 redis-server 127.0.0.1:6381 [cluster]root 10869 1 0 13:23 ? 00:00:01 redis-server 127.0.0.1:6382 [cluster]root 10875 1 0 13:23 ? 00:00:01 redis-server 127.0.0.1:6383 [cluster]root 14828 1 0 13:44 ? 00:00:00 redis-server 127.0.0.1:6384 [cluster]root 14869 1 0 13:44 ? 00:00:00 redis-server 127.0.0.1:6385 [cluster]

  1. 通过命令创建集群,4版本redis需要通过 redis-trib.rb 脚本创建,5以后就不要了。直接使用redis-cli即可以了

# cluster-replicas副本数为1表示每个master一个副本,设置后 redis会自动计算技术出mater节点个数redis-cli -a 111111 --cluster create localhost:6380 localhost:6381 localhost:6382 localhost:6383 localhost:6384 localhost:6385 --cluster-replicas 1#显示以下日志表示创建成功Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.>>> Performing hash slots allocation on 6 nodes...Master[0] -> Slots 0 - 5460Master[1] -> Slots 5461 - 10922Master[2] -> Slots 10923 - 16383Adding replica localhost:6384 to localhost:6380Adding replica localhost:6385 to localhost:6381Adding replica localhost:6383 to localhost:6382>>> Trying to optimize slaves allocation for anti-affinity[WARNING] Some slaves are in the same host as their masterM: b795b8405fe8766291c5cda7bd0d2dde75fa41d4 localhost:6380slots:[0-5460] (5461 slots) masterM: 434cc24b52276b7d5cbd8b0e50679194a86fbe5b localhost:6381slots:[5461-10922] (5462 slots) masterM: d01a9a365a0d940c8659a0494756a6ae8fcb1f32 localhost:6382slots:[10923-16383] (5461 slots) masterS: 692146649876582d075a7721c7a930f490cdc06f localhost:6383replicates b795b8405fe8766291c5cda7bd0d2dde75fa41d4S: 692146649876582d075a7721c7a930f490cdc06f localhost:6384replicates 434cc24b52276b7d5cbd8b0e50679194a86fbe5bS: 692146649876582d075a7721c7a930f490cdc06f localhost:6385replicates d01a9a365a0d940c8659a0494756a6ae8fcb1f32Can I set the above configuration? (type 'yes' to accept)

#此处需要输入yes

  1. 测试下是否搭建好,连主节点 上面日志 M 表示主节点

redis-cli -a 111111 -p 6380

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页