使用docker搭建 redis cluster 集群

这篇具有很好参考价值的文章主要介绍了使用docker搭建 redis cluster 集群。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

使用docker搭建redis-cluster集群

目录

1.拉取redis镜像

2.创建network

3.创建redis配置文件

4.创建redis容器

5.创建Redis Cluster集群

6.测试


1.拉取redis镜像

docker pull redis

2.创建network

docker容器创建的应用会默认使用 bridge,但是每次重启docker此网络的ip会动态变更,因此需要我们自己手动创建一个固定的network

#创建虚拟网卡
docker network create myredis
#查看虚拟网卡
docker network ls

使用docker搭建 redis cluster 集群

其它虚拟网卡相关命令

#查看虚拟网卡详细信息
docker network inspect myredis
#删除虚拟网卡
docker network rm myredis

3.创建redis配置文件

需要创建多个redis.conf配置文件,采用shell脚本的方式创建

createConf.sh 脚本内容如下所示:

#!/bin/sh
​
for port in $(seq 6380 6385);
do
mkdir -p /home/ap/chenby/redisCluster/node-${port}/conf
touch /home/ap/chenby/redisCluster/node-${port}/conf/redis.conf
cat  << EOF > /home/ap/chenby/redisCluster/node-${port}/conf/redis.conf
#节点端口
port ${port}
#添加访问认证
requirepass 1234
#如果主节点开启了访问认证,从节点访问主节点需要认证
masterauth 1234
#保护模式,默认值 yes,即开启。开启保护模式以后,需配置 bind ip 或者设置访问密码;关闭保护模式,外部网络可以直接访问
protected-mode no
#bind 0.0.0.0
​
#是否以守护线程的方式启动(后台启动),默认 no
daemonize no
#是否开启 AOF 持久化模式,默认 no
appendonly yes
#是否开启集群模式,默认 no
cluster-enabled yes 
#集群节点信息文件
cluster-config-file nodes.conf
#群节点连接超时时间
cluster-node-timeout 5000
#集群节点 IP
cluster-announce-ip 192.168.137.103
#集群节点映射端口
cluster-announce-port ${port}
#集群节点总线端口
cluster-announce-bus-port 1${port}
EOF
done

执行脚本,运行结果如下:

使用docker搭建 redis cluster 集群

4.创建redis容器

因为要创建多个容器,使用shell脚本的方式创建

redis.sh脚本内容:

#!/bin/sh
​
## 首次创建并运行redis容器
if [ "$1" = "create" ]; then
        for port in $(seq 6380 6385);
        do
              docker run -di --restart always --name redis-${port} --net myredis -p ${port}:${port} -p 1${port}:1${port} -v /home/ap/chenby/redisCluster/node-${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf -v /home/ap/chenby/redisCluster/node-${port}/data:/data redis redis-server /usr/local/etc/redis/redis.conf
        done
fi
​
##停止redis容器
if [ "$1" = "stop" ]; then
        for port in $(seq 6380 6385);
        do
                docker stop redis-${port}
        done
fi
​
##启动已有的redis容器
if [ "$1" = "start" ]; then
        for port in $(seq 6380 6385);
        do
                docker start redis-${port}
        done
fi
​
## 删除redis容器
if [ "$1" = "rm" ]; then
        for port in $(seq 6380 6385);
        do
                docker rm redis-${port}
        done
fi

执行命令 : sh redis.sh create

结果如下图所示,证明创建成功

使用docker搭建 redis cluster 集群

执行命令 docker ps -a 查看容器,如下图所示,证明创建并全部启动成功

使用docker搭建 redis cluster 集群

5.创建Redis Cluster集群

5.1 任意选择一个redis节点进入,创建redis集群

进入redis-6380容器:

docker exec -it redis-6380 /bin/bash

使用docker搭建 redis cluster 集群

5.2 创建集群

redis-cli -a 1234 --cluster create 192.168.137.103:6380 192.168.137.103:6381 192.168.137.103:6382 192.168.137.103:6383 192.168.137.103:6384 192.168.137.103:6385 --cluster-replicas 1

根据出现的信息,输入yes

使用docker搭建 redis cluster 集群

创建成功如下:

root@c81e4c88ef59:/data# redis-cli -a 1234 --cluster create 192.168.137.103:6380 192.168.137.103:6381 192.168.137.103:6382 192.168.137.103:6383 192.168.137.103:6384 192.168.137.103:6385 --cluster-replicas 1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.137.103:6384 to 192.168.137.103:6380
Adding replica 192.168.137.103:6385 to 192.168.137.103:6381
Adding replica 192.168.137.103:6383 to 192.168.137.103:6382
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 5de830891133b5621d06901d970ef84c026a6bd1 192.168.137.103:6380
   slots:[0-5460] (5461 slots) master
M: 39e4e1aa64ab168f0d875c0f187343382d7aadc0 192.168.137.103:6381
   slots:[5461-10922] (5462 slots) master
M: 30c40c7eb92bc3decdc9f8e0f7ac5c1d04865866 192.168.137.103:6382
   slots:[10923-16383] (5461 slots) master
S: 3049f1a9853529ca5cb2b0f9def43a1ae0b42064 192.168.137.103:6383
   replicates 5de830891133b5621d06901d970ef84c026a6bd1
S: 3638186047852a4c1d3c82805be3a2b46f63c235 192.168.137.103:6384
   replicates 39e4e1aa64ab168f0d875c0f187343382d7aadc0
S: 59260c049597ea1b2b05d61eb6c1e0c70de57b45 192.168.137.103:6385
   replicates 30c40c7eb92bc3decdc9f8e0f7ac5c1d04865866
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 192.168.137.103:6380)
M: 5de830891133b5621d06901d970ef84c026a6bd1 192.168.137.103:6380
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 39e4e1aa64ab168f0d875c0f187343382d7aadc0 192.168.137.103:6381
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 3638186047852a4c1d3c82805be3a2b46f63c235 192.168.137.103:6384
   slots: (0 slots) slave
   replicates 39e4e1aa64ab168f0d875c0f187343382d7aadc0
S: 59260c049597ea1b2b05d61eb6c1e0c70de57b45 192.168.137.103:6385
   slots: (0 slots) slave
   replicates 30c40c7eb92bc3decdc9f8e0f7ac5c1d04865866
S: 3049f1a9853529ca5cb2b0f9def43a1ae0b42064 192.168.137.103:6383
   slots: (0 slots) slave
   replicates 5de830891133b5621d06901d970ef84c026a6bd1
M: 30c40c7eb92bc3decdc9f8e0f7ac5c1d04865866 192.168.137.103:6382
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

5.3 查看节点相关信息

## 检查集群状态
redis-cli -a 1234 --cluster check 192.168.137.103:6382
## 连接集群某个节点
redis-cli -c -a 1234 -h 192.168.137.103 -p 6382
## 查看集群信息
cluster info
## 查看集群结点信息
cluster nodes

如下所示:

root@c81e4c88ef59:/data# redis-cli -a 1234 --cluster check 192.168.137.103:6382
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.137.103:6382 (30c40c7e...) -> 0 keys | 5461 slots | 1 slaves.
192.168.137.103:6380 (5de83089...) -> 0 keys | 5461 slots | 1 slaves.
192.168.137.103:6381 (39e4e1aa...) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.137.103:6382)
M: 30c40c7eb92bc3decdc9f8e0f7ac5c1d04865866 192.168.137.103:6382
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
M: 5de830891133b5621d06901d970ef84c026a6bd1 192.168.137.103:6380
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 59260c049597ea1b2b05d61eb6c1e0c70de57b45 192.168.137.103:6385
   slots: (0 slots) slave
   replicates 30c40c7eb92bc3decdc9f8e0f7ac5c1d04865866
S: 3638186047852a4c1d3c82805be3a2b46f63c235 192.168.137.103:6384
   slots: (0 slots) slave
   replicates 39e4e1aa64ab168f0d875c0f187343382d7aadc0
M: 39e4e1aa64ab168f0d875c0f187343382d7aadc0 192.168.137.103:6381
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 3049f1a9853529ca5cb2b0f9def43a1ae0b42064 192.168.137.103:6383
   slots: (0 slots) slave
   replicates 5de830891133b5621d06901d970ef84c026a6bd1
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
root@c81e4c88ef59:/data# redis-cli -c -a 1234 -h 192.168.137.103 -p 6382
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.137.103:6382> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:3
cluster_stats_messages_ping_sent:1150
cluster_stats_messages_pong_sent:1149
cluster_stats_messages_meet_sent:1
cluster_stats_messages_sent:2300
cluster_stats_messages_ping_received:1149
cluster_stats_messages_pong_received:1151
cluster_stats_messages_received:2300
192.168.137.103:6382> cluster nodes
30c40c7eb92bc3decdc9f8e0f7ac5c1d04865866 192.168.137.103:6382@16382 myself,master - 0 1668000952000 3 connected 10923-16383
5de830891133b5621d06901d970ef84c026a6bd1 192.168.137.103:6380@16380 master - 0 1668000953660 1 connected 0-5460
59260c049597ea1b2b05d61eb6c1e0c70de57b45 192.168.137.103:6385@16385 slave 30c40c7eb92bc3decdc9f8e0f7ac5c1d04865866 0 1668000953559 3 connected
3638186047852a4c1d3c82805be3a2b46f63c235 192.168.137.103:6384@16384 slave 39e4e1aa64ab168f0d875c0f187343382d7aadc0 0 1668000954662 2 connected
39e4e1aa64ab168f0d875c0f187343382d7aadc0 192.168.137.103:6381@16381 master - 0 1668000954000 2 connected 5461-10922
3049f1a9853529ca5cb2b0f9def43a1ae0b42064 192.168.137.103:6383@16383 slave 5de830891133b5621d06901d970ef84c026a6bd1 0 1668000954000 1 connected
192.168.137.103:6382> 
root@c81e4c88ef59:/data#

6.测试

6.1 进入6382节点,并使用set/get读写数据

root@c81e4c88ef59:/data# redis-cli -c -a 1234 -h 192.168.137.103 -p 6382
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.137.103:6382> set name helloWord
-> Redirected to slot [5798] located at 192.168.137.103:6381
OK
192.168.137.103:6381> set test 123456
OK
192.168.137.103:6381> get test
"123456"
192.168.137.103:6381> get name
"helloWord"

6.2 然后再进入6385节点,读取刚才写入的数据

root@c81e4c88ef59:/data# redis-cli -c -a 1234 -h 192.168.137.103 -p 6385
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.137.103:6385> get test
-> Redirected to slot [6918] located at 192.168.137.103:6381
"123456"
192.168.137.103:6381> get name
"helloWord"
192.168.137.103:6381> 

读到数据,证明Redis Cluster集群创建成功文章来源地址https://www.toymoban.com/news/detail-433286.html

到了这里,关于使用docker搭建 redis cluster 集群的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • docker服务器中redis-cluster集群配置(redis-5.0.7)

    因为需要使用到docker服务器下的redis-cluster集群环境,而以前redis3.2.8版本的redis搭配起来费事费力还没有成功,所以使用了较新一些的redis版本----redis-5.0.7。 默认:dockers已经安装成功 1.1下载tar包 1.2把进行安装 2.1 编写配置文件 #编写目录 mkdir -p /usr/local/docker-redis/redis-cluster #切

    2024年02月21日
    浏览(38)
  • 使用Docker搭建Redis主从集群

    欢迎来到 请回答1024 的博客 🍓🍓🍓欢迎来到 请回答1024的博客 关于博主 : 我是 请回答1024 ,一个追求数学与计算的边界、时间与空间的平衡,0与1的延伸的后端开发者。 博客特色 : 在我的博客中,开设了如下专栏( 点击可以进入专栏奥~ ): Java、MySQL、Redis、Spring、SpringB

    2024年04月24日
    浏览(31)
  • 使用Docker-Compose搭建Redis集群

    3主+3从 由于仅用于测试,故我这里只用1台服务器进行模拟 redis列表 在server上创建一个目录用于存放redis集群部署文件。这里我放的路径为/root/redis-cluster 在/opt/docker/redis-cluster目录下创建redis-1,redis-2,redis-3,redis-4,redis-5,redis-6文件夹 注意:port值不能都为6379,根据上面redis列表设

    2024年02月15日
    浏览(29)
  • 使用Docker搭建一个“一主两从”的 Redis 集群(超详细步骤)

    1、Redis 单机版安装 1.1 拉取 Redis 首先从 docker hub 拉取 Redis 镜像,这里拉取 7.0 版。 1.2 创建数据卷目录 首先要在宿主机/root 目录中创建一个目录 redis,将来用于存放外挂文件 redis.conf。 使用rz命令上传一份redis.conf: 1.3 修改 redis.conf 修改配置文件: 1.解除 IP 绑定 将 bind 行注释

    2024年02月21日
    浏览(36)
  • 远程服务器,使用docker搭建redis集群提示:SlaveConnectionPool no available Redis entries

    1、部署条件:         1.1 远程服务器         1.2 docker部署:参考 https://blog.csdn.net/qq_42971035/article/details/128171542  2、出现问题:         2.1 服务器部署服务,连接redis集群,会提示\\\"SlaveConnectionPool no available Redis entries\\\"。         2.2 本地连接远程服务器,提示\\\"connection 

    2024年02月15日
    浏览(44)
  • 条条大路通罗马系列—— 使用 Hiredis-cluster 连接 Amazon ElastiCache for Redis 集群

    前言 Amazon ElastiCache for Redis 是速度超快的内存数据存储,能够提供亚毫秒级延迟来支持 实时应用程序。适用于 Redis 的 ElastiCache 基于开源 Redis 构建,可与 Redis API 兼容,能够与 Redis 客户端配合工作,并使用开放的 Redis 数据格式来存储数据。适用于 Redis 的 ElastiCache 兼具开源

    2024年02月13日
    浏览(30)
  • 【redis】docker搭建redis集群

    docker搭建redis集群,超级简单方便。 参考文章: Redis 集群搭建详细指南 基于Docker的Redis集群搭建 redis: (error) CLUSTERDOWN The cluster is down Redis cluster集群模式的原理解析

    2024年02月14日
    浏览(28)
  • 【Redis】浅谈Redis-集群(Cluster)

    下面我们来看看redis的集群实现。 redis集群,即对redis的一种水平扩容,主要解决并发写量太大有性能瓶颈,单台redis容量限制的问题。 n3:一个集群至少有3个master,新master的选举需要大于半数的集群master节点同意才能选举成功,如果只有两个master节点,其中一个宕机了,达不

    2024年02月07日
    浏览(27)
  • docker搭建redis集群(三主三从)及重启redis集群

    从远程仓库先拉取一下redis的镜像文件,如果已经提前安装过镜像的,可以跳过此步骤: 看到图上标识,就说明当前镜像文件已经下载好了。 通过镜像文件,分别 启动6台redis容器实例 ,并且 数据卷挂载到宿主机 上(保障容器被意外删除后数据不丢失): 每一台实例都是按

    2024年04月16日
    浏览(47)
  • docker搭建redis集群

    1、配置文件 使用docker搭建redis集群必须要配置的内容,搭建几个集群,就需要几个配置文件。 配置文件中本节点 IP(cluster-announce-ip)、 端口(cluster-announce-port)、 总线端口(cluster-announce-bus-port) 需要单独配置,配置为 实际可访问到服务的地址 ,即docker映射后的端口。

    2024年03月15日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包