1、准备两台虚拟机或者物理机
node1 IP:192.168.123.78
node2 IP:192.168.123.82
2、安装docker和docker compose
3、安装node1,配置docker-compose.yml
version: '3'
services:
redis-rs1:
container_name: 'redis_node1'
image: 'redis:5.0.3'
restart: always
volumes:
- ./redis/data:/data
- ./redis/conf/redis.conf:/etc/redis/redis.conf
ports:
- 6379:6379
sysctls:
- net.core.somaxconn=1024
command: redis-server /etc/redis/redis.conf --port 6379 --requirepass 123456 --appendonly yes
sentinel:
container_name: 'sentinel'
image: 'redis:5.0.3'
restart: always
volumes:
- ./sentinel/conf/sentinel.conf:/etc/redis/sentinel.conf
ports:
- 26379:26379
command: redis-sentinel /etc/redis/sentinel.conf
获取配置文件
wget https://raw.githubusercontent.com/redis/redis/5.0/redis.conf
修改node1下的redis.conf
注释掉 bind
添加或修改 masterauth 123456
4、安装node2,配置docker-compose.yml
version: '3'
services:
redis-rs2:
container_name: 'redis_node2'
image: 'redis:5.0.3'
restart: always
volumes:
- ./redis/data:/data
- ./redis/conf/redis.conf:/etc/redis/redis.conf
ports:
- 6379:6379
sysctls:
- net.core.somaxconn=1024
command: redis-server /etc/redis/redis.conf --port 6379 --requirepass 123456 --appendonly yes --slaveof 192.168.3.53 6379 --masterauth 123456
sentinel:
container_name: 'sentinel'
image: 'redis:5.0.3'
restart: always
volumes:
- ./sentinel/conf/sentinel.conf:/etc/redis/sentinel.conf
ports:
- 26379:26379
command: redis-sentinel /etc/redis/sentinel.conf
获取配置文件
wget https://raw.githubusercontent.com/redis/redis/5.0/redis.conf
修改node1下的redis.conf
注释掉 bind
添加或修改 masterauth 123456
5、配置node1下的sentinel.conf
port 26379
#daemonize yes
#pidfile /var/run/redis-sentinel.pid
logfile "sentinel.log"
sentinel myid 02ba5344dfb96dc699bcefc2e1d105190c0e85e3
sentinel deny-scripts-reconfig yes
sentinel monitor master001 192.168.123.82 6379 1
sentinel down-after-milliseconds master001 10000
sentinel auth-pass master001 123456
sentinel config-epoch master001 13
sentinel leader-epoch master001 13
sentinel known-replica master001 192.168.123.78 6379
# Generated by CONFIG REWRITE
dir "/data"
sentinel known-sentinel master001 192.168.123.78 26379 02ba5344dfb96dc699bcefc2e1d105190c0e85e3
sentinel known-sentinel master001 192.168.123.82 26379 f5e14a1e2d376544dcc2ce88a37a710d5c8a65a4
sentinel current-epoch 13
sentinel announce-ip "192.168.123.78"
sentinel announce-port 26379
masterauth "123456"
6、配置node2下的sentinel.conf
port 26379
#daemonize yes
#pidfile /var/run/redis-sentinel.pid
logfile "sentinel.log"
sentinel myid f5e14a1e2d376544dcc2ce88a37a710d5c8a65a4
sentinel deny-scripts-reconfig yes
sentinel monitor master001 192.168.123.82 6379 1
sentinel down-after-milliseconds master001 10000
sentinel auth-pass master001 123456
sentinel config-epoch master001 13
sentinel leader-epoch master001 13
sentinel known-replica master001 192.168.123.78 6379
# Generated by CONFIG REWRITE
dir "/data"
sentinel known-sentinel master001 192.168.123.78 26379 02ba5344dfb96dc699bcefc2e1d105190c0e85e3
sentinel current-epoch 13
sentinel announce-ip "192.168.123.82"
sentinel announce-port 26379
7、关闭防火墙
firewall-cmd --permanent --add-port=6379/tcp
firewall-cmd --permanent --add-port=26379/tcp
firewall-cmd --reload
8、测试验证
进入到node1或者node2的redis容器中
docker exec -it 5942a4c958ce /bin/bash
9、查看redis的模式
root@5942a4c958ce:/data# redis-cli -h 127.0.0.1 -p 6379 -a "123456"
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:192.168.123.82
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:36973
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:08a5a246af256bcf10d1d3618805527fb1fe7544
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:36973
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:31055
repl_backlog_histlen:5919