安装redis
yum install -y wget gcc
wget http://download.redis.io/releases/redis-4.0.9.tar.gz
tar xzf redis-4.0.9.tar.gz
cd redis-4.0.9
#编译
make install
单台cluster
1 修改配置
新建6001 ,6002 ,6003,6004,6005 ,6006 6个文件
1) 复制 cp redis.conf /home/redis/6001
redis.conf 修改 redis-6001.conf
vi redis-6001.conf 如下:
port 6001
cluster-enabled yes
cluster-config-file "nodes-6001.conf"
cluster-node-timeout 5000
dir "/home/redis/6001"
bind 172.31.71.128
pidfile "/home/redis/6001/6001_pid.pid"
daemonize yes
# Generated by CONFIG REWRITE
masterauth "test1234"
requirepass "test1234"
notify-keyspace-events "gxE"
2)复制 cp redis.conf /home/redis/6002
redis.conf 修改 redis-6002.conf
vi redis-6002.conf 如下:
port 6002
cluster-enabled yes
cluster-config-file "nodes-6002.conf"
cluster-node-timeout 5000
dir "/home/redis/6001"
bind 172.31.71.128
pidfile "/home/redis/6001/6001_pid.pid"
daemonize yes
# Generated by CONFIG REWRITE
masterauth "test1234"
requirepass "test1234"
notify-keyspace-events "gxE"
3)复制 cp redis.conf /home/redis/ 6003,6004
vi redis-6003.conf 如下:
port 6003
cluster-enabled yes
cluster-config-file "nodes-6002.conf"
cluster-node-timeout 5000
dir "/home/redis/6003"
bind 172.31.71.129
pidfile "/home/redis/6003/6003_pid.pid"
daemonize yes
# Generated by CONFIG REWRITE
masterauth "test1234"
requirepass "test1234"
notify-keyspace-events "gxE"
vi redis-6004.conf 如下:
port 6004
cluster-enabled yes
cluster-config-file "nodes-6004.conf"
cluster-node-timeout 5000
dir "/home/redis/6004"
bind 172.31.71.129
pidfile "/home/redis/6004/6004_pid.pid"
daemonize yes
# Generated by CONFIG REWRITE
masterauth "test1234"
requirepass "test1234"
notify-keyspace-events "gxE"
4)复制 cp redis.conf /home/redis/ 6005,6006
vi redis-6005.conf 如下:
port 6005
cluster-enabled yes
cluster-config-file "nodes-6005.conf"
cluster-node-timeout 5000
dir "/home/redis/6005"
bind 172.31.71.130
pidfile "/home/redis/6005/6005_pid.pid"
daemonize yes
# Generated by CONFIG REWRITE
masterauth "test1234"
requirepass "test1234"
notify-keyspace-events "gxE"
vi redis-6006.conf 如下:
port 6006
cluster-enabled yes
cluster-config-file "nodes-6006.conf"
cluster-node-timeout 5000
dir "/home/redis/6006"
bind 172.31.71.130
pidfile "/home/redis/6006/6006_pid.pid"
daemonize yes
# Generated by CONFIG REWRITE
masterauth "test1234"
requirepass "test1234"
notify-keyspace-events "gxE"
2 启动
172.31.71.128
1) ./src/redis-server /home/redis/6001/redis-6001.conf
2) ./src/redis-server /home/redis/6002/redis-6002.conf
172.31.71.129
3) ./src/redis-server /home/redis/6003/redis-6003.conf
4) ./src/redis-server /home/redis/6004/redis-6004.conf
172.31.71.130
5) ./src/redis-server /home/redis/6005/redis-6005.conf
6) ./src/redis-server /home/redis/6006/redis-6006.conf
用:ps -ef|grep redis 查看是否起动成功
[root@had1 redis-4.0.9]# ps -ef|grep redis
root 9474 6815 0 10:05 pts/5 00:00:00 vi redis-6003.conf
root 9957 1 0 13:46 ? 00:00:00 ./src/redis-server 172.30.71.128:6001 [cluster]
root 9965 1 0 13:46 ? 00:00:00 ./src/redis-server 172.30.71.128:6002 [cluster]
root 9977 6815 0 13:50 pts/5 00:00:00 grep --color=auto redis
[root@had3 redis-4.0.9]# ps -aux|grep redis
root 8925 0.0 0.0 126656 1900 pts/3 T 13:53 0:00 vi redis-6005.conf
root 8938 0.0 0.3 145416 7572 ? Ssl 13:55 0:00 ./src/redis-server 172.30.71.130:6005 [cluster]
root 8943 0.0 0.3 145416 7576 ? Ssl 13:55 0:00 ./src/redis-server 172
[root@had2 redis-4.0.9]# ps -aux|grep redis
root 9335 0.0 0.1 149512 2680 ? Ssl 13:58 0:00 ./src/redis-server 172.30.71.129:6003 [cluster]
root 9340 0.1 0.1 149512 2680 ? Ssl 13:58 0:00 ./src/redis-server 172.30.71.129:6004 [cluster]
root 9355 0.0 0.0 112812 980 pts/4 R+ 13:59 0:00 grep --color=auto redis
如果是三台
[root@had1 redis-4.0.9]# ./src/redis-trib.rb create --replicas 1 172.30.71.128:6001 172.30.71.128:6002 172.30.71.129:6003 172.30.71.129:6004 172.30.71.130:6005 172.30.71.130:6006
>>> Creating cluster>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
172.30.71.128:6001
172.30.71.129:6003
172.30.71.130:6005
Adding replica 172.30.71.129:6004 to 172.30.71.128:6001
Adding replica 172.30.71.130:6006 to 172.30.71.129:6003
Adding replica 172.30.71.128:6002 to 172.30.71.130:6005
M: 17259dbfb616fea6214d8f6f9a2e4fa18d52cd30 172.30.71.128:6001
slots:0-5460 (5461 slots) master
S: 4379a02bb03e6039946a2da33421af0d9bc573dd 172.30.71.128:6002
replicates 08a84d11bea4636ffde76609720c1540ee5ad609
M: bdcb01f0972718ce02d585a44b0c9d50feb98810 172.30.71.129:6003
slots:5461-10922 (5462 slots) master
S: c30832c6308f1101d63d969b9df6a598f2cfd425 172.30.71.129:6004
replicates 17259dbfb616fea6214d8f6f9a2e4fa18d52cd30
M: 08a84d11bea4636ffde76609720c1540ee5ad609 172.30.71.130:6005
slots:10923-16383 (5461 slots) master
S: 7533edb850b2e5a48d1cde554eb9d349e2d7633b 172.30.71.130:6006
replicates bdcb01f0972718ce02d585a44b0c9d50feb98810
Can I set the above configuration? (type 'yes' to accept): yes
*** Aborting...
3 集群起动
3.1安装ruby(三台都要安装)
#yum install ruby -y
#再安装redis插件
#gem install redis
wget https://github.com/postmodern/ruby-install/archive/v0.8.1.tar.gz
tar xzvf v0.8.1.tar.gz
cd ruby-install-0.8.1/bin/
#看下ruby的最新版本列表
./ruby-install --latest
#安装
./ruby-install ruby 2.7.8
会输出Successfully installed ruby 2.7.8 into /opt/rubies/ruby-2.78
ln -s /opt/rubies/ruby-2.7.8/bin/ruby /usr/bin/ruby
ln -s /opt/rubies/ruby-2.7.8/bin/gem /usr/bin/gem
gem sources --add https://gems.ruby-china.com/ --remove https://rubygems.org/
#再安装redis插件
gem install redis
3.2 起动 redis集群
至少6个
>>> Creating cluster
*** ERROR: Invalid configuration for cluster creation.
*** Redis Cluster requires at least 3 master nodes.
*** This is not possible with 2 nodes and 1 replicas per node.
*** At least 6 nodes are required.
正确命令:
./src/redis-trib.rb create --replicas 1 172.31.71.128:6001 172.31.71.128:6002 172.31.71.128:6003 172.31.71.128:6004 172.31.71.128:6005 172.31.71.128:6006
返回如下则表示成功:
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
3.3常见出错1 :
Traceback (most recent call last):
2: from ./src/redis-trib.rb:25:in `<main>'
1: from /opt/rubies/ruby-2.7.8/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:83:in `require'
/opt/rubies/ruby-2.7.8/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:83:in `require': cannot load such file -- redis (LoadError)
需要安装 相对应版本
下载
wget https://cache.ruby-lang.org/pub/ruby/ruby-2.5.0.tar.gz
tar xf ruby-2.5.0.tar.gz
进入ruby目录
cd ruby-2.5.0
./configure 2 make && make install
3.4 常见出错2 :
gem install redis
[root@localhost redis-4.0.9]# gem install redis
ERROR: SSL verification error at depth 3: certificate has expired (10)
ERROR: Certificate /O=Digital Signature Trust Co./CN=DST Root CA X3 expired at 2021-09-30T14:01:15Z
ERROR: While executing gem ... (Gem::RemoteFetcher::FetchError)
SSL_connect returned=1 errno=0 state=error: certificate verify failed (certificate has expired) (https://index.ruby-china.com/quick/Marshal.4.8/redis-5.0.6.gemspec.rz)
~ 下载
wget wget https://rubygems.org/downloads/redis-4.2.3.gem
安装指定的redis模块
gem install -l /apps/redis-4.2.3.gem
4 访问redis集群
redis-cli -h 172.31.71.128 -p 6001 -a 123456 -c
5集群节点查看
## 登录集群后
cluster nodes
[root@localhost redis-4.0.9]# redis-cli -h 172.31.71.128 -p 6001 -a 123456 -c
172.31.71.128:6001> cluster nodes
2110c7f67c001fac71691dd64cec4051b93cadfa 172.31.71.128:6005@16005 slave c4824984fafba7dda4c55975f7f8745234a6968e 0 1686560943122 5 connected
26555850fc80419c44d96a4c0e58b20d0af8bbf2 172.31.71.128:6003@16003 master - 0 1686560942119 3 connected 10923-16383
0e1d949ce8785f798ba0e17c2aa2155a6d99df8e 172.31.71.128:6006@16006 slave 26555850fc80419c44d96a4c0e58b20d0af8bbf2 0 1686560942519 6 connected
3389a66d84d6462b4dbf5cc228c3a060465773b3 172.31.71.128:6004@16004 slave 258d00326ff3b038e1d5af6b4d06423a31161ce7 0 1686560943122 4 connected
c4824984fafba7dda4c55975f7f8745234a6968e 172.31.71.128:6002@16002 master - 0 1686560943523 2 connected 5461-10922
258d00326ff3b038e1d5af6b4d06423a31161ce7 172.31.71.128:6001@16001 myself,master - 0 1686560941000 1 connected 0-5460
6 集群状态查看
## 登录集群后
cluster info
[root@localhost redis-4.0.9]# redis-cli -h 172.31.71.128 -p 6001 -a 123456 -c
172.31.71.128:6001> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:1433
cluster_stats_messages_pong_sent:1407
cluster_stats_messages_sent:2840
cluster_stats_messages_ping_received:1402
cluster_stats_messages_pong_received:1433
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:2840
~