From 3e6adc38041652c1eeb4bebe899bfd822c6854d0 Mon Sep 17 00:00:00 2001 From: Josip Mrden Date: Tue, 29 Apr 2025 08:55:16 +0200 Subject: [PATCH] Add ha routing --- ha/routing/HA_register.cypherl | 8 +++ ha/routing/README.md | 27 ++++++++ ha/routing/docker-compose.yml | 123 +++++++++++++++++++++++++++++++++ ha/routing/license.cypherl | 2 + ha/routing/requirements.txt | 0 ha/routing/routing.py | 17 +++++ 6 files changed, 177 insertions(+) create mode 100644 ha/routing/HA_register.cypherl create mode 100644 ha/routing/README.md create mode 100644 ha/routing/docker-compose.yml create mode 100644 ha/routing/license.cypherl create mode 100644 ha/routing/requirements.txt create mode 100644 ha/routing/routing.py diff --git a/ha/routing/HA_register.cypherl b/ha/routing/HA_register.cypherl new file mode 100644 index 0000000..4bf037b --- /dev/null +++ b/ha/routing/HA_register.cypherl @@ -0,0 +1,8 @@ +ADD COORDINATOR 1 WITH CONFIG {"bolt_server": "localhost:7691", "coordinator_server": "coord1:10111", "management_server": "coord1:12121"}; +ADD COORDINATOR 2 WITH CONFIG {"bolt_server": "localhost:7692", "coordinator_server": "coord2:10112", "management_server": "coord2:12122"}; +ADD COORDINATOR 3 WITH CONFIG {"bolt_server": "localhost:7693", "coordinator_server": "coord3:10113", "management_server": "coord3:12123"}; + +REGISTER INSTANCE instance_1 WITH CONFIG {"bolt_server": "localhost:7687", "management_server": "instance1:13011", "replication_server": "instance1:10001"}; +REGISTER INSTANCE instance_2 WITH CONFIG {"bolt_server": "localhost:7688", "management_server": "instance2:13012", "replication_server": "instance2:10002"}; +REGISTER INSTANCE instance_3 WITH CONFIG {"bolt_server": "localhost:7689", "management_server": "instance3:13013", "replication_server": "instance3:10003"}; +SET INSTANCE instance_1 TO MAIN; diff --git a/ha/routing/README.md b/ha/routing/README.md new file mode 100644 index 0000000..f6883cf --- /dev/null +++ b/ha/routing/README.md @@ -0,0 +1,27 @@ + +# Memgraph HA deployment with docker compose + +This example demonstrates how to deploy Memgraph in a high availability mode with docker compose. + +## 🚀 How to Run Memgraph with Docker + +1. First, adjust the license in the `license.cypherl` file by providing your organization name and license key. +2. To run Memgraph, simply do the following command: + +```bash +docker compose up +``` + + +## 🔖 Version Compatibility + +This example was built and tested with: + +- **Memgraph MAGE v3.2.0** + +If you run into any issues or have questions, feel free to reach out on the [Memgraph Discord server](https://discord.gg/memgraph). We're happy to help! + + +## 🏢 Enterprise or Community? + +> 🛑 This example **requires Memgraph Enterprise**. diff --git a/ha/routing/docker-compose.yml b/ha/routing/docker-compose.yml new file mode 100644 index 0000000..62bb027 --- /dev/null +++ b/ha/routing/docker-compose.yml @@ -0,0 +1,123 @@ +services: + coord1: + image: "memgraph/memgraph-mage:3.2" + container_name: coord1 + volumes: + - ./license.cypherl:/tmp/init/license.cypherl:ro + - ./HA_register.cypherl:/tmp/init/HA_register.cypherl:ro + - mg_lib1:/var/lib/memgraph + - mg_log1:/var/log/memgraph + environment: + - MEMGRAPH_HA_CLUSTER_INIT_QUERIES=/tmp/init/HA_register.cypherl + command: ["--init-file=/tmp/init/license.cypherl", "--log-level=TRACE", "--also-log-to-stderr", "--bolt-port=7691", "--coordinator-id=1", "--coordinator-port=10111", "--management-port=12121", "--coordinator-hostname=coord1", "--nuraft-log-file=/var/log/memgraph/nuraft"] + networks: + memgraph_ha: + ipv4_address: 172.21.0.4 + ports: + - "7691:7691" + depends_on: + - instance1 + - instance2 + - instance3 + + coord2: + image: "memgraph/memgraph-mage:3.2" + container_name: coord2 + volumes: + - ./license.cypherl:/tmp/init/license.cypherl:ro + - mg_lib2:/var/lib/memgraph + - mg_log2:/var/log/memgraph + command: [ "--init-file=/tmp/init/license.cypherl", "--log-level=TRACE", "--also-log-to-stderr", "--bolt-port=7692", "--coordinator-id=2", "--coordinator-port=10112", "--management-port=12122", "--coordinator-hostname=coord2" , "--nuraft-log-file=/var/log/memgraph/nuraft"] + networks: + memgraph_ha: + ipv4_address: 172.21.0.2 + ports: + - "7692:7692" + depends_on: + - instance1 + - instance2 + - instance3 + + coord3: + image: "memgraph/memgraph-mage:3.2" + container_name: coord3 + volumes: + - ./license.cypherl:/tmp/init/license.cypherl:ro + - mg_lib3:/var/lib/memgraph + - mg_log3:/var/log/memgraph + command: [ "--init-file=/tmp/init/license.cypherl", "--log-level=TRACE", "--also-log-to-stderr", "--bolt-port=7693", "--coordinator-id=3", "--coordinator-port=10113", "--management-port=12123", "--coordinator-hostname=coord3" , "--nuraft-log-file=/var/log/memgraph/nuraft"] + + networks: + memgraph_ha: + ipv4_address: 172.21.0.3 + ports: + - "7693:7693" + depends_on: + - instance1 + - instance2 + - instance3 + + instance1: + image: "memgraph/memgraph-mage:3.2" + container_name: instance1 + volumes: + - ./license.cypherl:/tmp/init/license.cypherl:ro + - mg_lib4:/var/lib/memgraph + - mg_log4:/var/log/memgraph + command: ["--init-file=/tmp/init/license.cypherl", "--log-level=TRACE", "--also-log-to-stderr", "--bolt-port=7687", "--management-port=13011"] + networks: + memgraph_ha: + ipv4_address: 172.21.0.6 + ports: + - "7687:7687" + + instance2: + image: "memgraph/memgraph-mage:3.2" + container_name: instance2 + volumes: + - ./license.cypherl:/tmp/init/license.cypherl:ro + - mg_lib5:/var/lib/memgraph + - mg_log5:/var/log/memgraph + command: ["--init-file=/tmp/init/license.cypherl", "--log-level=TRACE", "--also-log-to-stderr", "--bolt-port=7688", "--management-port=13012"] + networks: + memgraph_ha: + ipv4_address: 172.21.0.7 + ports: + - "7688:7688" + + instance3: + image: "memgraph/memgraph-mage:3.2" + container_name: instance3 + volumes: + - ./license.cypherl:/tmp/init/license.cypherl:ro + - mg_lib6:/var/lib/memgraph + - mg_log6:/var/log/memgraph + command: ["--init-file=/tmp/init/license.cypherl", "--log-level=TRACE", "--also-log-to-stderr", "--bolt-port=7689", "--management-port=13013"] + networks: + memgraph_ha: + ipv4_address: 172.21.0.8 + ports: + - "7689:7689" + +volumes: + mg_lib1: + mg_lib2: + mg_lib3: + mg_lib4: + mg_lib5: + mg_lib6: + mg_log1: + mg_log2: + mg_log3: + mg_log4: + mg_log5: + mg_log6: + +networks: + memgraph_ha: + name: memgraph_ha + driver: bridge + ipam: + driver: default + config: + - subnet: "172.21.0.0/16" diff --git a/ha/routing/license.cypherl b/ha/routing/license.cypherl new file mode 100644 index 0000000..71a4ae2 --- /dev/null +++ b/ha/routing/license.cypherl @@ -0,0 +1,2 @@ +SET DATABASE SETTING 'organization.name' TO ''; +SET DATABASE SETTING 'enterprise.license' TO ''; diff --git a/ha/routing/requirements.txt b/ha/routing/requirements.txt new file mode 100644 index 0000000..e69de29 diff --git a/ha/routing/routing.py b/ha/routing/routing.py new file mode 100644 index 0000000..f1fea42 --- /dev/null +++ b/ha/routing/routing.py @@ -0,0 +1,17 @@ +from neo4j import GraphDatabase + +def execute_query(tx, query): + tx.run(query) + +uris = ["neo4j://localhost:7691", "neo4j://localhost:7692", "neo4j://localhost:7693"] +for uri in uris: + driver = GraphDatabase.driver(uri, auth=("", "")) + with driver.session() as session: + session.execute_write(execute_query, "CREATE (n)") + driver.close() + +for uri in uris: + driver = GraphDatabase.driver(uri, auth=("", "")) + with driver.session() as session: + session.execute_read(execute_query, "MATCH (n) RETURN n") + driver.close()