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
相关推荐
AAA修煤气灶刘哥1 天前
别让Redis「歪脖子」!一次搞定数据倾斜与请求倾斜的捉妖记
redis·分布式·后端
christine-rr2 天前
linux常用命令(4)——压缩命令
linux·服务器·redis
muyun28002 天前
Docker 下部署 Elasticsearch 8 并集成 Kibana 和 IK 分词器
elasticsearch·docker·容器
凯子坚持 c2 天前
精通 Redis list:使用 redis-plus-plus 的现代 C++ 实践深度解析
c++·redis·list
weixin_456904272 天前
跨域(CORS)和缓存中间件(Redis)深度解析
redis·缓存·中间件
波波烤鸭2 天前
Redis 高可用实战源码解析(Sentinel + Cluster 整合应用)
数据库·redis·sentinel
傻傻虎虎2 天前
【Docker】常用帮忙、镜像、容器、其他命令合集(2)
运维·docker·容器
MarkHard1232 天前
如何利用redis使用一个滑动窗口限流
数据库·redis·缓存
island13142 天前
【Redis#10】渐进式遍历 | 数据库管理 | redis_cli | RES
数据库·redis·bootstrap
心想事成的幸运大王2 天前
Redis的过期策略
数据库·redis·缓存