Before we begin be sure to download and install confd.
confd support the following backends:
- etcd
- consul
- environment variables
This guide assumes you have a working etcd, or consul server up and running and the ability to add new keys.
etcdctl set /myapp/database/url db.example.com
etcdctl set /myapp/database/user rob
curl -X PUT -d 'db.example.com' http://localhost:8500/v1/kv/myapp/database/url
curl -X PUT -d 'rob' http://localhost:8500/v1/kv/myapp/database/user
export MYAPP_DATABASE_URL=db.example.com
export MYAPP_DATABASE_USER=rob
The confdir is where template resource configs and source templates are stored. The default confdir is /etc/confd
. Create the confdir by executing the following command:
sudo mkdir -p /etc/confd/{conf.d,templates}
You don't have to use the default confdir
location. For example you can create the confdir under your home directory. Then you tell confd to use the new confdir
via the -confdir
flag.
mkdir -p ~/confd/{conf.d,templates}
Template resources are defined in TOML config files under the confdir
(i.e. ~/confd/conf.d/*.toml).
Lets create a simple template resource to manage the /tmp/myconfig.conf
configuration file.
src = "myconfig.conf.tmpl"
dest = "/tmp/myconfig.conf"
keys = [
"/myapp/database/url",
"/myapp/database/user",
]
Save the file under the confdir
directory, i.e. ~/confd/conf.d/myconfig.toml
Source templates are plain old Golang text templates, and are stored under the confdir
templates directory. Create the following source template and save it as ~/confd/templates/myconfig.conf.tmpl
# This a comment
[myconfig]
database_url = {{get "/myapp/database/url"}}
database_user = {{get "/myapp/database/user"}}
confd supports two modes of operation, daemon and onetime mode. In daemon mode, confd runs in the foreground and processing template resources every 5 mins by default. For this tutorial we are going to use onetime mode.
Assuming you etcd server is running at http://127.0.0.1:4001 you can run the following command to process the ~/confd/conf.d/myconfig.toml
template resource:
confd -verbose -onetime -node 'http://127.0.0.1:4001' -confdir ~/confd
Output:
2013-11-03T18:00:47-08:00 confd[21294]: NOTICE Starting confd
2013-11-03T18:00:47-08:00 confd[21294]: NOTICE etcd nodes set to http://127.0.0.1:4001
2013-11-03T18:00:47-08:00 confd[21294]: INFO Target config /tmp/myconfig.conf out of sync
2013-11-03T18:00:47-08:00 confd[21294]: INFO Target config /tmp/myconfig.conf has been updated
The dest
config should now be in sync with the template resource configuration.
cat /tmp/myconfig.conf
Output:
# This a comment
[myconfig]
database_url = db.example.com
database_user = rob