• HashiCorp Developer

  • HashiCorp Cloud Platform
  • Terraform
  • Packer
  • Consul
  • Vault
  • Boundary
  • Nomad
  • Waypoint
  • Vagrant
Consul
  • Install
  • Tutorials
  • Documentation
  • API
  • CLI
  • Try Cloud(opens in new tab)
  • Sign up
Interactive Labs

Skip to main content
8 tutorials
  • Store Data in Consul KV
  • Secure Consul Agent Communication with TLS Encryption
  • Secure Consul with Access Control Lists (ACLs)
  • Administer Consul Access Control Tokens with Vault
  • Automatically Rotate Gossip Encryption Keys Secured in Vault
  • Load Balancing Services in Consul Service Mesh with Envoy
  • Application Aware Intentions with Consul Service Mesh
  • Secure Service Communication with Consul Service Mesh and Envoy

  • Resources

  • Tutorial Library
  • Certifications
  • Community Forum
    (opens in new tab)
  • Support
    (opens in new tab)
  • GitHub
    (opens in new tab)
  1. Developer
  2. Consul
  3. Tutorials
  4. Interactive Labs
  5. Store Data in Consul KV

Store Data in Consul KV

  • 7min

  • ConsulConsul
  • VideoVideo
  • InteractiveInteractive

In addition to providing service discovery, integrated health checking, and securing network traffic, Consul includes a key value store, which you can use to dynamically configure applications, coordinate services, manage leader election, or serve as a data backend for Vault, along with a myriad of other uses.

In this tutorial, you will explore the Consul key value store (Consul KV) using the command line. The tutorial assumes that the Consul agent from the Run the Consul Agent tutorial is still running. If not you can start a new one by running consul agent -dev. Consul KV is enabled automatically on Consul agents; you don't need to enable it in Consul's configuration.

There are two ways to interact with the Consul KV store: the Consul CLI and UI. In this tutorial, you will use the CLI.

Launch Terminal

This tutorial includes a free interactive command-line lab that lets you follow along on actual cloud infrastructure.

Add data

First, insert or "put" some values into the KV store with the consul kv put command. The first entry after the command is the key, and the second entry is the value.

$ consul kv put redis/config/minconns 1
Success! Data written to: redis/config/minconns

Here the key is redis/config/maxconns and the value is set to 25.

$ consul kv put redis/config/maxconns 25
Success! Data written to: redis/config/maxconns

Notice that with the key entered below ("redis/config/users/admin"), you set a flags value of 42. Keys support setting a 64-bit integer flag value that isn't used internally by Consul, but can be used by clients to add metadata to any KV pair.

$ consul kv put -flags=42 redis/config/users/admin abcd1234
Success! Data written to: redis/config/users/admin

Query data

Now, query for the value of one of the keys you just entered.

$ consul kv get redis/config/minconns
1

Consul retains some additional metadata about the key-value pair. Retrieve the some metadata (including the "flag" you set) using the -detailed command line flag.

$ consul kv get -detailed redis/config/users/admin
CreateIndex      14
Flags            42
Key              redis/config/users/admin
LockIndex        0
ModifyIndex      14
Session          -
Value            abcd1234

List all the keys in the store using the recurse options. Results are returned in lexicographical order.

$ consul kv get -recurse
redis/config/maxconns:25
redis/config/minconns:1
redis/config/users/admin:abcd1234

Delete data

Delete a key from the Consul KV store, issue a "delete" call.

$ consul kv delete redis/config/minconns
Success! Deleted key: redis/config/minconns

Consul lets you interact with keys in a folder-like way. Although all the keys in the KV store are actually stored flat, Consul allows you to manipulate keys that share a certain prefix as a group, as if they were in folders or subfolders.

Delete all the keys with the redis prefix using the recurse option.

$ consul kv delete -recurse redis
Success! Deleted keys with prefix: redis

Modify existing data

Update the value of an existing key.

$ consul kv put foo bar
Success! Data written to: foo

Get the updated key.

$ consul kv get foo
bar

Put the new value at an extant "path".

$ consul kv put foo zip
Success! Data written to: foo

Check the updated path.

$ consul kv get foo
zip

Next steps

In this tutorial, you added, viewed, modified, and deleted entries in Consul's key value store.

Consul can perform atomic key updates using a Check-And-Set (CAS) operation, and includes other sophisticated options for writing keys and values. You can explore these options on the help page for the consul kv put command.

$ consul kv put -h

These are only a few examples of what the API supports. For the complete documentation, please see the HTTP API documentation and CLI documentation.

Now that Consul contains some interesting data including service registrations, keys, values, and intentions, continue to the Explore the Consul UI tutorial to locate all this data in the Consul web UI.

 Back to Collection
 Next

This tutorial also appears in:

  •  
    30 tutorials
    Associate Tutorial List
    Study for the Consul Associate exam by following these tutorials. Login to Learn and bookmark them to track your progress. Study the complete list of study materials (including docs) in the Certification Prep guides.
    • Consul

On this page

  1. Store Data in Consul KV
  2. Add data
  3. Query data
  4. Delete data
  5. Modify existing data
  6. Next steps
Give Feedback(opens in new tab)
  • Certifications
  • System Status
  • Terms of Use
  • Security
  • Privacy
  • Trademark Policy
  • Trade Controls
  • Give Feedback(opens in new tab)