Molet

Ansible自动搭建redis5集群

Molet linux 2023-01-24 522浏览 0

基于ansible全自动部署 redis5 三主三从集群,只需一条命令即可完成搭建!

注意关闭防火墙(创建集群可能失败哟~)

Ansible自动搭建redis5集群
Ansible自动搭建redis5集群

执行

[root@bunian work_yml]# ansible-playbook redis.yml
...
...

验证

  • 查看节点信息
TASK [create redis cluster] ***********************************************************************************************************************************************************************************
changed: [192.168.0.68]

PLAY RECAP ****************************************************************************************************************************************************************************************************
192.168.0.186              : ok=24   changed=18   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.0.68               : ok=25   changed=18   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.0.90               : ok=24   changed=18   unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@bunian work_yml]# redis-cli -h 192.168.0.68 -p 27000
192.168.0.68:27000> cluster nodes
9f247151098c7e6a4a9d9450e47856225b079b39 192.168.0.68:27001@37001 slave 16c3f24fc1fe1426bf2c6349582f9514249fa18b 0 1608810302914 5 connected
16c3f24fc1fe1426bf2c6349582f9514249fa18b 192.168.0.90:27000@37000 master - 0 1608810299887 5 connected 10923-16383
adb99349435a1ee49237456048d14d4e3176ca43 192.168.0.186:27001@37001 slave 1afdd68c03f07dffe913c07caf5bef20280ccbbe 0 1608810302000 4 connected
459f1c4a24bcaffb00154250f665e246e4eabf08 192.168.0.90:27001@37001 slave 48ae4b7c9e01366be6a41875b7114cc85bdb5c2d 0 1608810300000 6 connected
48ae4b7c9e01366be6a41875b7114cc85bdb5c2d 192.168.0.186:27000@37000 master - 0 1608810303922 3 connected 5461-10922
1afdd68c03f07dffe913c07caf5bef20280ccbbe 192.168.0.68:27000@37000 myself,master - 0 1608810297000 1 connected 0-5460
192.168.0.68:27000> 
  • 测试主从切换(这里关闭68的27000和27001节点)
[root@bunian home]# ps -ef |grep redis
root      23978      1  0 10:35 ?        00:00:00 /home/bunian/redis-5.0.5/src/redis-server 192.168.0.90:27000 [cluster]
root      23980      1  0 10:35 ?        00:00:00 /home/bunian/redis-5.0.5/src/redis-server 192.168.0.90:27001 [cluster]
root      23997   9445  0 10:38 pts/0    00:00:00 grep --color=auto redis
[root@bunian script]# ./shutdown.sh 
[root@bunian script]# ps -ef |grep redis
root      30039   2694  0 19:07 pts/0    00:00:00 grep --color=auto redis
[root@bunian script]#
[root@bunian 27000]# redis-cli -h 192.168.0.90 -p 27000
192.168.0.90:27000> cluster nodes
db9f53a88ca58a45c0937ee47a3059351c08be63 192.168.0.68:27000@37000 master,fail - 1608779020566 1608779017000 1 disconnected
ec9254a213de1d9c4d17ef2b1208e224b73bd2e2 192.168.0.90:27000@37000 myself,master - 0 1608779038000 5 connected 10923-16383
d55bafc8e5e06a17d9987ac50075118988710330 192.168.0.186:27000@37000 master - 0 1608779039000 3 connected 5461-10922
22399be1d3274c8a6494a13f96030ff144e8f8ea 192.168.0.90:27001@37001 slave d55bafc8e5e06a17d9987ac50075118988710330 0 1608779040117 6 connected
0b1c24d6db9152c19062a2e1cef5b893d8722e55 192.168.0.68:27001@37001 slave,fail ec9254a213de1d9c4d17ef2b1208e224b73bd2e2 1608779020566 1608779018000 5 disconnected
045ca4b6d55fd19f2b77bac013b0fa13e346b1cd 192.168.0.186:27001@37001 master - 0 1608779039000 7 connected 0-5460
192.168.0.90:27000> 
  • 可以看到68的27000和27001为disconnected状态,
  • 通过查看info replication结论如下表所示:

原主从关系为

Slots
68:27000 68:27001 0-5460
186:27000 186:27001 5461-10922
90:27000 90:27001 10923-16383

主从切换后关系为

Slots
186:27001 0-5460
186:27000 90:27001 5461-10922
90:27000 10923-16383
  • 尝试恢复68的两个节点
[root@bunian script]# ./start.sh 
[root@bunian script]# ps -ef |grep redis
root      37818      1  0 19:53 ?        00:00:00 /home/bunian/redis-5.0.5/src/redis-server 192.168.0.68:27000 [cluster]
root      37823      1  0 19:53 ?        00:00:00 /home/bunian/redis-5.0.5/src/redis-server 192.168.0.68:27001 [cluster]
root      37830   2694  0 19:54 pts/0    00:00:00 grep --color=auto redis
[root@bunian script]# redis-cli -h 192.168.0.90 -p 27001
192.168.0.90:27001> cluster nodes
48ae4b7c9e01366be6a41875b7114cc85bdb5c2d 192.168.0.186:27000@37000 master - 0 1608782056664 3 connected 5461-10922
1afdd68c03f07dffe913c07caf5bef20280ccbbe 192.168.0.68:27000@37000 slave adb99349435a1ee49237456048d14d4e3176ca43 0 1608782054000 7 connected
16c3f24fc1fe1426bf2c6349582f9514249fa18b 192.168.0.90:27000@37000 master - 0 1608782054000 5 connected 10923-16383
adb99349435a1ee49237456048d14d4e3176ca43 192.168.0.186:27001@37001 master - 0 1608782054648 7 connected 0-5460
9f247151098c7e6a4a9d9450e47856225b079b39 192.168.0.68:27001@37001 slave 16c3f24fc1fe1426bf2c6349582f9514249fa18b 0 1608782057671 5 connected
459f1c4a24bcaffb00154250f665e246e4eabf08 192.168.0.90:27001@37001 myself,slave 48ae4b7c9e01366be6a41875b7114cc85bdb5c2d 0 1608782055000 6 connected
192.168.0.90:27001> 

节点恢复后关系为

Slots
186:27001 68:27000 0-5460
186:27000 90:27001 5461-10922
90:27000 68:27001 10923-16383

继续浏览有关 Linux教程 的文章
发表评论