Etcd
etcd is a distributed key-value store. pgcli can run one or more etcd members as a standalone, top-level addon — shared infrastructure rather than a per-instance sidecar. This is useful as the DCS (Distributed Concurrent Store) backing a PostgreSQL HA stack, or as a general config/lock service.
Security: pgcli-managed etcd members run without TLS or authentication by default — any client that can reach a client port has full read/write access, so loopback binds and a trusted network are the baseline. Pass
--tlsto encrypt the client listener and mutually authenticate raft peers, and--authto require a username/password. There is still no multi-user or RBAC management:--authcreates a single user with therootrole. See TLS and Authentication.
Members are managed one pg addon install etcd at a time: the first member
bootstraps a cluster, and later members join the same named cluster on the fly
— from the same host or from another machine.
How It Works
- Shared infrastructure: etcd lives in the top-level
addons.etcdmap inpg.yaml, keyed by member name — not under any single instance. - Host network, configurable bind: each member runs with
--network host. By default its client/peer URLs bind to127.0.0.1— without--tlsand--auth, any reachable client has full access, so loopback-only exposure is the default posture for a single-host cluster. For cross-host clusters each member advertises a reachable address (--advertise-host) while also listening on loopback, so localetcdctland remote peers both work. - Optional TLS and authentication:
--tlscovers both interfaces (client HTTPS plus raft peer mTLS),--authadds a root-role username/password. Both are cluster-wide and fixed at bootstrap — see TLS and Authentication. - Dynamic membership: the first member starts with
--initial-cluster-state new; each subsequent member is registered withetcdctl member addagainst a running peer, then started with--initial-cluster-state existing. pgcli does this automatically — same-host via a member container, cross-host via a temporary etcdctl container. - Cluster identity: members sharing the same
--clustervalue join the same etcd cluster (it maps to etcd’s--initial-cluster-token). The default ispgcli-etcd. - Baked-in tuning: every member launches with periodic compaction
(
--auto-compaction-mode periodic,--auto-compaction-retention 24h) and an 8 GiB backend quota (--quota-backend-bytes 8589934592) — sane defaults for a small HA metadata store.
Deployment Topologies
pgcli supports two cluster shapes. Both use the same install commands — the difference is whether members share a host.
Single host — all members on one machine (testing / dev)
The classic layout for a dev box or CI: every member listens on loopback with a
different auto-assigned port. No --advertise-host needed (default
127.0.0.1), no firewall changes, nothing reachable from outside the host.
All three belong to cluster pgcli-etcd; m1 bootstraps, m2/m3 join on the fly.
This gives you real 3-node raft semantics but zero host isolation — the whole
cluster dies with one machine, which is exactly why it is a testing topology.
Cross host — one member per machine (production HA)
The production shape: spread members across machines (ideally 3 or 5, an odd
count — see Topology & Quorum). Each member advertises its host’s LAN address;
the first member must bootstrap with --advertise-host so remote peers can
dial it. Ports may repeat on every host since each binds its own interface.
Requirements: every member’s --advertise-host is a mutual-LAN-reachable
address, the firewall on each host lets the other members through on every
member’s client and peer ports (bidirectional peer traffic — e.g. open
2379-2386/tcp for a 4-member cluster on default ports), and all members share
the same --cluster token. Each host survives losing any one member; the
cluster only needs a majority of machines.
| Single host | Cross host | |
|---|---|---|
| Use for | dev, CI, functional testing | production HA |
--advertise-host |
omit (loopback default) | required, every member |
--join |
not used | every member after the first |
| Ports | unique per member on one host | may repeat, one host each |
| Firewall | none (loopback only) | open every member’s client+peer ports, both ways |
| Survives machine loss | no | yes (with quorum) |
TLS and Authentication
--tls and --auth are independent, optional, and cluster-wide properties
decided at bootstrap. Every member of a cluster must agree on each:
--tlsturns the client listener into HTTPS and the raft peer listener into mTLS. Every member presents a leaf signed by one per-cluster CA, and peers verify each other’s certificates against that CA.--authenables etcd’s auth store with a single user (defaultroot) that holds therootrole, then turns authentication on.
Without --tls, --auth still works, but the password crosses the network in
clear text (pgcli prints a warning). With --tls, client traffic is encrypted
and the server is authenticated; clients authenticate with the
username/password.
Single host
When --password is omitted on the first install, pgcli generates one and
prints it once:
Retrieve it later with pg addon password etcd --name m1. Additional members
on the same host inherit both settings (and the credentials) from the local
peer automatically:
Pass --password (and optionally --root-user) to choose your own; either
flag implies --auth. Re-running install with a new --password rotates the
root user’s password.
Cross-host cluster
The first member generates the CA locally; joining hosts fetch it from the cluster automatically — no certificate files to copy:
- The
--joinscheme must match:https://with--tls,http://without. - When the remote cluster has auth enabled,
--password(plus--root-userif notroot) is required — the CA fetch itself authenticates with those credentials. Without them the join fails with an explicit error. - The CA fetch is trust-on-first-use: verification is skipped for that single exchange, then all subsequent traffic is verified against the fetched CA.
--join works on hosts with no local CA only when the cluster CA was generated
by pgcli. With a manually distributed CA or BYO certificates, copy the CA to
every host yourself (below).
Manual CA distribution (pg cert --ca)
Instead of letting the first member generate a CA, mint one with
pg cert --ca and place it on every host before installing. The pair must
live at <base_dir>/tls/etcd/<cluster>/ (<base_dir> defaults to ~/.pgcli):
The pair is read from that directory (the key kept at 0600) and reused as-is; each member then gets its own leaf signed by that CA. Use the same pair on every host, including the bootstrap host. This mode does not publish the CA into etcd — distribute it yourself.
A CA generated by a current pg cert --ca carries no extended key usage
restriction, so it can sign both server and client certificates (raft peers
present their certificate as a client cert). Older pgcli releases wrote a
serverAuth-only CA; peer mTLS rejects those chains, and pgcli now refuses such
a CA up front with a message to regenerate it.
Bring your own member certificate
--tls-cert / --tls-key use an externally issued leaf for this member
(implies --tls); the optional --tls-ca is the CA clients verify against,
defaulting to the leaf itself for a self-signed certificate. The leaf must
carry the clientAuth EKU (or no EKU at all) because raft peers verify it as a
client certificate. BYO material is never copied into etcd and the CA is not
published — distribute both yourself.
What gets published, and the risk
In generated-certificate mode (plain --tls, no BYO pair) the first member
writes the cluster CA pair into etcd itself, at /_pgcli/pki/etcd/ca.crt and
/_pgcli/pki/etcd/ca.key, so other hosts can fetch it during --join:
- Before
--authis enabled, anyone who can reach the client port can read the CA private key (it is published before auth is turned on, since the publish itself runs without credentials). With--auth, reading it requires the credentials. - Anyone holding that key can mint certificates this cluster trusts. Keep
client ports on a private network and firewall 2379, or avoid publication
entirely by distributing a
pg cert --caCA or BYO certificates manually.
The install summary prints this warning whenever a generated CA is published.
Consumers that are not compatible
Patroni’s DCS configuration and RabbitMQ’s etcd peer discovery still dial
plain HTTP without credentials. Enabling --tls or --auth on a cluster
they consume disconnects them. When pgcli finds such a consumer in the local
config (a Patroni cluster whose etcd members/endpoints match this member, or a
RabbitMQ instance using it for discovery), the install prints a [!] warning.
Keep such etcd clusters plaintext and network-isolated until those integrations
support TLS/auth.
Changing TLS/auth later
TLS is fixed for the life of a member: its advertised peer URL scheme is part of the cluster’s membership record. Switching an initialized member, or mixing TLS and non-TLS members in one cluster, is rejected — destroy and re-create the cluster instead:
Authentication is also cluster-wide. To enable it on an existing plaintext
cluster, re-install the bootstrap member with --auth --password ... first,
then re-install every other member with --auth so each records the
credentials (same-host peers inherit them; cross-host members must pass
--password). Disabling auth again requires re-creating the cluster.
Inspecting authentication
Authentication status and users/roles can be queried directly:
View the password (only present when Auth: true; it is stored in pg.yaml
and can only be read on a host that has the member configured):
pg addon list also shows Auth: enabled / Auth: disabled per member. This
version has no multi-user/RBAC management commands; etcd’s native commands can
be passed through pg etcdctl.
Commands
Create the first member
The client port starts at 2379 and the peer port takes the next free port
(2380), auto-allocated from etcd_start_port.
Add members to the same cluster
With m1 running, installing another member of the same --cluster joins it:
pgcli finds a running peer to act as coordinator, registers m2/m3 via
etcdctl member add, and starts them with --initial-cluster-state existing.
Ports continue to auto-assign without collisions (m2 → 2381/2382, m3 →
2383/2384). A freshly-grown cluster briefly lacks quorum during leader
re-election; pgcli retries the membership operations automatically, so you do
not need to wait between installs.
Use a different --cluster to keep members in a separate etcd cluster.
Join a member on another machine
Members on different hosts form one cluster too. Each cross-host member must
advertise an address the other members can reach, set with
--advertise-host. On the machine that already hosts a member, bootstrap as
usual, passing its LAN address:
Then on the new host, point --join at any existing member’s client endpoint.
pgcli registers the member through that endpoint (from a temporary etcdctl
container — no local member container or image needed; the image is pulled on
demand), takes the authoritative ETCD_INITIAL_CLUSTER from the response, and
starts the member:
Notes for cross-host clusters:
- Every member needs a reachable
--advertise-host— including the first. Its advertised peer URL propagates into the cluster’s membership list, so a first member started on the default127.0.0.1can never be joined from another host. If you plan a cross-host cluster, pass the LAN address at bootstrap time. --clusteris required with--joinand must match the remote cluster’s token — membership is checked by that cluster, not by the local config.--advertise-hostis required with--join; without it the other members could register a peer URL they can’t dial.- Ports may repeat across hosts (
2379/2380on both) since each host binds its own interfaces. - A member’s client/peer ports must be reachable between hosts — open your
firewall for every member’s ports, in both directions. Peers dial each
other’s peer ports bidirectionally, and clients (and
--join/pg etcdctl) reach the client ports, so allow both. Auto-assignment means the exact numbers vary per member — read them from the install summary orpg addon list(or pin them with--client-port/--peer-port) and open that range, e.g.2379-2386/tcpfor a 4-member cluster on default ports. Skipping this is the usual cause of a member hanging withetcdserver: no leaderor a--jointhat times out. - Re-running the same
--joininstall for an already-registered name fails with a clear error; deregister first viapg etcdctl member remove <hex-id>against the cluster.
Inspect with pg etcdctl
pg etcdctl runs etcd’s client from a short-lived container — no need to
exec into a member (and it works even on a host with no member
installed, pulling the image on demand). The target comes from
ETCDCTL_ENDPOINTS, falling back to the first configured member:
etcdctl flags that pg’s own parser would reject (-w table, --hex, …) go
after --.
List
etcd members appear under the Infra add-ons (etcd) section:
A TLS-enabled member also prints a CA: line with the cluster CA path used for
client verification (ready to use with pg etcdctl --ca on a non-member host).
Start and stop
After a host reboot or a manual stop, bring a member back up without
re-running install — no member add, the existing config and on-disk data are
reused:
start is safe to repeat: an already-running member is a no-op, and a member
whose container was removed is recreated from its data with
--initial-cluster-state existing. For a cross-host cluster, confirm the
member’s peers are reachable (quorum intact) before starting it.
Remove a member
Removal first deregisters the member from the cluster (resolving its hex
member ID — etcd v3.5 member remove takes an ID, not a name), then deletes
the container and the member’s data directory. Remaining members stay healthy
as long as quorum holds.
Cross-host members must be deregistered separately. The automatic
deregistration above only works when another running member of the same
cluster is present in the local pg.yaml — pgcli has no view of members that
live on other hosts. So on a cross-host cluster, removing a member from the
host that runs it deletes the container and data but leaves its entry in the
cluster’s membership list (a “stale” member) — the surviving members keep
trying to peer with the now-deleted host.
Remove it in two steps instead:
pg etcdctl needs a reachable peer to talk to — point ETCDCTL_ENDPOINTS at
a member of that cluster that is still running (on a host that still has a
running member, this falls back automatically; otherwise set it explicitly),
then remove by ID (see Inspect with pg etcdctl).
Verify with pg etcdctl member list afterward: the removed name should be gone.
Parameters
| Flag | Description | Default |
|---|---|---|
--name |
Member name, and the pg.yaml config key |
etcd |
--cluster |
Cluster identity (--initial-cluster-token); same value = same cluster |
pgcli-etcd |
--client-port |
Client host port (0 = auto-assign from etcd_start_port) |
auto |
--peer-port |
Peer host port (0 = auto-assign, next free port) | auto |
--image |
etcd image tag | quay.io/coreos/etcd:v3.5.30 |
--data-dir |
Data dir root — absolute, or relative to base_dir; each member uses <root>/<name>/data |
<base_dir>/addon/etcd |
--advertise-host |
Host advertised in this member’s peer/client URLs (empty = 127.0.0.1 for single-host; set a LAN IP or FQDN for cross-host) |
127.0.0.1 |
--join |
Client endpoint of an existing member to join cross-host, e.g. https://10.0.0.1:2379 (implies --initial-cluster-state existing; requires --advertise-host and --cluster; scheme must match --tls) |
— |
--tls |
TLS on both interfaces — client HTTPS and raft peer mTLS; certificates generated under <base_dir>/tls/etcd/<cluster>/ |
off |
--tls-cert / --tls-key |
Bring-your-own member certificate and key (implies --tls; the leaf needs the clientAuth EKU or no EKU) |
— |
--tls-ca |
CA clients verify against with --tls-cert/--tls-key (defaults to the certificate itself) |
— |
--auth |
Enable username/password authentication with a single root-role user | off |
--root-user |
Authentication user name (implies --auth) |
root |
--password |
Authentication password (implies --auth); generated on first install when omitted |
generated |
Container name follows the namespace convention:
pgcli-etcd-<namespace>-<name> (namespace omitted when unset).
Data Directory Layout
Data is always laid out as <root>/<name>/data, so multiple members on one
host never share a directory:
--data-dir sets the root. A relative value resolves against the config’s
base_dir; the resolved root is persisted, so the config stays portable:
pg addon remove deletes only that member’s <name>/data dir and leaves the
shared root in place.
Connecting
Use pg etcdctl, which runs etcd’s v3 client from a short-lived container
against a member’s client URL. No local member container or image is required
(the image is pulled on demand). etcdctl’s native flags go after --.
From a member host (bootstrap or joined node)
Whenever the local pg.yaml configures any member of the cluster — whether it
is the bootstrap member or one that joined later — pg etcdctl injects that
member’s CA (ETCDCTL_CACERT) and credentials (ETCDCTL_USER; the password
stays out of ps) automatically, with no extra flags:
Without ETCDCTL_ENDPOINTS the target falls back to the first locally
configured member. To query another member of the same cluster, set it
explicitly — the injected CA/credentials come from the local member, but every
member shares the same cluster CA and auth user, so they work too:
Retrieve the password with pg addon password etcd --name m1 on any host that
has the member configured.
From a non-member host
On a host with no configured etcd member — for example a third machine used
only as a client — pg etcdctl injects nothing. Provide the endpoint, CA and
credentials explicitly:
-
Look up the node’s CA path in the
CA:line ofpg addon liston any member host, then copy that file to the client host (generated mode:~/.pgcli/tls/etcd/<cluster>/ca.crt; for manual/BYO setups use your own CA): -
Point at a member’s client URL, pass the host CA with
--ca(pg mounts it into the etcdctl container), and the credentials via--userafter--:The password is available from
pg addon password etcd --name m1on a member host.
An etcdctl --cacert passed after -- refers to a path inside the helper
container, so a host path there does not work (use --ca instead). For a quick
check without the CA, skip verification — etcdctl only honors skip-verify once
it builds a TLS config, so disable its default plaintext transport too:
A non-member host must be able to reach a member’s client port (allow it through the firewall, e.g. 2379/tcp).
Topology & Quorum
etcd requires a quorum (majority) of members to accept writes:
| Members | Quorum | Tolerates failures |
|---|---|---|
| 1 | 1 | 0 |
| 3 | 2 | 1 |
| 5 | 3 | 2 |
- Use an odd number of members — 3 or 5 for production HA.
- Removing members can drop the cluster below quorum (e.g. 3 → 1 surviving); it will then be unable to commit writes. Keep a majority running.
Configuration
After install, pg.yaml records each member under the top-level addons.etcd:
Port base is configurable via top-level etcd_start_port (default 2379). A
cross-host member records the address it advertises:
A TLS/authenticated member records the cluster-wide settings too:
auth_password is stored in clear text (like other addon secrets) and is what
pg addon password etcd --name m1 reads back. A BYO member additionally
records tls_cert_file, tls_key_file and tls_ca_file; in generated-
certificate mode those stay empty and the paths are derived from the cluster
name under <base_dir>/tls/etcd/<cluster>/.
Auto-start on Boot
Containers also carry a --restart unless-stopped policy, which a per-container
conmon monitor enforces even without a podman daemon — it covers crashes but
not host reboots. To bring members up after a reboot, enable autostart
per member:
This sets autostart: true on the member in pg.yaml and installs/refreshes
the boot service (see Auto-start on Boot). Boot is
start-only: it starts the member’s existing container and never re-runs
member add — an initialized member reloads its cluster from disk and rejoins.
On a cross-host cluster, run the command on each host for that host’s
member(s). pg autostart status lists every member’s state.
Notes
- Single-node vs cluster:
--name m1alone gives a one-member cluster; install more members with the same--clusterto grow it. - Production-ready defaults: every member already launches with periodic compaction and an 8 GiB backend quota (see How It Works), suitable for a production DCS without further tuning.
- Reinstall is idempotent: re-running
pg addon install etcd --name <m>on a running member recreates its container with updated flags; a member already registered in the cluster is not re-added. - TLS/auth are bootstrap decisions: start cross-host clusters with
--tls --authfrom the first member; an initialized member cannot switch schemes without being destroyed and re-created. - Manual and BYO certificates stay out of etcd: only a CA that pgcli
generated is published for
--jointo fetch — distribute manual or BYO material yourself.