redis主从配置情况下从服务器是只读不能写的,通过哨兵模式自动切换主备,程序配置需要将所有节点配置进去
准备两台虚拟机,系统centos7
ip分配
host1:192.168.3.53
host2:192.168.3.54
安装docker
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum list docker-ce --showduplicates | sort -r
yum install -y docker-ce-18.09.0 docker-ce-cli-18.09.0
systemctl start docker.service
systemctl enable docker.service
docker version
安装docker-compose
curl -L https://get.daocloud.io/docker/compose/releases/download/1.29.2/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose version
host1的docker-compose.yml
version: '3'
services:
redis-rs1:
container_name: 'redis_rs1'
image: 'redis:5.0.3'
restart: always
volumes:
- ~/commpwr/redis/data:/data
- ~/commpwr/redis/conf/redis.conf:/etc/redis/redis.conf
ports:
- 6379:6379
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:
- ~/commpwr/sentinel/conf/sentinel.conf:/etc/redis/sentinel.conf
ports:
- 26379:26379
command: redis-sentinel /etc/redis/sentinel.conf
host2的docker-compose.yml
version: '3'
services:
redis-rs2:
container_name: 'redis_rs2'
image: 'redis:5.0.3'
restart: always
volumes:
- ~/commpwr/redis/data:/data
- ~/commpwr/redis/conf/redis.conf:/etc/redis/redis.conf
ports:
- 6379:6379
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:
- ~/commpwr/sentinel/conf/sentinel.conf:/etc/redis/sentinel.conf
ports:
- 26379:26379
command: redis-sentinel /etc/redis/sentinel.conf
redis.conf下载
5.0版本
wget https://raw.githubusercontent.com/redis/redis/5.0/redis.conf
下载后修改如下内容
注释掉 bind
添加或修改masterauth 123456
host1的sentinel.conf
# bind 127.0.0.1 192.168.1.1
# protected-mode no
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.3.53 6379 1
sentinel down-after-milliseconds master001 10000
sentinel auth-pass master001 123456
sentinel config-epoch master001 4
sentinel leader-epoch master001 5
sentinel known-replica master001 192.168.3.54 6379
# Generated by CONFIG REWRITE
dir "/data"
sentinel known-sentinel master001 192.168.3.54 26379 f5e14a1e2d376544dcc2ce88a37a710d5c8a65a4
sentinel known-sentinel master001 192.168.3.53 26379 02ba5344dfb96dc699bcefc2e1d105190c0e85e3
sentinel current-epoch 11
sentinel announce-ip "192.168.3.53"
sentinel announce-port 26379
masterauth "123456"
host2的sentinel.conf
# bind 127.0.0.1 192.168.1.1
# protected-mode no
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.3.53 6379 1
sentinel down-after-milliseconds master001 10000
sentinel auth-pass master001 123456
sentinel config-epoch master001 4
sentinel leader-epoch master001 11
sentinel known-replica master001 192.168.3.54 6379
# Generated by CONFIG REWRITE
dir "/data"
sentinel known-sentinel master001 192.168.3.53 26379 02ba5344dfb96dc699bcefc2e1d105190c0e85e3
sentinel current-epoch 11
sentinel announce-ip "192.168.3.54"
sentinel announce-port 26379
防火墙配置
firewall-cmd --permanent --add-port=6379/tcp
firewall-cmd --permanent --add-port=26379/tcp
firewall-cmd --reload
切换演练
host1执行
docker stop redis_rs1
host2 redis控制台执行
info replication
可以看到host2变成了master
host1执行
docker start redis_rs1
host1redis控制台执行
info replication
可以看到host1成了从节点并连接上了host2的主节点。