当前位置:网站首页>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、可能会出的错
注五、博主自言
边栏推荐
- [mindspore] [read graph data] cannot read mindrecord format graph data
- Socket error Event: 32 Error: 10053. Connection closing...Socket close
- Technology cloud report: more than zero trust, the wild hope of Parra's "Digital Security Cloud strategy"
- 10. < tag dynamic programming and subsequence, subarray> lt.53. maximum subarray and + lt.392. Judge subsequence DBC
- Cloud native guide: what is cloud native infrastructure
- Software designer afternoon real topic: 2009-2022
- Technology cloud report: what is the difference between zero trust and SASE? The answer is not really important
- 各厂商网络虚拟化的优势
- tiktok手机网络环境怎么设置?tiktok怎么破播放量?
- [wp]ctfshow-web getting started - Explosion
猜你喜欢

Six axis sensor use learning record

Sentinel simple current limiting and degradation demo problem record

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

Connecting to the database warning establishing SSL connection without server's identity verification is not recommended

wallys//IPQ5018/IPQ6010/PD-60 802.3AT Input Output 10/100/1000M

Ml programming skills:

Legal mix of collations for operation 'Union' (bug record)

PMP practice once a day | don't get lost in the exam -7.25

4. Server startup of source code analysis of Nacos configuration center

Do you still have certificates to participate in the open source community?
随机推荐
Technology cloud report: more than zero trust, the wild hope of Parra's "Digital Security Cloud strategy"
Error when creating dataset with mindscore
A high efficiency 0-delay 0-copy QT player scheme based on Hisilicon 3559
一元函数积分学_分部积分法
The query data returned by the print database is null or the default value. Does not match the value returned by the database
PMP adopts the latest exam outline, here is [agile project management]
软件设计师下午真题:2009-2022
Application of conductive slip ring in mechanical equipment
Cloud native guide: what is cloud native infrastructure
Google pixel 6A off screen fingerprint scanner has major security vulnerabilities
Rainbond插件扩展:基于Mysql-Exporter监控Mysql
wallys//IPQ5018/IPQ6010/PD-60 802.3AT Input Output 10/100/1000M
Software designer afternoon real topic: 2009-2022
redis源码 -ziplist
Do you still have certificates to participate in the open source community?
Ml programming skills:
Bash does not add single quotes to your string
Aircraft PID control (rotor flight control)
PyTorch 模型 onnx 文件的导出和调用
Dataframe first performs grouping operation and then combines output