redis官方集群搭建

1.安装ruby

wget http://cache.ruby-lang.org/pub/ruby/2.6/ruby-2.6.3.tar.gz
tar xvf ruby-2.6.3.tar.gz 
cd ruby-2.6.3
./configure --prefix=/usr/local/ruby
make && make install

配置环境变量

vim /etc/profile
export PATH=$PATH:/usr/local/ruby/bin
source /etc/profile 
ruby -v 查看ruby安装版本

2.其他软件依赖

安装zlib

wget http://zlib.net/zlib-1.2.11.tar.gz
tar xvf zlib-1.2.11.tar.gz 
cd zlib-1.2.11
./configure --prefix=/usr/local/zlib
make && make install

安装openssl

yum  install  openssl  openssl-devel

重新编译ruby

zlib报错

cd ruby-2.6.3/ext/zlib
ruby extconf.rb
//在操作下一步之前需要修改Makefile文件中的zlib.o: $(top_srcdir)/include/ruby.h,将$(top_srcdir)修改为../..如下
//zlib.o: ../../include/ruby.h
//这一步如果不修改,make时会爆出另外一个错误
//make:*** No rule to make target `/include/ruby.h', needed by `zlib.o'.  Stop
make && make install

openssl报错

cd ruby-2.6.3/ext/openssl/
ruby extconf.rb
//同样修改Makefile中的$(top_srcdir)为../..
make && make install

3.创建集群

cd /usr/local/
mkdir redis-cluster
cd redis-cluster/
mkdir 7001 7002 7003 7004 7005 7006
wget http://rubygems.org/downloads/redis-4.1.2.gem
gem install redis-4.1.2.gem

编辑配置文件

daemonize yes #开启后台运行 

port 7001 #工作端口 

bind 127.0.0.1 

dir /usr/local/redis-cluster/7001/ #指定工作目录,rdb,aof持久化文件将会放在该目录下,不同实例一定要配置不同的工作目录 

cluster-enabled yes #启用集群模式 

cluster-config-file nodes-7001.conf #生成的集群配置文件名称,集群搭建成功后会自动生成,在工作目录下 

cluster-node-timeout 5000 #节点宕机发现时间,可以理解为主节点宕机后从节点升级为主节点时间 

appendonly yes #开启AOF模式 

pidfile /var/run/redis_7001.pid #pid file所在目录

4.启动集群

启动redis

redis-server ./7001/redis_7001.conf 

redis-server ./7002/redis_7002.conf 

redis-server ./7003/redis_7003.conf 

redis-server ./7004/redis_7004.conf 

redis-server ./7005/redis_7005.conf 

redis-server ./7006/redis_7006.conf

查看

[root@localhost redis-cluster]# ps -ef | grep redis
root 964 1 0 23:49 ? 00:00:00 redis-server 127.0.0.1:7001 [cluster]
root 2256 1 0 23:50 ? 00:00:00 redis-server 127.0.0.1:7002 [cluster]
root 3045 1 0 23:51 ? 00:00:00 redis-server 127.0.0.1:7003 [cluster]
root 3833 1 0 23:51 ? 00:00:00 redis-server 127.0.0.1:7004 [cluster]
root 4502 1 0 23:52 ? 00:00:00 redis-server 127.0.0.1:7005 [cluster]
root 5142 1 0 23:53 ? 00:00:00 redis-server 127.0.0.1:7006 [cluster]
root 5255 4099 0 23:53 pts/0 00:00:00 grep --color=auto redis

创建集群,新版不再使用redis-trib.rb 创建集群

[root@localhost redis-cluster]# cp /root/redis-5.0.5/src/redis-cli /usr/bin/
[root@localhost redis-cluster]# redis-cli --cluster create 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 127.0.0.1:7006 --cluster-replicas 1
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
Adding replica 127.0.0.1:7006 to 127.0.0.1:7002
Adding replica 127.0.0.1:7004 to 127.0.0.1:7003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 40efb049d5cd6951b221162f5b94ce51ffa50ae0 127.0.0.1:7001
 slots:[0-5460] (5461 slots) master
M: deb9997c3324f1a32de6b7cedc8348824de3864c 127.0.0.1:7002
 slots:[5461-10922] (5462 slots) master
M: 54e73e4c77b338adbebc5e21cd394c84acc165d3 127.0.0.1:7003
 slots:[10923-16383] (5461 slots) master
S: e3ae8307fba3d3c749b1ad781538cc62263f9cc1 127.0.0.1:7004
 replicates 40efb049d5cd6951b221162f5b94ce51ffa50ae0
S: 82e9d0a40f5ce138206ada942d67cf7f7c88fd47 127.0.0.1:7005
 replicates deb9997c3324f1a32de6b7cedc8348824de3864c
S: 6a64f6452513e5fb8df174f480dde82c2bc6609f 127.0.0.1:7006
 replicates 54e73e4c77b338adbebc5e21cd394c84acc165d3
Can I set the above configuration? (type 'yes' to accept): yes
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: 40efb049d5cd6951b221162f5b94ce51ffa50ae0 127.0.0.1:7001
 slots:[0-5460] (5461 slots) master
 1 additional replica(s)
S: 6a64f6452513e5fb8df174f480dde82c2bc6609f 127.0.0.1:7006
 slots: (0 slots) slave
 replicates 40efb049d5cd6951b221162f5b94ce51ffa50ae0
S: 82e9d0a40f5ce138206ada942d67cf7f7c88fd47 127.0.0.1:7005
 slots: (0 slots) slave
 replicates 54e73e4c77b338adbebc5e21cd394c84acc165d3
M: deb9997c3324f1a32de6b7cedc8348824de3864c 127.0.0.1:7002
 slots:[5461-10922] (5462 slots) master
 1 additional replica(s)
M: 54e73e4c77b338adbebc5e21cd394c84acc165d3 127.0.0.1:7003
 slots:[10923-16383] (5461 slots) master
 1 additional replica(s)
S: e3ae8307fba3d3c749b1ad781538cc62263f9cc1 127.0.0.1:7004
 slots: (0 slots) slave
 replicates deb9997c3324f1a32de6b7cedc8348824de3864c
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

5.测试

[root@localhost redis-cluster]# redis-cli -c -h 127.0.0.1 -p 7001
127.0.0.1:7001> info
# Server
redis_version:5.0.5
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:25b019f0504175f1
redis_mode:cluster
os:Linux 3.10.0-693.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:4.8.5
process_id:964
run_id:aef0762ea92b4754584f38de8eaf67a2edf435f8
tcp_port:7001
uptime_in_seconds:1172
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:16160657
executable:/usr/local/redis-cluster/redis-server
config_file:/usr/local/redis-cluster/./7001/redis_7001.conf

# Clients
connected_clients:1
client_recent_max_input_buffer:2
client_recent_max_output_buffer:0
blocked_clients:0

# Memory
used_memory:2572576
used_memory_human:2.45M
used_memory_rss:3121152
used_memory_rss_human:2.98M
used_memory_peak:2603976
used_memory_peak_human:2.48M
used_memory_peak_perc:98.79%
used_memory_overhead:2511806
used_memory_startup:1392352
used_memory_dataset:60770
used_memory_dataset_perc:5.15%
allocator_allocated:2571472
allocator_active:3083264
allocator_resident:3083264
total_system_memory:1928765440
total_system_memory_human:1.80G
used_memory_lua:37888
used_memory_lua_human:37.00K
used_memory_scripts:0
used_memory_scripts_human:0B
number_of_cached_scripts:0
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
allocator_frag_ratio:1.20
allocator_frag_bytes:511792
allocator_rss_ratio:1.00
allocator_rss_bytes:0
rss_overhead_ratio:1.01
rss_overhead_bytes:37888
mem_fragmentation_ratio:1.21
mem_fragmentation_bytes:549680
mem_not_counted_for_evict:110
mem_replication_backlog:1052656
mem_clients_slaves:16922
mem_clients_normal:49694
mem_aof_buffer:110
mem_allocator:libc
active_defrag_running:0
lazyfree_pending_objects:0

# Persistence
loading:0
rdb_changes_since_last_save:1
rdb_bgsave_in_progress:0
rdb_last_save_time:1559664369
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:204800
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:55
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:7
total_commands_processed:190
instantaneous_ops_per_sec:1
total_net_input_bytes:60118
total_net_output_bytes:65355
instantaneous_input_kbps:0.04
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:135
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=7006,state=online,offset=279,lag=0
master_replid:ab3e6dbb43f8c9ca448cfdcf37fc4ca289712751
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:279
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:279

# CPU
used_cpu_sys:0.463310
used_cpu_user:0.506206
used_cpu_sys_children:0.001978
used_cpu_user_children:0.000000

# Cluster
cluster_enabled:1

# Keyspace
db0:keys=1,expires=0,avg_ttl=0
127.0.0.1:7001> 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:315
cluster_stats_messages_pong_sent:320
cluster_stats_messages_sent:635
cluster_stats_messages_ping_received:315
cluster_stats_messages_pong_received:315
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:635
127.0.0.1:7001> cluster nodes
6a64f6452513e5fb8df174f480dde82c2bc6609f 127.0.0.1:7006@17006 slave 40efb049d5cd6951b221162f5b94ce51ffa50ae0 0 1559664538190 6 connected
40efb049d5cd6951b221162f5b94ce51ffa50ae0 127.0.0.1:7001@17001 myself,master - 0 1559664538000 1 connected 0-5460
82e9d0a40f5ce138206ada942d67cf7f7c88fd47 127.0.0.1:7005@17005 slave 54e73e4c77b338adbebc5e21cd394c84acc165d3 0 1559664537000 5 connected
deb9997c3324f1a32de6b7cedc8348824de3864c 127.0.0.1:7002@17002 master - 0 1559664537000 2 connected 5461-10922
54e73e4c77b338adbebc5e21cd394c84acc165d3 127.0.0.1:7003@17003 master - 0 1559664538000 3 connected 10923-16383
e3ae8307fba3d3c749b1ad781538cc62263f9cc1 127.0.0.1:7004@17004 slave deb9997c3324f1a32de6b7cedc8348824de3864c 0 1559664537587 4 connected
127.0.0.1:7001> set name lisi
-> Redirected to slot [5798] located at 127.0.0.1:7002
OK
127.0.0.1:7002> set addr beijing
-> Redirected to slot [12790] located at 127.0.0.1:7003
OK
127.0.0.1:7003>

redis官方集群搭建
http://www.jcwit.com/article/131/
作者
Carlos
发布于
2019年6月5日
许可协议