Consul
Consul KV Get
Command: consul kv get
Corresponding HTTP API Endpoint: [GET] /v1/kv/:key
The kv get
command is used to retrieve the value from Consul's KV
store at the given key name. If no key exists with that name, an error is
returned. If a key exists with that name but has no data, nothing is returned.
A key name or prefix is required.
Note: When reading many entries under a given prefix, it may be worth
considering kv export
instead. The kv export output
can be used with kv import
to move entire trees between
Consul clusters. Alternatively, the transaction API provides
support for performing up to 64 KV operations atomically.
The table below shows this command's required ACLs. Configuration of blocking queries and agent caching are not supported from commands, but may be from the corresponding HTTP endpoint.
ACL Required |
---|
key:read |
Usage
Usage: consul kv get [options] [KEY_OR_PREFIX]
API Options
-ca-file=<value>
- Path to a CA file to use for TLS when communicating with Consul. This can also be specified via theCONSUL_CACERT
environment variable.-ca-path=<value>
- Path to a directory of CA certificates to use for TLS when communicating with Consul. This can also be specified via theCONSUL_CAPATH
environment variable.-client-cert=<value>
- Path to a client cert file to use for TLS whenverify_incoming
is enabled. This can also be specified via theCONSUL_CLIENT_CERT
environment variable.-client-key=<value>
- Path to a client key file to use for TLS whenverify_incoming
is enabled. This can also be specified via theCONSUL_CLIENT_KEY
environment variable.-http-addr=<addr>
- Address of the Consul agent with the port. This can be an IP address or DNS address, but it must include the port. This can also be specified via theCONSUL_HTTP_ADDR
environment variable. In Consul 0.8 and later, the default value is http://127.0.0.1:8500, and https can optionally be used instead. The scheme can also be set to HTTPS by setting the environment variableCONSUL_HTTP_SSL=true
. This may be a unix domain socket usingunix:///path/to/socket
if the agent is configured to listen that way.-tls-server-name=<value>
- The server name to use as the SNI host when connecting via TLS. This can also be specified via theCONSUL_TLS_SERVER_NAME
environment variable.-token=<value>
- ACL token to use in the request. This can also be specified via theCONSUL_HTTP_TOKEN
environment variable. If unspecified, the query will default to the token of the Consul agent at the HTTP address.-token-file=<value>
- File containing the ACL token to use in the request instead of one specified via the-token
argument orCONSUL_HTTP_TOKEN
environment variable. This can also be specified via theCONSUL_HTTP_TOKEN_FILE
environment variable.
-datacenter=<name>
- Name of the datacenter to query. If unspecified, the query will default to the datacenter of the Consul agent at the HTTP address.-stale
- Permit any Consul server (non-leader) to respond to this request. This allows for lower latency and higher throughput, but can result in stale data. This option has no effect on non-read operations. The default value is false.
Enterprise Options
-namespace=<string>
- Specifies the namespace to query. If not provided, the namespace will be inferred from the request's ACL token, or will default to thedefault
namespace. Namespaces are a Consul Enterprise feature added in v1.7.0.
-partition=<string>
- Specifies the partition to query. If not provided, the partition will be inferred from the request's ACL token, or will default to thedefault
partition. Partitions are a Consul Enterprise feature added in v1.11.0.
KV Get Options
-base64
- Base 64 encode the value. The default value is false.-detailed
- Provide additional metadata about the key in addition to the value such as the ModifyIndex and any flags that may have been set on the key. The default value is false.-keys
- List keys which start with the given prefix, but not their values. This is especially useful if you only need the key names themselves. This option is commonly combined with the -separator option. The default value is false.-recurse
- Recursively look at all keys prefixed with the given path. The default value is false.-separator=<string>
- String to use as a separator for recursive lookups. The default value is "/", and only used when paired with the-keys
flag. This will limit the prefix of keys returned, only up to the given separator.
Examples
To retrieve the value for the key named "redis/config/connections" in the KV store:
$ consul kv get redis/config/connections
5
This will return the original raw value stored in Consul.
If the key with the given name does not exist, an error is returned.
$ consul kv get not-a-real-key
Error! No key exists at: not-a-real-key
Detailed Output
To view detailed information about the key, specify the -detailed
flag.
This will output all known metadata about the key including ModifyIndex
and any user-supplied flags:
$ consul kv get -detailed redis/config/connections
CreateIndex 336
Flags 0
Key redis/config/connections
LockIndex 0
ModifyIndex 336
Session -
Value 5
Recursively Reading By Prefix
To treat the path as a prefix and list all entries which start with the given
prefix, specify the -recurse
flag:
$ consul kv get -recurse redis/
redis/config/connections:5
redis/config/cpu:128
redis/config/memory:512
Alternatively, combine with the -detailed
flag to list detailed information
about all entries under a prefix:
$ consul kv get -recurse -detailed redis
CreateIndex 336
Flags 0
Key redis/config/connections
LockIndex 0
ModifyIndex 336
Session -
Value 5
CreateIndex 472
Flags 0
Key redis/config/cpu
LockIndex 0
ModifyIndex 472
Session -
Value 128
CreateIndex 471
Flags 0
Key redis/config/memory
LockIndex 0
ModifyIndex 471
Session -
Value 512
Listing Keys
To just list the keys which start with the specified prefix, use the -keys
option instead. This is more performant and results in a smaller payload:
$ consul kv get -keys redis/config/
redis/config/connections
redis/config/cpu
redis/config/memory
By default, the -keys
operation uses a separator of "/", meaning it will not
recurse beyond that separator. You can choose a different separator by setting
-separator="<string>"
.
$ consul kv get -keys -separator="c" redis
redis/c
Alternatively, you can disable the separator altogether by setting it to the empty string:
$ consul kv get -keys -separator="" redis
redis/config/connections
redis/config/cpu
redis/config/memory
To list all keys at the root, simply omit the prefix parameter:
$ consul kv get -keys
memcached/
redis/