# Redis集群的搭建
准备6台redis服务器,具体要求如下:
|ip地址 | 端口 | etho | 日志文件名 |
---|---|---|---
192.168.166.207 | 6380 | 都可以接收连接请求 | redis51.log
192.168.166.207 | 7701 | 都可以接收连接请求 | redis52.log
192.168.166.208 | 6380 | 都可以接收连接请求 | redis53.log
192.168.166.208 | 7701 | 都可以接收连接请求 | redis54.log
192.168.166.209 | 6380 | 都可以接收连接请求 | redis55.log
192.168.166.209 | 7701 | 都可以接收连接请求 | redis56.log
注意:##以上为3台主redis,剩下的3台分别在主redis上做多实例。开放端口。
# 环境准备
**# 1 在6台redis服务器上运行服务,按照如下要求修改配置文件后,重启redis服务。**
```
]#vim /etc/redis/6380.conf
bind 192.168.166.207 #只指定物理接口ip地址
port 6380 #不允许相同
daemonize yes
cluster-enabled yes
pidfile /var/run/redis_51.pid #不允许相同
pidfile nodes-6380.conf #不允许相同
cluster-node-timeout 5000 #集群节点之间通信超时时间单位秒
:wq
```
**# 2.服务启动**
```
1>.停止服务
]# redis-cli -h 192.168.166.207 -p 6380 [ -a 密码 ] shutdown
2>.启动服务
]# /etc/init.d/redis_6380 start
3>.连接服务
[root@host56 ~]# redis-cli -h 192.168.4.56 -p 6056
```
**# 3.安装解释ruby代码的软件包**
```
[root@host51 ~]# yum -y install ruby rubygems
[root@host51 ~]# rpm -q rubygems ruby
rubygems-2.0.14.1-30.el7.noarch
ruby-2.0.0.648-30.el7.x86_64
```
**# 4.安装ruby连接redis 接口程序 gem**
```
[root@host51 redis-cluster]# which gem
/usr/bin/gem
[root@host51 redis-cluster]# gem install redis
注意:在这里版本很重要,一定要查看自己gem和ruby的版本 ,否则集群无法创建成功。
补充:对于以上的升级方案:
1>.首先安装rvm安装会使用的包:
#yum install gcc-c++ patch readline readline-devel zlib zlib-devel \
libyaml-devel libffi-devel openssl-devel make \
bzip2 autoconf automake libtool bison iconv-devel sqlite-devel
2>.之后便是安装rvm:
curl -sSL https://rvm.io/mpapis.asc | gpg --import -
curl -L get.rvm.io | bash -s stable
3>.配置rvm的运行环境:
source /etc/profile.d/rvm.sh
rvm reload
4>.查看版本库:
rvm list known
5>.最后便可安装ruby了,当然版本可以任选,反正我选2.4.4
rvm install 2.4.4
6>.检查安装情况
rvm list
7>.设置默认运行的ruby版本:
rvm use 2.4.2 --default
8>.升级gem
wget https://rubygems.org/rubygems/rubygems-2.7.7.zip
gem update --system
gem install rubygems-update
update_rubygems
ruby setup.rb --help
```
**# 3.生成创建集群的脚本redis-trib.rb**
```
[root@host51 src]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@host51 src]# cd
[root@host51 ~]# cp redis-4.0.8/src/redis-trib.rb /usr/local/bin/
```
**# 4.创建集群:**
```
[root@host51 ~]# redis-trib.rb create --replicas 1 \
192.168.166.207:6380 \
192.168.166.207:7001 \
192.168.166.208:6380 \
192.168.166.208:7001 \
192.168.166.209:6380 \
192.168.166.209:7001
cat /var/lib/redis/6052/nodes-6052.conf #集群配置文件
每台 redis服务 在本机登录 查看集群信息
```
**# 5.检测集群**
```
redis-trib.rb check 192.168.166.207:6380
>>> Performing Cluster Check (using node 192.168.166.207:6380)
S: b33290c16e796b87b41aa3749e373c57786749d8 192.168.166.207:6380
slots: (0 slots) slave
replicates b6aaa9768e32356b82a80b74c96b43700a6689fa
M: 9f35587e04ef19a9662525a7653b36a620d89ca5 192.168.166.207:7701
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: daafc5ff19fb57a359d2a32f05705fc37e9d8a12 192.168.166.208:6380
slots: (0 slots) slave
replicates 1c0260b0b0ffc998f1b29852b01d1d0d9196acb3
M: b6aaa9768e32356b82a80b74c96b43700a6689fa 192.168.166.209:6380
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 1c0260b0b0ffc998f1b29852b01d1d0d9196acb3 192.168.166.208:7701
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: f4103866ad625d94d48f7251b43c42e9471c458a 192.168.166.209:7701
slots: (0 slots) slave
replicates 9f35587e04ef19a9662525a7653b36a620d89ca5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
```