因設計10萬級以上qps秒殺開源系統,需部署redis 集羣
雖說單機多節點,實際與真實多物理機多節點方式幾乎一樣
一、安裝redis
1、安裝gcc環境
yum install gcc-c++
2、下載源碼包並解壓
wget http://download.redis.io/releases/redis-3.2.4.tar.gz
tar -zxvf redis-3.2.4.tar.gz
cd redis-3.2.4
3、安裝ruby2.4.1並編譯(一定要安裝大於2.2.2的ruby,要不然redis在make的時候會報錯)
wget http://ftp.ruby-lang.org/pub/ruby/2.4/ruby-2.4.1.tar.gz
tar -zxvf ruby-2.4.1.tar.gz
cd ruby-2.4.1
mkdir -p /usr/local/ruby
./configure --prefix=/usr/local/ruby
make && make install
ln -s /usr/local/ruby/bin/ruby /usr/bin/ruby
4、安裝
make
make install PREFIX=/usr/local/redis
cp redis.conf /usr/local/redis/bin/redis.conf
chmod +x /usr/local/redis/bin/redis.conf
5、啓動redis
./redis-server redis.conf
6、驗證
[root@host-172-16-80-177 bin]# ./redis-cli -p 6379
127.0.0.1:6379> get
(error) ERR wrong number of arguments for 'get' command
127.0.0.1:6379> set aa hzb
OK
127.0.0.1:6379> get aa
"hzb"
二、搭建redis集羣(集羣模式)
1、在/usr/local/下面創建redis-cluster目錄
mkdir -p /usr/local/redis-cluster
2、複製/usr/local/redis/bin目錄到redis-cluster裏面並重命名爲redis1
cd /usr/local
cp -r redis/bin redis-cluster/redis1
請注意:這步cp 操作請先執行flushdb清庫操作,否則後續部署集羣會遇到各種坑
3、將redis1在同目錄複製3份分別起名redis2,redis3,redis4,redis5,redis6(必須6個節點以上才能創建集羣)
[root@host-172-16-80-177 redis-cluster]# pwd
/usr/local/redis-cluster
[root@host-172-16-80-177 redis-cluster]# cp -r redis1 redis2
[root@host-172-16-80-177 redis-cluster]# cp -r redis1 redis3
[root@host-172-16-80-177 redis-cluster]# cp -r redis1 redis4
[root@host-172-16-80-177 redis-cluster]# cp -r redis1 redis5
[root@host-172-16-80-177 redis-cluster]# cp -r redis1 redis6
分別修改爲redis-7001.conf,redis-7002.conf 後面依次到redis-7006.conf
以redis-7001爲例:
daemonize yes
Port 7001
logfile "./redis-7001.log"
protected-mode no
pidfile /var/run/redis_7001.pid
cluster-enabled yes
4、將redis-trib.rb復到制redis-cluster目錄裏面
[root@host-172-16-80-177 redis-cluster]# cp /root/redis-3.2.4/src/redis-trib.rb .
[root@host-172-16-80-177 redis-cluster]# ll
total 76
drwxr-xr-x 2 root root 4096 Oct 19 22:37 redis1
drwxr-xr-x 2 root root 4096 Oct 19 22:48 redis2
drwxr-xr-x 2 root root 4096 Oct 19 22:49 redis3
drwxr-xr-x 2 root root 4096 Oct 19 22:49 redis4
drwxr-xr-x 2 root root 4096 Oct 19 22:49 redis5
drwxr-xr-x 2 root root 4096 Oct 19 22:49 redis6
-rwxr-xr-x 1 root root 60852 Oct 19 22:52 redis-trib.rb
5、安裝gem
yum install rubygems -y
gem install redis
上面這一步可能會出現:
Fetching: redis-4.0.1.gem (100%)
ERROR: Error installing redis:
redis requires Ruby version >= 2.2.2.
解決辦法參考:https://www.cnblogs.com/PatrickLiu/p/8454579.html
7、分別進入redis1,redis2,redis3,redis4,redis5,redis6裏面執行
./redis-server redis-7001.conf
......
將6個節點啓動
[root@k8s-node-1 redis6]# ps -ef | grep redis
root 81658 1 0 19:10 ? 00:00:01 ./redis-server 192.168.176.136:7001 [cluster]
root 81737 1 0 19:10 ? 00:00:01 ./redis-server 192.168.176.136:7002 [cluster]
root 82221 1 0 19:14 ? 00:00:00 ./redis-server 192.168.176.136:7003 [cluster]
root 82471 1 0 19:17 ? 00:00:00 ./redis-server 192.168.176.136:7004 [cluster]
root 82651 1 0 19:18 ? 00:00:00 ./redis-server 192.168.176.136:7005 [cluster]
root 82961 1 0 19:21 ? 00:00:00 ./redis-server 192.168.176.136:7006 [cluster]
root 82997 10818 0 19:21 pts/2 00:00:00 grep --color=auto redis
8、用redis-trib.rb構建集羣
cd /usr/local/redis-cluster/
./redis-trib.rb create --replicas 1 192.168.176.136:7001 192.168.176.136:7002 192.168.176.136:7003 192.168.176.136:7004 192.168.176.136:7005 192.168.176.136:7006
出現下面的join信息纔算部署成功!
如果出現的其他問題請參考解決方案:https://blog.csdn.net/vtopqx/article/details/50235737
如果一直Waiting for the cluster to join請參考解決方案 :https://blog.csdn.net/truong/article/details/52531103
9驗證
場景:
一個個手寫shell是不是很麻煩啊,所以可以批量編寫簡單的shell執行一鍵redis集羣啓動,注意vi 手寫進去,否則會出現
No such file or directory 等格式問題
#!/bin/bash
RETVAL=0
redis_path="/usr/local/redis-cluster/"
cd ${redis_path}
cd redis1/
./redis-server /usr/local/redis-cluster/redis1/redis-7001.conf
cd ../redis2/
./redis-server /usr/local/redis-cluster/redis2/redis-7002.conf
cd ../redis3/
./redis-server /usr/local/redis-cluster/redis3/redis-7003.conf
cd ../redis4/
./redis-server /usr/local/redis-cluster/redis4/redis-7004.conf
cd ../redis5/
./redis-server /usr/local/redis-cluster/redis5/redis-7005.conf
cd ../redis6/
./redis-server /usr/local/redis-cluster/redis6/redis-7006.conf
exit $RETVAL
參考
集羣
https://blog.csdn.net/vtopqx/article/details/50235737
https://www.cnblogs.com/boshen-hzb/p/7699783.html
http://blog.51yip.com/nosql/1725.html
http://blog.51yip.com/nosql/1726.html redis cluster 添加 刪除 重分配 節點
https://blog.csdn.net/truong/article/details/52531103
腳本
https://blog.csdn.net/love__coder/article/details/7693390
https://blog.csdn.net/caopingtao/article/details/79023928
https://blog.csdn.net/fengyong7723131/article/details/53196382