前提:本试验环境已经提前安装了docker和docker-compose
说明:本次部署是单机伪集群,想要部署真正的集群,需要将秒个主件拆分到各个机器上去部署,只修改ip地址
1、下载redis的相关镜像
本人已经将做好的redis镜像放到阿里云的仓库了,要下载,直接执行下面的命令即可
docker pull registry.cn-hangzhou.aliyuncs.com/boshen-ns/redis-sentinel:
docker pull registry.cn-hangzhou.aliyuncs.com/boshen-ns/redis:
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-
mkdir -p /home/redis-sentinel-cluster/data/sentinel-
3、创建2个哨兵的配置文件 (集群启动后会自动更新该配置文件 )
/home/redis-sentinel-cluster/sentinel-1.conf
port dir "/data"
sentinel monitor mymaster 172.16.1.11
sentinel down-after-milliseconds mymaster
sentinel failover-timeout mymaster
sentinel parallel-syncs mymaster 1
/home/redis-sentinel-cluster/sentinel-2.conf
port dir "/data"
sentinel monitor mymaster 172.16.1.11
sentinel down-after-milliseconds mymaster
sentinel failover-timeout mymaster
sentinel parallel-syncs mymaster
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:
container_name: master
restart: always
network_mode: "host"
volumes:
- ./data/master:/data
command: redis-server --port
slave1:
image: registry.cn-hangzhou.aliyuncs.com/boshen-ns/redis:
container_name: slave1
restart: always
network_mode: "host"
volumes:
- ./data/slave1:/data
command: redis-server --port --slaveof 172.16.1.11
slave2:
image: registry.cn-hangzhou.aliyuncs.com/boshen-ns/redis:
container_name: slave2
restart: always
network_mode: "host"
volumes:
- ./data/slave2:/data
command: redis-server --port --slaveof 172.16.1.11
sentinel-:
image: registry.cn-hangzhou.aliyuncs.com/boshen-ns/redis-sentinel:
container_name: sentinel-
restart: always
network_mode: "host"
volumes:
- ./sentinel-.conf:/etc/redis/sentinel.conf
- ./data/sentinel-:/data
sentinel-:
image: registry.cn-hangzhou.aliyuncs.com/boshen-ns/redis-sentinel:
container_name: sentinel-
restart: always
network_mode: "host"
volumes:
- ./sentinel-.conf:/etc/redis/sentinel.conf
- ./data/sentinel-:/data
5、启动redis集群
cd /home/redis-sentinel-cluster
docker-compose up -d
6、验证集群
1)连接基本一个sentinel
docker exec -it sentinel- redis-cli -h 172.16.1.11 -p
2) 查看基本信息
172.16.1.11:> info
# Server
redis_version:3.2.
redis_git_sha1:
redis_git_dirty:
redis_build_id:b0df607ad3315254
redis_mode:sentinel
os:Linux 3.10.-.el7.x86_64 x86_64
arch_bits:
multiplexing_api:epoll
gcc_version:6.3.
process_id:
run_id:ff490c3e11d3b9f8c4ca6fd2456c4d2cffa20bdc
tcp_port:
uptime_in_seconds:
uptime_in_days:
hz:
lru_clock:
executable:/data/redis-server
config_file:/etc/redis/sentinel.conf # Clients
connected_clients:
client_longest_output_list:
client_biggest_input_buf:
blocked_clients: # 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:
total_commands_processed:
instantaneous_ops_per_sec:
total_net_input_bytes:
total_net_output_bytes:
instantaneous_input_kbps:0.13
instantaneous_output_kbps:0.01
rejected_connections:
sync_full:
sync_partial_ok:
sync_partial_err:
expired_keys:
evicted_keys:
keyspace_hits:
keyspace_misses:
pubsub_channels:
pubsub_patterns:
latest_fork_usec:
migrate_cached_sockets: # Sentinel
sentinel_masters:
sentinel_tilt:
sentinel_running_scripts:
sentinel_scripts_queue_length:
sentinel_simulate_failure_flags:
master0:name=mymaster,status=ok,address=172.16.1.11:16379,slaves=2,sentinels=2
3)相看master
172.16.1.11:> sentinel masters
) ) "name"
) "mymaster"
) "ip"
) "172.16.1.11"
) "port"
) "16379"
) "runid"
) "657bd7c958c8a2267efd5d0148eac74db6dac780"
) "flags"
) "master"
) "link-pending-commands"
) "0"
) "link-refcount"
) "1"
) "last-ping-sent"
) "0"
) "last-ok-ping-reply"
) "1025"
) "last-ping-reply"
) "1025"
) "down-after-milliseconds"
) "5000"
) "info-refresh"
) "4163"
) "role-reported"
) "master"
) "role-reported-time"
) "4209375"
) "config-epoch"
) "0"
) "num-slaves"
) "2"
) "num-other-sentinels"
) "1"
) "quorum"
) "2"
) "failover-timeout"
) "5000"
) "parallel-syncs"
) "1"
4)查看slaves
172.16.1.11:> sentinel slaves mymaster
) ) "name"
) "172.16.1.11:16380"
) "ip"
) "172.16.1.11"
) "port"
) "16380"
) "runid"
) "07c0f9d673db676594da6ec38f12d9e7bbea32b2"
) "flags"
) "slave"
) "link-pending-commands"
) "0"
) "link-refcount"
) "1"
) "last-ping-sent"
) "0"
) "last-ok-ping-reply"
) "759"
) "last-ping-reply"
) "759"
) "down-after-milliseconds"
) "5000"
) "info-refresh"
) "4278"
) "role-reported"
) "slave"
) "role-reported-time"
) "4319936"
) "master-link-down-time"
) "0"
) "master-link-status"
) "ok"
) "master-host"
) "172.16.1.11"
) "master-port"
) "16379"
) "slave-priority"
) "100"
) "slave-repl-offset"
) "584317"
) ) "name"
) "172.16.1.11:16381"
) "ip"
) "172.16.1.11"
) "port"
) "16381"
) "runid"
) "6a1038dc76a386fe4137fe22f1dca7286f0774c6"
) "flags"
) "slave"
) "link-pending-commands"
) "0"
) "link-refcount"
) "1"
) "last-ping-sent"
) "0"
) "last-ok-ping-reply"
) "759"
) "last-ping-reply"
) "759"
) "down-after-milliseconds"
) "5000"
) "info-refresh"
) "4278"
) "role-reported"
) "slave"
) "role-reported-time"
) "4319936"
) "master-link-down-time"
) "0"
) "master-link-status"
) "ok"
) "master-host"
) "172.16.1.11"
) "master-port"
) "16379"
) "slave-priority"
) "100"
) "slave-repl-offset"
) "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:> 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:> get boshen_name
"boshenboshen"
172.16.1.11:>
由报错可知,从节点,不能写数据
7) 停掉master容器,查看是否能切换主节点
由以上结果可知:master节点迁移到了slave1上