如何为双活Redis Enterprise搭建基于Docker的开发环境?

译文
数据库 其他数据库 开源 Redis
最近我们发布了一篇关于如何使用双活Redis Enterprise来开发应用程序的教程。为了模拟生产环境,开发人员或测试人员需要一种小型化的开发环境,很容易用Docker来搭建。我们在本文中介绍创建基于Docker的Redis Enterprise集群的步骤,这一切通过命令行来完成。

【51CTO.com快译】Redis Enterprise这种双活数据库是地域分布式应用程序的理想选择。其架构基于无冲突复制数据类型(CRDT)方面是突破性的学术研究。这种方法与其他双活数据库相比具有许多优点,包括如下:

1. 为读写操作提供本地延迟

2. 为简单和复杂的数据类型提供内置的冲突解决方案

3. 跨区域故障切换

4. 简化实施了诸多用例,比如积分榜、分布式缓存、共享会话和多用户计费等。

最近我们发布了一篇关于如何使用双活Redis Enterprise来开发应用程序的教程。为了模拟生产环境,开发人员或测试人员需要一种小型化的开发环境,很容易用Docker来搭建。

Redis Enterprise在Docker hub上以redislabs/redis的形式存在,我们已经在Redis Enterprise说明文档页面和docker hub本身上介绍了如何在Docker上搭建Redis Enterprise的详细逐步说明。

我们在本文中介绍创建基于Docker的Redis Enterprise集群的步骤,这一切通过命令行来完成。下面大体介绍了整个过程(更多详细信息如下):

1. 安装数据库

1)创建一个3个节点的Redis Enterprise集群,每个节点在单独的子网上

2)创建基于CRDT的Redis Enterprise数据库

3)连接到三个不同的实例

2. 验证安装的环境

3. 拆分网络

4. 恢复连接

5. 停止Redis Enterprise

在开始之前,确保你已有一个bash shell,并为docker进程分配了足够的内存。你可以进入到Docker -> Preferences -> Advanced来检查内存。

Docker内存***项高级选项卡

图1:Docker内存***项高级选项卡

1. 安装数据库

下列脚本在3节点集群上创建基于CRDT的Redis Enterprise数据库。将其保存在文件中并为其命名,比如“create_3_node_cluster.sh”。然后将模式改成可执行(chmod + x create_3_node_cluster.sh),并运行脚本([path] /create_3_node_cluster.sh)。 

  1. #!/bin/bash  
  2. Delete the bridge networks if they already exist  
  3. docker network rm network1 2>/dev/null  
  4. docker network rm network2 2>/dev/null  
  5. docker network rm network3 2>/dev/null  
  6. Create new bridge networks  
  7. echo “Creating new subnets…”  
  8. docker network create network1 –subnet=172.18.0.0/16 –gateway=172.18.0.1  
  9. docker network create network2 –subnet=172.19.0.0/16 –gateway=172.19.0.1  
  10. docker network create network3 –subnet=172.20.0.0/16 –gateway=172.20.0.1  
  11. # Start 3 docker containers. Each container is a node in a separate network  
  12. # These commands pull redislabs/redis from the docker hub. Because of the  
  13. # port mapping rules, Redis Enterprise instances are available on ports  
  14. # 12000, 12002, 12004  
  15. echo “”  
  16. echo “Starting Redis Enterprise as Docker containers…”  
  17. docker run -d –cap-add sys_resource -h rp1 –name rp1 -p 8443:8443 -p 9443:9443 -p 12000:12000 –network=network1 –ip=172.18.0.2 redislabs/redis  
  18. docker run -d –cap-add sys_resource -h rp2 –name rp2 -p 8445:8443 -p 9445:9443 -p 12002:12000 –network=network2 –ip=172.19.0.2 redislabs/redis  
  19. docker run -d –cap-add sys_resource -h rp3 –name rp3 -p 8447:8443 -p 9447:9443 -p 12004:12000 –network=network3 –ip=172.20.0.2 redislabs/redis  
  20. Connect the networks  
  21. docker network connect network2 rp1  
  22. docker network connect network3 rp1  
  23. docker network connect network1 rp2  
  24. docker network connect network3 rp2  
  25. docker network connect network1 rp3  
  26. docker network connect network2 rp3  
  27. # Sleep while the nodes start. Increase the sleep time if your nodes take  
  28. # longer than 60 seconds to start  
  29. echo “”  
  30. echo “Waiting for the servers to start…”  
  31. sleep 60  
  32. Create 3 Redis Enterprise clusters – one for each network. You can login to  
  33. # a cluster as https://localhost:8443/ (or 8445, 8447). The user name is  
  34. # r@r.com, password is password. Change the user  
  35. echo “”  
  36. echo “Creating clusters”  
  37. docker exec -it rp1 /opt/redislabs/bin/rladmin cluster create name cluster1.local username r@r.com password test  
  38. docker exec -it rp2 /opt/redislabs/bin/rladmin cluster create name cluster2.local username r@r.com password test  
  39. docker exec -it rp3 /opt/redislabs/bin/rladmin cluster create name cluster3.local username r@r.com password test  
  40. Create the CRDB  
  41. echo “”  
  42. echo “Creating a CRDB”  
  43. docker exec -it rp1 /opt/redislabs/bin/crdb-cli crdb create –name mycrdb –memory-size 512mb –port 12000 –replication false –shards-count 1 –instance fqdn=cluster1.local,username=r@r.com,password=test –instance fqdn=cluster2.local,username=r@r.com,password=test –instance fqdn=cluster3.local,username=r@r.com,password=test 

 

2. 验证安装的环境

在端口12000、12002和12004上运行redis-cli,验证你可以连接到所有三个Redis Enterprise端口。如果你将应用程序连接到Redis Enterprise,需要应用程序的三个实例连接到三个不同的端口。比如: 

  1. $ redis-cli -p 12000  
  2. 127.0.0.1:12000> incr counter  
  3. (integer) 1  
  4. 127.0.0.1:12000> get counter  
  5. “1” 

 

3. 拆分网络

拆分网络可帮助你在Redis Enterprise副本之间引入“网络分区”。你在设计应用程序时,必须设计成副本断开连接后可以顺畅运行。该脚本帮助你隔离三个副本。将该脚本保存在文件“split_networks.sh”中,并在运行之前更改模式,让它成为可执行(chmod +x split_networks.sh)。 

  1. #!/bin/bash  
  2. docker network disconnect network2 rp1  
  3. docker network disconnect network3 rp1  
  4. docker network disconnect network1 rp2  
  5. docker network disconnect network3 rp2  
  6. docker network disconnect network1 rp3  
  7. docker network disconnect network2 rp3 

 

4. 恢复连接

你运行脚本“split_netorks.sh”后,本地副本会停止与其他副本共享数据库更新。恢复连接将让它们能够交换所有更新,并获得同样的最终状态,这归功于Redis Enterprise提供了很强的最终一致性。下列脚本恢复副本之间的网络连接。将这保存在文件“restore_networks.sh”中,并更改模式让它成为可执行(chmod +x restore_networks.sh)。 

  1. #!/bin/bash  
  2. docker network connect network2 rp1  
  3. docker network connect network3 rp1  
  4. docker network connect network1 rp2  
  5. docker network connect network3 rp2  
  6. docker network connect network1 rp3  
  7. docker network connect network2 rp3 

 

5. 停止Redis Enterprise

完成开发和测试后,只要运行下列脚本,就可以终止Redis Enterprise的所有三个节点。将该文件保存在文件中,并将文件命名为“stop.sh”,更改模式,让它成为可执行(chmod +x stop.sh)。 

  1. #!/bin/bash  
  2. docker stop rp1 rp2 rp3  
  3. docker rm rp1 rp2 rp3  
  4. docker network rm network1  
  5. docker network rm network2  
  6. docker network rm network3 

 

就是这样。完成了上述步骤后,现在你有了自己的基于Docker的Redis Enterprise双活数据库环境。若有任何问题,欢迎留言交流。

原文标题:How to Set Up a Docker-based Development Environment for Active-Active Redis Enterprise,作者:Roshan Kumar 

【51CTO译稿,合作站点转载请注明原文译者和出处为51CTO.com】

 

责任编辑:庞桂玉 来源: 51CTO
相关推荐

2016-03-02 09:50:09

docker测试环境

2015-01-04 09:49:37

PHPDocker开发环境

2016-11-03 09:49:04

2022-06-24 10:11:15

DockerLinux

2017-07-11 13:30:12

RedisDockerLinux

2010-02-03 14:37:10

Python 开发环境

2010-09-07 17:27:54

Carbide.c++Symbian移动开发

2009-06-10 16:30:05

基于Eclipse的PWindows

2016-09-08 16:04:59

JavaDocker前端

2023-04-07 08:28:14

2009-07-03 16:56:37

JSP开发环境

2015-12-30 13:58:00

DockerGit开发环境

2015-01-05 14:16:16

DockerFig自动化容器编排

2014-08-11 10:15:01

Docker开发环境

2014-02-14 11:42:32

VDI

2011-03-15 15:51:12

netfilteriptables

2021-12-06 15:05:41

鸿蒙HarmonyOS应用

2014-09-04 09:35:17

2020-02-24 10:23:29

协作环境员工网络

2011-08-16 15:41:47

UbuntuPython
点赞
收藏

51CTO技术栈公众号