# Redis主从集群的搭建
**# 1.环境部署。**
准备6台redis服务器,具体要求如下:
|ip地址 | 端口 | etho | 日志文件名
---|---|---|---
192.168.11.234 | 6379 | 都可以接收连接请求 | redis6379.log
192.168.11.234 | 6380 | 都可以接收连接请求 | redis6380.log
192.168.11.235 | 6379 | 都可以接收连接请求 | redis6379.log
192.168.11.235 | 6380 | 都可以接收连接请求 | redis6380.log
192.168.11.236 | 6379 | 都可以接收连接请求 | redis6379.log
192.168.11.236 | 6380 | 都可以接收连接请求 | redis6380.log
**# 2.安装软件**
```
~# wget http://download.redis.io/releases/redis-4.0.11.tar.gz
~# tar xvf redis-4.0.11.tar.gz
~# apt-get install gcc
~# apt-get install make
~# cd redis-4.0.11/
~# make && make install
~# ps -ef | grep redis
```
**# 3.在6台redis服务器上运行服务,按照如下要求修改配置文件后,重启redis服务。**
```
]#vim /etc/redis/6379.conf
bind 192.168.11.234 #只指定物理接口ip地址
port 6379 #不允许相同
daemonize yes #开机自启动(守护进程)
cluster-enabled yes # 开启集群模式
pidfile /var/run/redis_6379.pid #不允许相同
pidfile nodes-6379.conf #不允许相同
cluster-node-timeout 5000 #集群节点之间通信超时时间单位秒
masterauth D^f1KKv%
requirepass D^f1KKv% #设置集群密码
:wq
更改配置文件:(密码配置)
]# vim /etc/init.d/redis_6379
$CLIEXEC -p $REDISPORT -a D^f1KKv% shutdown
```
**# 4.安装解释ruby代码的软件包**
```
~# apt-get install ruby rubygems
~# sudo apt-get update
sudo apt-get upgrade
sudo apt-get install make gcc openssl libssl-dev git tig monit unzip
sudo apt-get install imagemagick libmagickwand-dev
sudo apt-get install libxslt-dev libxml2-dev
安装ruby连接redis 接口程序 gem
[root@host51 redis-cluster]# which gem
/usr/bin/gem
~# gem install redis
# vim :/usr/local/rvm/gems/ruby-2.4.4/gems/redis-4.0.2/lib/redis/client.rb (集群设置密码登陆需要配置这个文件)
class Redis
class Client
DEFAULTS = {
:url => lambda { ENV["REDIS_URL"] },
:scheme => "redis",
:host => "127.0.0.1",
:port => 6379,
:path => nil,
:timeout => 5.0,
:password => "D^f1KKv%",
:db => 0,
:driver => nil,
:id => nil,
:tcp_keepalive => 0,
:reconnect_attempts => 1,
:inherit_socket => false
}
```
**# 5.生成创建集群的脚本redis-trib.rb**
```
~# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
~# cd
~# cp redis-4.0.11/src/redis-trib.rb /usr/local/bin/
停止服务
]# redis-cli -h 192.168.11.234 -p 6379 [ -a 密码 ] shutdown
启动服务
]# /etc/init.d/redis_6379 start
连接服务
[root@host56 ~]# redis-cli -h 192.168.11.234 -p 6379
```
**# 6.创建集群:**
```
redis-trib.rb create --replicas 1 \
192.168.11.234:6379 \
192.168.11.234:6380 \
192.168.11.235:6379 \
192.168.11.235:6380 \
192.168.11.236:6379 \
192.168.11.236:6380
cat /var/lib/redis/6052/nodes-6052.conf #集群配置文件
```
**# 7.检测集群**
```
连接集群中的主机:
redis-cli -c -h 192.168.11.234 -p 6379
192.168.11.234:6379> CLUSTER info
192.168.11.234:6379> CLUSTER nodes
192.168.11.234:6379> quit
~# redis-trib.rb check 192.168.11.234:6379
>>> Performing Cluster Check (using node 192.168.11.234:6379)
S: b33290c16e796b87b41aa3749e373c57786749d8 192.168.11.234:6379
slots: (0 slots) slave
replicates b6aaa9768e32356b82a80b74c96b43700a6689fa
M: 9f35587e04ef19a9662525a7653b36a620d89ca5 192.168.11.235:6379
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: daafc5ff19fb57a359d2a32f05705fc37e9d8a12 192.168.11.235:6380
slots: (0 slots) slave
replicates 1c0260b0b0ffc998f1b29852b01d1d0d9196acb3
M: b6aaa9768e32356b82a80b74c96b43700a6689fa 192.168.11.236:6380
slots:5461-10922 (5462 slots) master
1 additional replica(s)
M: 1c0260b0b0ffc998f1b29852b01d1d0d9196acb3 192.168.11.236:6379
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: f4103866ad625d94d48f7251b43c42e9471c458a 192.168.11.234:6380
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.
```