本地搭建redis叢集
- 下載並解壓
cd redis-3.2.4
make && make install
3. 將 redis-trib.rb 複製到 /usr/local/bin 目錄下
cd src
cp redis-trib.rb /usr/local/bin/
4.建立redis 節點
在 /usr/local/ 目錄下建立 redis-cluster 目錄;
mkdir redis-cluster
在redis-cluster目錄下建立名為7000、7001、7002、7003、7004、7005的目錄,並將redis-3.2.4目錄下 redis.conf 拷貝到這六個目錄中
cp redis.conf ./usr/local/7000 ./usr/local/7001 ./usr/local/7002 ./usr/local/7003 ./usr/local/7004 ./usr/local/7005
編輯每個目錄下的redis.conf
daemonize yes //redis後臺執行
pidfile /var/run/redis_7000.pid //pidfile檔案對應7000,7002,7003,7004,7005
port 7000 //埠7000,7002,7003,7004,7005
cluster-enabled yes //開啟叢集
cluster-config-file nodes_7000.conf //叢集的配置 配置檔案首次啟動自動生成7000,7002,7003,7004,7005
cluster-node-timeout 5000 //節點超時時間
appendonly yes //aof日誌開啟 有需要就開啟,它會每次寫操作都記錄一條日誌
5.啟動節點
redis-server redis_cluster/7000/redis.conf
redis-server redis_cluster/7001/redis.conf
redis-server redis_cluster/7002/redis.conf
redis-server redis_cluster/7003/redis.conf
redis-server redis_cluster/7004/redis.conf
redis-server redis_cluster/7005/redis.conf
6.檢查 redis 啟動情況
ps -ef | grep redis
root 2895 1 0 11:03 ? 00:00:13 redis-server *:7004 [cluster]
root 2910 1 0 11:07 ? 00:00:10 redis-server *:7001 [cluster]
root 3006 5023 0 14:01 pts/4 00:00:00 grep redis
root 11447 1 0 00:13 ? 00:01:06 redis-server *:7005 [cluster]
root 11470 1 0 00:17 ? 00:01:05 redis-server *:7003 [cluster]
root 11477 1 0 00:17 ? 00:01:07 redis-server *:7002 [cluster]
root 11493 1 0 00:19 ? 00:01:07 redis-server *:7000 [cluster]
7.安裝ruby
//不能yum安裝 ,版本太低,要求至少2.2.2
wget https://cache.ruby-lang.org/pub/ruby/2.4/ruby-2.4.2.tar.gz
tar -xvzf ruby-2.4.2.tgz
cd ruby-2.4.2
./configure
make && make install
yum -y install ruby-devel rubygems rpm-build
gem install redis
8.建立叢集
Redis 官方提供了 redis-trib.rb 這個工具,就在解壓目錄的 src 目錄中,將它複製到 /usr/local/bin 目錄中,可以直接在命令列中使用了。使用下面這個命令即可完成安裝。
redis-trib.rb create –replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005
9.驗證效果
redis-cli -c -p 7000 // -c 是通過叢集方式
set name 122
-> Redirected to slot [5798] located at 127.0.0.1:7004
OK
10.模擬其中一臺宕機
殺死7001程序
root 2910 1 0 11:07 ? 00:00:11 redis-server *:7001 [cluster]
kill 2910
執行儲存,轉到slave 7004
set name 122
-> Redirected to slot [5798] located at 127.0.0.1:7004
OK
11.將7001 重啟加入叢集
redis-server /usr/local/redis-cluster/7001/redis.conf //啟動7001
redis-trib.rb fix 127.0.0.1:7001 //修復7001對應的槽位
redis-trib.rb check 127.0.0.1:7001 //新增到叢集中
Performing Cluster Check (using node 127.0.0.1:7001)
M: 21a659d47405118f15009e982cdb800501f25da4 127.0.0.1:7001
slots:0-16383 (16384 slots) master
0 additional replica(s)
[OK] All nodes agree about slots configuration.
Check for open slots…
Check slots coverage…
[OK] All 16384 slots covered.