Docker 搭建redis集群

分布式集群
1、redis分布式集群(每个节点存储一部分桶数据)

redis.conf配置文件内容如下(这里有些配置不生效的就在启动创建容器上加)

bash 复制代码
daemonize no
# 不限制访问
bind 0.0.0.0
# redis 端口号
port 6379
# 设置密码
requirepass "123456"
# 持久化
appendonly yes
#运行外部访问ip
protected-mode no
#开启集群模式
cluster-enabled yes
#集群配置文件
cluster-config-file nodes.conf
#链接超时时间
cluster-node-timeout 5000
#链接的主机IP
cluster-announce-ip 192.168.1.10
#集群的端口
cluster-announce-port 6379
#总线端口
cluster-announce-bus-port 16379
2、主节点192.168.1.10
bash 复制代码
#主节点创建命令
docker run --name redis -p 6379:6379 -p 16379:16379 --privileged=true -v /home/redis/data:/data -v /home/redis/redis.conf:/usr/local/bin/redis.conf redis:v1.0 --requirepass "123456" --daemonize "no" --protected "no" --appendonly "yes" --bind "0.0.0.0" --cluster-enabled "yes" --cluster-config-file "nodes6379.conf" --cluster-node-timeout "5000" --cluster-announce-ip "192.168.1.10" --cluster-announce-port "6379" --cluster-announce-bus-port "6379"

192.168.1.11从节点需要的执行下面命令

bash 复制代码
docker run --name redisSlave -p 6379:6379 --privileged=true -v /home/redis/data:/data -v /home/redis/redis.conf:/usr/local/bin/redis.conf redis:v1.0 --requirepass "123456" --replicaof 192.168.1.10 6379 --masterauth "123456" --protected-mode "no" --appendonly "yes" --bind "0.0.0.0"

使用 info replication执行发现要是没有挂上主节点(下图为参考),那就需要登录从节点后执行

slaveof 192.168.1.10 去关联上主节点(这样是一主一从)

bash 复制代码
#登录从节点命令
docker exec -it redisSlave redis-cli -p 6379 -a 123456
3、3主3从

其他主节点也是这样创建后,3主节点进行关联,执行命令

--cluster-replicas 1 :为每一个主节点创建一个副本所以这儿 3主3从

bash 复制代码
#这个是3主3从
redis-cli -a 123456 --cluster create 192.168.1.10:6379 192.168.1.11:6379 192.168.1.12:6379 192.168.1.13:6379 192.168.1.14:6379 192.168.1.15:6379 --cluster-replicas 1

#这个是3主节点 没有从节点
redis-cli -a 123456 --cluster create 192.168.1.10:6379 192.168.1.11:6379 192.168.1.12:6379 --cluster-replicas 0
redis哨兵模式
1、哨兵模式需要先按照主从模式(192.168.1.10),从节点(192.168.1.11)要是没关联上就登录后执行 slaveof 192.168.1.10 去关联上主节点(这样是一主一从)
bash 复制代码
#主节点创建命令
docker run --name redis -p 6379:6379 --privileged=true -v /home/redis/data:/data -v /home/redis/redis.conf:/usr/local/bin/redis.conf redis:v1.0 --requirepass "123456" --daemonize "no" --daemonize "no" --bind "0.0.0.0" --appendonly "no"
bash 复制代码
#从节点创建命令
docker run --name redis -p 6379:6379 --privileged=true -v /home/redis/data:/data -v /home/redis/redis.conf:/usr/local/bin/redis.conf redis:v1.0 --requirepass "123456" --protected-mode "no" --daemonize "no" --bind "0.0.0.0" --appendonly "yes"  --replicaof 192.168.1.10 6379 --masterauth "123456"
2、哨兵创建,配置文件sentinel.conf如下
bash 复制代码
port 26379
daemonize no
protected-mode no
#哨兵监控服务 当有1台宕机 既算宕机 监控主节点的配置 mymaster名字随便起;2因为是三台哨兵,所以大于等于半数票数2
sentinel monitor mymaster 192.168.1.10
# 配置主节点密码
sentinel auth-pass mymaster 123456
3、创建哨兵模式的主节点
bash 复制代码
# 主节点sentinel  192.168.1.12
docker run --net=host -it  --name redisSentinel -p 26379:26379 -v /home/redis/sentinel.conf:/usr/local/bin/sentinel.conf  redis:v1.0 redis-sentinel /usr/local/bin/sentinel.conf

# 从节点01 sentinel 192.168.1.13 从节点的sentinel.conf和主节点一样
docker run --net=host -it  --name redisSentinel -p 26379:26379 -v /home/redis/sentinel.conf:/usr/local/bin/sentinel.conf  redis:v1.0 redis-sentinel /usr/local/bin/sentinel.conf

进入sentinel 验证查看

bash 复制代码
#进入命令
docker exec -it redisSentinel redis-cli -p 26379 -a 123456

# 查看信息 哨兵集群
info sentinel

# 展示如下信息
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_tilt_since_seconds:-1
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.1.10:6379,slaves=1,sentinels=2


# 其他命令
sentinel master mymaster
sentinel sentinels mymaster
sentinel slaves mymaster
相关推荐
haixingtianxinghai1 小时前
Redis真的是单线程吗?
数据库·redis·缓存
多云的夏天1 小时前
docker容器部署-windows-ubuntu
java·docker·容器
淡泊if3 小时前
1.2GB → 98MB,我的 Docker 镜像瘦身实战记录
运维·docker·容器
Sst的头号粉丝3 小时前
Docker——cgroups
运维·docker·容器
庞轩px4 小时前
缓存Key设计的“七要七不要”
java·jvm·redis·缓存
❀͜͡傀儡师4 小时前
Docker 部署Datart BI工具完整指南(PostgreSQL 持久化存储)
docker·postgresql·容器
l1t5 小时前
解决用docker安装umbra数据库遇到的FATAL:Operation not permitted错误
数据库·docker·容器
last demo5 小时前
docker存储
运维·docker·容器
難釋懷5 小时前
Redis分片集群手动故障转移
数据库·redis·缓存
无名-CODING5 小时前
从零开始!Vue3+SpringBoot前后端分离项目Docker部署实战(上):环境搭建与数据库容器化
数据库·spring boot·docker