Getting a graphQL server up and running against CockroachDB on Kubernetes

Cloud Platform (intermediate level)

posted on 1st Feb 2018

In Getting cockroachDB running with Kubernetes I covered how to get your cockroach database orchestrated by Kubernetes. Next you'll want to get your API running. In this example I'm going to run it as a pod on the same Kubernetes cluster, then expose it as a service. This is actually the easy part. The more complicated part is exposing that service externally over SSL. That'll be the next post.

The server App

I'm not going to go into the entire code of the app here now, as this assumes you have your own server app and this article is just about how to integrate it with your back end DB on Kubernetes. I'll cover the whole topic of GraphQL its own right in a different section. I also assume you've read Getting cockroachDB running with Kubernetes to get grounded on the language of Kubernetes.

I'm using express to expose my API. Here's the code for the initialization part, which is of interest for the Kubernetes configuration. The rest of server code is in ./app/server and not in scope of for this article.

Deploying

Now we're ready to deploy and create a service. As usual we'll use .yaml files. It's a good idea to use kubectl apply rather than create, as this will update any previous attempts, whereas create will complain and fail.

Ports and service types

With the various redirections going on, I find the most confusing part is keeping track of port numbers through the various components. We'll need to know these port numbers later on as the next step is to use google endpoints to expose this service externally, but first - some definitions of the complicated world of Kubernetes ports and IPS and how they relate to services.

ClusterIP

You'll see from the earlier listing that each service has a type. A ClusterIP type of service can only be reached from inside the cluster. So you would see <none> as the EXTERNAL-IP of of one of these types of services. In the service listing above, the cockroachdb-public service is only accessible within the cluster - It's only the API that talks to the database that should be exposed externally.

LoadBalancer

This type of service can be used to expose a service externally, and will provide an external-ip that can be reached from outside.