linux7.0下2台服务器安装redis4.0.2集群

Posted renjiaxiaoshu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux7.0下2台服务器安装redis4.0.2集群相关的知识,希望对你有一定的参考价值。

分配端口和ip
172.16.1.23 redis01 172.16.1.24 redis02  
redis01:7000 redis02:7003    
redis01:7001   redis02:7004   
redis01:7002 redis02:7005  
1 安装gcc  Redis在linux上的安装首先必须先安装gcc,这个是用来编译redis的源文件的
# yum install gcc -y
2、下载解压redis的源文件  https://redis.io/download
# cd /root
#wget http://download.redis.io/releases/redis-4.0.2.tar.gz
# tar zxvf redis-4.0.2.tar.gz
3 使用make命令编译
#cd redis-4.0.2/
#make  出现一大堆内容 Hint: It‘s a good idea to run ‘make test‘ ;)提示成功 如果提示错误输入 
#make MALLOC
=libc
#cd src (src在redis
-4.0.2目录下)
#make test 提示You need tcl
8.5 or newer in order to run the Redis test make: *** [test] Error 1 在这里发现有错误 需要安装tcl
4安装TCL并再次测试  tcl(Tool Command Language)工具脚本语言,是Linux内的一种语言包
#yum install tcl -y
#make test 再次测试  这里反正检查的一大堆,最后出现 \o/ All tests passed without errors! 就ok
5安装 到/usr/local/redis
[[email protected] src]# make PREFIX=/usr/local/redis install
Hint: Its a good idea to run make test ;)
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
    INSTALL install
   安装成功
[[email protected] redis-4.0.7]# cd /usr/local/redis/bin
[[email protected] bin]# ll
redis-benchmark redis-check-aof  redis-check-rdb redis-cli redis-sentinel -> redis-server redis-server
这些都是一个个的命令
6:创建配制节点
服务器1:
mkdir -p /usr/local/redis/redis_cluster/7000
mkdir -p /usr/local/redis/redis_cluster/7001
mkdir -p /usr/local/redis/redis_cluster/7002
cp /root/redis-4.0.2/redis.conf /usr/local/redis/redis_cluster/7000
cp /root/redis-4.0.2/redis.conf /usr/local/redis/redis_cluster/7001
cp /root/redis-4.0.2/redis.conf /usr/local/redis/redis_cluster/7002
服务器2:
mkdir -p /usr/local/redis/redis_cluster/7003
mkdir -p /usr/local/redis/redis_cluster/7004
mkdir -p /usr/local/redis/redis_cluster/7005
cp /root/redis-4.0.2/redis.conf /usr/local/redis/redis_cluster/7003
cp /root/redis-4.0.2/redis.conf /usr/local/redis/redis_cluster/7004
cp /root/redis-4.0.2/redis.conf /usr/local/redis/redis_cluster/7005
7:修改redis.conf    切记:不能设置密码,否则集群启动时会连接不上
port  7000  7001 7002 7003 7004 7005       //端口根据对应的文件夹去配制端口   
bind 本机ip                                //根据本机所在的IP或hostname去配制 node1 node2 node3
daemonize    yes                          //redis后台运行
pidfile  /var/run/redis_7000.pid          //pidfile文件对应7000,7001,7002,7003,7004,7005
cluster-enabled  yes                      //开启集群  把注释#去掉
cluster-config-file  nodes_7000.conf      //集群的配置  配置文件首次启动自动生成 7000,7001,7002,7003,7004,7005
cluster-node-timeout  15000               //请求超时  默认15秒,可自行设置
appendonly  yes                           //aof日志开启  有需要就开启,它会每次写操作都记录一条日
8:复制src目录中的redis-trib.rb 到/usr/local/redis/bin目录
cp /usr/local/redis/redis-trib.rb /usr/local/redis/bin
9.1 redis-trib.rb环境准备(该文件存在于redis-4.0.2/src/目录中)    //只需要在其中一台上执行此步骤!! 
    redis-trib.rb是采用Ruby实现的Redis集群管理工具。内部通过Cluster相关命令帮助
    我们简化集群创建、检查、槽迁移和均衡等常见操作,使用之前需要安装Ruby依赖环境。 centos7/rhel7安装较高版本ruby2.2/2.3/2.4+
       我的yum时网易的,用yum安装ruby的版本为2.0.0 在创建集群的时候提示需要高于2.2.2的版本  所以不能用yum安装 建议采用方法二 更换yum源

 

       :换yum源安装
    ~]# yum install centos-release-scl-rh    //会在/etc/yum.repos.d/目录下多出一个CentOS-SCLo-scl-rh.repo源
    ~]# yum install rh-ruby23  -y    //直接yum安装即可  
       软件集合的软件包安装在/opt目录下, 要使用它,需要加载环境变量,如下所示:
    ~]# scl  enable  rh-ruby23 bash    //必要一步
    ~]# ruby -v    //查看安装版本
    续:接上文提到的报错问题:“redis requires Ruby version >= 2.2.2”
    ~]# gem install redis
    然后,redis-trib.rb这个二进制文件就可以使用了,用来构建redis集群
9.2 这样安装后每次重启或者重新连接crt时发现  不能输入ruby -v  等命令 自然也就无法输入./redis-trib.rb check 172.16.1.23:7002  这些检测命令,需要设置登录时自动启用Ruby 2.3,按如下所示进行配置
# ruby -v
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
# which ruby
/opt/rh/rh-ruby23/root/usr/bin/ruby        //查看软件包安装路径
#cd /etc/profile.d/
#touch rh-ruby23.sh             //新建一个文件
#vim rh-ruby23.sh
  #!/bin/bash
  source /opt/rh/rh-ruby23/enable
  export X_SCLS="`scl enable rh-ruby23 ‘echo $X_SCLS‘`"
  export PATH=$PATH:/opt/rh/rh-ruby23/root/usr/local/bi
10:启动各个节点
1服务器:
# cd /usr/local/redis/bin/
./redis-server /usr/local/redis/redis_cluster/7000/redis.conf
./redis-server /usr/local/redis/redis_cluster/7001/redis.conf
./redis-server /usr/local/redis/redis_cluster/7002/redis.conf
2服务器:
# cd /usr/local/redis/bin/
./redis-server /usr/local/redis/redis_cluster/7003/redis.conf
./redis-server /usr/local/redis/redis_cluster/7004/redis.conf
./redis-server /usr/local/redis/redis_cluster/7005/redis.conf
11:检查各节点是否启动
ps -ef | grep redis 查看进程
netstat -tnlp | grep redis 查看端口
 
 12:创建集群  只在一台上配置即可
进入node1的bin目录下,执行以下脚本
#./redis-trib.rb create --replicas 1 172.16.1.23:7000 172.16.1.23:7001 172.16.1.23:7002 172.16.1.24:7003 172.16.1.24:7004 172.16.1.24:7005
13:验证集群
1 简单命令
# cd /usr/local/redis/bin
[[email protected] bin]# ll
dump.rdb  redis-benchmark  redis-check-aof  redis-check-rdb  redis-cli
redis-sentinel -> redis-server  redis-server
[[email protected] bin]# ./redis-cli    连接客户端
127.0.0.1:6379> ping                      检查网络是否可以
PONG
127.0.0.1:6379> set name kobe  设置一个键值对
OK
127.0.0.1:6379> get name
"kobe"
127.0.0.1:6379> keys *     查看所有的键
1) "name"
127.0.0.1:6379> del name 删除name这个键
(integer) 1
127.0.0.1:6379> keys *
(empty list or set

 

# cd /usr/local/redis/bin/
# ./redis-cli -h 172.16.1.23 -p 7000 -c
172.16.1.23:7000> ping
PONG
[[email protected] bin]# ./redis-cli -h 172.16.1.24 -p 7005 -c
172.16.1.24:7005> ping
PONG
 
redis-cli -h 172.16.1.23 -p 7000 -c
redis-cli -h 172.16.1.24 -p 7003 -c
......

 

 
# ./redis-trib.rb create --replicas 1 172.16.1.23:7000 172.16.1.23:7001 172.16.1.23:7002 172.16.1.24:7003 172.16.1.24:7004 172.16.1.24:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
172.16.1.23:7000
172.16.1.24:7003
172.16.1.23:7001
Adding replica 172.16.1.24:7004 to 172.16.1.23:7000
Adding replica 172.16.1.23:7002 to 172.16.1.24:7003
Adding replica 172.16.1.24:7005 to 172.16.1.23:7001
M: 2334afd272c9b59aa4f4f95b8ff0748e05f0fbcb 172.16.1.23:7000
   slots:0-5460 (5461 slots) master
M: 32ef3e2869ae96886c11b27fbc9b633638cb295a 172.16.1.23:7001
   slots:10923-16383 (5461 slots) master
S: 37cceba97e369b8161a35c5282f04ef548bad243 172.16.1.23:7002
   replicates bf55a1c9a9e956675ab25784239e09f73f97370e
M: bf55a1c9a9e956675ab25784239e09f73f97370e 172.16.1.24:7003
   slots:5461-10922 (5462 slots) master
S: da4f3faeff559039e91c7972616192a41c2392d3 172.16.1.24:7004
   replicates 2334afd272c9b59aa4f4f95b8ff0748e05f0fbcb
S: ec31f8bbd5d1fe994e5f78ec1afe7cdca5a70ecb 172.16.1.24:7005
   replicates 32ef3e2869ae96886c11b27fbc9b633638cb295a
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 172.16.1.23:7000)
M: 2334afd272c9b59aa4f4f95b8ff0748e05f0fbcb 172.16.1.23:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: da4f3faeff559039e91c7972616192a41c2392d3 172.16.1.24:7004
   slots: (0 slots) slave
   replicates 2334afd272c9b59aa4f4f95b8ff0748e05f0fbcb
S: ec31f8bbd5d1fe994e5f78ec1afe7cdca5a70ecb 172.16.1.24:7005
   slots: (0 slots) slave
   replicates 32ef3e2869ae96886c11b27fbc9b633638cb295a
S: 37cceba97e369b8161a35c5282f04ef548bad243 172.16.1.23:7002
   slots: (0 slots) slave
   replicates bf55a1c9a9e956675ab25784239e09f73f97370e
M: 32ef3e2869ae96886c11b27fbc9b633638cb295a 172.16.1.23:7001
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: bf55a1c9a9e956675ab25784239e09f73f97370e 172.16.1.24:7003
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
# ./redis-trib.rb check 172.16.1.23:7002
>>> Performing Cluster Check (using node 172.16.1.23:7002)
S: 37cceba97e369b8161a35c5282f04ef548bad243 172.16.1.23:7002
   slots: (0 slots) slave
   replicates bf55a1c9a9e956675ab25784239e09f73f97370e
S: ec31f8bbd5d1fe994e5f78ec1afe7cdca5a70ecb 172.16.1.24:7005
   slots: (0 slots) slave
   replicates 32ef3e2869ae96886c11b27fbc9b633638cb295a
S: da4f3faeff559039e91c7972616192a41c2392d3 172.16.1.24:7004
   slots: (0 slots) slave
   replicates 2334afd272c9b59aa4f4f95b8ff0748e05f0fbcb
M: 2334afd272c9b59aa4f4f95b8ff0748e05f0fbcb 172.16.1.23:7000
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 32ef3e2869ae96886c11b27fbc9b633638cb295a 172.16.1.23:7001
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: bf55a1c9a9e956675ab25784239e09f73f97370e 172.16.1.24:7003
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

 

# clear
# ./redis-cli -h 172.16.1.23 -p 7000 -c
172.16.1.23:7000> ping
PONG
172.16.1.23:7000>
# ./redis-cli -h 172.16.1.23 -p 7001 -c
172.16.1.23:7001> ping
PONG
172.16.1.23:7001>
# ./redis-cli -h 172.16.1.23 -p 7002 -c
172.16.1.23:7002> ping
PONG
172.16.1.23:7002> ping [message]
# ./redis-cli -h 172.16.1.24 -p 7003 -c
172.16.1.24:7003> ping
PONG
172.16.1.24:7003>
# ./redis-cli -h 172.16.1.24 -p 7004 -c
172.16.1.24:7004> ping
PONG
172.16.1.24:7004>
# ./redis-cli -h 172.16.1.24 -p 7005 -c
172.16.1.24:7005> ping
PONG
172.16.1.24:7005>

 

 
 
 
 
 
 





以上是关于linux7.0下2台服务器安装redis4.0.2集群的主要内容,如果未能解决你的问题,请参考以下文章

CENTOS7下安装REDIS4.0.11

阿里云 CentOS7安装redis4.0.9并开启远程访问

redis4.0.8安装部署

redis4.0.2 安装

centos6.5安装redis4.0

安装Redis4.0.10在Linux环境搭建