docker-compose安装redis-sentinel集群(1主+2副+2哨兵)
Posted 波神
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker-compose安装redis-sentinel集群(1主+2副+2哨兵)相关的知识,希望对你有一定的参考价值。
前提:本试验环境已经提前安装了docker和docker-compose
说明:本次部署是单机伪集群,想要部署真正的集群,需要将秒个主件拆分到各个机器上去部署,只修改ip地址
1、下载redis的相关镜像
本人已经将做好的redis镜像放到阿里云的仓库了,要下载,直接执行下面的命令即可
docker pull registry.cn-hangzhou.aliyuncs.com/boshen-ns/redis-sentinel:3 docker pull registry.cn-hangzhou.aliyuncs.com/boshen-ns/redis:3
2、创建redis的data目录,主要是为了持久化保存redis的数据
mkdir -p /home/redis-sentinel-cluster/data/master mkdir -p /home/redis-sentinel-cluster/data/slave1 mkdir -p /home/redis-sentinel-cluster/data/slave2 mkdir -p /home/redis-sentinel-cluster/data/sentinel-1 mkdir -p /home/redis-sentinel-cluster/data/sentinel-2
3、创建2个哨兵的配置文件 (集群启动后会自动更新该配置文件 )
/home/redis-sentinel-cluster/sentinel-1.conf
port 26379 dir "/data" sentinel monitor mymaster 172.16.1.11 16379 2 sentinel down-after-milliseconds mymaster 5000 sentinel failover-timeout mymaster 5000 sentinel parallel-syncs mymaster 1
/home/redis-sentinel-cluster/sentinel-2.conf
port 26380 dir "/data" sentinel monitor mymaster 172.16.1.11 16379 2 sentinel down-after-milliseconds mymaster 5000 sentinel failover-timeout mymaster 5000 sentinel parallel-syncs mymaster 1
1)monitor 指的是初始化的监控主切点,ip和端口,后面的数字2代表,必须2个sentinel才能判断主节点是否失败
2)down-after-milliseconds 指的是超过5000秒,且没有回复,则判定主节点不可达
3)failover-timeout 指的是故障转移时间
4)parallel-syncs 指的是故障转移到新的主节点时,从节点的复制节点数量
4、创建 /home/redis-sentinel-cluster/docker-compose.yml
version: \'2.1\' services: master: image: registry.cn-hangzhou.aliyuncs.com/boshen-ns/redis:3 container_name: master restart: always network_mode: "host" volumes: - ./data/master:/data command: redis-server --port 16379 slave1: image: registry.cn-hangzhou.aliyuncs.com/boshen-ns/redis:3 container_name: slave1 restart: always network_mode: "host" volumes: - ./data/slave1:/data command: redis-server --port 16380 --slaveof 172.16.1.11 16379 slave2: image: registry.cn-hangzhou.aliyuncs.com/boshen-ns/redis:3 container_name: slave2 restart: always network_mode: "host" volumes: - ./data/slave2:/data command: redis-server --port 16381 --slaveof 172.16.1.11 16379 sentinel-1: image: registry.cn-hangzhou.aliyuncs.com/boshen-ns/redis-sentinel:3 container_name: sentinel-1 restart: always network_mode: "host" volumes: - ./sentinel-1.conf:/etc/redis/sentinel.conf - ./data/sentinel-1:/data sentinel-2: image: registry.cn-hangzhou.aliyuncs.com/boshen-ns/redis-sentinel:3 container_name: sentinel-2 restart: always network_mode: "host" volumes: - ./sentinel-2.conf:/etc/redis/sentinel.conf - ./data/sentinel-2:/data
5、启动redis集群
cd /home/redis-sentinel-cluster
docker-compose up -d
6、验证集群
1)连接基本一个sentinel
docker exec -it sentinel-1 redis-cli -h 172.16.1.11 -p 26379
2) 查看基本信息
172.16.1.11:26379> info # Server redis_version:3.2.12 redis_git_sha1:00000000 redis_git_dirty:0 redis_build_id:b0df607ad3315254 redis_mode:sentinel os:Linux 3.10.0-693.el7.x86_64 x86_64 arch_bits:64 multiplexing_api:epoll gcc_version:6.3.0 process_id:1 run_id:ff490c3e11d3b9f8c4ca6fd2456c4d2cffa20bdc tcp_port:26379 uptime_in_seconds:4022 uptime_in_days:0 hz:15 lru_clock:8184650 executable:/data/redis-server config_file:/etc/redis/sentinel.conf # Clients connected_clients:2 client_longest_output_list:0 client_biggest_input_buf:0 blocked_clients:0 # CPU used_cpu_sys:4.11 used_cpu_user:3.45 used_cpu_sys_children:0.01 used_cpu_user_children:0.00 # Stats total_connections_received:3 total_commands_processed:5826 instantaneous_ops_per_sec:1 total_net_input_bytes:323533 total_net_output_bytes:36329 instantaneous_input_kbps:0.13 instantaneous_output_kbps:0.01 rejected_connections:0 sync_full:0 sync_partial_ok:0 sync_partial_err:0 expired_keys:0 evicted_keys:0 keyspace_hits:0 keyspace_misses:0 pubsub_channels:0 pubsub_patterns:0 latest_fork_usec:0 migrate_cached_sockets:0 # Sentinel sentinel_masters:1 sentinel_tilt:0 sentinel_running_scripts:0 sentinel_scripts_queue_length:0 sentinel_simulate_failure_flags:0 master0:name=mymaster,status=ok,address=172.16.1.11:16379,slaves=2,sentinels=2
3)相看master
172.16.1.11:26379> sentinel masters 1) 1) "name" 2) "mymaster" 3) "ip" 4) "172.16.1.11" 5) "port" 6) "16379" 7) "runid" 8) "657bd7c958c8a2267efd5d0148eac74db6dac780" 9) "flags" 10) "master" 11) "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "1025" 19) "last-ping-reply" 20) "1025" 21) "down-after-milliseconds" 22) "5000" 23) "info-refresh" 24) "4163" 25) "role-reported" 26) "master" 27) "role-reported-time" 28) "4209375" 29) "config-epoch" 30) "0" 31) "num-slaves" 32) "2" 33) "num-other-sentinels" 34) "1" 35) "quorum" 36) "2" 37) "failover-timeout" 38) "5000" 39) "parallel-syncs" 40) "1"
4)查看slaves
172.16.1.11:26379> sentinel slaves mymaster 1) 1) "name" 2) "172.16.1.11:16380" 3) "ip" 4) "172.16.1.11" 5) "port" 6) "16380" 7) "runid" 8) "07c0f9d673db676594da6ec38f12d9e7bbea32b2" 9) "flags" 10) "slave" 11) "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "759" 19) "last-ping-reply" 20) "759" 21) "down-after-milliseconds" 22) "5000" 23) "info-refresh" 24) "4278" 25) "role-reported" 26) "slave" 27) "role-reported-time" 28) "4319936" 29) "master-link-down-time" 30) "0" 31) "master-link-status" 32) "ok" 33) "master-host" 34) "172.16.1.11" 35) "master-port" 36) "16379" 37) "slave-priority" 38) "100" 39) "slave-repl-offset" 40) "584317" 2) 1) "name" 2) "172.16.1.11:16381" 3) "ip" 4) "172.16.1.11" 5) "port" 6) "16381" 7) "runid" 8) "6a1038dc76a386fe4137fe22f1dca7286f0774c6" 9) "flags" 10) "slave" 11) "link-pending-commands" 12) "0" 13) "link-refcount" 14) "1" 15) "last-ping-sent" 16) "0" 17) "last-ok-ping-reply" 18) "759" 19) "last-ping-reply" 20) "759" 21) "down-after-milliseconds" 22) "5000" 23) "info-refresh" 24) "4278" 25) "role-reported" 26) "slave" 27) "role-reported-time" 28) "4319936" 29) "master-link-down-time" 30) "0" 31) "master-link-status" 32) "ok" 33) "master-host" 34) "172.16.1.11" 35) "master-port" 36) "16379" 37) "slave-priority" 38) "100" 39) "slave-repl-offset" 40) "584317"
5)连接master,并存入一个key和value
[root@localhost redis-sentinel-cluster]# docker exec -it master redis-cli -h 172.16.1.11 -p 16379 172.16.1.11:16379> set boshen_name boshenboshen OK
6)连接slave1,获取boshen_name的值
[root@localhost redis-sentinel-cluster]# [root@localhost redis-sentinel-cluster]# docker exec -it slave1 redis-cli -h 172.16.1.11 -p 16380 172.16.1.11:16380> get boshen_name "boshenboshen" 172.16.1.11:16380>
由报错可知,从节点,不能写数据
7) 停掉master容器,查看是否能切换主节点
由以上结果可知:master节点迁移到了slave1上
以上是关于docker-compose安装redis-sentinel集群(1主+2副+2哨兵)的主要内容,如果未能解决你的问题,请参考以下文章
-bash: docker-compose: command not foundlinux 安装 docker-compose