欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

Docker快速搭建Redis集群的方法示例

程序员文章站 2022-09-06 22:31:29
什么是redis集群redis集群是redis提供的分布式数据库方案,集群通过分片(sharding)来进行数据共享,并提供复制和故障转移功能。节点一个redis集群通常由多个节点(node)组成,在...

什么是redis集群

redis集群是redis提供的分布式数据库方案,集群通过分片(sharding)来进行数据共享,并提供复制和故障转移功能。

节点

一个redis集群通常由多个节点(node)组成,在刚开始的时候,每个节点都是相互独立的,它们都处于一个只包含自己的集群当中,要组建一个真正可工作的集群,我们必须将各个独立的节点连接起来,构成一个包含多个节点的集群。

集群配置

配置文件

下载配置文件:

调整 cluster 节点配置

# 开启cluster集群
cluster-enabled yes

# 集群配置文件
cluster-config-file nodes-6379.conf

# 集群节点超时
cluster-node-timeout 15000

docker快速搭建redis集群

安装redis

参考文章:

准备工作

├── conf
│  ├── redis.conf
│  └── sentinel.conf
├── redis
│  ├── data_6379
│  ├── data_6380
│  ├── data_6381
│  ├── data_6382
│  ├── data_6383
│  └── data_6384
└── scripts
  ├── cluster.sh
  ├── run.sh
  └── sentinel.sh

run.sh 脚本文件

#!/usr/bin/env bash
set -e

# 脚本当前目录
cpath=$(cd $(dirname "$0") || exit; pwd)

# 根目录
dirpath=$(dirname "$cpath")

# 获取端口
port="$1"
if [[ ! "$port" ]]; then
 port=6379
fi

# 创建数据目录
mkdir -p "$dirpath"/redis/data_"$port"

# 删除已启动服务
containerid=$(docker ps -a | grep "redis_$port" | awk -f' ' '{print $1}')
if [[ "$containerid" ]]; then
  docker rm -f ${containerid} > /dev/null
fi

# 启动服务
containername=redis_"$port"
docker run -itd --privileged=true -p "$port":6379 --name ${containername} \
-v="$dirpath"/conf/redis.conf:/etc/redis/redis.conf \
-v="$dirpath"/redis/data_"$port":/data \
redis \
redis-server /etc/redis/redis.conf > /dev/null

# 获取容器ip地址
dockerip=$(docker inspect -f "{{.networksettings.ipaddress}}" "$containername")

# 获取容器启动状态
isrunning=$(docker inspect -f "{{.state.running}}" "$containername")
if [[ "$isrunning" == "true" ]]; then
  echo "容器:$containername - ip:$dockerip - 启动成功"
fi

cluster.sh 脚本文件

#!/usr/bin/env bash
set -e

# 脚本当前目录
cpath=$(cd $(dirname "$0") || exit; pwd)

# 启动集群数量
num="$1"
if [[ ! "$num" ]]; then
 num=6
fi

sport=6378
for((i=1;i<=$num;i++)); do
  sh ${cpath}/run.sh $(($sport+$i))
done

启动服务

执行脚本文件,默认创建6个节点

sh scripts/cluster.sh

脚本返回结果

容器:redis_6379 - ip:172.17.0.2 - 启动成功
容器:redis_6380 - ip:172.17.0.3 - 启动成功
容器:redis_6381 - ip:172.17.0.4 - 启动成功
容器:redis_6382 - ip:172.17.0.5 - 启动成功
容器:redis_6383 - ip:172.17.0.6 - 启动成功
容器:redis_6384 - ip:172.17.0.7 - 启动成功

执行 docker ps 确实是否启动成功

root@desktop-q13ei52:~/docker-config/redis# docker ps
container id    image        command         created       status       ports          names
c0601df1a456    redis        "docker-entrypoint.s…"  27 seconds ago   up 26 seconds    0.0.0.0:6384->6379/tcp  redis_6384
6fecf70465b8    redis        "docker-entrypoint.s…"  27 seconds ago   up 26 seconds    0.0.0.0:6383->6379/tcp  redis_6383
1af15e90b7a0    redis        "docker-entrypoint.s…"  28 seconds ago   up 27 seconds    0.0.0.0:6382->6379/tcp  redis_6382
6c495f31a5df    redis        "docker-entrypoint.s…"  28 seconds ago   up 28 seconds    0.0.0.0:6381->6379/tcp  redis_6381
e54fd9fd0550    redis        "docker-entrypoint.s…"  29 seconds ago   up 28 seconds    0.0.0.0:6380->6379/tcp  redis_6380
be92ad2f7046    redis        "docker-entrypoint.s…"  29 seconds ago   up 29 seconds    0.0.0.0:6379->6379/tcp  redis_6379

到此为止,6个独立集群节点创建完毕,目前还无法正常工作。

创建集群

此处可以跳过,本人是为了省事

获取容器为redis_开始所有的容器ip地址

docker inspect -f "{{.networksettings.ipaddress}}:6379" `docker ps | grep redis_ | awk -f' ' '{print $1}'` | sort |xargs | sed 's/ /, /g'

# 返回结果
# 172.17.0.2:6379, 172.17.0.3:6379, 172.17.0.4:6379, 172.17.0.5:6379, 172.17.0.6:6379, 172.17.0.7:6379

初次创建集群执行

./redis-cli --cluster create 172.17.0.2:6379, 172.17.0.3:6379, 172.17.0.4:6379, 172.17.0.5:6379, 172.17.0.6:6379, 172.17.0.7:6379 --cluster-replicas 1

输出结果

licas 1
>>> performing hash slots allocation on 6 nodes...
master[0] -> slots 0 - 5460
master[1] -> slots 5461 - 10922
master[2] -> slots 10923 - 16383
adding replica 172.17.0.6:6379 to 172.17.0.2:6379
adding replica 172.17.0.7:6379 to 172.17.0.3:6379
adding replica 172.17.0.5:6379 to 172.17.0.4:6379
m: e8da1fef656984de3ec2a677edc8d9c48d01cd95 172.17.0.2:6379
  slots:[0-5460] (5461 slots) master
m: 68b925ab0fbbc1a632c1754587fb6dad3fa14c91 172.17.0.3:6379
  slots:[5461-10922] (5462 slots) master
m: 0a46ab2f6d176738b55fe699c2df1c34f8200d06 172.17.0.4:6379
  slots:[10923-16383] (5461 slots) master
s: bd3064ad5297dfc258e9236943455c589be8b2a3 172.17.0.5:6379
  replicates 0a46ab2f6d176738b55fe699c2df1c34f8200d06
s: f1d8c897882d29e6538b1158525493b3b782289a 172.17.0.6:6379
  replicates e8da1fef656984de3ec2a677edc8d9c48d01cd95
s: 619e1cb52f39e07b321719b77fc3631fa6293cef 172.17.0.7:6379
  replicates 68b925ab0fbbc1a632c1754587fb6dad3fa14c91
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 172.17.0.2:6379)
m: e8da1fef656984de3ec2a677edc8d9c48d01cd95 172.17.0.2:6379
  slots:[0-5460] (5461 slots) master
  1 additional replica(s)
s: f1d8c897882d29e6538b1158525493b3b782289a 172.17.0.6:6379
  slots: (0 slots) slave
  replicates e8da1fef656984de3ec2a677edc8d9c48d01cd95
s: bd3064ad5297dfc258e9236943455c589be8b2a3 172.17.0.5:6379
  slots: (0 slots) slave
  replicates 0a46ab2f6d176738b55fe699c2df1c34f8200d06
m: 0a46ab2f6d176738b55fe699c2df1c34f8200d06 172.17.0.4:6379
  slots:[10923-16383] (5461 slots) master
  1 additional replica(s)
s: 619e1cb52f39e07b321719b77fc3631fa6293cef 172.17.0.7:6379
  slots: (0 slots) slave
  replicates 68b925ab0fbbc1a632c1754587fb6dad3fa14c91
m: 68b925ab0fbbc1a632c1754587fb6dad3fa14c91 172.17.0.3:6379
  slots:[5461-10922] (5462 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.

连接集群

通过客户端连接

redis-cli -c <端口>

执行命令:cluster info

127.0.0.1:6379> 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:1
cluster_stats_messages_ping_sent:104
cluster_stats_messages_pong_sent:120
cluster_stats_messages_sent:224
cluster_stats_messages_ping_received:115
cluster_stats_messages_pong_received:104
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:224

看到:cluster_state:ok 说明集群已可以正常工作

客户端控制台:cluster help

127.0.0.1:6379> cluster help
 1) cluster <subcommand> arg arg ... arg. subcommands are:
 2) addslots <slot> [slot ...] -- assign slots to current node.
 3) bumpepoch -- advance the cluster config epoch.
 4) count-failure-reports <node-id> -- return number of failure reports for <node-id>.
 5) countkeysinslot <slot> - return the number of keys in <slot>.
 6) delslots <slot> [slot ...] -- delete slots information from current node.
 7) failover [force|takeover] -- promote current replica node to being a master.
 8) forget <node-id> -- remove a node from the cluster.
 9) getkeysinslot <slot> <count> -- return key names stored by current node in a slot.
10) flushslots -- delete current node own slots information.
11) info - return information about the cluster.
12) keyslot <key> -- return the hash slot for <key>.
13) meet <ip> <port> [bus-port] -- connect nodes into a working cluster.
14) myid -- return the node id.
15) nodes -- return cluster configuration seen by node. output format:
16)   <id> <ip:port> <flags> <master> <pings> <pongs> <epoch> <link> <slot> ... <slot>
17) replicate <node-id> -- configure current node as replica to <node-id>.
18) reset [hard|soft] -- reset current node (default: soft).
19) set-config-epoch <epoch> - set config epoch of current node.
20) setslot <slot> (importing|migrating|stable|node <node-id>) -- set slot state.
21) replicas <node-id> -- return <node-id> replicas.
22) saveconfig - force saving cluster configuration on disk.
23) slots -- return information about slots range mappings. each range is made of:
24)   start, end, master and replicas ip addresses, ports and ids

查看客户端提供的集群相关命令:redis-cli --cluster help

cluster manager commands:
 create     host1:port1 ... hostn:portn
         --cluster-replicas <arg>
 check     host:port
         --cluster-search-multiple-owners
 info      host:port
 fix      host:port
         --cluster-search-multiple-owners
 reshard    host:port
         --cluster-from <arg>
         --cluster-to <arg>
         --cluster-slots <arg>
         --cluster-yes
         --cluster-timeout <arg>
         --cluster-pipeline <arg>
         --cluster-replace
 rebalance   host:port
         --cluster-weight <node1=w1...noden=wn>
         --cluster-use-empty-masters
         --cluster-timeout <arg>
         --cluster-simulate
         --cluster-pipeline <arg>
         --cluster-threshold <arg>
         --cluster-replace
 add-node    new_host:new_port existing_host:existing_port
         --cluster-slave
         --cluster-master-id <arg>
 del-node    host:port node_id
 call      host:port command arg arg .. arg
 set-timeout  host:port milliseconds
 import     host:port
         --cluster-from <arg>
         --cluster-copy
         --cluster-replace

到此这篇关于docker快速搭建redis集群的方法示例的文章就介绍到这了,更多相关docker搭建redis集群内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!