一、redis節點準備
1、上傳redis安裝文件,並解壓
[root@master redis-cluster]# ll
總用量 1708
drwxrwxr-x. 6 root root 4096 12月 12 2018 redis-4.0.12
-rw-r--r--. 1 root root 1740544 8月 29 22:12 redis-4.0.12.tar.gz
2、安裝並編譯redis
cd redis-4.0.12
make && make install
3、創建六個文件夾,7000、7001、7002、7003、7004、7005.
[root@master redis-cluster]# ls
7000 7001 7002 7003 7004 7005
4、將redis的src文件夾下的redis文件拷貝到六個文件夾並分別修改
[root@master redis-cluster]# cd redis-4.0.12
[root@master redis-4.0.12]# ls
00-RELEASENOTES CONTRIBUTING deps Makefile README.md runtest runtest-sentinel src utils
BUGS COPYING INSTALL MANIFESTO redis.conf runtest-cluster sentinel.conf tests
[root@master redis-4.0.12]# cp redis.conf ../7000
#配置
#redis後臺運行
daemonize yes
#pidfile文件 7000~7005
pidfile /var/run/redis_7000.pid
#7000~7005
port 7000
#開啓集羣
cluster-enabled yes
#配置文件首次啓動自動生成 7000~7005
cluster-config-file nodes_7000.conf
#請求超時
cluster-node-timeout 5000
#aof日誌
appendonly no
#保護機制
protected-mode no
#綁定地址,需要別的機器能ping通的地址
bind 192.168.21.137
或者將bind 127.0.0.1 注掉
5、創建start-all.sh文件
cd 7000
redis-server redis.conf
cd ..
cd 7001
redis-server redis.conf
cd ..
cd 7002
redis-server redis.conf
cd ..
cd 7003
redis-server redis.conf
cd ..
cd 7004
redis-server redis.conf
cd ..
cd 7005
redis-server redis.conf
cd ..
6、添加可執行權限
[root@master redis-cluster]# chmod u+x start-all.sh
7、啓動節點並檢查是否成功
[root@master redis-cluster]# ./start-all.sh
4742:C 29 Aug 22:49:39.392 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4742:C 29 Aug 22:49:39.392 # Redis version=4.0.12, bits=64, commit=00000000, modified=0, pid=4742, just started
4742:C 29 Aug 22:49:39.392 # Configuration loaded
4744:C 29 Aug 22:49:39.408 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4744:C 29 Aug 22:49:39.408 # Redis version=4.0.12, bits=64, commit=00000000, modified=0, pid=4744, just started
4744:C 29 Aug 22:49:39.408 # Configuration loaded
4746:C 29 Aug 22:49:39.533 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4746:C 29 Aug 22:49:39.534 # Redis version=4.0.12, bits=64, commit=00000000, modified=0, pid=4746, just started
4746:C 29 Aug 22:49:39.534 # Configuration loaded
4754:C 29 Aug 22:49:39.674 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4754:C 29 Aug 22:49:39.675 # Redis version=4.0.12, bits=64, commit=00000000, modified=0, pid=4754, just started
4754:C 29 Aug 22:49:39.675 # Configuration loaded
4759:C 29 Aug 22:49:39.783 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4759:C 29 Aug 22:49:39.783 # Redis version=4.0.12, bits=64, commit=00000000, modified=0, pid=4759, just started
4759:C 29 Aug 22:49:39.783 # Configuration loaded
4764:C 29 Aug 22:49:39.853 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4764:C 29 Aug 22:49:39.853 # Redis version=4.0.12, bits=64, commit=00000000, modified=0, pid=4764, just started
4764:C 29 Aug 22:49:39.854 # Configuration loaded
[root@master redis-cluster]# ps -aux | grep redis
root 4743 0.3 0.9 147360 9612 ? Ssl 22:49 0:00 redis-server *:7000 [cluster]
root 4745 0.3 0.9 147360 9612 ? Ssl 22:49 0:00 redis-server *:7001 [cluster]
root 4753 0.4 0.9 147360 9612 ? Ssl 22:49 0:00 redis-server *:7003 [cluster]
root 4758 0.4 0.9 147360 9612 ? Ssl 22:49 0:00 redis-server *:7002 [cluster]
root 4760 0.4 0.9 147360 9608 ? Ssl 22:49 0:00 redis-server *:7004 [cluster]
root 4765 0.4 0.9 147360 9612 ? Ssl 22:49 0:00 redis-server *:7005 [cluster]
root 4773 0.0 0.0 112720 984 pts/0 S+ 22:49 0:00 grep --color=auto redis
[root@master redis-cluster]# netstat -tnlp | grep redis
tcp 0 0 0.0.0.0:7004 0.0.0.0:* LISTEN 4760/redis-server *
tcp 0 0 0.0.0.0:7005 0.0.0.0:* LISTEN 4765/redis-server *
tcp 0 0 0.0.0.0:17000 0.0.0.0:* LISTEN 4743/redis-server *
tcp 0 0 0.0.0.0:17001 0.0.0.0:* LISTEN 4745/redis-server *
tcp 0 0 0.0.0.0:17002 0.0.0.0:* LISTEN 4758/redis-server *
tcp 0 0 0.0.0.0:17003 0.0.0.0:* LISTEN 4753/redis-server *
tcp 0 0 0.0.0.0:17004 0.0.0.0:* LISTEN 4760/redis-server *
tcp 0 0 0.0.0.0:17005 0.0.0.0:* LISTEN 4765/redis-server *
tcp 0 0 0.0.0.0:7000 0.0.0.0:* LISTEN 4743/redis-server *
tcp 0 0 0.0.0.0:7001 0.0.0.0:* LISTEN 4745/redis-server *
tcp 0 0 0.0.0.0:7002 0.0.0.0:* LISTEN 4758/redis-server *
tcp 0 0 0.0.0.0:7003 0.0.0.0:* LISTEN 4753/redis-server *
二、創建集羣
redis 官方提供了一個 redis-trib.rb
(src目錄下) 工具用於搭建集羣。很明顯是 ruby 寫的,所以需要 ruby 環境。
1、安裝基礎的環境
[root@localhost redis-cluster]# yum -y install zlib ruby ruby-devel rubygems rpm-build
2、用 gem 安裝 redis 接口
Fetching: redis-4.1.2.gem (100%)
Successfully installed redis-4.1.2
Parsing documentation for redis-4.1.2
Installing ri documentation for redis-4.1.2
Done installing documentation for redis after 4 seconds
1 gem installed
3、確保開放端口
4、將redis安裝目錄src下的文件redis-trib.rb拷貝到redis-cluster目錄下
[root@master src]# cp redis-trib.rb /usr/local/redis-cluster/
運行 redis-trib.rb
查看 集羣創建幫助
5、創建集羣(前提:節點已啓動)
#其中 --replicas 1 意思爲爲每個 master 分配 1 各 slave
[root@master redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.1.106:7000 192.168.1.106:7001 192.168.1.106:7002 192.168.1.106:7003 192.168.1.106:7004 192.168.1.106:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.1.106:7000
192.168.1.106:7001
192.168.1.106:7002
Adding replica 192.168.1.106:7004 to 192.168.1.106:7000
Adding replica 192.168.1.106:7005 to 192.168.1.106:7001
Adding replica 192.168.1.106:7003 to 192.168.1.106:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 29e9c1ac41c7dcf799a1d9c3ed8cd6cb8e4195fe 192.168.1.106:7000
slots:0-5460 (5461 slots) master
M: 03add56b8313d087da36e878c52d5a64fb1123a3 192.168.1.106:7001
slots:5461-10922 (5462 slots) master
M: e1f3cdc41c008d731f84171b5fa96186608725f6 192.168.1.106:7002
slots:10923-16383 (5461 slots) master
S: 4d07f84542282c0845d250e8f3b99d8b4b120c79 192.168.1.106:7003
replicates e1f3cdc41c008d731f84171b5fa96186608725f6
S: 99a6fbe963492188ff136213c59519313c41862d 192.168.1.106:7004
replicates 29e9c1ac41c7dcf799a1d9c3ed8cd6cb8e4195fe
S: 5b01b3995061ee96fcf023ac3cb43487f6230d2d 192.168.1.106:7005
replicates 03add56b8313d087da36e878c52d5a64fb1123a3
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join.......
>>> Performing Cluster Check (using node 192.168.1.106:7000)
M: 29e9c1ac41c7dcf799a1d9c3ed8cd6cb8e4195fe 192.168.1.106:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
M: 03add56b8313d087da36e878c52d5a64fb1123a3 192.168.1.106:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
S: 99a6fbe963492188ff136213c59519313c41862d 192.168.1.106:7004
slots: (0 slots) slave
replicates 29e9c1ac41c7dcf799a1d9c3ed8cd6cb8e4195fe
S: 4d07f84542282c0845d250e8f3b99d8b4b120c79 192.168.1.106:7003
slots: (0 slots) slave
replicates e1f3cdc41c008d731f84171b5fa96186608725f6
M: e1f3cdc41c008d731f84171b5fa96186608725f6 192.168.1.106:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 5b01b3995061ee96fcf023ac3cb43487f6230d2d 192.168.1.106:7005
slots: (0 slots) slave
replicates 03add56b8313d087da36e878c52d5a64fb1123a3
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
創建集羣成功
6、測試集羣
[root@master redis-cluster]# redis-cli -h 192.168.1.106 -p 7000
192.168.1.106:7000>
三、安裝過程中遇到的問題
問題1、用 gem 安裝 redis 接口,無響應
解決辦法:更換gem源代碼鏡像
1、查看當前gem的源地址
[root@master ~]# gem sources #列出默認源
*** CURRENT SOURCES ***
https://rubygems.org/
2、刪除默認源代碼鏡像
[root@master ~]# gem sources --remove https://rubygems.org/
https://rubygems.org/ removed from sources
3、更換鏡像,添加科大源
[root@master ~]# gem sources -a https://mirrors.ustc.edu.cn/rubygems/ #添加科大源
https://mirrors.ustc.edu.cn/rubygems/ added to sources
問題2、gem安裝redis報錯,redis requires Ruby version >= 2.3.0.
[root@master ~]# gem install redis
Fetching: redis-4.1.2.gem (100%)
ERROR: Error installing redis:
redis requires Ruby version >= 2.3.0.
解決:CentOS7 yum庫中ruby的版本支持到 2.0.0,可gem 安裝redis需要最低是2.2.2,採用rvm來更新ruby
1、查看當前ruby的版本
[root@master ~]# ruby --version
ruby 2.0.0p648 (2015-12-16) [x86_64-linux]
2、安裝RVM需先安裝crul,安裝curl
yum install curl
3、安裝RVM,按提示進行相關操作
[root@master ~]# curl -L get.rvm.io | bash -s stable
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 194 100 194 0 0 89 0 0:00:02 0:00:02 --:--:-- 89
100 24535 100 24535 0 0 4108 0 0:00:05 0:00:05 --:--:-- 8873
Downloading https://github.com/rvm/rvm/archive/1.29.9.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.9/1.29.9.tar.gz.asc
gpg: 已創建目錄‘/root/.gnupg’
gpg: 新的配置文件‘/root/.gnupg/gpg.conf’已建立
gpg: 警告:在‘/root/.gnupg/gpg.conf’裏的選項於此次運行期間未被使用
gpg: 鑰匙環‘/root/.gnupg/pubring.gpg’已建立
gpg: 於 2019年07月10日 星期三 16時31分02秒 CST 創建的簽名,使用 RSA,鑰匙號 39499BDB
gpg: 無法檢查簽名:沒有公鑰
GPG signature verification failed for '/usr/local/rvm/archives/rvm-1.29.9.tgz' - 'https://github.com/rvm/rvm/releases/download/1.29.9/1.29.9.tar.gz.asc'! Try to install GPG v2 and then fetch the public key:
gpg2 --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
or if it fails:
command curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
command curl -sSL https://rvm.io/pkuczynski.asc | gpg2 --import -
In case of further problems with validation please refer to https://rvm.io/rvm/security
解決安裝RVM過程中報錯的兩種方式
- 添加公鑰
curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
curl -sSL https://rvm.io/pkuczynski.asc | gpg2 --import -
- 或者直接執行
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
然後再次執行curl -L get.rvm.io | bash -s stable 安裝RVM成功。
4、查看配置文件
[root@master ~]# find / -name rvm -print
/usr/local/rvm
/usr/local/rvm/src/rvm
/usr/local/rvm/src/rvm/bin/rvm
/usr/local/rvm/src/rvm/lib/rvm
/usr/local/rvm/src/rvm/scripts/rvm
/usr/local/rvm/bin/rvm
/usr/local/rvm/lib/rvm
/usr/local/rvm/scripts/rvm
5、激活RVM,使配置生效
[root@master ~]# source /usr/local/rvm/scripts/rvm
6、下載RVM依賴
[root@master ~]# rvm requirements
Checking requirements for centos.
Installing requirements for centos.
Installing required packages: autoconf, automake, bison, libffi-devel, libtool, readline-devel, sqlite-devel..........
Requirements installation successful.
7、查看rvm庫中已知的ruby版本
rvm list known
8、使用RVM更換ruby的版本爲2.3.8
root@master ~]# rvm use 2.3.8 # 使用一個ruby版本
Using /usr/local/rvm/gems/ruby-2.3.8
[root@master ~]# rvm use 2.3.8 --default # 設爲默認
Using /usr/local/rvm/gems/ruby-2.3.8
[root@master ~]# rvm remove 2.0.0 # 刪除一個已知版本
ruby-2.0.0-p648 - #already gone
Using /usr/local/rvm/gems/ruby-2.3.8
[root@master ~]# ruby -v # 查看當前版本
ruby 2.3.8p459 (2018-10-18 revision 65136) [x86_64-linux]