Redis部署3节点集群,这里使用一台机器模拟,和实际多台服务器部署本质没区别。
操作系统:CentOS7 端口分配:7000-7005
1、下载redis-4.0.13并解压
解压目录: /usr/local/redis-4.0.13
建立软连接:ln -s redis-4.0.1 redis
安装依赖包:yum install tcl tcl-devel
2、编译安装
指定安装目录到:/usr/local/redis
make && make test && make install PREFIX=/usr/local/redis
3、创建节点上集群目录和配置文件
node1服务器:
mkdir -p /usr/local/redis/redis_cluster/7000
mkdir -p /usr/local/redis/redis_cluster/7001
mkdir -p /usr/local/redis/redis_cluster/7002
mkdir -p /usr/local/redis/redis_cluster/7003
mkdir -p /usr/local/redis/redis_cluster/7004
mkdir -p /usr/local/redis/redis_cluster/7005
cp /usr/local/redis/redis.conf /usr/loca/redis/redis_cluster/7000
cp /usr/local/redis/redis.conf /usr/loca/redis/redis_cluster/7001
cp /usr/local/redis/redis.conf /usr/loca/redis/redis_cluster/7002
cp /usr/local/redis/redis.conf /usr/loca/redis/redis_cluster/7003
cp /usr/local/redis/redis.conf /usr/loca/redis/redis_cluster/7004
cp /usr/local/redis/redis.conf /usr/loca/redis/redis_cluster/7005
4、修改redis.conf配置文件
切记:不能设置密码,否则集群启动时会连接不上
port 7000
//端口根据对应的文件夹去配置端口 7000,7001,7002,7003,7004,7005
bind 本机ip
//根据本机所在的IP或hostname去配置如: node1 node2 node3
daemonize yes
//redis后台运行
pidfile /var/run/redis_7000.pid
//pidfile文件对应7000,7001,7002,7003,7004,7005
cluster-enabled yes
//开启集群 把注释#去掉
cluster-config-file nodes_7000.conf
//集群的配置 配置文件首次启动自动生成 7000,7001,7002,7003,7004,7005
cluster-node-timeout 15000
//请求超时 默认15秒,可自行设置
appendonly yes
//aof日志开启 有需要就开启,它会每次写操作都记录一条日志
5、复制src目录中的redis-trib.rb 到/usr/local/redis/bin目录
[root@dbserver ~]# cp /usr/local/redis/src/redis-trib.rb /usr/local/redis/bin
6、安装ruby环境
[root@dbserver ~]# yum install -y ruby rubygems
查看ruby和rubygems版本
[root@dbserver ~]# rpm -qa |grep ruby
ruby-irb-2.0.0.648-39.el7_9.noarch
rubygem-io-console-0.4.2-39.el7_9.x86_64
rubygem-rdoc-4.0.0-39.el7_9.noarch
rubygems-2.0.14.1-39.el7_9.noarch
ruby-libs-2.0.0.648-39.el7_9.x86_64
rubygem-json-1.7.7-39.el7_9.x86_64
ruby-2.0.0.648-39.el7_9.x86_64
rubygem-bigdecimal-1.2.0-39.el7_9.x86_64
rubygem-psych-2.0.0-39.el7_9.x86_64
[root@dbserver ~]#
7、安装接口
使用命令安装: gem install redis-4.0.0.rc1.gem
# gem install redis-4.0.0.rc1.gem
Successfully installed redis-4.0.0.rc1
Parsing documentation for redis-4.0.0.rc1
1 gem installed
如果不成功需要去下载后安装,下载地址
https://rubygems.org/gems/redis/versions/4.0.0.rc1
https://rubygems.org/downloads/redis-4.0.0.rc1.gem
安装命令:gem install -l ./redis-4.0.0.rc1.gem
# gem install -l ./redis-4.0.0.rc1.gem
Successfully installed redis-4.0.0.rc1
Parsing documentation for redis-4.0.0.rc1
1 gem installed
8、启动各个节点
node1服务器:
/usr/local/redis/bin/redis-server /usr/local/redis/redis_cluster/7000/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis/redis_cluster/7001/redis.conf
node2服务器:
/usr/local/redis/bin/redis-server /usr/local/redis/redis_cluster/7002/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis/redis_cluster/7003/redis.conf
node3服务器:
/usr/local/redis/bin/redis-server /usr/local/redis/redis_cluster/7004/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis/redis_cluster/7005/redis.conf
[root@dbserver bin]# /usr/local/redis/bin/redis-server /usr/local/redis/redis_cluster/7000/redis.conf
32292:C 27 Nov 16:17:31.070 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
32292:C 27 Nov 16:17:31.070 # Redis version=4.0.13, bits=64, commit=00000000, modified=0, pid=32292, just started
32292:C 27 Nov 16:17:31.070 # Configuration loaded
[root@dbserver bin]# /usr/local/redis/bin/redis-server /usr/local/redis/redis_cluster/7001/redis.conf
32305:C 27 Nov 16:17:36.272 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
32305:C 27 Nov 16:17:36.273 # Redis version=4.0.13, bits=64, commit=00000000, modified=0, pid=32305, just started
32305:C 27 Nov 16:17:36.273 # Configuration loaded
[root@dbserver bin]# /usr/local/redis/bin/redis-server /usr/local/redis/redis_cluster/7002/redis.conf
32310:C 27 Nov 16:17:50.165 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
32310:C 27 Nov 16:17:50.165 # Redis version=4.0.13, bits=64, commit=00000000, modified=0, pid=32310, just started
32310:C 27 Nov 16:17:50.165 # Configuration loaded
[root@dbserver bin]# /usr/local/redis/bin/redis-server /usr/local/redis/redis_cluster/7003/redis.conf
32315:C 27 Nov 16:17:51.700 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
32315:C 27 Nov 16:17:51.700 # Redis version=4.0.13, bits=64, commit=00000000, modified=0, pid=32315, just started
32315:C 27 Nov 16:17:51.700 # Configuration loaded
[root@dbserver bin]# /usr/local/redis/bin/redis-server /usr/local/redis/redis_cluster/7004/redis.conf
32320:C 27 Nov 16:17:55.046 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
32320:C 27 Nov 16:17:55.047 # Redis version=4.0.13, bits=64, commit=00000000, modified=0, pid=32320, just started
32320:C 27 Nov 16:17:55.047 # Configuration loaded
[root@dbserver bin]# /usr/local/redis/bin/redis-server /usr/local/redis/redis_cluster/7005/redis.conf
32325:C 27 Nov 16:17:58.870 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
32325:C 27 Nov 16:17:58.870 # Redis version=4.0.13, bits=64, commit=00000000, modified=0, pid=32325, just started
32325:C 27 Nov 16:17:58.870 # Configuration loaded
[root@dbserver bin]#
9、检查各节点是否启动
查看redis进程
[root@dbserver bin]# ps -ef |grep redis
root 32293 1 0 16:17 ? 00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:7000 [cluster]
root 32306 1 0 16:17 ? 00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:7001 [cluster]
root 32311 1 0 16:17 ? 00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:7002 [cluster]
root 32316 1 0 16:17 ? 00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:7003 [cluster]
root 32321 1 0 16:17 ? 00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:7004 [cluster]
root 32326 1 0 16:17 ? 00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:7005 [cluster]
root 32338 1988 0 16:18 pts/1 00:00:00 grep --color=auto redis
[root@dbserver bin]#
查看redis端口
[root@dbserver bin]# netstat -tnlp | grep redis
tcp 0 0 127.0.0.1:17000 0.0.0.0:* LISTEN 32293/redis-server
tcp 0 0 127.0.0.1:17001 0.0.0.0:* LISTEN 32306/redis-server
tcp 0 0 127.0.0.1:17002 0.0.0.0:* LISTEN 32311/redis-server
tcp 0 0 127.0.0.1:17003 0.0.0.0:* LISTEN 32316/redis-server
tcp 0 0 127.0.0.1:17004 0.0.0.0:* LISTEN 32321/redis-server
tcp 0 0 127.0.0.1:17005 0.0.0.0:* LISTEN 32326/redis-server
tcp 0 0 127.0.0.1:7000 0.0.0.0:* LISTEN 32293/redis-server
tcp 0 0 127.0.0.1:7001 0.0.0.0:* LISTEN 32306/redis-server
tcp 0 0 127.0.0.1:7002 0.0.0.0:* LISTEN 32311/redis-server
tcp 0 0 127.0.0.1:7003 0.0.0.0:* LISTEN 32316/redis-server
tcp 0 0 127.0.0.1:7004 0.0.0.0:* LISTEN 32321/redis-server
tcp 0 0 127.0.0.1:7005 0.0.0.0:* LISTEN 32326/redis-server
[root@dbserver bin]#
10、创建集群
进入节点的redis的bin目录下,执行以下脚本
./redis-trib.rb create --replicas 1 node1:7000 node1:7001 node2:7002 node2:7003 node3:7004 node3:7005
[root@dbserver bin]# pwd
/usr/local/redis/bin
[root@dbserver bin]#
[root@dbserver 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:7003127.0.0.1:7004 127.0.0.1:7005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:7000
127.0.0.1:7001
127.0.0.1:7002
Adding replica 127.0.0.1:7004 to 127.0.0.1:7000
Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
Adding replica 127.0.0.1:7003 to 127.0.0.1:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: bd6478c5e2bc6d7a433ee7c55b5dfec22b222dfb 127.0.0.1:7000
slots:0-5460 (5461 slots) master
M: 315dee4f51b29cf095771026f6323419c2b6f351 127.0.0.1:7001
slots:5461-10922 (5462 slots) master
M: ff1bb22b10653817a0d7956e5a20ec89d08739e2 127.0.0.1:7002
slots:10923-16383 (5461 slots) master
S: 754df846ac37f39f4caa2afe49494bc44c20596a 127.0.0.1:7003
replicates ff1bb22b10653817a0d7956e5a20ec89d08739e2
S: 297053468b7180be7dbe2f969d43239c067416db 127.0.0.1:7004
replicates bd6478c5e2bc6d7a433ee7c55b5dfec22b222dfb
S: bb823028863cb5de63d23f558e9479b7fd20d621 127.0.0.1:7005
replicates 315dee4f51b29cf095771026f6323419c2b6f351
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 127.0.0.1:7000)
M: bd6478c5e2bc6d7a433ee7c55b5dfec22b222dfb 127.0.0.1:7000
slots:0-5460 (5461 slots) master
1 additional replica(s)
S: bb823028863cb5de63d23f558e9479b7fd20d621 127.0.0.1:7005
slots: (0 slots) slave
replicates 315dee4f51b29cf095771026f6323419c2b6f351
S: 754df846ac37f39f4caa2afe49494bc44c20596a 127.0.0.1:7003
slots: (0 slots) slave
replicates ff1bb22b10653817a0d7956e5a20ec89d08739e2
M: ff1bb22b10653817a0d7956e5a20ec89d08739e2 127.0.0.1:7002
slots:10923-16383 (5461 slots) master
1 additional replica(s)
S: 297053468b7180be7dbe2f969d43239c067416db 127.0.0.1:7004
slots: (0 slots) slave
replicates bd6478c5e2bc6d7a433ee7c55b5dfec22b222dfb
M: 315dee4f51b29cf095771026f6323419c2b6f351 127.0.0.1:7001
slots:5461-10922 (5462 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
[root@dbserver bin]#
11、验证集群
连接集群,记住参数-c不可以少
redis-cli -h node1 -p 7000 -c
redis-cli -h node2 -p 7001 -c
redis-cli -h node3 -p 7002 -c
[root@dbserver redis]# redis-cli -h 127.0.0.1 -c -p 7000
127.0.0.1:7000> 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:3985
cluster_stats_messages_pong_sent:3763
cluster_stats_messages_sent:7748
cluster_stats_messages_ping_received:3758
cluster_stats_messages_pong_received:3985
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:7748
127.0.0.1:7000>
[root@dbserver bin]# redis-cli -p 7000 -c
127.0.0.1:7000> info
# Server
redis_version:4.0.13
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:fead8a8b524ee9ee
redis_mode:cluster
os:Linux 3.10.0-1160.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:4.8.5
process_id:32293
run_id:2bc05235f858511318aa2261a506511b9a5128a6
tcp_port:7000
uptime_in_seconds:220
uptime_in_days:0
hz:10
lru_clock:8592887
executable:/usr/local/redis/bin/redis-server
config_file:/usr/local/redis/redis_cluster/7000/redis.conf
# Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
# Memory
used_memory:2641672
used_memory_human:2.52M
used_memory_rss:12165120
used_memory_rss_human:11.60M
used_memory_peak:2641672
used_memory_peak_human:2.52M
used_memory_peak_perc:100.00%
used_memory_overhead:2560040
used_memory_startup:1444960
used_memory_dataset:81632
used_memory_dataset_perc:6.82%
total_system_memory:2076565504
total_system_memory_human:1.93G
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:4.60
mem_allocator:jemalloc-4.0.3
active_defrag_running:0
lazyfree_pending_objects:0
# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1669537197
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:8581120
aof_enabled:1
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
aof_last_cow_size:0
aof_current_size:0
aof_base_size:0
aof_pending_rewrite:0
aof_buffer_length:0
aof_rewrite_buffer_length:0
aof_pending_bio_fsync:0
aof_delayed_fsync:0
# Stats
total_connections_received:5
total_commands_processed:95
instantaneous_ops_per_sec:0
total_net_input_bytes:56594
total_net_output_bytes:36730
instantaneous_input_kbps:0.02
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:1
sync_partial_ok:0
sync_partial_err:1
expired_keys:0
expired_stale_perc:0.00
expired_time_cap_reached_count:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:1544
migrate_cached_sockets:0
slave_expires_tracked_keys:0
active_defrag_hits:0
active_defrag_misses:0
active_defrag_key_hits:0
active_defrag_key_misses:0
# Replication
role:master
connected_slaves:1
slave0:ip=127.0.0.1,port=7004,state=online,offset=98,lag=0
master_replid:362ef8f78cbbfa2430e5ebe56895b9e1f6275682
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:98
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:98
# CPU
used_cpu_sys:0.38
used_cpu_user:0.06
used_cpu_sys_children:0.01
used_cpu_user_children:0.00
# Cluster
cluster_enabled:1
# Keyspace
127.0.0.1:7000>