今天小编给大家分享一下docker compose如何安装redis集群的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
文件名:redis-cluster.tmpl
# redis端口 port ${PORT} #redis 访问密码 requirepass 123456 #redis 访问Master节点密码 masterauth 123456 # 关闭保护模式 protected-mode no # 开启集群 cluster-enabled yes # 集群节点配置 cluster-config-file nodes.conf # 超时 cluster-node-timeout 5000 # 集群节点IP host模式为宿主机IP # cluster-announce-ip 192.168.1.10 # 集群节点端口 7001 - 7006 cluster-announce-port ${PORT} cluster-announce-bus-port 1${PORT} # 开启 appendonly 备份模式 appendonly yes # 每秒钟备份 appendfsync everysec # 对aof文件进行压缩时,是否执行同步操作 no-appendfsync-on-rewrite no # 当目前aof文件大小超过上一次重写时的aof文件大小的100%时会再次进行重写 auto-aof-rewrite-percentage 100 # 重写前AOF文件的大小最小值 默认 64mb auto-aof-rewrite-min-size 64mb # 日志配置 # debug:会打印生成大量信息,适用于开发/测试阶段 # verbose:包含很多不太有用的信息,但是不像debug级别那么混乱 # notice:适度冗长,适用于生产环境 # warning:仅记录非常重要、关键的警告消息 loglevel notice # 日志文件路径 logfile "/data/redis.log"
文件名:redis-cluster-config.sh
for port in `seq 7001 7006`; do \ mkdir -p ./${port}/conf \ && PORT=${port} envsubst < ./redis-cluster.tmpl > ./${port}/conf/redis.conf \ && mkdir -p ./${port}/data; \ done
执行 redis-cluster-config.sh
生成配置文件
文件名:docker-compose.yml
version: '3' services: redis-7001: image: redis:6 container_name: redis-7001 command: ["redis-server", "/usr/local/etc/redis/redis.conf"] volumes: - ./7001/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./7001/data:/data ports: - "7001:7001" - "17001:17001" environment: # 设置时区为上海,否则时间会有问题 - TZ=Asia/Shanghai logging: options: max-size: '100m' max-file: '10' redis-7002: container_name: redis-7002 - ./7002/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./7002/data:/data - "7002:7002" - "17002:17002" redis-7003: container_name: redis-7003 - ./7003/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./7003/data:/data - "7003:7003" - "17003:17003" redis-7004: container_name: redis-7004 - ./7004/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./7004/data:/data - "7004:7004" - "17004:17004" redis-7005: container_name: redis-7005 - ./7005/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./7005/data:/data - "7005:7005" - "17005:17005" redis-7006: container_name: redis-7006 - ./7006/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./7006/data:/data - "7006:7006" - "17006:17006"
注:192.168.44.65 为 宿主机ip
ip | port |
---|---|
192.168.44.65 | 7001 |
192.168.44.65 | 7002 |
192.168.44.65 | 7003 |
192.168.44.65 | 7004 |
192.168.44.65 | 7005 |
192.168.44.65 | 7006 |
执行命令:
注: cluster-replicas
表示 一个主节点有几个从节点
C:\Users\billow>docker exec -it redis-7001 redis-cli -p 7001 -a 123456 --cluster create 192.168.44.65:7001 192.168.44.65:7002 192.168.44.65:7003 192.168.44.65:7004 192.168.44.65:7005 192.168.44.65:7006 --cluster-replicas 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 192.168.44.65:7005 to 192.168.44.65:7001 Adding replica 192.168.44.65:7006 to 192.168.44.65:7002 Adding replica 192.168.44.65:7004 to 192.168.44.65:7003 >>> Trying to optimize slaves allocation for anti-affinity [WARNING] Some slaves are in the same host as their master M: 741c7bb537888ff97866896abb22da27c22f8eaf 192.168.44.65:7001 slots:[0-5460] (5461 slots) master M: e5f687462a0c47f51c3abf4b8d77d646a3d63fa9 192.168.44.65:7002 slots:[5461-10922] (5462 slots) master M: 9936d3690d971eccfc8b8ed43c5defc66f318454 192.168.44.65:7003 slots:[10923-16383] (5461 slots) master S: b7b6fa6a4766c32c5d42d759548d6fcdf2b9159b 192.168.44.65:7004 replicates e5f687462a0c47f51c3abf4b8d77d646a3d63fa9 S: 2275c996f25a8815342e2435af073eb29565778b 192.168.44.65:7005 replicates 9936d3690d971eccfc8b8ed43c5defc66f318454 S: f69a5d0ae974816396a9d9e613c13f6ceea25de2 192.168.44.65:7006 replicates 741c7bb537888ff97866896abb22da27c22f8eaf 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.44.65:7001) M: 741c7bb537888ff97866896abb22da27c22f8eaf 192.168.44.65:7001 slots:[0-5460] (5461 slots) master 1 additional replica(s) S: f69a5d0ae974816396a9d9e613c13f6ceea25de2 172.25.0.11:7006 slots: (0 slots) slave replicates 741c7bb537888ff97866896abb22da27c22f8eaf M: 9936d3690d971eccfc8b8ed43c5defc66f318454 172.25.0.7:7003 slots:[10923-16383] (5461 slots) master 1 additional replica(s) M: e5f687462a0c47f51c3abf4b8d77d646a3d63fa9 172.25.0.8:7002 slots:[5461-10922] (5462 slots) master 1 additional replica(s) S: 2275c996f25a8815342e2435af073eb29565778b 172.25.0.10:7005 slots: (0 slots) slave replicates 9936d3690d971eccfc8b8ed43c5defc66f318454 S: b7b6fa6a4766c32c5d42d759548d6fcdf2b9159b 172.25.0.9:7004 slots: (0 slots) slave replicates e5f687462a0c47f51c3abf4b8d77d646a3d63fa9 [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered.
命令:
-- 集群、分配插槽 docker exec -it redis-7001 redis-cli -p 7001 -a 123456 --cluster create 192.168.44.65:7001 192.168.44.65:7002 192.168.44.65:7003 192.168.44.65:7004 192.168.44.65:7005 192.168.44.65:7006 --cluster-replicas 1 -- 测试 docker exec -it redis-7001 redis-cli -h 192.168.44.65 -p 7003 -a 123456 -c -- 查看集群状态 cluster nodes -- 查看分片信息 cluster slots -- 查看集群信息 cluster info
执行结果:
C:\Users\billow>docker exec -it redis-7001 redis-cli -h 192.168.44.65 -p 7003 -a 123456 -c
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.44.65:7003>
192.168.44.65:7003>
192.168.44.65:7003>
192.168.44.65:7003>
192.168.44.65:7003> set name admin
-> Redirected to slot [5798] located at 172.25.0.8:7002
OK
172.25.0.8:7002> get name
"admin"
172.25.0.8:7002> cluster nodes
2275c996f25a8815342e2435af073eb29565778b 172.25.0.10:7005@17005 slave 9936d3690d971eccfc8b8ed43c5defc66f318454 0 1648086940436 3 connected
f69a5d0ae974816396a9d9e613c13f6ceea25de2 172.25.0.11:7006@17006 slave 741c7bb537888ff97866896abb22da27c22f8eaf 0 1648086939000 1 connected
b7b6fa6a4766c32c5d42d759548d6fcdf2b9159b 172.25.0.9:7004@17004 slave e5f687462a0c47f51c3abf4b8d77d646a3d63fa9 0 1648086939710 2 connected
741c7bb537888ff97866896abb22da27c22f8eaf 172.25.0.6:7001@17001 master - 0 1648086939392 1 connected 0-5460
e5f687462a0c47f51c3abf4b8d77d646a3d63fa9 172.25.0.8:7002@17002 myself,master - 0 1648086939000 2 connected 5461-10922
9936d3690d971eccfc8b8ed43c5defc66f318454 172.25.0.7:7003@17003 master - 0 1648086938868 3 connected 10923-16383
172.25.0.8:7002>
172.25.0.8:7002>
172.25.0.8:7002>
172.25.0.8:7002>
172.25.0.8:7002>
172.25.0.8:7002> cluster slots
1) 1) (integer) 0
2) (integer) 5460
3) 1) "172.25.0.6"
2) (integer) 7001
3) "741c7bb537888ff97866896abb22da27c22f8eaf"
4) 1) "172.25.0.11"
2) (integer) 7006
3) "f69a5d0ae974816396a9d9e613c13f6ceea25de2"
2) 1) (integer) 5461
2) (integer) 10922
3) 1) "172.25.0.8"
2) (integer) 7002
3) "e5f687462a0c47f51c3abf4b8d77d646a3d63fa9"
4) 1) "172.25.0.9"
2) (integer) 7004
3) "b7b6fa6a4766c32c5d42d759548d6fcdf2b9159b"
3) 1) (integer) 10923
2) (integer) 16383
3) 1) "172.25.0.7"
2) (integer) 7003
3) "9936d3690d971eccfc8b8ed43c5defc66f318454"
4) 1) "172.25.0.10"
2) (integer) 7005
3) "2275c996f25a8815342e2435af073eb29565778b"
172.25.0.8:7002> 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:2
cluster_stats_messages_ping_sent:906
cluster_stats_messages_pong_sent:911
cluster_stats_messages_meet_sent:1
cluster_stats_messages_sent:1818
cluster_stats_messages_ping_received:911
cluster_stats_messages_pong_received:906
cluster_stats_messages_received:1817
添加两个新的节点用于手动扩容
文件名:add-redis-docker.yml
version: '3' services: redis-7007: image: redis:6 container_name: redis-7007 command: ["redis-server", "/usr/local/etc/redis/redis.conf"] volumes: - ./7007/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./7007/data:/data ports: - "7007:7007" - "17007:17007" environment: # 设置时区为上海,否则时间会有问题 - TZ=Asia/Shanghai logging: options: max-size: '100m' max-file: '10' redis-7008: container_name: redis-7008 - ./7008/conf/redis.conf:/usr/local/etc/redis/redis.conf - ./7008/data:/data - "7008:7008" - "17008:17008" networks: default: external: name: learn_shop
执行命令:docker-compose -f add-redis-docker.yml up
-- 进入任意节点 docker exet -it redis-7001 bash -- 添加主节点(192.168.44.65:7001 -a 123456 这个可以是任何已存在的节点,主要用于获取集群信息) redis-cli --cluster add-node 192.168.44.65:7007 192.168.44.65:7001 -a 123456
结果:
root@aa1e004c00ff:/data# redis-cli --cluster add-node 192.168.44.65:7007 192.168.44.65:7001 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 192.168.44.65:7007 to cluster 192.168.44.65:7001
>>> Performing Cluster Check (using node 192.168.44.65:7001)
M: 741c7bb537888ff97866896abb22da27c22f8eaf 192.168.44.65:7001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 2275c996f25a8815342e2435af073eb29565778b 172.25.0.6:7005
slots: (0 slots) slave
replicates 9936d3690d971eccfc8b8ed43c5defc66f318454
M: 9936d3690d971eccfc8b8ed43c5defc66f318454 172.25.0.8:7003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: f69a5d0ae974816396a9d9e613c13f6ceea25de2 172.25.0.11:7006
slots: (0 slots) slave
replicates 741c7bb537888ff97866896abb22da27c22f8eaf
S: b7b6fa6a4766c32c5d42d759548d6fcdf2b9159b 172.25.0.9:7004
slots: (0 slots) slave
replicates e5f687462a0c47f51c3abf4b8d77d646a3d63fa9
M: e5f687462a0c47f51c3abf4b8d77d646a3d63fa9 172.25.0.7:7002
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.
>>> Send CLUSTER MEET to node 192.168.44.65:7007 to make it join the cluster.
[OK] New node added correctly.
查看刚加的主节点:
127.0.0.1:7001> cluster nodes 741c7bb537888ff97866896abb22da27c22f8eaf 172.25.0.10:7001@17001 myself,master - 0 1648104789000 1 connected 0-5460 2275c996f25a8815342e2435af073eb29565778b 172.25.0.6:7005@17005 slave 9936d3690d971eccfc8b8ed43c5defc66f318454 0 1648104789523 3 connected 6f0c7d0ae166717d645e84b48b72b9706c7c758a 172.25.0.13:7007@17007 master - 0 1648104790133 0 connected 9936d3690d971eccfc8b8ed43c5defc66f318454 172.25.0.8:7003@17003 master - 0 1648104790546 3 connected 10923-16383 f69a5d0ae974816396a9d9e613c13f6ceea25de2 172.25.0.11:7006@17006 slave 741c7bb537888ff97866896abb22da27c22f8eaf 0 1648104790000 1 connected b7b6fa6a4766c32c5d42d759548d6fcdf2b9159b 172.25.0.9:7004@17004 slave e5f687462a0c47f51c3abf4b8d77d646a3d63fa9 0 1648104790000 2 connected e5f687462a0c47f51c3abf4b8d77d646a3d63fa9 172.25.0.7:7002@17002 master - 0 1648104791062 2 connected 5461-10922
-- 进入任意节点 docker exet -it redis-7001 bash -- 添加从节点(192.168.44.65:7001 -a 123456 这个可以是任何已存在的节点,主要用于获取集群信息)(6f0c7d0ae166717d645e84b48b72b9706c7c758a 为刚加的主节点 id) redis-cli --cluster add-node --cluster-slave --cluster-master-id 6f0c7d0ae166717d645e84b48b72b9706c7c758a 192.168.44.65:7008 192.168.44.65:7001 -a 123456
结果:
127.0.0.1:7001> cluster nodes
741c7bb537888ff97866896abb22da27c22f8eaf 172.25.0.10:7001@17001 myself,master - 0 1648105151000 1 connected 0-5460
2275c996f25a8815342e2435af073eb29565778b 172.25.0.6:7005@17005 slave 9936d3690d971eccfc8b8ed43c5defc66f318454 0 1648105154016 3 connected
6f0c7d0ae166717d645e84b48b72b9706c7c758a 172.25.0.13:7007@17007 master - 0 1648105152000 0 connected
9936d3690d971eccfc8b8ed43c5defc66f318454 172.25.0.8:7003@17003 master - 0 1648105153000 3 connected 10923-16383
f69a5d0ae974816396a9d9e613c13f6ceea25de2 172.25.0.11:7006@17006 slave 741c7bb537888ff97866896abb22da27c22f8eaf 0 1648105152000 1 connected
6478c692a149130d56538aea69b54d2abc1c98b4 172.25.0.12:7008@17008 slave 6f0c7d0ae166717d645e84b48b72b9706c7c758a 0 1648105152000 0 connected
b7b6fa6a4766c32c5d42d759548d6fcdf2b9159b 172.25.0.9:7004@17004 slave e5f687462a0c47f51c3abf4b8d77d646a3d63fa9 0 1648105153599 2 connected
e5f687462a0c47f51c3abf4b8d77d646a3d63fa9 172.25.0.7:7002@17002 master - 0 1648105153000 2 connected 5461-10922
刚加入的主从节点还不能使用,因为还没有分配插槽
rebalance平衡集群节点slot数量
语法:
rebalance host:port --weight <arg> --auto-weights --threshold <arg> --use-empty-masters --timeout <arg> --simulate --pipeline <arg> host:port:这个是必传参数,用来从一个节点获取整个集群信息,相当于获取集群信息的入口。 --weight <arg>:节点的权重,格式为node_id=weight,如果需要为多个节点分配权重的话,需要添加多个--weight <arg>参数,即--weight b31e3a2e=5 --weight 60b8e3a1=5,node_id可为节点名称的前缀,只要保证前缀位数能唯一区分该节点即可。没有传递–weight的节点的权重默认为1。 --auto-weights:这个参数在rebalance流程中并未用到。 --threshold <arg>:只有节点需要迁移的slot阈值超过threshold,才会执行rebalance操作。具体计算方法可以参考下面的rebalance命令流程的第四步。 --use-empty-masters:rebalance是否考虑没有节点的master,默认没有分配slot节点的master是不参与rebalance的,设置--use-empty-masters可以让没有分配slot的节点参与rebalance。 --timeout <arg>:设置migrate命令的超时时间。 --simulate:设置该参数,可以模拟rebalance操作,提示用户会迁移哪些slots,而不会真正执行迁移操作。 --pipeline <arg>:与reshar的pipeline参数一样,定义cluster getkeysinslot命令一次取出的key数量,不传的话使用默认值为10。
-- 分配插槽(192.168.44.65:7001 -a 123456 这个可以是任何已存在的节点,主要用于获取集群信息) redis-cli --cluster rebalance --cluster-threshold 1 --cluster-use-empty-masters 192.168.44.65:7001 -a 123456
如果需要模拟执行添加 : --cluster-simulate
结果:
root@aa1e004c00ff:/data# redis-cli --cluster rebalance --cluster-threshold 1 --cluster-use-empty-masters 192.168.44.65:7001 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 192.168.44.65:7001)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Rebalancing across 4 nodes. Total weight = 4.00
Moving 1366 slots from 172.25.0.7:7002 to 172.25.0.13:7007
#########################################################################################
Moving 1365 slots from 172.25.0.8:7003 to 172.25.0.13:7007
#########################################################################################
Moving 1365 slots from 192.168.44.65:7001 to 172.25.0.13:7007
#########################################################################################
查看节点信息:
127.0.0.1:7001> cluster nodes
741c7bb537888ff97866896abb22da27c22f8eaf 172.25.0.10:7001@17001 myself,master - 0 1648105892000 1 connected 1365-5460
2275c996f25a8815342e2435af073eb29565778b 172.25.0.6:7005@17005 slave 9936d3690d971eccfc8b8ed43c5defc66f318454 0 1648105893536 3 connected
6f0c7d0ae166717d645e84b48b72b9706c7c758a 172.25.0.13:7007@17007 master - 0 1648105894566 8 connected 0-1364 5461-6826 10923-12287
9936d3690d971eccfc8b8ed43c5defc66f318454 172.25.0.8:7003@17003 master - 0 1648105893000 3 connected 12288-16383
f69a5d0ae974816396a9d9e613c13f6ceea25de2 172.25.0.11:7006@17006 slave 741c7bb537888ff97866896abb22da27c22f8eaf 0 1648105893000 1 connected
6478c692a149130d56538aea69b54d2abc1c98b4 172.25.0.12:7008@17008 slave 6f0c7d0ae166717d645e84b48b72b9706c7c758a 0 1648105894576 8 connected
b7b6fa6a4766c32c5d42d759548d6fcdf2b9159b 172.25.0.9:7004@17004 slave e5f687462a0c47f51c3abf4b8d77d646a3d63fa9 0 1648105893000 2 connected
e5f687462a0c47f51c3abf4b8d77d646a3d63fa9 172.25.0.7:7002@17002 master - 0 1648105894566 2 connected 6827-10922
root@aa1e004c00ff:/data# redis-cli -p 7001 -a 123456 -c Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 127.0.0.1:7001> set 1 1 -> Redirected to slot [9842] located at 172.25.0.7:7002 OK 172.25.0.7:7002> set 2 2 -> Redirected to slot [5649] located at 172.25.0.13:7007 OK 172.25.0.13:7007> set 3 3 -> Redirected to slot [1584] located at 172.25.0.10:7001 OK 172.25.0.10:7001> set 4 4 -> Redirected to slot [14039] located at 172.25.0.8:7003 OK 172.25.0.8:7003> set 5 5 -> Redirected to slot [9974] located at 172.25.0.7:7002 OK 172.25.0.7:7002> set 6 6 -> Redirected to slot [5781] located at 172.25.0.13:7007 OK 172.25.0.13:7007> set 7 7 -> Redirected to slot [1716] located at 172.25.0.10:7001 OK 172.25.0.10:7001> 172.25.0.10:7001> 172.25.0.10:7001> 172.25.0.10:7001> 172.25.0.10:7001> get 1 -> Redirected to slot [9842] located at 172.25.0.7:7002 "1" 172.25.0.7:7002> get 2 -> Redirected to slot [5649] located at 172.25.0.13:7007 "2" 172.25.0.13:7007> get 3 -> Redirected to slot [1584] located at 172.25.0.10:7001 "3" 172.25.0.10:7001> get 4 -> Redirected to slot [14039] located at 172.25.0.8:7003 "4" 172.25.0.8:7003> get 5 -> Redirected to slot [9974] located at 172.25.0.7:7002 "5" 172.25.0.7:7002> get 6 -> Redirected to slot [5781] located at 172.25.0.13:7007 "6" 172.25.0.13:7007> get 7 -> Redirected to slot [1716] located at 172.25.0.10:7001 "7"
集群 cluster info :打印集群的信息 cluster nodes :列出集群当前已知的所有节点( node),以及这些节点的相关信息。 节点 cluster meet <ip> <port> :将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子。 cluster forget <node_id> :从集群中移除 node_id 指定的节点。 cluster replicate <node_id> :将当前节点设置为 node_id 指定的节点的从节点。 cluster saveconfig :将节点的配置文件保存到硬盘里面。 槽(slot) cluster addslots <slot> [slot ...] :将一个或多个槽( slot)指派( assign)给当前节点。 cluster delslots <slot> [slot ...] :移除一个或多个槽对当前节点的指派。 cluster flushslots :移除指派给当前节点的所有槽,让当前节点变成一个没有指派任何槽的节点。 cluster setslot <slot> node <node_id> :将槽 slot 指派给 node_id 指定的节点,如果槽已经指派给另一个节点,那么先让另一个节点删除该槽>,然后再进行指派。 cluster setslot <slot> migrating <node_id> :将本节点的槽 slot 迁移到 node_id 指定的节点中。 cluster setslot <slot> importing <node_id> :从 node_id 指定的节点中导入槽 slot 到本节点。 cluster setslot <slot> stable :取消对槽 slot 的导入( import)或者迁移( migrate)。 键 cluster keyslot <key> :计算键 key 应该被放置在哪个槽上。 cluster countkeysinslot <slot> :返回槽 slot 目前包含的键值对数量。 cluster getkeysinslot <slot> <count> :返回 count 个 slot 槽中的键
以上就是“docker compose如何安装redis集群”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。