Open mercyblitz opened 5 months ago
3主3从6节点:
root@docker-desktop:/data# redis-cli -p 6300 127.0.0.1:6300> 127.0.0.1:6300> 127.0.0.1:6300> 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:187 cluster_stats_messages_pong_sent:190 cluster_stats_messages_sent:377 cluster_stats_messages_ping_received:185 cluster_stats_messages_pong_received:187 cluster_stats_messages_meet_received:5 cluster_stats_messages_received:377 total_cluster_links_buffer_limit_exceeded:0
指令测试(需要 redis-cli -c 客户端模式):
127.0.0.1:6300> set auther walklown -> Redirected to slot [13357] located at 127.0.0.1:6302 OK 127.0.0.1:6302> set f01 walklown -> Redirected to slot [2604] located at 127.0.0.1:6300 OK 127.0.0.1:6300>
1.准备配置文件模板 redis.conf.tmpl
port ${PORT}
requirepass 123456
masterauth 123456
protected-mode no
daemonize no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-announce-ip ${IP}
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
2.快速创建
IP=192.168.1.149 #替换成本机ip
for port in `seq 6379 6381`; do
mkdir -p ${port}/conf
&& PORT=${port} envsubst < redis.conf.tmpl> ${port}/conf/redis.conf
&& mkdir -p ${port}/data;
done
3.docker-compose.yml
version: "3"
# 定义服务,可以多个
services:
redis-6379: # 服务名称
image: redis:6.2.7 # 创建容器时所需的镜像
container_name: redis-6379 # 容器名称
network_mode: "host" # host 网络模式
volumes: # 数据卷,目录挂载
- ./6379/config/redis.conf:/usr/local/etc/redis/redis.conf
- ./6379/data:/data
command: redis-server /usr/local/etc/redis/redis.conf # 覆盖容器启动后默认执行的命令
ports:
# 使用宿主机的端口映射到容器的端口
# 宿主机:容器
- 6379:6379
redis-6380:
image: redis:6.2.7
container_name: redis-6380
network_mode: "host"
volumes:
- ./6380/config/redis.conf:/usr/local/etc/redis/redis.conf
- ./6380/data:/data
command: redis-server /usr/local/etc/redis/redis.conf
ports:
# 使用宿主机的端口映射到容器的端口
# 宿主机:容器
- 6380:6380
redis-6381:
image: redis:6.2.7
container_name: redis-6381
network_mode: "host"
volumes:
- ./6381/config/redis.conf:/usr/local/etc/redis/redis.conf
- ./6381/6381/data:/data
command: redis-server /usr/local/etc/redis/redis.conf
ports:
# 使用宿主机的端口映射到容器的端口
# 宿主机:容器
- 6381:6381
4.启动容器
docker-compose up -d
5.进入任一容器
docker exec -it redis-6379 bash
$ cd /usr/local/bin/
$ redis-cli -a 123456 --cluster create 192.168.1.149:6379 192.168.1.149:6380 192.168.1.149:6381
输入yes后得到正确提示,此时redis-cluster搭建成功
>>> Performing hash slots allocation on 3 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
M: f76ffd50609346ab502562626a3ed9c3ad8eb280 192.168.1.149:6379
slots:[0-5460] (5461 slots) master
M: f21515084bd905d64c27515233e0ec9fdd3ac64c 192.168.1.149:6380
slots:[5461-10922] (5462 slots) master
M: 9e0aa1bf96fd22658741310e2e976d7ab9b40e2c 192.168.1.149:6381
slots:[10923-16383] (5461 slots) master
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.1.149:6379)
M: f76ffd50609346ab502562626a3ed9c3ad8eb280 192.168.1.149:6379
slots:[0-5460] (5461 slots) master
M: f21515084bd905d64c27515233e0ec9fdd3ac64c 192.168.1.149:6380
slots:[5461-10922] (5462 slots) master
M: 9e0aa1bf96fd22658741310e2e976d7ab9b40e2c 192.168.1.149:6381
slots:[10923-16383] (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
要求: