Docker环境搭建redis集群(主从模式)
序
本文将介绍如何在docker中部署redis集群(主从模式)。
部署环境
CentOS 7 host A 192.168.199.200 (master)
CentOS 7 host B 192.168.199.201 (slave)
部署
host A 部署master
写主服务配置文件redis_master.conf
daemonize no
pidfile "/var/run/redis.pid"
port 6379
timeout 300
loglevel warning
logfile "redis.log"
databases 16
rdbcompression yes
dbfilename "redis.rdb"
dir "/data"
requirepass password
masterauth password
maxclients 10000
maxmemory 1000mb
maxmemory-policy allkeys-lru
appendonly no
appendfsync always
启动主redis服务
docker run --name redis_master -p 6379:6379 -v $(pwd)/redis_master.conf:/data/redis_master.conf --restart=always -d redis:latest redis-server redis_master.conf
host B 部署slave
写从服务配置文件redis_slave.conf
daemonize no
pidfile "/var/run/redis.pid"
port 6379
timeout 300
loglevel warning
logfile "redis.log"
databases 16
rdbcompression yes
dbfilename "redis.rdb"
dir "/data"
requirepass password
masterauth password
maxclients 10000
maxmemory 1000mb
maxmemory-policy allkeys-lru
appendonly no
appendfsync always
slaveof 192.168.199.200 6379
启动从redis服务
docker run --name redis_slave -p 6379:6379 -v $(pwd)/redis_slave.conf:/data/redis_slave.conf --restart=always -d redis:latest redis-server redis_slave.conf
写哨兵集群配置文件sentinel.conf
daemonize no
port 26379
dir "/tmp"
sentinel monitor mymaster 192.168.199.200 6379 2
sentinel down-after-milliseconds mymaster 60000
sentinel auth-pass mymaster password
sentinel config-epoch mymaster 0
sentinel leader-epoch mymaster 0
启动sentinel服务
docker run --name sentinel -p 26379:26379 -v $(pwd)/sentinel.conf:/data/sentinel.conf --restart=always -d redis:latest redis-sentinel sentinel.conf
查看部署结果
host A 运行如下命令
[root@localhost ~]# docker inspect redis_master | grep IPA
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAMConfig": null,
"IPAddress": "172.17.0.2",
[root@localhost ~]# docker run -it redis redis-cli -h 172.17.0.2
172.17.0.2:6379> info
NOAUTH Authentication required.
172.17.0.2:6379> auth password
OK
172.17.0.2:6379> info
# Server
redis_version:3.2.8
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:9c531c9c1d171a62
redis_mode:standalone
os:Linux 3.10.0-514.10.2.el7.x86_64 x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.9.2
process_id:1
run_id:3f9915c39a62b684e2f20594c72fe3d1633f115b
tcp_port:6379
uptime_in_seconds:5289
uptime_in_days:0
hz:10
lru_clock:13508852
executable:/data/redis-server
config_file:/data/redis_master.conf
# Clients
connected_clients:3
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
# Memory
used_memory:1932944
used_memory_human:1.84M
used_memory_rss:6893568
used_memory_rss_human:6.57M
used_memory_peak:1973896
used_memory_peak_human:1.88M
total_system_memory:1023963136
total_system_memory_human:976.53M
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:1048576000
maxmemory_human:1000.00M
maxmemory_policy:allkeys-lru
mem_fragmentation_ratio:3.57
mem_allocator:jemalloc-4.0.3
# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1489900357
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
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
# Stats
total_connections_received:7
total_commands_processed:8721
instantaneous_ops_per_sec:1
total_net_input_bytes:416036
total_net_output_bytes:1265209
instantaneous_input_kbps:0.03
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:1
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:1
pubsub_patterns:0
latest_fork_usec:4755
migrate_cached_sockets:0
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.199.201,port=6379,state=online,offset=237002,lag=0
master_repl_offset:237002
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:2
repl_backlog_histlen:237001
# CPU
used_cpu_sys:9.30
used_cpu_user:3.33
used_cpu_sys_children:0.05
used_cpu_user_children:0.02
# Cluster
cluster_enabled:0
# Keyspace