docker 部署redis集群 配置

docker的网络模式

网桥模式每次重启容器都有可能导致容器ip地址变化,需要固定ip的自己自定义网络,这里介绍的是默认网络模式

docker创建容器

bash 复制代码
docker run --name redis6379 -p 6379:6379 -p 16379:16379  -v  /etc/redis/redis6379:/etc/redis -d  --restart=always  redisContainerID redis-server /etc/redis/redis.conf --appendonly yes --requirepass 123456

docker run --name redis6378-p 6378:6379 -p 16378:16379  -v  /etc/redis/redis6378:/etc/redis -d  --restart=always  redisContainerID redis-server /etc/redis/redis.conf --appendonly yes --requirepass 123456

docker run --name redis6377-p 6377:6379 -p 16377:16379  -v  /etc/redis/redis6377:/etc/redis -d  --restart=always  redisContainerID  redis-server /etc/redis/redis.conf --appendonly yes --requirepass 123456
手动创建目录:

进入宿主主机/etc/redis/目录下分别创建redis6379 redis6378 redis6377文件夹

bash 复制代码
mkdir redis6379 redis6378 redis6377

由于这里没有配置网络三个redis容器ip是docker局域网内部随机分配的,列如IP分配的是

redis6379 172.17.0.9

redis6378 172.17.0.8

redis6377 172.17.0.7

参数:

--name:设置容器名称,如 --name redis6379 当容器创建成功docker ps -a 就能找到名字为redis6379的容器
-p:端口映射 宿主机端口:容器端口
-v:文件目录映射 宿主机目录:容器目录 这样不用每次进入容器去操作文件,可以直接再宿主主机目录下操作 注意(坑): redis.conf有些配置项需要填写路径不能写宿主主机的路径 ,仍要写容器的路径
-d:容器启动 --restart=always 每次随着docker启动而启动
redis-server /etc/redis/redis.conf :redis启动以/etc/redis/redis.conf路径下的配置文件启动
--appendonly :开启持久化
--requirepass:设置密码

配置文件:

分别进入redis6379 redis6378 redis6377目录执行一下命令

bash 复制代码
wget http://download.redis.io/redis-stable/redis.conf

下载配置文件

如果文件过多,可以先下载一个然后执行一下命令(awk,xargs二选一,根据自己喜好)

awk:
bash 复制代码
echo 6378:6377| awk 'BEGIN{RS="[:]";} {cmd="cp /etc/redis/redis6379/redis.conf /etc/redis/redis"$0;system(cmd)}'
xargs:
bash 复制代码
echo {6377..6378}|xargs -n1 | xargs -I{} cp /etc/redis/redis6379/redis.conf /etc/redis/redis{}/

设计

redis6379 172.17.0.9 主节点

redis6378 172.17.0.8 从节点

redis6377 172.17.0.7 从节点

主节点(redis6379 172.17.0.9)配置文件:

主要修改:

bash 复制代码
cluster-enabled yes
protected-mode yes  如果没有设置密码则只能本地主机能访问
port 6379 容器端口
bind 172.17.0.9
#daemonize yes 这个不知道为什么我开启 redis容器启动不起来,这里我是关闭的,这个如果开启需要配置pidfile
#pidfile:/etc/redis/pidfile.pid 记录后台自起进程PID
logfile "/etc/redis/redis.log" 这里要写容器的路径不是宿主主机,但是要在映射的宿主主机目录下创建redis.log文件或者进入该容器在/etc/redis/下创建redis.log
dir /etc/redis/data    这里要写容器的路径不是宿主主机,但是要在映射的宿主主机目录下创建data目录或者进入该容器在/etc/redis/下创建data目录
#replicaof 172.17.0.9 6379 由于这里是主节点所欲不需要配置该项
#masterauth 123456 由于这里是主节点所欲不需要配置该项
requirepass 123456 
appendonly yes
cluster-config-file /etc/redis/nodes-6379.conf
cluster-node-timeout 15000

从节点1(redis6378 172.17.0.8)配置文件:

主要修改:

bash 复制代码
cluster-enabled no (坑)这里从节点不能开启
protected-mode yes  如果没有设置密码则只能本地主机能访问
port 6379 容器端口
bind 172.17.0.8
#daemonize yes 这个不知道为什么我开启 redis容器启动不起来,这里我是关闭的,这个如果开启需要配置pidfile
#pidfile:/etc/redis/pidfile.pid 记录后台自起进程PID
logfile "/etc/redis/redis.log" 这里要写容器的路径不是宿主主机,但是要在映射的宿主主机目录下创建redis.log文件或者进入该容器在/etc/redis/下创建redis.log
dir /etc/redis/data    这里要写容器的路径不是宿主主机,但是要在映射的宿主主机目录下创建data目录或者进入该容器在/etc/redis/下创建data目录
replicaof 172.17.0.9 6379 由于这里是主节点所欲不需要配置该项
masterauth 123456 由于这里是主节点所欲不需要配置该项
requirepass 123456 
appendonly yes
cluster-config-file /etc/redis/nodes-6378.conf
cluster-node-timeout 15000

从节点2(redis6377 172.17.0.7)配置文件:

主要修改:

bash 复制代码
cluster-enabled yes
protected-mode yes  如果没有设置密码则只能本地主机能访问
port 6379 容器端口
bind 172.17.0.9
#daemonize yes 这个不知道为什么我开启 redis容器启动不起来,这里我是关闭的,这个如果开启需要配置pidfile
#pidfile:/etc/redis/pidfile.pid 记录后台自起进程PID
logfile "/etc/redis/redis.log" 这里要写容器的路径不是宿主主机,但是要在映射的宿主主机目录下创建redis.log文件或者进入该容器在/etc/redis/下创建redis.log
dir /etc/redis/data    这里要写容器的路径不是宿主主机,但是要在映射的宿主主机目录下创建data目录或者进入该容器在/etc/redis/下创建data目录
#replicaof 172.17.0.9 6379 由于这里是主节点所欲不需要配置该项
#masterauth 123456 由于这里是主节点所欲不需要配置该项
requirepass 123456 
appendonly yes
cluster-config-file /etc/redis/nodes-6379.conf
cluster-node-timeout 15000

从节点1(redis6378 172.17.0.8)配置文件:

主要修改:

bash 复制代码
cluster-enabled no (坑)这里从节点不能开启
protected-mode yes  如果没有设置密码则只能本地主机能访问
port 6379 容器端口
bind 172.17.0.7
#daemonize yes 这个不知道为什么我开启 redis容器启动不起来,这里我是关闭的,这个如果开启需要配置pidfile
#pidfile:/etc/redis/pidfile.pid 记录后台自起进程PID
logfile "/etc/redis/redis.log" 这里要写容器的路径不是宿主主机,但是要在映射的宿主主机目录下创建redis.log文件或者进入该容器在/etc/redis/下创建redis.log
dir /etc/redis/data    这里要写容器的路径不是宿主主机,但是要在映射的宿主主机目录下创建data目录或者进入该容器在/etc/redis/下创建data目录
replicaof 172.17.0.9 6379 由于这里是主节点所欲不需要配置该项
masterauth 123456 由于这里是主节点所欲不需要配置该项
requirepass 123456 
appendonly yes
cluster-config-file /etc/redis/nodes-6377.conf
cluster-node-timeout 15000

具体配置参数:请查看
https://blog.csdn.net/sunboylife/article/details/108464722

重启容器:

bash 复制代码
docker  restart redis6379  redis6378 redis6377

进入主节点

bash 复制代码
docker exec -ti redis6379 /bin/bash

登录客户端

redis-cli -h 172.17.0.9 -a 123456

查看节点信息

bash 复制代码
 info replication

效果:

相关推荐
绯雨千叶12 小时前
修改Docker镜像和容器的默认存储目录(迁移原有数据)
运维·docker·容器
一只落魄的蜂鸟12 小时前
《图解技术体系》Three architectures and application scenarios of Redis
数据库·redis·缓存
悬弧12 小时前
第1章:Dashboard初体验 - 你的可视化K8s控制台
云原生·容器·kubernetes
就叫飞六吧13 小时前
docker一键部署gitlab
docker·容器·gitlab
ernesto_ji16 小时前
docker部署nginxUI
docker
遇见火星21 小时前
CentOS7 通过源码安装 Redis
数据库·redis·缓存
K哥112521 小时前
【9天Redis系列】基础+全局命令
数据库·redis·缓存
f***R821 小时前
redis分页查询
数据库·redis·缓存
2***c4351 天前
Redis——使用 python 操作 redis 之从 hmse 迁移到 hset
数据库·redis·python
2***d8851 天前
redis的启动方式
数据库·redis·bootstrap