当前位置:网站首页>Redis集群搭建
Redis集群搭建
2022-07-22 18:13:00 【奥利给_加油】
一.Redis集群搭建的前提
首先,在centos7上安装Redis和Docker
1)Redis安装教程
2) 安装好docker和docker-compose
(1)Docker安装教程
(2)Docker-Compose安装教程
3)启动Docker
# 查看docker服务是否启动
sudo systemctl status docker
# 如果没有启动
sudo systemctl start docker
4)启动Redis(参考上面的Redis安装教程)
二.Redis集群搭建
Redis集群的哨兵模式是一种特殊的模式,首先Redis提供了哨兵的命令,哨兵是一个独立的进程,作为进程,它会独立运行。其原理是哨兵通过发送命令,等待Redis服务器响应,从而监控运行的多个Redis实例。
哨兵模式作用:
- 通过发送命令,让Redis服务器返回监控其运行状态,包括主服务器和从服务器。
- 当哨兵监测到master宕机,会自动将slave切换成master,然后通过发布订阅模式通知其他的从服务器,修改配置文件,让它们切换主机。

除了监控Redis服务之外,哨兵之间也会互相监控。本文采用一主、双从、三哨兵方式
部署方式为:docker compose:
第一步:创建redis docker-compose.yml配置文件 目录,配置文件可根据需要调整
cd #首先进入根目录下/root
mkdir redis
vi docker-compose.yml
docker-compose.yml内容如下:
version: '3.4'
services:
master:
image: redis
container_name: redis-master
restart: always
command: redis-server --port 16380 --requirepass 123456 # 16380 是定义的主库端口,默认:6379; --requirepass 123456 是redis密码。
ports:
- 16380:16380 # 将容器的16380端口映射到宿主机的16380端口上,第一个16380为宿主机端口。
slave1:
image: redis
container_name: redis-slave-1
restart: always
command: redis-server --slaveof 127.0.0.1 16380 --port 16381 --requirepass 123456 --masterauth 123456 # 127.0.0.1 16380 为主库ip和端口(此处我们使用宿主机的ip和主库映射出来的端口);16381 是定义的从库端口,默认:6379; --requirepass 123456 是redis密码; --masterauth 123456 是主库的密码。
ports:
- 16381:16381
slave2:
image: redis
container_name: redis-slave-2
restart: always
command: redis-server --slaveof 127.0.0.1 16380 --port 16382 --requirepass 123456 --masterauth 123456 # 127.0.0.1 16380 为主库ip和端口(此处我们使用宿主机的ip和主库映射出来的端口);16382 是定义的从库端口,默认:6379; --requirepass 123456 是redis密码; --masterauth 123456 是主库的密码。
ports:
- 16382:16382
第二步:执行启动命令
在当前目录下执行启动命令
docker-compose -f docker-compose.yml up -d

第三步:创建sentinel docker-compose.yml配置文件
cd #进入根目录下/root
mkdir sentinel
vi docker-compose.yml
docker-compose.yml 文件内容如下:
version: '3.4'
services:
sentinel1:
image: redis
container_name: redis-sentinel-1
command: redis-sentinel /root/sentinel/sentinel1.conf # 自定义路径,可更改,但是需要和volumes中的路径相同。
restart: always
ports:
- 26380:26380
volumes:
- ./sentinel1.conf:/root/sentinel/sentinel1.conf # 自定义路径,可更改,但是需要和command中的路径相同。
sentinel2:
image: redis
container_name: redis-sentinel-2
command: redis-sentinel /root/sentinel/sentine2.conf
restart: always
ports:
- 26381:26381
volumes:
- ./sentinel2.conf:/root/sentinel/sentine2.conf
sentinel3:
image: redis
container_name: redis-sentinel-3
command: redis-sentinel /root/sentinel/sentine3.conf
restart: always
ports:
- 26382:26382
volumes:
- ./sentinel3.conf:/root/sentinel/sentine3.conf
sentinel1.conf
port 26380
daemonize no
pidfile /var/run/redis-sentinel.pid
dir /tmp
sentinel monitor mymaster 127.0.0.1 16380 2 # 主机 ip 与 端口;2表示当有两个sentinel认为主机失效时才会执行切换
sentinel auth-pass mymaster 123456 # 主机密码
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
sentinel2.conf
port 26381
daemonize no
pidfile /var/run/redis-sentinel.pid
dir /tmp
sentinel monitor mymaster 127.0.0.1 16380 2 # 主机 ip 与 端口;2表示当有两个sentinel认为主机失效时才会执行切换
sentinel auth-pass mymaster 123456 # 主机密码
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
sentinel3.conf
port 26382
daemonize no
pidfile /var/run/redis-sentinel.pid
dir /tmp
sentinel monitor mymaster 127.0.0.1 16380 2 # 主机 ip 与 端口;2表示当有两个sentinel认为主机失效时才会执行切换
sentinel auth-pass mymaster 123456 # 主机密码
sentinel down-after-milliseconds mymaster 30000
sentinel parallel-syncs mymaster 1
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
第四步:执行启动命令
在当前目录下执行启动命令
docker-compose -f docker-compose.yml up -d

边栏推荐
- Firewall knowledge, principle, equipment, manufacturer research summary report
- User and group management
- 初学者备战蓝桥杯历程(大学编程学习历程记录,题目思路献给需要备考蓝桥杯的同学)
- 堆基础练习题 —— 1
- Zstuacm summer camp flag bearer
- 第一个PWN 栈溢出简单题
- STM32 learning - DHT11 temperature and humidity sensor sampling drive and reporting in cjson format
- [SUCTF 2019]EasySQL
- 13. 编写程序,其中自定义一函数,用来判断一个整数是否为素数,主函数输入一个数,输出是否为素数。
- NLP学习路线图(思维导图),非常的全面和清晰!
猜你喜欢

Chapter6 convolutional neural network (CNN)

蓝桥杯31天冲刺之二十一day(C语言)

2019_ AAAI_ Multi-Interactive Memory Network for Aspect Based Multimodal Sentiment Analysis

中国电子信息产业发展研究院院长张立:打造我国主导的开源价值链

scikit-learn——机器学习应用开发的步骤

DB207-ASEMI整流桥一般用在什么地方,DB207参数尺寸

Intel(中国)云基础设施软件研发总监王庆:Intel在云原生里的技术发展和展望

Saisissez une chaîne de caractères à partir du clavier et affichez différents caractères et le nombre d'occurrences de chaque caractère. (la sortie n'est pas séquentielle) résoudre le problème en util

图卷积神经网络(GCN)浅浅析

pwn ——ret2libc3
随机推荐
Difference between get request and post request
更新C语言笔记
pwn栈溢出基础练习题——1
2019_AAAI_Multi-Interactive Memory Network for Aspect Based Multimodal Sentiment Analysis
机器学习理论基础
Configure IP address
User and group management
栈溢出基础练习题——4(写有64和32位两种攻击方式)
LC: Sword finger offer 03. repeated numbers in the array
【NumPy】
【基础3】——结构与函数
栈溢出基础练习题——3 (内有32和64位区别的对比)
2019_ AAAI_ ICCN
Solution of cross domain problems
C51 single chip microcomputer digital (display hours, minutes and seconds)
Enter two strings STR1 and STR2, and count the number of times that the string STR2 appears in STR1.
ROPgadget初识 ——— ret2syscall
Common problems of multiple processes - how to lock the same parent thread variable (critical resource) when creating multiple threads so that the shared parent thread variable is not repeatedly modif
Remember a way to connect raspberry pie wirelessly without a display screen and can't find IP
【基础7】——异常,捕获、自定义异常