Skip to content

Use etcdctl

Put, get, watch and lease keys with etcdctl over mutual TLS.

This exercise writes a key, watches changes from a snapshot revision, and deletes a key by revoking its Lease. It requires a running development tenant (Quick start), etcdctl 3.6.15 (or 3.7.2), and jq. Keep the /tutorial/ prefix free for this walkthrough.

Configure the client

Export the etcd v3 environment variables in each terminal, replacing the endpoint and certificate paths with those for your tenant:

export ETCDCTL_API=3
export ETCDCTL_ENDPOINTS=https://pgetcd.example.test:2379
export ETCDCTL_CACERT=/path/to/server-ca.crt
export ETCDCTL_CERT=/path/to/tenant-client.crt
export ETCDCTL_KEY=/path/to/tenant-client.key
export ETCDCTL_DIAL_TIMEOUT=5s
export ETCDCTL_COMMAND_TIMEOUT=10s

etcdctl version
etcdctl endpoint status --write-out=table

The endpoint hostname or IP must match the gateway server certificate (server.crt), and the client certificate (tenant-client.crt) must carry the tenant’s URI SAN (spiffe://pgetcd/tenant/demo). ETCDCTL_CACERT points to the CA that signed the gateway server certificate.

etcdctl endpoint status prints the tenant’s logical member ID, current revision, advertised version (3.6.0), and PostgreSQL relation size. etcdctl endpoint health reports healthy from v0.1.0-rc2; on v0.1.0-rc1 it fails because Maintenance.Alarm was unimplemented. If endpoint status fails, see Troubleshooting.

Read and write keys

Write and read back a key under /tutorial/:

etcdctl put /tutorial/hello world
etcdctl get /tutorial/hello
etcdctl get /tutorial/ --prefix --write-out=json

The first command prints OK. The second prints the key and world. The JSON output shows header.revision along with the key’s create_revision, mod_revision, and version (keys and values in JSON output are base64-encoded).

Write the same value a second time:

etcdctl put /tutorial/hello world
etcdctl get /tutorial/hello --write-out=json

Both version and mod_revision advance even when the value does not change. Do not hard-code revision numbers in scripts; concurrent writes or Lease expirations in the same tenant also advance the revision counter.

Read a snapshot, then watch

In the first terminal, read a prefix snapshot and start a Watch at header.revision + 1:

TUTORIAL_SNAPSHOT=$(etcdctl get /tutorial/ --prefix --write-out=json)
printf '%s\n' "$TUTORIAL_SNAPSHOT" | jq .
TUTORIAL_START=$(printf '%s\n' "$TUTORIAL_SNAPSHOT" | jq '.header.revision + 1')
etcdctl --command-timeout=0s watch /tutorial/ --prefix --prev-kv --rev="$TUTORIAL_START"

In a second terminal with the same ETCDCTL_* variables, update and delete the key:

etcdctl put /tutorial/hello updated
etcdctl del /tutorial/hello

The first terminal prints PUT and DELETE events with their previous key-value pairs (--prev-kv). Passing --rev="$TUTORIAL_START" ensures that any write committed between the get snapshot and the watch registration is still delivered, provided that revision has not been logically compacted.

Press Ctrl-C to stop the Watch. If the requested start revision has been compacted, take a fresh get snapshot and start a new Watch. For Watch stream and buffer limits, see Troubleshooting — Capacity.

Note

jq uses IEEE 754 double-precision numbers. For production applications where revisions can exceed $2^{53} - 1$, compute revision + 1 in a client that preserves 64-bit integers, such as the Go client example.

Attach and revoke a Lease

Grant a 30-second Lease:

etcdctl lease grant 30

Copy the hexadecimal Lease ID from the output and attach /tutorial/leased to it:

TUTORIAL_LEASE=0123456789abcdef
etcdctl put /tutorial/leased temporary --lease="$TUTORIAL_LEASE"
etcdctl lease timetolive "$TUTORIAL_LEASE" --keys
etcdctl lease revoke "$TUTORIAL_LEASE"
etcdctl get /tutorial/leased

Revoking the Lease atomically deletes /tutorial/leased in the same PostgreSQL transaction, so the final get returns no rows.

To keep a Lease alive continuously, grant a new Lease and run etcdctl --command-timeout=0s lease keep-alive <lease-id>. For Lease recovery behavior after a PostgreSQL restart, see Watch, Lease and history.

Clean up

Delete any remaining keys under /tutorial/:

etcdctl del /tutorial/ --prefix

Next, see Use the Go client for conditional transactions.

Scan to open this page
https://mimus.pgsty.com/docs/etcd/tutorial/etcdctl/