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
相关推荐
API开发9 天前
苹果芯片macOS安装版Homebrew(亲测) ,一键安装node、python、vscode等,比绿色软件还干净、无污染
vscode·python·docker·nodejs·openssl·brew·homebrew
进击的码码码码N9 天前
Docker 镜像加速
运维·docker·容器
笨手笨脚の9 天前
Redis 源码分析-Redis 中的事件驱动
数据库·redis·缓存·select·nio·epoll·io模型
Q_w77429 天前
基于 Docker 的服务部署探索(Day 2)
运维·docker·容器
(:满天星:)9 天前
Redis哨兵模式深度解析与实战部署
linux·服务器·网络·数据库·redis·缓存·centos
weixin_438335409 天前
Spring Boot:运用Redis统计用户在线数量
java·spring boot·redis
white.tie9 天前
docker方式启动Jenkins
docker·容器·jenkins
十六点五9 天前
Redis(2)——AOF持久化
java·redis·后端
编程乐学(Arfan开发工程师)9 天前
75、单元测试-嵌套测试
前端·javascript·redis·python·单元测试·bootstrap
编程乐学(Arfan开发工程师)9 天前
74、单元测试-前置条件
redis·python·阿里云·单元测试·云计算·bootstrap