当前位置:网站首页>Docker 搭建 Redis Cluster集群
Docker 搭建 Redis Cluster集群
2022-07-25 20:08:00 【InfoQ】
共勉一、环境
- 阿里云服务器 CentOS 8
- docker版本为20.10.7
- redis 镜像 (拉取为默认最新镜像)


- 下载Redis镜像
- 编写Redis配置文件
- 启动Redis 容器
- 创建Redis Cluster 集群。
- 进行实际测试
二、前期准备
2.1、搜索、拉取redis镜像
docker search redis
docker pull redis


2.2、Docker 容器网络
- 创建虚拟网卡
- 创建虚拟网卡,主要是用于redis-cluster能于外界进行网络通信,一般常用桥接模式。
docker network create myredis
- 查看Docker 网卡信息
docker network ls

docker network inspect myredis

docker network rm myredis #删除网卡命令 多个中间 空格隔开
docker network --help #显示可带参数等
2.3、编写配置文件
shllefor port in $(seq 6379 6384);
do
mkdir -p /home/redis/node-${port}/conf
touch /home/redis/node-${port}/conf/redis.conf
cat << EOF > /home/redis/node-${port}/conf/redis.conf
port ${port}
requirepass 1234
bind 0.0.0.0
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 服务器就填公网ip,或者内部对应容器的ip
cluster-announce-port ${port}
cluster-announce-bus-port 1${port}
EOF
done
port:节点端口;
requirepass:设置密码,访问时需要验证
protected-mode:保护模式,默认值 yes,即开启。开启保护模式以后,需配置bind ip或者设置访问密码;关闭保护模式,外部网络可以直接访问;
daemonize:是否以守护线程的方式启动(后台启动),默认 no;
appendonly:是否开启 AOF 持久化模式,默认 no;
cluster-enabled:是否开启集群模式,默认 no;
cluster-config-file:集群节点信息文件;
cluster-node-timeout:集群节点连接超时时间;
cluster-announce-ip:集群节点 IP
注意:如果你想要你的redis集群可以供外网访问,这里直接填 服务器的IP 地址即可
- 如若为了安全,只是在服务器内部进行访问,这里还需要做一些修改。
cluster-announce-port:集群节点映射端口;
cluster-announce-bus-port:集群节点总线端口。


treeyum install -y tree
三、启动容器
3.1、启动redis容器
for port in $(seq 6379 6384); \
do \
docker run -it -d -p ${port}:${port} -p 1${port}:1${port} \
--privileged=true -v /home/redis/node-${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
--privileged=true -v /home/redis/node-${port}/data:/data \
--restart always --name redis-${port} --net myredis \
--sysctl net.core.somaxconn=1024 redis redis-server /usr/local/etc/redis/redis.conf
done
- -it:交互
- -d:后台运行,容器启动完成后打印容器
- --privileged:是否让docker 应用容器 获取宿主机root权限(特殊权限-)
- -p :端口映射
- -v:文件挂载
- --sysctl参数来设置系统参数,通过这些参数来调整系统性能
- --restart always:在容器退出时总是重启容器
- --name :给容器取名
- --net myredis :使用我们创建的虚拟网卡 (想详细了解,可以去看看Docker 网络方面知识)

docker ps -a
3.2、创建Redis Cluster集群
1、进入redis-6379 容器
docker exec -it redis-6379 /bin/bash

2、创建集群
redis-cli -a 之前设置的密码 --cluster create 配置文件中的IP地址:6379 IP地址:6380 IP地址:6381 IP地址:6382 IP地址:6383 IP地址:6384 --cluster-replicas 1


3、查看节点相关信息
redis-cli -c -a 1234
cluster info
cluster nodes
四、测试
4.1、本机测试


4.2、外网测试



4.3、出错可能会用到的命令
for port in $(seq 6379 6384);
do
docker stop redis-${port}
done
for port in $(seq 6379 6384);
do
docker rm redis-${port}
done
4.4、可能会出的错
注五、博主自言
边栏推荐
- C language learning diary 3 - realloc function
- Day7: ordered binary tree (binary search tree)
- Share 25 useful JS single line codes
- SecureCRT garbled code solution [easy to understand]
- From Tong Dai to "Tong Dai" and then to brand, the beauty of sudden profits has changed and remained unchanged
- Digital informatization (enumerate assumptions first, and then see whether the conditions are met) (1089 werewolf kill - simple version)
- 什么是聚类分析?聚类分析方法的类别[通俗易懂]
- 笔记——记录一个CannotFindDataSourceException: dynamic-datasource can not find primary datasource问题解决
- Oracle database download, installation, use tutorial and problem summary
- Six axis sensor use learning record
猜你喜欢

Vivo official website app full model UI adaptation scheme

PMP采用最新考纲,这里有【敏捷项目管理】

4、Nacos 配置中心源码解析之 服务端启动

How much memory does bitmap occupy in the development of IM instant messaging?

9.< tag-动态规划和子序列, 子数组>lt.718. 最长重复子数组 + lt.1143. 最长公共子序列

Stochastic gradient descent method, Newton method, impulse method, adagrad, rmsprop and Adam optimization process and understanding

连接数据库警告 Establishing SSL connection without server‘s identity verification is not recommended.

C language learning diary 3 - realloc function

推荐系统专题 | MiNet:跨域CTR预测

如何保证定制滑环质量
随机推荐
Arrow 之 Parquet
Prescan quick start to master the special functions of prescan track editing in lecture 18
Technology cloud report: more than zero trust, the wild hope of Parra's "Digital Security Cloud strategy"
PreScan快速入门到精通第十九讲之PreScan执行器配置、轨迹同步及非配多个轨迹
Yyds dry inventory how to locate browser page crash
Partial interpretation of yolov7 paper [including my own understanding]
Technology cloud report: what is the difference between zero trust and SASE? The answer is not really important
Dataframe first performs grouping operation and then combines output
Rainbond插件扩展:基于Mysql-Exporter监控Mysql
CarSim simulation quick start (XIV) - CarSim Simulink joint simulation
Software designer afternoon real topic: 2009-2022
推荐系统专题 | MiNet:跨域CTR预测
LP dual currency pledge liquidity mining DAPP system development logic analysis
CarSim仿真快速入门(十五)—CarSim传感器仿真之ADAS Sensor Objects (1)
Recommended system topic | Minet: cross domain CTR prediction
【云原生 | 从零开始学Kubernetes】八、命名空间资源配额以及标签
PMP每日一练 | 考试不迷路-7.25
RepVGG网络中重参化网络结构解读【附代码】
Three skills of interface request merging, and the performance is directly exploded!
Analysis of CMS station building system of common PHP in China