etcd

package
v0.40.0-rc.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 26, 2026 License: MIT Imports: 11 Imported by: 0

README

etcd Backend

The etcd backend enables confd to retrieve configuration data from etcd, a distributed key-value store. This backend uses the etcd v3 API.

Configuration

Basic Connection

Connect to etcd without authentication:

confd etcd --node http://127.0.0.1:2379 --onetime

Multiple nodes for high availability:

confd etcd \
  --node http://etcd1.example.com:2379 \
  --node http://etcd2.example.com:2379 \
  --node http://etcd3.example.com:2379 --onetime
Authentication
Username/Password
confd etcd --node http://127.0.0.1:2379 \
  --basic-auth --username admin --password secret --onetime
TLS Client Certificates
confd etcd --node https://127.0.0.1:2379 \
  --client-cert /path/to/client.crt \
  --client-key /path/to/client.key \
  --client-ca-keys /path/to/ca.crt --onetime
TLS with Authentication
confd etcd --node https://127.0.0.1:2379 \
  --client-cert /path/to/client.crt \
  --client-key /path/to/client.key \
  --client-ca-keys /path/to/ca.crt \
  --basic-auth --username admin --password secret --onetime

Options

Flag Description Default
-n, --node etcd node address (can be specified multiple times) -
--basic-auth Enable basic authentication false
--username Username for basic auth -
--password Password for basic auth -
--client-cert Path to client certificate -
--client-key Path to client private key -
--client-ca-keys Path to CA certificate -
--scheme URI scheme (http or https) http
--client-insecure Skip TLS certificate verification false

Basic Example

Add keys to etcd:

etcdctl put /myapp/database/url "db.example.com"
etcdctl put /myapp/database/user "admin"
etcdctl put /myapp/database/password "secret123"

Create template resource (/etc/confd/conf.d/myapp.toml):

[template]
src = "myapp.conf.tmpl"
dest = "/etc/myapp/config.conf"
keys = [
  "/myapp/database",
]

Create template (/etc/confd/templates/myapp.conf.tmpl):

[database]
url = {{getv "/myapp/database/url"}}
user = {{getv "/myapp/database/user"}}
password = {{getv "/myapp/database/password"}}

Run confd:

confd etcd --node http://127.0.0.1:2379 --onetime

Advanced Example

Using DNS SRV Records

Discover etcd nodes via DNS SRV records:

confd etcd \
  --srv-record _etcd-client._tcp.example.com \
  --scheme https --onetime
Watch Mode with TLS
confd etcd \
  --node https://etcd.example.com:2379 \
  --client-ca-keys /etc/ssl/certs/etcd-ca.crt \
  --watch
Kubernetes Deployment
apiVersion: v1
kind: Pod
metadata:
  name: myapp
spec:
  containers:
  - name: myapp
    env:
    - name: ETCD_USERNAME
      valueFrom:
        secretKeyRef:
          name: etcd-credentials
          key: username
    - name: ETCD_PASSWORD
      valueFrom:
        secretKeyRef:
          name: etcd-credentials
          key: password
    command:
    - confd
    - etcd
    - --node=http://etcd.default.svc:2379
    - --basic-auth
    - --username=$(ETCD_USERNAME)
    - --password=$(ETCD_PASSWORD)
    - --watch

Watch Mode Support

Watch mode is supported for the etcd backend. confd uses etcd's native watch API for efficient real-time updates.

confd etcd --node http://127.0.0.1:2379 --watch

When keys change in etcd, confd immediately detects the change and re-renders affected templates.

Per-Resource Backend Configuration

Instead of using the global backend, individual template resources can specify their own etcd backend configuration. This allows mixing backends within a single confd instance.

Add a [backend] section to your template resource file:

[template]
src = "myapp.conf.tmpl"
dest = "/etc/myapp/config.conf"
keys = [
  "/myapp/database",
]

[backend]
backend = "etcd"
nodes = ["https://etcd.example.com:2379"]
basic_auth = true
username = "admin"
password = "secret"
client_cert = "/path/to/client.crt"
client_key = "/path/to/client.key"
client_cakeys = "/path/to/ca.crt"

Available backend options:

  • backend - Must be "etcd"
  • nodes - Array of etcd node addresses
  • scheme - "http" or "https"
  • basic_auth - Enable basic authentication
  • username - Username for basic auth
  • password - Password for basic auth
  • client_cert - Path to client certificate
  • client_key - Path to client private key
  • client_cakeys - Path to CA certificate
  • client_insecure - Skip TLS certificate verification

Connection Behavior

  • Dial timeout: 5 seconds
  • Keep-alive: 10 seconds interval, 3 seconds timeout
  • Transaction limit: 128 operations per transaction (etcd v3 default)
  • Automatic reconnection: Watch connections automatically reconnect after disconnection

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a wrapper around the etcd client

func NewEtcdClient

func NewEtcdClient(machines []string, cert, key, caCert string, clientInsecure bool, basicAuth bool, username string, password string, dialTimeout time.Duration) (*Client, error)

NewEtcdClient returns an *etcd.Client with a connection to named machines.

func (*Client) Close

func (c *Client) Close() error

Close closes the etcd client connection.

func (*Client) GetValues

func (c *Client) GetValues(ctx context.Context, keys []string) (map[string]string, error)

GetValues queries etcd for keys prefixed by prefix.

func (*Client) HealthCheck

func (c *Client) HealthCheck(ctx context.Context) error

HealthCheck verifies the backend connection is healthy. It checks the status of the first etcd endpoint.

func (*Client) HealthCheckDetailed

func (c *Client) HealthCheckDetailed(ctx context.Context) (*types.HealthResult, error)

HealthCheckDetailed provides detailed health information for the etcd backend.

func (*Client) WatchPrefix

func (c *Client) WatchPrefix(ctx context.Context, prefix string, keys []string, waitIndex uint64, stopChan chan bool) (uint64, error)

type Watch

type Watch struct {
	// contains filtered or unexported fields
}

A watch only tells the latest revision

func (*Watch) WaitNext

func (w *Watch) WaitNext(ctx context.Context, lastRevision int64, notify chan<- int64)

Wait until revision is greater than lastRevision

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL