(Redis系列二)Centos7 搭redis3.2.12集群-3主3从的6个节点服

1、虚拟机环境

使用的Linux环境已经版本:

Centos 7   64位系统

 

主机ip:

172.16.0.9 

172.16.0.10

172.16.0.11

每台服务器是1主1从,实验3台服务器课成为3主3从。,3个主的不挂,3个从的挂了没事,但是如果一个主的挂了他会切换至该服务器的其它从的,如果该从的也挂了那么集群就不能获取数据了

Redis安装的项目目录、日志、配置文件等都存放在/data/redis/目录下。

2、下载相关的安装包以及解压

首先在172.16.0.9 机器操作:

1

2

3

cd /data/soft

wget http://download.redis.io/releases/redis-3.2.12.tar.gz

tar -zxvf redis-3.2.12.tar.gz

 3、安装

在/data/soft/redis-3.2.12/目录下执行:

1

make install PREFIX=/data/soft/redis-3.2.12

 4、配置信息

创建集群配置、log日志、数据所在的文件夹:

1

2

cd /data/soft/redis-3.2.12/

mkdir cluster-conf

 创建集群端口文件夹:

1

2

3

cd cluster-conf

mkdir 6379

mkdir 6380

 复制配置文件到/data/soft/redis-3.2.12/cluster-conf/6379目录下:

1

cp /data/soft/redis-3.2.12/redis.conf  /data/soft/redis-3.2.12/cluster-conf/6379

修改6379目录下redis.conf配置文件(以下标红的在原配置中注释,添加以下内网至头部):

#注释掉ip或者设置0.0.0.0都是局域网访问
bind 0.0.0.0
##端口号
port 6379

#指定了记录日志的文件
 logfile "/data/soft/redis-3.2.12/cluster-conf/6379/redis.log"

#该目录要事先创建好,数据目录,数据库的写入会在这个目录。rdb、aof文件也会写在这个目录
  dir /data/soft/redis-3.2.12/cluster-conf/6379/
#是否开启集群
   cluster-enabled yes

#集群配置文件的名称,每个节点都有一个集群相关的配置文件,持久化保存集群的信息,
#这个文件并不需要手动配置,这个配置文件有Redis生成并更新,
#每个Redis集群节点需要一个单独的配置文件,请确保与实例运行的系统中配置文件名称不冲突。
   cluster-config-file nodes-6379.conf

#节点互连超时的阀值。集群节点超时毫秒数
    cluster-node-timeout 5000

#默认redis使用的是rdb方式持久化,这种方式在许多应用中已经足够用了,
#但是redis如果中途宕机,会导致可能有几分钟的数据丢失,
#根据save来策略进行持久化,Append Only File是另一种持久化方式,可以提供更好的持久化特性,
#Redis会把每次写入的数据在接收后都写入 appendonly.aof 文件,
#每次启动时Redis都会先把这个文件的数据读入内存里,先忽略RDB文件。
  appendonly yes

 复制6379目录下redis.conf至6380并修改端口及存储路径:

[root@i-ea1bnx05 6379]# cp /data/soft/redis-3.2.12/cluster-conf/6379/redis.conf /data/soft/redis-3.2.12/cluster-conf/6380/

[root@i-ea1bnx05 6380]# vim /data/soft/redis-3.2.12/cluster-conf/6380/redis.conf 

 5、使用scp复制安装和配置好的redis复制到其他服务器上

已经在172.16.0.9机器配置好,复制到 172.16.0.10 和 172.16.0.11 机器

在两台新机器上创建文件夹

mkdir /data/soft

 复制:

1

2

sscp -r /data/soft/redis-3.2.12 root@172.16.0.10:/data/soft/

#输入10机器密码即可传输数据。

1

2

scp -r /data/soft/redis-3.2.12 root@172.16.0.11:/data/soft/

#输入11机器密码即可传输数据。

 

6、启动3台机器的redis

3台机器执行启动方式:

[root@i-ea1bnx05 6380]# ps -ef|grep redis

root     12500  6029  0 15:43 pts/0    00:00:00 grep --color=auto redis

[root@i-ea1bnx05 /]# /data/soft/redis-3.2.12/bin/redis-server /data/soft/redis-3.2.12/cluster-conf/6379/redis.conf &

[1] 12666

[root@i-ea1bnx05 /]# /data/soft/redis-3.2.12/bin/redis-server /data/soft/redis-3.2.12/cluster-conf/6380/redis.conf &   

[2] 12678

[root@i-ea1bnx05 /]# ps -ef|grep redis

root     12666  6029  0 15:45 pts/0    00:00:00 /data/soft/redis-3.2.12/bin/redis-server 0.0.0.0:6379 [cluster]

root     12678  6029  0 15:45 pts/0    00:00:00 /data/soft/redis-3.2.12/bin/redis-server 0.0.0.0:6380 [cluster]

root     12682  6029  0 15:45 pts/0    00:00:00 grep --color=auto redis

[root@i-ea1bnx05 6379]#  netstat -tunlp|grep redis

tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      12666/redis-server  

tcp        0      0 0.0.0.0:6380            0.0.0.0:*               LISTEN      12678/redis-server  

tcp        0      0 0.0.0.0:16379           0.0.0.0:*               LISTEN      12666/redis-server  

tcp        0      0 0.0.0.0:16380           0.0.0.0:*               LISTEN      12678/redis-server  

[root@i-ea1bnx05 6379]# 

7、创建集群

部署环境

yum install ruby -y

yum install rubygems -y

gem install redis

报错,提示ruby版本需要大于2.2.2版本。CentOS7 yum库中ruby的版本支持到 2.0.0,可gem 安装redis需要最低是2.2.2

需要安装个高版本的ruby

1

2

3

4

5

6

#安装curl

yum install curl -y

#安装rvm

#gpg2 --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3

#curl -L get.rvm.io | bash -s stable

#source /usr/local/rvm/scripts/rvm

 

------------------------------------------------------------------------若有问题请查看开始------------------------------------------------------------------------

如果安装出错,提示以下内容的话请注意以下操作,重新导入公钥

gpg2 --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB(不同服务器生成的可能不同,如果提示无法链接就多试几下,因为

pool.sks-keyservers.net网址不太稳定)

[root@host-172-22-14-132 ~]# curl -L get.rvm.io | bash -s stable                                                                
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   194  100   194    0     0    106      0  0:00:01  0:00:01 --:--:--   106
100 24173  100 24173    0     0   8006      0  0:00:03  0:00:03 --:--:-- 54950
Downloading https://github.com/rvm/rvm/archive/1.29.7.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.7/1.29.7.tar.gz.asc
gpg: 于 2019年01月04日 星期五 06时01分48秒 CST 创建的签名,使用 RSA,钥匙号 39499BDB
gpg: 无法检查签名:没有公钥
GPG signature verification failed for '/usr/local/rvm/archives/rvm-1.29.7.tgz' - 'https://github.com/rvm/rvm/releases/download/1.29.7/1.29.7.tar.gz.asc'! Try to install GPG v2 and then fetch the public key:

    gpg2 --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

or if it fails:

    command curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
    command curl -sSL https://rvm.io/pkuczynski.asc | gpg2 --import -

In case of further problems with validation please refer to https://rvm.io/rvm/security

[root@host-172-22-14-132 ~]# gpg2 --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
gpg: 下载密钥‘D39DC0E3’,从 hkp 服务器 pool.sks-keyservers.net
gpg: 下载密钥‘39499BDB’,从 hkp 服务器 pool.sks-keyservers.net
gpg: 密钥 D39DC0E3:“Michal Papis (RVM signing) <mpapis@gmail.com>”未改变
gpg: 密钥 39499BDB:公钥“Piotr Kuczynski <piotr.kuczynski@gmail.com>”已导入
gpg: 没有找到任何绝对信任的密钥
gpg: 合计被处理的数量:2
gpg:           已导入:1  (RSA: 1)
gpg:           未改变:1
[root@host-172-22-14-132 ~]# curl -L get.rvm.io | bash -s stable
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   194  100   194    0     0    116      0  0:00:01  0:00:01 --:--:--   116
100 24173  100 24173    0     0  10139      0  0:00:02  0:00:02 --:--:-- 8427k
Downloading https://github.com/rvm/rvm/archive/1.29.7.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.7/1.29.7.tar.gz.asc
gpg: 于 2019年01月04日 星期五 06时01分48秒 CST 创建的签名,使用 RSA,钥匙号 39499BDB
gpg: 完好的签名,来自于“Piotr Kuczynski <piotr.kuczynski@gmail.com>”
gpg: 警告:这把密钥未经受信任的签名认证!
gpg:       没有证据表明这个签名属于它所声称的持有者。
主钥指纹: 7D2B AF1C F37B 13E2 069D  6956 105B D0E7 3949 9BDB
GPG verified '/usr/local/rvm/archives/rvm-1.29.7.tgz'
Creating group 'rvm'
Installing RVM to /usr/local/rvm/
Installation of RVM in /usr/local/rvm/ is almost complete:

  * First you need to add all users that will be using rvm to 'rvm' group,
    and logout - login again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`.

  * To start using RVM you need to run `source /etc/profile.d/rvm.sh`
    in all your open shell windows, in rare cases you need to reopen all shell windows.
  * Please do NOT forget to add your users to the rvm group.
     The installer no longer auto-adds root or users to the rvm group. Admins must do this.
     Also, please note that group memberships are ONLY evaluated at login time.
     This means that users must log out then back in before group membership takes effect!
[root@host-172-22-14-132 ~]# 

-----------------------------------------------------------------------------这里结束-------------------------------------------------------------------------

 

查看rvm库中已知的ruby版本

[root@i-ea1bnx05 6379]# rvm list known
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.10]
[ruby-]2.3[.7]
[ruby-]2.4[.4]
[ruby-]2.5[.1]
[ruby-]2.6[.0-preview2]

查看现在的版本

[root@i-ea1bnx05 6379]# ruby --version
ruby 2.0.0p648 (2015-12-16) [x86_64-linux]

使用一个ruby新版本

[root@i-ea1bnx05 6379]# rvm install 2.4.4 

使用ruby新版本

rvm use  2.4.4

卸载一个旧版本

rvm remove 2.0.0 

 查看现在版本

在重新安装ruby相关的redis就可以了

[root@i-ea1bnx05 6379]# gem install redis
Fetching: redis-4.0.2.gem (100%)
Successfully installed redis-4.0.2
Parsing documentation for redis-4.0.2
Installing ri documentation for redis-4.0.2
Done installing documentation for redis after 1 seconds
1 gem installed

开启集群

[root@i-ea1bnx05 6379]# /data/soft/redis-3.2.12/src/redis-trib.rb create --replicas 1 172.16.0.9:6379 172.16.0.9:6380 172.16.0.10:6379 172.16.0.10:6380 172.16.0.11:6379 172.16.0.11:6380
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
172.16.0.9:6379
172.16.0.10:6379
172.16.0.11:6379
Adding replica 172.16.0.10:6380 to 172.16.0.9:6379
Adding replica 172.16.0.9:6380 to 172.16.0.10:6379
Adding replica 172.16.0.11:6380 to 172.16.0.11:6379
M: b4f27f6441fc4e5dbf049268236bab7c0a62d4ef 172.16.0.9:6379
   slots:0-5460 (5461 slots) master
S: 81e21ecba318e62a13c196c3ce64b75ac02b08ad 172.16.0.9:6380
   replicates de36ff808165790059cbc67b82fdc8b10c05b67d
M: de36ff808165790059cbc67b82fdc8b10c05b67d 172.16.0.10:6379
   slots:5461-10922 (5462 slots) master
S: 5e03f8867ae368f3a79fd4dd6c49eb46750fa8e7 172.16.0.10:6380
   replicates b4f27f6441fc4e5dbf049268236bab7c0a62d4ef
M: 6b9dba0f0e9939b6020a1e20a8918a0f6a80fb68 172.16.0.11:6379
   slots:10923-16383 (5461 slots) master
S: d34f949cd71e296d8b65c5c9420dfcdddae2b09e 172.16.0.11:6380
   replicates 6b9dba0f0e9939b6020a1e20a8918a0f6a80fb68
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.0.9:6379)
M: b4f27f6441fc4e5dbf049268236bab7c0a62d4ef 172.16.0.9:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 5e03f8867ae368f3a79fd4dd6c49eb46750fa8e7 172.16.0.10:6380
   slots: (0 slots) slave
   replicates b4f27f6441fc4e5dbf049268236bab7c0a62d4ef
M: de36ff808165790059cbc67b82fdc8b10c05b67d 172.16.0.10:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: 6b9dba0f0e9939b6020a1e20a8918a0f6a80fb68 172.16.0.11:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: d34f949cd71e296d8b65c5c9420dfcdddae2b09e 172.16.0.11:6380
   slots: (0 slots) slave
   replicates 6b9dba0f0e9939b6020a1e20a8918a0f6a80fb68
S: 81e21ecba318e62a13c196c3ce

 slots: (0 slots) slave
   replicates de36ff808165790059cbc67b82fdc8b10c05b67d
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

完成安装

 

进服务器测试

[root@i-omhjrmzm bin]# /data/soft/redis-3.2.12/bin/redis-cli -c -h 172.16.0.9 -p 6379

172.16.0.9:6379> set aaa bbb

172.16.0.9:6379> keys *
1) "bbb"
172.16.0.9:6379> get aaa
-> Redirected to slot [10439] located at 172.16.0.10:6379
"bbb"
172.16.0.10:6379> exit

 

如果需要关闭redis集群

[root@i-omhjrmzm bin]# netstat -tunlp|grep redis
tcp        0      0 0.0.0.0:6379            0.0.0.0:*               LISTEN      14546/redis-server  
tcp        0      0 0.0.0.0:6380            0.0.0.0:*               LISTEN      9626/redis-server 0 
tcp        0      0 0.0.0.0:16379           0.0.0.0:*               LISTEN      14546/redis-server  
tcp        0      0 0.0.0.0:16380           0.0.0.0:*               LISTEN      9626/redis-server 0 

[root@i-omhjrmzm 6379]# kill 9626
[root@i-omhjrmzm 6379]# kill 14546

其它两台也是

 

如果设置密码

则修改配置文件redis.conf

vim /data/soft/redis-3.2.12/cluster-conf/6379/redis.conf

vim /data/soft/redis-3.2.12/cluster-conf/6380/redis.conf

增加一列

requirepass “111111”

 

或者在线修改,上面的没试通,下面的ok,登录这6个节点,全部执行一次,

masterauth为从节点访问主节点的密码requirepass为客户端程序访问redis时候的连接密码

[root@i-omhjrmzm bin]# redis-cli -c -p 6379

config set masterauth wuwei111

config set requirepass wuwei111

再登录6个节点,执行config rewrite,就会把配置写入启动文件redis.conf中

[root@i-omhjrmzm bin]# redis-cli -c -p 6379 -a wuwei111

config rewrite

重启动服务启动操作:

1、启动了6个节点的redis

/data/soft/redis-3.2.12/bin/redis-server /data/soft/redis-3.2.12/cluster-conf/6379/redis.conf &
/data/soft/redis-3.2.12/bin/redis-server /data/soft/redis-3.2.12/cluster-conf/6380/redis.conf &
2、启动集群

/data/soft/redis-3.2.12/src/redis-trib.rb create --replicas 1 172.16.0.9:6379 172.16.0.9:6380 172.16.0.10:6379 172.16.0.10:6380 172.16.0.11:6379 172.16.0.11:6380

 

检查集群状态命令,该命令是在其它集群服务器上执行的一个效果:

[root@host-172-22-14-105 ~]# /data/soft/redis-3.2.12/src/redis-trib.rb check 127.0.0.1:6379
>>> Performing Cluster Check (using node 127.0.0.1:6379)
S: 36b161ce74c0ffe9cda57f6fef88880265563a47 127.0.0.1:6379
   slots: (0 slots) slave
   replicates e2e04a6ddf07159d90b9ec017e450dbc41071c72
M: 39e7746fac2f559ac67071154b9762e3a700fc1a 172.22.14.107:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 224173cd8fd8314ccb8e3556232685a54cbe83dd 172.22.14.105:6380
   slots: (0 slots) slave
   replicates 3b077d12437ff1eee6c4cca0f1f7af05c3c892c8
M: e2e04a6ddf07159d90b9ec017e450dbc41071c72 172.22.14.106:6380
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 3b077d12437ff1eee6c4cca0f1f7af05c3c892c8 172.22.14.106:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
S: fd806e5e86c10c119a3930a666f896729ccb892c 172.22.14.107:6380
   slots: (0 slots) slave
   replicates 39e7746fac2f559ac67071154b9762e3a700fc1a
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@host-172-22-14-105 ~]# 

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值