Hasura is one of the leading vendors in the GraphQL ecosystem. They offer an open source engine that connects to your databases and microservices, and then auto-generates a production-ready GraphQL backend. GraphQL is a query language (more specifically a specification) for your API, and a server-side runtime for executing queries by using a type system you define for your data. GraphQL is often used for microservices, mobile apps, and as an alternative to REST. Although GraphQL isn’t tied to any specific database or storage engine and is instead backed by your existing code and data, YugabyteDB is a perfect complement to GraphQL giving you horizontal scalability, fault tolerance, and global data distribution in a single system.

In this blog post we’ll show you how to do:

  • Install a 3 node YugabyteDB cluster on Google Kubernetes Engine (GKE)
  • Deploy the Hasura GraphQL Engine on GKE
  • Stand up Hasura’s “realtime-location-tracking” app locally and connect to resources on GKE

The “realtime location application” is built using React and is powered by Hasura GraphQL Engine backed by a 3 node YugabyteDB cluster. It has an interface for users to track location of a vehicle using Hasura live queries, in real time. The application makes use of Hasura GraphQL Engine’s real-time capabilities using subscription.

New to distributed SQL or YugabyteDB? Read on.

What Is Distributed SQL?

  • Distributed SQL databases are becoming popular with organizations interested in moving data infrastructure to the cloud or cloud native environments. This is often motivated by the desire to reduce TCO or move away from the horizontal scaling limitations of monolithic RDBMS like Oracle, PostgreSQL, MySQL, and SQL Server. The basic characteristics of Distributed SQL are:
  • They must have a SQL API for querying and modeling data, with support for traditional RDBMS features like foreign keys, partial indexes, stored procedures, and triggers.
  • Automatic distributed query execution so that no single node becomes a bottleneck.
  • Should support automatic and transparent distributed data storage. This includes indexes, which should be sharded across multiple nodes of the cluster so that no single node becomes a bottleneck. Data distribution ensures high performance and high availability.
  • Distributed SQL systems should also provide for strongly consistent replication and distributed ACID transactions.

For a deeper discussion about what Distributed SQL is, check out, “What is Distributed SQL?”

What Is YugabyteDB?

YugabyteDB is an open source, high-performance distributed SQL database built on a scalable and fault-tolerant design inspired by Google Spanner. YugabyteDB is PostgreSQL wire compatible, cloud native, offers deep integration with GraphQL projects, plus supports advanced RDBMS features like stored procedures, triggers and UDFs.

Step 1: Install YugabyteDB on a GKE Cluster Using Helm 3

In this section we are going to install YugabyteDB on the cluster. The complete steps are documented here. We’ll assume you already have a GKE cluster up and running as a starting point.

The first thing to do is to add the charts repository.

Java

1

$ helm repo add yugabytedb https://charts.yugabyte.com

Now, fetch the updates.

Java

1

$ helm repo update

Create a namespace. In this case we’ll call it yb-demo.

Java

1

$ kubectl create namespace yb-demo

Expected output:

Java

1

namespace/yb-demo created

We are now ready to install YugabyteDB. In the command below we’ll be specifying values for a resource constrained environment.

Java

1

$ helm install yb-demo yugabytedb/yugabyte \

2

--set resource.master.requests.cpu=1,resource.master.requests.memory=1Gi,\

3

resource.tserver.requests.cpu=1,resource.tserver.requests.memory=1Gi,\

4

enableLoadBalancer=True --namespace yb-demo --wait

To check the status of the cluster, execute the below command:

Java

1

$ kubectl get services --namespace yb-demo

check yugabytedb cluster status hasura graphql distributed sql gke tutorial

Note: the external-IP for yb-tserver-service which we are going to use to establish a connection between YugabyteDB and Hasura. In this demo, the IP is 35.224.XX.XX and the YSQL port is 5433.

Step 2: Setting up Hasura to Use YugabyteDB

Get the Hasura Kubernetes deployment and service files by executing the commands below.

Java

1

$ wget https://raw.githubusercontent.com/hasura/graphql-engine/master/install-manifests/kubernetes/deployment.yaml

Java

1

$ wget https://raw.githubusercontent.com/hasura/graphql-engine/master/install-manifests/kubernetes/svc.yaml

Modify the database URL in deployment.yaml file to include the IP of YugabyteDB. This file can be edited using a text editor like vi. Note that by default the yugabyte user in YugabyteDB doesn’t have a password and the default database is yugabyte.

For the purposes of this tutorial, the modification should look like this:

Java

1

value: postgres://yugabyte:@35.224.XX.XX:5433/yugabyte

Note: If you’d like everything to run in the yb-demo namespace, make sure to modify the namespace value in both the deployment.yaml and svc.yaml files. This is the setup I have chosen for this demo.

After saving the file use kubectl to create a Hasura deployment using the commands below:

Java

1

$ kubectl create -f deployment.yaml

2

3

deployment.apps/hasura created

Java

1

$ kubectl create -f svc.yaml

2

3

service/hasura created

To find the external IP and open the Hasura console execute the command below:

Java

1

$ kubectl get services --namespace yb-demo

find the external IP hasura yugabytedb distributed sql gke tutorial

Now use http://<EXTERNAL-IP>/console to access the Hasura console. In this case it is 34.68.XX.XX. You should now see the Hasura console.

see the hasura console graphql distributed sql yugabytedb tutorial

#tutorial #kubernetes #postgresql #graphql #distributed sql #sql

Deploying a Real-Time Location App With Hasura GraphQL Engine and Distributed SQL
1.20 GEEK