Interacting with etcd
Users mostly interact with etcd by putting or getting the value of a key. This section describes how to do that by using etcdctl, a command line tool for interacting with etcd server. The concepts described here should apply to the gRPC APIs or client library APIs.
The API version used by etcdctl to speak to etcd may be set to version 2 or 3 via the ETCDCTL_API environment variable. By default, etcdctl on master (3.4) uses the v3 API and earlier versions (3.3 and earlier) default to the v2 API.
Note that any key that was created using the v2 API will not be able to be queried via the v3 API. A v3 API etcdctl get of a v2 key will exit with 0 and no key data, this is the expected behaviour.
Find versions
etcdctl version and Server API version can be useful in finding the appropriate commands to be used for performing various operations on etcd.
Here is the command to find the versions:
Write a key
Applications store keys into the etcd cluster by writing to keys. Every stored key is replicated to all etcd cluster members through the Raft protocol to achieve consistency and reliability.
Here is the command to set the value of key foo to bar:
Also a key can be set for a specified interval of time by attaching lease to it.
Here is the command to set the value of key foo1 to bar1 for 10s.
The lease id 1234abcd in the above command refers to id returned on creating the lease of 10s. This id can then be attached to the key.
Read keys
Applications can read values of keys from an etcd cluster. Queries may read a single key, or a range of keys.
Suppose the etcd cluster has stored the following keys:
Here is the command to read the value of key foo:
Here is the command to read the value of key foo in hex format:
Here is the command to read only the value of key foo:
Here is the command to range over the keys from foo to foo3:
foo3 is excluded since the range is over the half-open interval [foo, foo3), excluding foo3.
Here is the command to range over all keys prefixed with foo:
Here is the command to range over all keys prefixed with foo, limiting the number of results to 2:
Here is the command to range over all keys prefixed with foo using the RangeStream
RPC. The result is identical to a unary Range:
--stream does not support --order, --sort-by, or revision filters.
Read past version of keys
Applications may want to read superseded versions of a key. For example, an application may wish to roll back to an old configuration by accessing an earlier version of a key. Alternatively, an application may want a consistent view over multiple keys through multiple requests by accessing key history. Since every modification to the etcd cluster key-value store increments the global revision of an etcd cluster, an application can read superseded keys by providing an older etcd revision.
Suppose an etcd cluster already has the following keys:
Here are an example to access the past versions of keys:
Read keys which are greater than or equal to the byte value of the specified key
Applications may want to read keys which are greater than or equal to the byte value of the specified key.
Suppose an etcd cluster already has the following keys:
Here is the command to read keys which are greater than or equal to the byte value of key b :
Delete keys
Applications can delete a key or a range of keys from an etcd cluster.
Suppose an etcd cluster already has the following keys:
Here is the command to delete key foo:
Here is the command to delete keys ranging from foo to foo9:
Here is the command to delete key zoo with the deleted key value pair returned:
Here is the command to delete keys having prefix as zoo:
Here is the command to delete keys which are greater than or equal to the byte value of key b :
Watch key changes
Applications can watch on a key or a range of keys to monitor for any updates.
Here is the command to watch on key foo:
Here is the command to watch on key foo in hex format:
Here is the command to watch on a range key from foo to foo9:
Here is the command to watch on keys having prefix foo:
Here is the command to watch on multiple keys foo and zoo:
Watch historical changes of keys
Applications may want to watch for historical changes of keys in etcd. For example, an application may wish to receive all the modifications of a key; if the application stays connected to etcd, then watch is good enough. However, if the application or etcd fails, a change may happen during the failure, and the application will not receive the update in real time. To guarantee the update is delivered, the application must be able to watch for historical changes to keys. To do this, an application can specify a historical revision on a watch, just like reading past version of keys.
Suppose we finished the following sequence of operations:
Here is an example to watch the historical changes:
Here is an example to watch only from the last historical change:
Watch progress
Applications may want to check the progress of a watch to determine how up-to-date the watch stream is. For example, if a watch is used to update a cache, it can be useful to know if the cache is stale compared to the revision from a quorum read.
Progress requests can be issued using the “progress” command in interactive watch session to ask the etcd server to send a progress notify update in the watch stream:
The revision number in the progress notify response is the revision from the local etcd server node that the watch stream is connected to. If this node is partitioned and not part of quorum, this progress notify revision might be lower than the revision returned by a quorum read against a non-partitioned etcd server node.
Compacted revisions
As we mentioned, etcd keeps revisions so that applications can read past versions of keys. However, to avoid accumulating an unbounded amount of history, it is important to compact past revisions. After compacting, etcd removes historical revisions, releasing resources for future use. All superseded data with revisions before the compacted revision will be unavailable.
Here is the command to compact the revisions:
The current revision of etcd server can be found using get command on any key (existent or non-existent) in json format. Example is shown below for mykey which does not exist in etcd server:
Grant leases
Applications can grant leases for keys from an etcd cluster. When a key is attached to a lease, its lifetime is bound to the lease’s lifetime which in turn is governed by a time-to-live (TTL). Each lease has a minimum time-to-live (TTL) value specified by the application at grant time. The lease’s actual TTL value is at least the minimum TTL and is chosen by the etcd cluster. Once a lease’s TTL elapses, the lease expires and all attached keys are deleted.
Here is the command to grant a lease:
Revoke leases
Applications revoke leases by lease ID. Revoking a lease deletes all of its attached keys.
Suppose we finished the following sequence of operations:
Here is the command to revoke the same lease:
Keep leases alive
Applications can keep a lease alive by refreshing its TTL so it does not expire.
Suppose we finished the following sequence of operations:
Here is the command to keep the same lease alive:
Get lease information
Applications may want to know about lease information, so that they can be renewed or to check if the lease still exists or it has expired. Applications may also want to know the keys to which a particular lease is attached.
Suppose we finished the following sequence of operations:
Here is the command to get information about the lease:
Here is the command to get information about the lease along with the keys attached with the lease: