Overview

If you want to dynamically modify service configuration files through a WebUI or API in your business, you can use confd to achieve this. The backend supports the following data types:

  • etcd
  • consul
  • vault
  • environment variables
  • redis
  • zookeeper
  • dynamodb
  • stackengine
  • rancher

This way, we can store configurations in a distributed Key/Value store, and modifying configurations no longer requires SSH to the remote server. Here we use consul as the backend storage because it comes with a UI, which is convenient for practice.

Consul

docker run -d \
-p 8500:8500 \
--name consul-master \
consul agent -node=master -datacenter=betterde -bind=0.0.0.0 \
-ui -client=0.0.0.0 -server -bootstrap-expect=1

After successful execution, you can access http://localhost:8500 to verify that the service is running properly.

curl -X PUT -d 'api.betterde.om' http://localhost:8500/v1/kv/nginx/api/domain

Result

confd

Go to download

Creating Configuration File Directory

sudo mkdir -p /etc/confd/{conf.d,templates}

Creating Resource Template

touch /etc/confd/conf.d/nginx.toml
[template]
src = "nginx.conf.tmpl"
dest = "/tmp/nginx.conf"
keys = [
    "/nginx/api/domain",
]

check_cmd = "/usr/local/opt/nginx/bin/nginx -t -c {{.src}}"
reload_cmd = "/usr/local/opt/nginx/bin/nginx -s reload"
  • src: The template file for service configuration
  • dest: The directory for the configuration file generated by confd
  • keys: Custom keys
  • check_cmd: Command for configuration checking, requires service support (optional)
  • reload_cmd: Command for reloading configuration, requires service support (optional)

Creating Configuration Template

touch /etc/confd/templates/nginx.conf.tmpl
user zuber-imac staff;
worker_processes auto;

events {
    worker_connections  1024;
}

http {
    default_type  application/octet-stream;

    sendfile on;
    keepalive_timeout  65;
    client_max_body_size 128M;

    gzip  on;
    gzip_comp_level 5;
    gzip_min_length 256;
    gzip_proxied any;
    gzip_vary on;
    ssi on;

    gzip_types
    application/atom+xml
    application/javascript
    application/json
    application/rss+xml
    application/vnd.ms-fontobject
    application/x-font-ttf
    application/x-web-app-manifest+json
    application/xhtml+xml
    application/xml
    font/opentype
    image/svg+xml
    image/x-icon
    text/css
    text/plain
    text/x-component;

    server {
        listen 80;
        server_name  {{getv "/nginx/api/domain"}};
        root /var/www/html;
        location / {
            index index.html;
        }
    }
}

Running confd

./confd -watch -backend consul -node localhost:8500

Now if you modify the value of /nginx/api/domain, it will automatically regenerate the nginx configuration file to /tmp/nginx.conf and update nginx’s configuration.

I hope this is helpful, Happy hacking…