Dependable Microservices via Lattice

An introduction to Cloud Foundrys Lattice and tutorial using a Grails 3 application..

Object Partners

Grails 3 makes development of microservices awesome. Docker makes ops for developers of said microservices awesome. However, once we get past “hello, world” and into HOLY TRAFFIC there’s a whole new layer of complexity around scalability and reliability. There have been a handful of tools emerging to target these issues, but in this post I want to introduce Cloud Foundry’s Lattice. If you haven’t heard of Lattice here’s the elevator pitch to save you a click:

What is it?

A project that helps manage a cluster of containers to keep things scalable and reliable so you don’t have to reinvent that wheel during the worst possible time (when you’re app is finally attracting load).

What will it address for these containers?

  • Scheduling: balancing containers across available resources as things change
  • Routing: configuring routes so started containers can service requests as soon as they are ready (and pull out containers as soon as their health check fails)
  • Self Healing: Lattice uses declarative application deployment via Terraform and will automatically right the ship if the system state doesn’t match what you’re expecting
  • Status Streaming: Logs from all the containers get aggregated and streamed to one spot for real-time monitoring
  • Check out lattice’s documentation for the full feature set including cool capabilities like A/B testing and blue-green deployments

Using Lattice locally with Grails

Let’s go through a simple tutorial of getting a bare Grails 3 app deployed to Docker and clustered with Lattice.

1) Installing and running Lattice

Lattice’s Getting Started docs do a great job explaining how to get everything set up and running. Make sure to follow the instructions all the way through the launching of their sample “lattice-app” to make sure everything is installed correctly.

2) Installing Grails

I would recommend using GVM to install and manage your Grails versions, but it can also be downloaded and installed from the Grails website. For this tutorial you will need Grails 3 or higher. I’m using 3.0.1.

3) Creating the Grails app

grails create-app grails-lattice-app
cd grails-lattice-app

# Run the app and make sure you get the stock welcome page
grails run-app

4) Setting up a Docker Hub account

You will need to create an account on Docker Hub. We will use it to store the image that will be run in the cluster.

5) Getting the container ready

Much of this is borrowed from the spring-boot-docker guide (hurray for boot-based Grails :-)), so check there for more info. We will start by creating a Dockerfile in the project:

mkdir src/main/docker
vim src/main/docker/Dockerfile

In the Dockerfile, use the following instructions:

FROM java:8
VOLUME /tmp
ADD grails-lattice-app-0.1.jar app.jar
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

Now we need to add the following pieces to build.gradle:

buildscript {
    ...
    dependencies {
        ...
        classpath('se.transmode.gradle:gradle-docker:1.2')
    }
}

group = {your-docker-hub-id}

...
apply plugin: 'docker'

task buildDocker(type: Docker, dependsOn: build) {
  push = true
  applicationName = jar.baseName
  dockerfile = file('src/main/docker/Dockerfile')
  doFirst {
    copy {
      from jar
      into stageDir
    }
  }
}

Make sure you replace “group” with your Docker Hub id, or else it will fail while pushing the image. Next we will build the Docker image and push it to Docker Hub (NOTE: if using a Mac, execute this in your boot2docker terminal):

./gradlew build buildDocker

6) Creating the cluster

ltc create grails-lattice-app {your-docker-hub-id}/grails-lattice-app:0.1

It should print out a URL to hit the application such as http://grails-lattice-app.192.168.11.11.xip.io. It may take a while for the image to download so don’t panic if you get a timeout message or the URL gives you an error. Keep checking the status of the application with the command

ltc status grails-lattice-app

Once the “Instances” field has the value “1/1” it means the application is deployed and should serve requests. You’re now ready to scale up, crash instances to watch recovery, and all the other fun basic operations from Lattice’s Getting Started docs.

Should I be using Lattice?

Depends on your team size and deployment needs. Lattice is being created for single user/single tenant clusters and is ideal for small teams and simpler use cases. It is a small subset of a full Cloud Foundry Elastic Runtime deployment and serves as its impressive gateway drug. Even if you aren’t planning on using Lattice in production, its ease of use makes deploying and experimenting with a local environment of microservices useful.

Share this Post

Related Blog Posts

Devops

Chef Tips & Hints - A Restrospective

December 23rd, 2014

Chef lessons learned. Dealing with Attributes, Databags, Roles, Cookbooks, and Berkshelf.

Object Partners

About the author