Redis高可用环境搭建(主从配置)
Redis主从模式
主从机器一般是分开的两台机器,这里为了演示方便,使用了一台机器,所以配置文件中,处理端口的区分之外,还需要注意日志文件,数据库文件,pid文件等的区分。
启动redis命令
redis-server redis.conf
主从配置
默认redis.conf是redis的配置文件,默认端口是6379,这里为了不占用6379端口,默认使用6380作为主库的端口,6381、6382作为从从库的端口,复制一份redis-conf到redis-6380.conf,修改配置如下:
#演示方便,开放ip连接
bind 0.0.0.0
#后台运行
daemonize yes
#pid文件 6380端口作为主库端口
pidfile /var/run/redis_6380.pid
dbfilename dump-6380.rdb
#日志文件
logfile "6380.log"
再复制一份到redis-6381.conf作为从库的配置文件,修改文件如下:
bind 0.0.0.0
port 6381
daemonize yes
pidfile /var/run/redis_6381.pid
dbfilename dump-6380.rdb
logfile "6381.log"
#slaveof表示作为从库的配置 ip(阿里云ip)+主库的端口
slaveof 39.106.115.134 6380
#从库只能读操作
slave-read-only yes
同样方法,复制一份到redis-6382.conf,也是作为另一个从库的配置信息(一主二从三哨兵)
启动redis主库和从库
[root@localhost redis-5.0.4]#./src/redis-server redis-6380.conf
[root@localhost redis-5.0.4]#./src/redis-server redis-6381.conf
ps -aux | grep redis查看redis是否启动成功:
进入主库客户端,查看info信息:
[root@localhost redis-5.0.4]#./src/redis-cli -p 6380
#执行info命令
127.0.0.1:6380>info
测试主从配置是否成功,在主库(6380)set,从库(6381)get:
没问题,到这一步,表明redis主从配置已经生效!
最后的主库的配置信息:
# Server
redis_version:5.0.4
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:96931704ebdbf98e
redis_mode:standalone
os:Linux 3.10.0-514.26.2.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
atomicvar_api:atomic-builtin
gcc_version:4.8.5
process_id:8511
run_id:d2cea8e5a6fa00ce7aa25125188f547640e8d9ae
tcp_port:6380
uptime_in_seconds:1506
uptime_in_days:0
hz:10
configured_hz:10
lru_clock:3148059
executable:/opt/redis-5.0.4/./src/redis-server
config_file:/opt/redis-5.0.4/redis-6380.conf
# Clients
connected_clients:1
client_recent_max_input_buffer:2
client_recent_max_output_buffer:0
blocked_clients:0
# Memory
used_memory:1944792
used_memory_human:1.85M
used_memory_rss:5369856
used_memory_rss_human:5.12M
used_memory_peak:1984440
used_memory_peak_human:1.89M
used_memory_peak_perc:98.00%
used_memory_overhead:1924338
used_memory_startup:792152
used_memory_dataset:20454
used_memory_dataset_perc:1.77%
allocator_allocated:2370176
allocator_active:2748416
allocator_resident:9494528
total_system_memory:1928933376
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.16
allocator_frag_bytes:378240
allocator_rss_ratio:3.45
allocator_rss_bytes:6746112
rss_overhead_ratio:0.57
rss_overhead_bytes:-4124672
mem_fragmentation_ratio:2.82
mem_fragmentation_bytes:3467320
mem_not_counted_for_evict:0
mem_replication_backlog:1048576
mem_clients_slaves:33844
mem_clients_normal:49694
mem_aof_buffer:0
mem_allocator:jemalloc-5.1.0
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:1596983556
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
rdb_last_cow_size:249856
aof_enabled:0
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
# Stats
total_connections_received:5
total_commands_processed:1538
instantaneous_ops_per_sec:1
total_net_input_bytes:56005
total_net_output_bytes:43826
instantaneous_input_kbps:0.04
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:2
sync_partial_ok:0
sync_partial_err:0
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:259
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:2 #二从
slave0:ip=39.106.115.134,port=6381,state=online,offset=2187,lag=1
slave1:ip=39.106.115.134,port=6382,state=online,offset=2187,lag=1
master_replid:02f446ef905e9ee4acd61324ef5b9339ae2eb2a0
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:2187
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2187
# CPU
used_cpu_sys:0.529351
used_cpu_user:1.124310
used_cpu_sys_children:0.001777
used_cpu_user_children:0.000000
# Cluster
cluster_enabled:0
# Keyspace
db0:keys=1,expires=0,avg_ttl=0
配置哨兵
sentinel.conf默认是redis的哨兵配置文件,默认端口是26379,同样我们拷贝一份到sentinel-26380.conf、sentinel-26381.conf、sentinel-26382.conf,修改sentinel-26380.conf配置如下:
默认redis哨兵配置redis.sentinel配置信息如下:
port 27379
daemonize no
pidfile /var/run/redis-sentinel.pid
logfile ""
#工作目录
dir /tmp
#哨兵主库名称为mymaster,2表示选举的时候投票数量需要超过2才能成为领导
sentinel monitor mymaster 127.0.0.1 6379 2
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
#故障选举切换的超时时间
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
sentinel-26380.conf配置修改如下:
port 26380
daemonize yes
pidfile /var/run/redis-sentinel.pid
logfile ""
#工作目录
dir /opt/redis-5.0.4/data
#哨兵主库名称为mymaster,2表示选举的时候投票数量需要超过2才能成为领导
sentinel monitor mymaster 39.106.115.134 6380 2
#30秒时间的领导选举主观下线
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
#故障选举切换的超时时间
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
sentinel-26381.conf配置修改如下:
port 26381
daemonize yes
pidfile /var/run/redis-sentinel.pid
logfile ""
#工作目录
dir /opt/redis-5.0.4/data
#哨兵主库名称为mymaster,2表示选举的时候投票数量需要超过2才能成为领导
sentinel monitor mymaster 39.106.115.134 6380 2
#30秒时间的领导选举主观下线
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
#故障选举切换的超时时间
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
sentinel-26382.conf配置修改如下:
port 26382
daemonize yes
pidfile /var/run/redis-sentinel.pid
logfile ""
#工作目录
dir /opt/redis-5.0.4/data
#哨兵主库名称为mymaster,2表示选举的时候投票数量需要超过2才能成为领导
sentinel monitor mymaster 39.106.115.134 6380 2
#30秒时间的领导选举主观下线
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
#故障选举切换的超时时间
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
启动sentinel
[root@iz2zeg4xqcv95kl86o2w9bz redis-5.0.4]# ./src/redis-sentinel sentinel-26380.conf
[root@iz2zeg4xqcv95kl86o2w9bz redis-5.0.4]# ps -aux | grep redis
root 8340 0.1 0.3 159620 7208 ? Ssl 22:06 0:04 ./src/redis-server 0.0.0.0:6381
root 8511 0.1 0.2 159620 4528 ? Ssl 22:07 0:04 ./src/redis-server 0.0.0.0:6380
root 10744 0.1 0.4 156548 7836 ? Ssl 22:32 0:02 ./src/redis-server 0.0.0.0:6382
root 14079 0.2 0.2 153992 4632 ? Ssl 23:09 0:00 ./src/redis-sentinel *:26380 [sentinel]
root 14217 0.1 0.1 153992 2664 ? Ssl 23:11 0:00 ./src/redis-sentinel *:26381 [sentinel]
root 14256 0.0 0.1 153992 2556 ? Ssl 23:11 0:00 ./src/redis-sentinel *:26382 [sentinel]
root 14263 0.0 0.0 112812 972 pts/1 R+ 23:11 0:00 grep --color=auto redis
systemd+ 24650 0.1 0.6 52912 11900 ? Ssl Jun20 83:27 redis-server *:6379
表明redis哨兵服务已经启动成功~
查看哨兵信息,执行./src/redis-cli -p 26380 info:
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
#主库名称为mymaster,地址为39.106.115.134,从库数量2,哨兵数量为3
master0:name=mymaster,status=ok,address=39.106.115.134:6380,slaves=2,sentinels=3
到这步,redis的一主二从三哨兵该可以集群环境搭建已经成功!
以上是 Redis高可用环境搭建(主从配置) 的全部内容, 来源链接: utcz.com/z/519246.html