Redis 单机集群搭建与远程访问开启

开启远程访问

解决办法:修改 redis.conf 配置文件:

  1. 注释掉 bind 127.0.0.1
  2. protected-mode yes 改为 protected-mode no
  3. 重启服务 src/redis-server redis.conf &
(error) DENIED Redis is running in protected mode because protected mode is enabled, no bind address was specified, no authentication password is requested to clients. In this mode connections are only accepted from the loopback interface. If you want to connect from external computers to Redis you may adopt one of the following solutions: 1) Just disable protected mode sending the command 'CONFIG SET protected-mode no' from the loopback interface by connecting to Redis from the same host the server is running, however MAKE SURE Redis is not publicly accessible from internet if you do so. Use CONFIG REWRITE to make this change permanent. 2) Alternatively you can just disable the protected mode by editing the Redis configuration file, and setting the protected mode option to 'no', and then restarting the server. 3) If you started the server manually just for testing, restart it with the '--protected-mode no' option. 4) Setup a bind address or an authentication password. NOTE: You only need to do one of the above things in order for the server to start accepting connections from the outside.

搭建 Redis 单机集群

解决办法:修改 redis.config,添加 cluster-enabled yes

(error) ERR This instance has cluster support disabled
第一步:安装 Redis

Reids 安装包里有个集群工具,要复制到 /usr/local/bin 里去 cp redis-3.2.9/src/redis-trib.rb /usr/local/bin

第二步:修改配置,创建节点

我们现在要搞六个节点,三主三从,端口规定分别是 7001,7002,7003,7004,7005,7006

我们先在 root 目录下新建一个 redis_cluster 目录,然后该目录下再创建 6 个目录,分别是 7001,7002,7003,7004,7005,7006,用来存在 redis 配置文件;

这里我们要使用 redis 集群,要先修改 redis 的配置文件 redis.conf

[root@localhost ~]# mkdir redis_cluster 新建目录
[root@localhost ~]# cd redis_cluster/
[root@localhost redis_cluster]# mkdir 7001 7002 7003 7004 7005 7006
[root@localhost redis_cluster]# ll
总用量 0
drwxr-xr-x. 2 root root 6 727 17:18 7001
drwxr-xr-x. 2 root root 6 727 17:18 7002
drwxr-xr-x. 2 root root 6 727 17:18 7003
drwxr-xr-x. 2 root root 6 727 17:18 7004
drwxr-xr-x. 2 root root 6 727 17:18 7005
drwxr-xr-x. 2 root root 6 727 17:18 7006

先复制一份配置文件到 7001 目录下:

[root@localhost redis_cluster]# cd
[root@localhost ~]# cp redis-3.2.9/redis.conf redis_cluster/7001/

我们修改下这个配置文件

vim redis_cluster/7001/redis.conf

修改以下几个配置参数:

  • port 7001: 6 个节点配置文件分别是 7001-7006
  • daemonize yes: redis 后台运行
  • pidfile /var/run/redis_7001.pid : pidfile文件对应 7001-7006
  • cluster-enabled yes: 开启集群
  • cluster-config-file nodes_7001.conf: 保存节点配置,自动创建,自动更新对应 7001-7006
  • cluster-node-timeout 5000: 集群超时时间,节点超过这个时间没反应就断定是宕机
  • appendonly yes: 存储方式,aof,将写操作记录保存到日志中

7001 下的修改完后,我们把7001下的配置分别复制到 7002-7006 然后对应的再修改下配置即可, 编辑后面 5 个配置文件,把 port ,pidfile,cluster-config-file 分别修改下即可;

[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7002/
[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7003/
[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7004/
[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7005/
[root@localhost ~]# cp redis_cluster/7001/redis.conf redis_cluster/7006/
[root@localhost ~]# vim redis_cluster/7002/redis.conf 
[root@localhost ~]# vim redis_cluster/7003/redis.conf 
[root@localhost ~]# vim redis_cluster/7004/redis.conf 
[root@localhost ~]# vim redis_cluster/7005/redis.conf 
[root@localhost ~]# vim redis_cluster/7006/redis.conf 
第三步:启动 6 个节点的 redis
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7001/redis.conf 
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7002/redis.conf 
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7003/redis.conf 
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7004/redis.conf 
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7005/redis.conf 
[root@localhost ~]# /usr/local/redis/bin/redis-server redis_cluster/7006/redis.conf 

启动六个节点,查找下 redis 进程信息,显示以下信息,说明都启动成功了

[root@localhost ~]# ps -ef | grep redis  

root    9501    1   0   17:38   ?   00:00:00    /usr/local/redis/bin/redis-server 127.0.0.1:7001 [cluster]
root    9512    1   0   17:45   ?   00:00:00    /usr/local/redis/bin/redis-server 127.0.0.1:7002 [cluster]
root    9516    1   0   17:45   ?   00:00:00    /usr/local/redis/bin/redis-server 127.0.0.1:7003 [cluster]
root    9520    1   0   17:45   ?   00:00:00    /usr/local/redis/bin/redis-server 127.0.0.1:7004 [cluster]
root    9524    1   0   17:45   ?   00:00:00    /usr/local/redis/bin/redis-server 127.0.0.1:7005 [cluster]
root    9528    1   0   17:45   ?   00:00:00    /usr/local/redis/bin/redis-server 127.0.0.1:7006 [cluster]
第四步:创建集群

redis官方提供了 redis-trib.rb 工具,第一步里已经房到里 bin 下 ;

但是在使用之前 需要安装ruby,以及 redis 和 ruby 连接:

[root@localhost ~]# yum -y install ruby ruby-devel rubygems rpm-build
[root@localhost ~]# gem install redis

创建集群:

[root@localhost ~]# redis-trib.rb create --replicas 1  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
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7001
127.0.0.1:7002
127.0.0.1:7003
Adding replica 127.0.0.1:7004 to 127.0.0.1:7001
Adding replica 127.0.0.1:7005 to 127.0.0.1:7002
Adding replica 127.0.0.1:7006 to 127.0.0.1:7003
M: bfcfcdc304b011023fa568e044ea23ea6bc03c3c 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
M: d61e66e49e669b99d801f22f6461172696fdd1c9 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
M: aa6bc3f1e1174c3a991c01882584707c2408ec18 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
S: 7908a60306333c5d7c7c5e7ffef44bdf947ef0a4 127.0.0.1:7004
   replicates bfcfcdc304b011023fa568e044ea23ea6bc03c3c
S: 1d2341fd3b79ef0fccb8e3a052bba141337c6cdd 127.0.0.1:7005
   replicates d61e66e49e669b99d801f22f6461172696fdd1c9
S: f25b35f208dc96605ee4660994d2ac52f39ac870 127.0.0.1:7006
   replicates aa6bc3f1e1174c3a991c01882584707c2408ec18
Can I set the above configuration? (type 'yes' to accept): 

最后问我们是否接受上面的设置,输入yes 就表示接受,我们输入 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 127.0.0.1:7001)
M: bfcfcdc304b011023fa568e044ea23ea6bc03c3c 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: f25b35f208dc96605ee4660994d2ac52f39ac870 127.0.0.1:7006
   slots: (0 slots) slave
   replicates aa6bc3f1e1174c3a991c01882584707c2408ec18
M: d61e66e49e669b99d801f22f6461172696fdd1c9 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: 1d2341fd3b79ef0fccb8e3a052bba141337c6cdd 127.0.0.1:7005
   slots: (0 slots) slave
   replicates d61e66e49e669b99d801f22f6461172696fdd1c9
M: aa6bc3f1e1174c3a991c01882584707c2408ec18 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 7908a60306333c5d7c7c5e7ffef44bdf947ef0a4 127.0.0.1:7004
   slots: (0 slots) slave
   replicates bfcfcdc304b011023fa568e044ea23ea6bc03c3c
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

从运行结果看主节点就是 7001, 7002, 7003 从节点分别是 7004, 7005, 7006

7001 分配到的哈希槽是 0-5460

7002 分配到的哈希槽是 5461-10922

7003 分配到的哈希槽是 10923-16383

显示配置哈希槽,以及集群创建成功,可以用了。

第五步:集群数据测试**

我们先连接任意一个节点,然后添加一个 key:

redis-cli 是 redis 默认的客户端工具,启动时加上`-c`参数,-p指定端口,就可以连接到集群。

连接任意一个节点端口, 我们连接 7002:

[root@localhost ~]# /usr/local/redis/bin/redis-cli -c -p 7002
127.0.0.1:7002> set xxx  'fdafda'
-> Redirected to slot [4038] located at 127.0.0.1:7001
OK
127.0.0.1:7001> 

前面说过 Redis Cluster 值分配规则,所以分配 key 的时候,它会使用 CRC16(‘my_name’)%16384 算法,来计算,将这个 key 放到哪个节点,这里分配到了4038slot 就分配到了 7001(0-5460) 这个节点上。所以有:Redirected to slot [4038] located at 127.0.0.1:7001

我们从其他集群节点 ,都可以获取到数据

127.0.0.1:7001> exit
[root@localhost ~]# /usr/local/redis/bin/redis-cli -c -p 7005
127.0.0.1:7005> get xxx
-> Redirected to slot [4038] located at 127.0.0.1:7001
"fdafda"
127.0.0.1:7001> 
第六步:集群宕机测试

假如我们干掉一个节点,比如 7002 这个主节点

[root@localhost ~]#  ps -ef | grep redis
root       9501      1  0 17:38 ?        00:00:02 /usr/local/redis/bin/redis-server 127.0.0.1:7001 [cluster]
root       9512      1  0 17:45 ?        00:00:01 /usr/local/redis/bin/redis-server 127.0.0.1:7002 [cluster]
root       9516      1  0 17:45 ?        00:00:01 /usr/local/redis/bin/redis-server 127.0.0.1:7003 [cluster]
root       9520      1  0 17:45 ?        00:00:02 /usr/local/redis/bin/redis-server 127.0.0.1:7004 [cluster]
root       9524      1  0 17:45 ?        00:00:01 /usr/local/redis/bin/redis-server 127.0.0.1:7005 [cluster]
root       9528      1  0 17:45 ?        00:00:01 /usr/local/redis/bin/redis-server 127.0.0.1:7006 [cluster]
root       9601   2186  0 18:12 pts/0    00:00:00 grep --color=auto redis
[root@localhost ~]# kill -9 9512
[root@localhost ~]#  ps -ef | grep redis
root       9501      1  0 17:38 ?        00:00:02 /usr/local/redis/bin/redis-server 127.0.0.1:7001 [cluster]
root       9516      1  0 17:45 ?        00:00:01 /usr/local/redis/bin/redis-server 127.0.0.1:7003 [cluster]
root       9520      1  0 17:45 ?        00:00:02 /usr/local/redis/bin/redis-server 127.0.0.1:7004 [cluster]
root       9524      1  0 17:45 ?        00:00:01 /usr/local/redis/bin/redis-server 127.0.0.1:7005 [cluster]
root       9528      1  0 17:45 ?        00:00:01 /usr/local/redis/bin/redis-server 127.0.0.1:7006 [cluster]
root       9603   2186  0 18:12 pts/0    00:00:00 grep --color=auto redis

然后再来看下集群的情况

[root@localhost ~]# redis-trib.rb check 127.0.0.1:7001
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: bfcfcdc304b011023fa568e044ea23ea6bc03c3c 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: f25b35f208dc96605ee4660994d2ac52f39ac870 127.0.0.1:7006
   slots: (0 slots) slave
   replicates aa6bc3f1e1174c3a991c01882584707c2408ec18
M: 1d2341fd3b79ef0fccb8e3a052bba141337c6cdd 127.0.0.1:7005
   slots:5461-10922 (5462 slots) master
   0 additional replica(s)
M: aa6bc3f1e1174c3a991c01882584707c2408ec18 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 7908a60306333c5d7c7c5e7ffef44bdf947ef0a4 127.0.0.1:7004
   slots: (0 slots) slave
   replicates bfcfcdc304b011023fa568e044ea23ea6bc03c3c
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

我们发现 7005 本来是从节点,由于他对应的主节点挂了,就自动变成主节点 master,所有会有最后一个说明 All 16384 slots covered. 所有哈希槽都可覆盖了; 集群可以正常使用;

假如我们把 7005 也干掉,试试看

[root@localhost ~]# kill -9 9524
[root@localhost ~]#  ps -ef | grep redis
root       9501      1  0 17:38 ?        00:00:03 /usr/local/redis/bin/redis-server 127.0.0.1:7001 [cluster]
root       9516      1  0 17:45 ?        00:00:02 /usr/local/redis/bin/redis-server 127.0.0.1:7003 [cluster]
root       9520      1  0 17:45 ?        00:00:03 /usr/local/redis/bin/redis-server 127.0.0.1:7004 [cluster]
root       9528      1  0 17:45 ?        00:00:02 /usr/local/redis/bin/redis-server 127.0.0.1:7006 [cluster]
root       9610   2186  0 18:16 pts/0    00:00:00 grep --color=auto redis

查看下集群情况

[root@localhost ~]# redis-trib.rb check 127.0.0.1:7001
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: bfcfcdc304b011023fa568e044ea23ea6bc03c3c 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: f25b35f208dc96605ee4660994d2ac52f39ac870 127.0.0.1:7006
   slots: (0 slots) slave
   replicates aa6bc3f1e1174c3a991c01882584707c2408ec18
M: aa6bc3f1e1174c3a991c01882584707c2408ec18 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 7908a60306333c5d7c7c5e7ffef44bdf947ef0a4 127.0.0.1:7004
   slots: (0 slots) slave
   replicates bfcfcdc304b011023fa568e044ea23ea6bc03c3c
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[ERR] Not all 16384 slots are corered by nodes.

这里我们发现出事了,因为主从节点都挂了 所以有一部分哈希槽没得分配;

最后一句 [ERR] Not all 16384 slots are covered by nodes. 没有完全覆盖,所以不能正常使用集群;

参考文章:http://blog.csdn.net/dbreawbpcj/article/details/76360099

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Redis是一个开源的内存数据存储系统,常用于缓存、消息队列等场景。Docker是一个容器化平台,可以将应用程序及其依赖项打包成一个独立的容器,方便部署和管理。搭建Redis Docker集群可以提高系统的可用性和性能。 以下是Redis Docker集群搭建的步骤: 1. 安装Docker:首先需要在服务器上安装Docker,可以根据操作系统类型选择相应的安装方式。 2. 创建Docker网络:使用Docker命令创建一个自定义的网络,用于容器之间的通信。例如,可以执行以下命令创建名为"redis-net"的网络: ``` docker network create redis-net ``` 3. 创建Redis配置文件:在本地创建一个目录,用于存放Redis配置文件。在该目录下创建一个名为"redis.conf"的文件,并添加以下内容: ``` bind 0.0.0.0 protected-mode no cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 appendonly yes ``` 4. 创建Redis容器:使用Docker命令创建多个Redis容器,并将它们连接到之前创建的网络中。例如,可以执行以下命令创建3个Redis容器: ``` docker run -d --name redis1 --net redis-net -v /path/to/redis.conf:/usr/local/etc/redis/redis.conf redis redis-server /usr/local/etc/redis/redis.conf docker run -d --name redis2 --net redis-net -v /path/to/redis.conf:/usr/local/etc/redis/redis.conf redis redis-server /usr/local/etc/redis/redis.conf docker run -d --name redis3 --net redis-net -v /path/to/redis.conf:/usr/local/etc/redis/redis.conf redis redis-server /usr/local/etc/redis/redis.conf ``` 5. 创建Redis集群:使用Redis的官方工具"redis-trib.rb"来创建Redis集群。首先需要进入其中一个Redis容器,执行以下命令安装工具: ``` docker exec -it redis1 bash apt-get update apt-get install -y ruby gem install redis ``` 6. 初始化集群:在Redis容器中执行以下命令初始化集群: ``` redis-trib.rb create --replicas 1 <ip1>:<port1> <ip2>:<port2> <ip3>:<port3> ``` 其中,<ip1>:<port1>、<ip2>:<port2>、<ip3>:<port3>分别为三个Redis容器的IP地址和端口号。 至此,Redis Docker集群搭建完成。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值