---
# System prepended metadata

title: 'Redis cluster '
tags: [AVATAR]

---

# Redis cluster

Redis 是一個 in-memory 的 key-value database，常被用在需要快取（Cache）一些資料的場合，可以減輕許多後端資料庫的壓力。


### 環境
* redis
* ruby
* rubygems

![image](https://hackmd.io/_uploads/HkFgtTzDa.png)

### 資夾結構
* log
* RDB
 
![image](https://hackmd.io/_uploads/SkYbYpfPa.png)

### 設計規劃
* Master node   : port 7000
* Slave  node   : port 7100
```
$ su - redis
$ cd /avatar/redis/redis-3.0.7/src
$./redis-cli -c -h 10.37.35.58 -p 7000
  > Auth 1q2w#E$R
  > cluster nodes

"(check if only one master on this server for one node maintenance)
(if two masters in the same node, login to the slave node and do cluster failover)
ex. 
$ ./redis-cli -c -h 10.37.35.58 -p 7000
   > Auth <密碼>
   > cluster failover
   > cluster nodes"
$ exit
$ ps -ef | grep redis | grep -v grep | awk '{print $2}'  | xargs kill 
$ ps -ef|grep redis
$ exit
```


redis 他們是一個 cach DB 
盡量讓每一台的 master 在不同台上
像是 Q1的master 是 Q2的slave, Q2 的master  是Q3的 slave 這樣
可以上他 fail 的時候 別台 連貫接管
![image](https://hackmd.io/_uploads/r1A0YpGDT.png)



---

# 安裝

### 準備一些基礎套件
```
yum install gcc*
yum install gcc glibc glibc-common gd gd-devel -y
yum install gcc-c++ patch readline readline-devel zlib zlib-devel -y
yum install libyaml-devel libffi-devel openssl-devel make -y
yum install bzip2 autoconf automake libtool bison iconv-devel sqlite-devel -y
```
### 安裝版本

```
redis-3.0.7.tar.gz
ruby-2.3.0.tar.gz
rubygems-1.8.25.tar.gz
```

#### 下載安裝包&安裝
Install Redis Software
裝ruby
```
tar xvzf ruby-2.3.0.tar.gz
cd ruby-2.3.0`
./configure
make
make test
sudo make install
```
裝 rubygems
```
tar xvzf rubygems-1.8.25.tgz 
cd rubygems-1.8.25
sudo ruby setup.rb config
sudo ruby setup.rb setup
sudo ruby setup.rb install
```
 裝 redis
```
tar -zxvf redis-3.0.7.tar.gz
cd redis-3.0.7
make
make test
sudo make install
```

2.	Setting Redis Configuration
1.	Create Directory
```
# mkdir –p redisDB
# redisDB
# cp /avatar/redis/redis-3.0.7/redis.conf /avatar/redis/redis-3.0.7/redisDB/redis.conf.temp
```
2.	Modify redis.conf
```
#cp redis.conf.temp redis_1.conf
#cp redis.conf.temp redis_2.conf
#cp redis.conf.temp redis_3.conf
redis_1_7000/
```
 

redis_2_7100/
 1.	Create Directory
```
# mkdir –p redisDB
# redisDB
# cp /avatar/redis/redis-3.0.7/redis.conf /avatar/redis/redis-3.0.7/redisDB/redis.conf.temp
```
2.	Modify redis.conf
#cp redis.conf.temp redis_1.conf
#cp redis.conf.temp redis_2.conf
#cp redis.conf.temp redis_3.conf
redis_1_7000/
 ![image](https://hackmd.io/_uploads/HkVQSBfP0.png)


redis_2_7100/
 ![image](https://hackmd.io/_uploads/SJvzBBzDC.png)
![image](https://hackmd.io/_uploads/S1hfHHMD0.png)

 
redis_3_7200/
 ![image](https://hackmd.io/_uploads/HJ5QBHzwA.png)

#vi redis_1.conf 	(for 1st redis DB master & slave)
port 7000 
daemonize yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
logfile "/avatar/redis_data/redis_1_7000/log/redis_1_7000.log"
dir /avatar/redis_data/redis_1_7000/RDB/
#### vi redis_2.conf 	(for 2nd redis DB master & slave)
port 7100 
daemonize yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
logfile "/avatar/redis_data/redis_2_7100/log/redis_2_7100.log"
dir /avatar/redis_data/redis_2_7100/RDB/

#### vi redis_3.conf 	(for 3rd redis DB master & slave)
port 7200 
daemonize yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
logfile "/avatar/redis_data/redis_3_7200/log/redis_3_7200.log"
dir /avatar/redis_data/redis_3_7200/RDB/

In PRD1 
```
# mkdir /avatar/redis_data/redis_1_7000/RDB
# mkdir /avatar/redis_data/redis_1_7000/log
# mkdir /avatar/redis_data/redis_3_7200/RDB
# mkdir /avatar/redis_data/redis_3_7200/log
```
In PRD2
```
# mkdir /avatar/redis_data/redis_1_7000/RDB
# mkdir /avatar/redis_data/redis_1_7000/log
# mkdir /avatar/redis_data/redis_2_7100/RDB
# mkdir /avatar/redis_data/redis_2_7100/log
```
In PRD3
```
# mkdir /avatar/redis_data/redis_2_7100/RDB
# mkdir /avatar/redis_data/redis_2_7100/log
# mkdir /avatar/redis_data/redis_3_7200/RDB
# mkdir /avatar/redis_data/redis_3_7200/log
```

Dispatch redis.conf to all DB server
In PRD1
```
# cd /avatar/redis_data
# cp redis_1.conf /avatar/redis_data/redis_1_7000/redis.conf
# cp redis_3.conf /avatar/redis_data/redis_3_7200/redis.conf
# scp redis_1.conf xx.xx.xx.xx:/avatar/redis_data/redis_1_7000/redis.conf (PRD2)
# scp redis_2.conf xx.xx.xx.xx:/avatar/redis_data/redis_2_7100/redis.conf (PRD2)
# scp redis_2.conf xx.xx.xx.xx:/avatar/redis_data/redis_1_7100/redis.conf (PRD3)
# scp redis_3.conf xx.xx.xx.xx:/avatar/redis_data/redis_1_7200/redis.conf (PRD3)
```

Start 6 redis DB in PRD1, PRD2 and PRD3
(1)In PRD1
```
# cd /avatar/redis_data/redis_1_7000
# redis-server ./redis.conf
# cd /avatar/redis_data/redis_3_7200
# redis-server ./redis.conf
```
(2)In PRD2
```
# cd /avatar/redis_data/redis_1_7000
# redis-server ./redis.conf
# cd /avatar/redis_data/redis_2_7100
# redis-server ./redis.conf
```
(3)In PRD3
```
# cd /avatar/redis_data/redis_2_7100
# redis-server ./redis.conf
# cd /avatar/redis_data/redis_3_7200
# redis-server ./redis.conf
```

3.	Create Redis Cluster
Back to PRD1
#### gem install redis 
#### wget https://rubygems.global.ssl.fastly.net/gems/redis-3.2.1.gem
(//此步驟因無網路無法成功改執行下面的指令)

```
# sudo gem install -l ./redis-3.2.1.gem
# cd /avatar/redis/redis-3.0.7/src/
```
3.1 Start to create master nodes for redis cluster
(1):7000 (2):7100 (3):7200
#./redis-trib.rb  create --replicas 0 10.37.37.145:7000 10.37.37.146:7100 10.37.37.147:7200  
#### yes 同意集群

3.1 Adding slave nodes for each master node 
slave 分布
(1),(2)
(2),(3)
(1),(3)
#./redis-trib.rb add-node --slave 10.37.37.146:7000 10.37.37.145:7000
#./redis-trib.rb add-node --slave 10.37.37.147:7100 10.37.37.146:7100
#./redis-trib.rb add-node --slave 10.37.37.145:7200 10.37.37.147:7200

 
redis_3_7200/
 
#vi redis_1.conf 	(for 1st redis DB master & slave)
```
port 7000 
daemonize yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
logfile "/avatar/redis_data/redis_1_7000/log/redis_1_7000.log"
dir /avatar/redis_data/redis_1_7000/RDB/
```
#### vi redis_2.conf 	(for 2nd redis DB master & slave)
```
port 7100 
daemonize yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
logfile "/avatar/redis_data/redis_2_7100/log/redis_2_7100.log"
dir /avatar/redis_data/redis_2_7100/RDB/
```

#### vi redis_3.conf 	(for 3rd redis DB master & slave)
```
port 7200 
daemonize yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
logfile "/avatar/redis_data/redis_3_7200/log/redis_3_7200.log"
dir /avatar/redis_data/redis_3_7200/RDB/
```

In PRD1 
```
# mkdir /avatar/redis_data/redis_1_7000/RDB
# mkdir /avatar/redis_data/redis_1_7000/log
# mkdir /avatar/redis_data/redis_3_7200/RDB
# mkdir /avatar/redis_data/redis_3_7200/log
```
In PRD2
```
# mkdir /avatar/redis_data/redis_1_7000/RDB
# mkdir /avatar/redis_data/redis_1_7000/log
# mkdir /avatar/redis_data/redis_2_7100/RDB
# mkdir /avatar/redis_data/redis_2_7100/log
```
In PRD3
```
# mkdir /avatar/redis_data/redis_2_7100/RDB
# mkdir /avatar/redis_data/redis_2_7100/log
# mkdir /avatar/redis_data/redis_3_7200/RDB
# mkdir /avatar/redis_data/redis_3_7200/log
```

Dispatch redis.conf to all DB server
In PRD1
```
# cd /avatar/redis_data
# cp redis_1.conf /avatar/redis_data/redis_1_7000/redis.conf
# cp redis_3.conf /avatar/redis_data/redis_3_7200/redis.conf
# scp redis_1.conf xx.xx.xx.xx:/avatar/redis_data/redis_1_7000/redis.conf (PRD2)
# scp redis_2.conf xx.xx.xx.xx:/avatar/redis_data/redis_2_7100/redis.conf (PRD2)
# scp redis_2.conf xx.xx.xx.xx:/avatar/redis_data/redis_1_7100/redis.conf (PRD3)
# scp redis_3.conf xx.xx.xx.xx:/avatar/redis_data/redis_1_7200/redis.conf (PRD3)
```

Start 6 redis DB in PRD1, PRD2 and PRD3
(1)In PRD1
```
# cd /avatar/redis_data/redis_1_7000
# redis-server ./redis.conf
# cd /avatar/redis_data/redis_3_7200
# redis-server ./redis.conf
```
(2)In PRD2
```
# cd /avatar/redis_data/redis_1_7000
# redis-server ./redis.conf
# cd /avatar/redis_data/redis_2_7100
# redis-server ./redis.conf
```
(3)In PRD3
```
# cd /avatar/redis_data/redis_2_7100
# redis-server ./redis.conf
# cd /avatar/redis_data/redis_3_7200
# redis-server ./redis.conf
```

3.	Create Redis Cluster
Back to PRD1
# gem install redis 
# wget https://rubygems.global.ssl.fastly.net/gems/redis-3.2.1.gem
(//此步驟因無網路無法成功改執行下面的指令)

```
# sudo gem install -l ./redis-3.2.1.gem
# cd /avatar/redis/redis-3.0.7/src/
```
3.1 Start to create master nodes for redis cluster
(1):7000 (2):7100 (3):7200
#./redis-trib.rb  create --replicas 0 10.37.37.145:7000 10.37.37.146:7100 10.37.37.147:7200  
# yes 同意集群

3.1 Adding slave nodes for each master node 
slave 分布
(1),(2)
(2),(3)
(1),(3)
#./redis-trib.rb add-node --slave 10.37.37.146:7000 10.37.37.145:7000
#./redis-trib.rb add-node --slave 10.37.37.147:7100 10.37.37.146:7100
#./redis-trib.rb add-node --slave 10.37.37.145:7200 10.37.37.147:7200
