linux 安装 Redis 集群(手动版)

一 redis 安装

1.安装部分依赖库 :因为redis是使用C++写的 ,所有首先安装各种依赖包:

$ yum install gcc-c++

2. 选择要下载的redis版本:必须为redis 3.0版本及以上(3.0 后才支持集群功能),推荐5.0 版本。本人使用的5.0 版本

$ wget http://download.redis.io/releases/redis-5.0.5.tar.gz
$ tar -zxvf redis-5.0.5.tar.gz
$ mv redis-5.0.5 redis

3.编译redis源代码

$ cd redis
$ make
$ cd ./src
$ make install

4.安装ruby : 必须为2.5版本及以上才能被redis使用,通过yum安装的话版本不适用于redis,所以此处通过手动安装。

1)下载ruby稳定版

$ wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.1.tar.gz
$ tar -xvf ruby-2.5.1.tar.gz
$ cd ruby-2.5.1
$ ./configure --prefix=/usr/local/rvm  //此目录为ruby最终安装目录
$ make && make install
//安装后 查看下ruby 版本
$ /usr/local/rvm/bin/ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]

2)ruby写入环境变量

$ vi /etc/profile
//在末尾添加 ruby路径与 上步最终安装目录一致 且不要覆盖原有PATH
RUBY_HOME=/usr/local/rvm
export PATH=$PATH:$RUBY_HOME/bin
% source /etc/profile

3)安装ruby所需 zlib包

$yum -y install zlib-devel
$cd ruby-2.5.1/ext/zlib
$ruby ./extconf.rb
$make
$make install

// 此步骤若出现 以下编译错误 
// make: *** 没有规则可以创建“zlib.o”需要的目标“/include/ruby.h”。 停止。
// 解决方案为 在Makefile顶部中的增加top_srcdir = ../..

4)安装 ruby 所需 openssl

yum install openssl-devel -y
$cd ruby-2.5.1/ext/openssl
$ruby ./extconf.rb
$make
$make install

// 此步骤若出现 以下编译错误 
// make: *** 没有规则可以创建“zlib.o”需要的目标“/include/ruby.h”。 停止。
// 解决方案为 在Makefile顶部中的增加top_srcdir = ../..

5)安装 gem (gem为 ruby 的一个库)

gem install redis
//出现异常:/usr/bin/gem: No such file or directory
//解决方案:ln -s /usr/local/rvm/bin/gem /usr/bin/gem

二 配置 redis 集群

redis 集群配置最少为 6 个redis-server 实例对象,支持分布式部署(配置在不同的物理机上)。此例仅配置一台物理机上讲解,配置 6 个redis-server 实例对象,对应端口号为7000,7001,7002 ,7003,7004,7005。以下为配置集群 实例不够时的提示

*** ERROR: Invalid configuration for cluster creation.
*** Redis Cluster requires at least 3 master nodes.
*** This is not possible with 3 nodes and 1 replicas per node.
*** At least 6 nodes are required.

若配置不同机器,按照上述一步骤先配置redis

$ cd redis
$ mkdir redisCluster 
$ cd redisCluster 
$ mkdir 7000
$ mkdir 7001
$ mkdir 7002
$ mkdir 7003
$ mkdir 7004
$ mkdir 7005

配置redis 配置文件: 拷贝redis 目录下redis.conf 分别放置在7000 ~ 7005 文件夹中。重命名各个文件夹下的 redis.conf 为 redis7000.conf redis7001.conf redis7002.conf redis7003.conf redis7004.conf redis7005.conf

$ echo /usr/local/redis/redisCluster/7000 /usr/local/redis/redisCluster/7001 /usr/local/redis/redisCluster/7002  /usr/local/redis/redisCluster/7003  /usr/local/redis/redisCluster/7004  /usr/local/redis/redisCluster/7005 | xargs -n 1 cp -v redis.conf

分别修改 redis7001.conf ~ redis7005.conf 为以下内容

bind 127.0.0.1        // 绑定的ip 地址 
port xxx              // 修改为所要启动的端口号 如:7000 7001 或 7002 
daemonize yes         // 后台启动
pidfile  /var/run/redis_7000.pid          //pidfile文件对应7000,7001,7002
cluster-enabled  yes                      //开启集群 
cluster-config-file nodes_7000.conf       //集群配置文件 分别对应  nodes_7000.conf nodes_7001.conf nodes_7002.conf 等
cluster-node-timeout  5000                //请求超时  优化为5秒
appendonly  yes                           //aof日志开启  用于持久化

启动 各个 redis 进程

$ redis-server /usr/local/redis/redisCluster/7000/redis7000.conf 
12403:C 18 Oct 2019 11:03:48.503 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12403:C 18 Oct 2019 11:03:48.503 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12403, just started
12403:C 18 Oct 2019 11:03:48.503 # Configuration loaded
$ redis-server /usr/local/redis/redisCluster/7001/redis7001.conf 
12417:C 18 Oct 2019 11:03:57.097 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12417:C 18 Oct 2019 11:03:57.097 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12417, just started
12417:C 18 Oct 2019 11:03:57.097 # Configuration loaded
$ redis-server /usr/local/redis/redisCluster/7002/redis7002.conf
12431:C 18 Oct 2019 11:04:02.845 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12431:C 18 Oct 2019 11:04:02.845 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12431, just started
12431:C 18 Oct 2019 11:04:02.845 # Configuration loaded
$ redis-server /usr/local/redis/redisCluster/7003/redis7003.conf  
12723:C 18 Oct 2019 11:13:56.317 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12723:C 18 Oct 2019 11:13:56.317 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12723, just started
12723:C 18 Oct 2019 11:13:56.317 # Configuration loaded
$ redis-server /usr/local/redis/redisCluster/7004/redis7004.conf
12737:C 18 Oct 2019 11:14:05.812 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12737:C 18 Oct 2019 11:14:05.812 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12737, just started
12737:C 18 Oct 2019 11:14:05.812 # Configuration loaded
$ redis-server /usr/local/redis/redisCluster/7005/redis7005.conf
12751:C 18 Oct 2019 11:14:13.707 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
12751:C 18 Oct 2019 11:14:13.707 # Redis version=5.0.5, bits=64, commit=00000000, modified=0, pid=12751, just started
12751:C 18 Oct 2019 11:14:13.707 # Configuration loaded

启动集群

如出现类似Connection refused,参考下面步骤,否则跳过此步骤

【通过ip访问:(非127.0.0.1)

 

究其原因:

redis.conf文件中配置了访问限制,通过bind来限制了ip访问,默认为127.0.0.1

注释掉bind之后,本地可以通过ip访问,但是其他主机无法访问,在redis3之后,有一个protected-mode 参数,默认开启 yes

 

需要添加参数 requirepass abc (密码),或将protected-mode 修改为no。重启redis服务即可

当开启密码验证时,使用 redis-cli 需要 增加参数 -a pass】
————————————————

$ redis-cli --cluster create 127.0.0.1:7000  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 --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 127.0.0.1:7004 to 127.0.0.1:7000
Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
Adding replica 127.0.0.1:7003 to 127.0.0.1:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 3164b50d6730a8ef5a8ce6dfbed7c62dbee2d218 127.0.0.1:7000
   slots:[0-5460] (5461 slots) master
M: 1f11498d90ac3d7597a4ffcd03e7bdb3a7694290 127.0.0.1:7001
   slots:[5461-10922] (5462 slots) master
M: 9538716cb886950454827ba3964bf5d1e72510cc 127.0.0.1:7002
   slots:[10923-16383] (5461 slots) master
S: db41dd4dbc796bfba395c6b7414b023f2056bd4b 127.0.0.1:7003
   replicates 1f11498d90ac3d7597a4ffcd03e7bdb3a7694290
S: d45e14db110b5be6bc289a07ce091a7e42e5582c 127.0.0.1:7004
   replicates 9538716cb886950454827ba3964bf5d1e72510cc
S: cd72ecfe4d8965ce004b6b5dd3ef98d8d1bbd626 127.0.0.1:7005
   replicates 3164b50d6730a8ef5a8ce6dfbed7c62dbee2d218
Can I set the above configuration? (type 'yes' to accept): 

输入 yes

[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

以上配置为 3 主 3 从 redis集群 ,

所有集群启动成功 哈哈 ~

posted @ 2022-04-02 15:01  风吹稻香  阅读(193)  评论(0编辑  收藏  举报