Fetching a remote configuration using Consul and Go (golang)

Post to Twitter

Nowadays its pretty common to spin up a Docker container that will run an application to do some sort of work or processing. Many times this application requires some sort of configuration to set it up correctly to run. With Docker and an app built in Go (or any other language) you could spin up the container and set an environment variable which the application could then read. Let’s assume the environment variable is just an address that points to something like ectd or Consul where the full configuration settings are stored. Let’s go over how this can be done using Go (golang – excuse the redundancy on Go/golang I use this to help the search engines out).

I’ll assume you have Go installed and running, if not, please visit here. I will also assume you have Consul running, if you don’t I have a handy Vagrant script you can use to spin up a dual Consul cluster to test with. Follow the instructions here.

Create a new project folder for your Go project. I’m going to create a new folder called helloconfig. In my case the project folder is located here:

Inside the new project folder create a file, in this case we can just call it main.go. In the same folder create another file called config.json. Inside the config.json file add the following:

    "msg": "Hello World",
    "the-answer": 42

At the command prompt we need to grab a couple dependencies.

$ go get github.com/spf13/viper
$ go get github.com/xordataexchange/crypt/bin/crypt

For the configuration I’m going to use Viper which supports all sorts of config options (see my previous intro post on Viper) including remote configuration via Consul or etcd. I’m going to also use crypt to set some the config data in Consul.

Note: crypt supports encryption and in a production setting you want to take advantage of this, for this article I’m not going to setup any sort of encryption – I leave that as an exercise for the reader.

Let’s go ahead and push the contents of the config.json up to Consul.

$ crypt set -backend="consul" -endpoint="" -plaintext /config/helloconfig.json /Users/chadlung/GoProjects/src/github.com/chadlung/helloconfig/config.json

Make sure that was set:

$ crypt get -backend="consul" -endpoint="" -plaintext /config/helloconfig.json

Expected output:

    "msg": "Hello World",
    "the-answer": 42

That looks good. Let’s move onto the actual Go code now which is pretty easy. Inside the main.go file add the following:

Run the application:

$ go run main.go 

Expected output:

Msg: Hello World
Universal Answer: 42

You could now easily add to the code above to read the (Consul connection) config values in line #12 from an environment variable in which case you could pass that into the program easily using Docker if you wanted. With that you simply need to build your Go application, spin it up in a Docker container passing in the initial Consul connection data as an environment variable (or via a command line argument to the application itself) and that would be enough minimal info for the app to then load its full configuration data from Consul. Having your config centralized in something like Consul or etcd allows you to update it easily for all applications and means the deployment of the applications (especially with Go statically compiled) is easier.

Post to Twitter

This entry was posted in Go, Open Source. Bookmark the permalink.

2 Responses to Fetching a remote configuration using Consul and Go (golang)

  1. alvaro says:


    Have you test envconsul?


  2. Chad Lung says:


    I have not. Didn’t know it existed until now so thanks for the tip! 🙂


Comments are closed.