Consul by HashiCorp is a versatile tool that serves multiple functions in a modern DevOps environment. It’s widely used for service discovery, health checks, load balancing, and, notably, as a distributed key-value (KV) store. The KV store in Consul is perfect for storing dynamic configuration data, feature flags, secrets, and metadata in a highly available, consistent manner across your infrastructure such that it can be dynamically accessed by services in a distributed system. Using Docker to configure Consul’s KV store allows for quick setup and isolated environments, making it ideal for testing and development.
This tutorial will walk you through the process of setting up and configuring Consul’s KV store using Docker. By the end, you will have a fully functional Consul instance running in Docker, with KV pairs configured and accessible. This setup is essential for dynamic service configuration and state management in distributed systems.
Before you begin, ensure you have the following:
Let’s pull the official Consul image from Docker Hub. This image is maintained by HashiCorp and includes everything you need to run Consul.
Log in to your Ubuntu Droplet’s console and run:
Now that the Consul image is downloaded, you can start a new Consul container. This container will serve as your Consul server and will allow you to interact with the KV store.
To start the container, run:
Here’s what this command does:
-d
runs the container in detached mode (in the background).--name=consul-server
assigns a name to the container.-e CONSUL_BIND_INTERFACE=eth0
sets the network interface that Consul should bind to. This is necessary for proper network communication.-p 8500:8500
maps the Consul web UI and API port to the host.-p 8600:8600/udp
maps the DNS service port for service discovery.This step is crucial as it sets up the core Consul service, which you will use to configure the KV store.
To ensure that Consul is running correctly, you need to verify the container status and access the Consul UI.
First, run docker ps
to list all running containers and verify that the Consul container is running.
Now, check if the Consul is accessible, open a web browser, and navigate to http://localhost:8500
. You should see the Consul UI.
This verification step is important to confirm that your Consul instance is running without any issues before storing data in the KV store (Step 5).
If your Consul instance needs to be accessed externally (e.g., from other nodes in a cluster), you must adjust your firewall settings to allow traffic on the necessary ports.
For example, if you’re running Consul on a cloud instance, you may need to allow inbound traffic on ports 8500 (HTTP API) and 8600 (DNS). The specific commands will vary based on your firewall solution (UFW, iptables, etc.).
This step ensures that your Consul instance is accessible from other machines, which is essential for distributed configurations.
With Consul running, you can now use the KV store to store configuration data. You can add key-value pairs using the Consul CLI or the web UI.
To store a key-value pair via the CLI, run:
Here’s what this command does:
-it
- Launches the interactive terminal from the local system to the container.consul kv put
- kv put command writes the data to the given path KV store.config/db_host
- path to store the value.192.168.1.100
- Value.Using the Web UI,
http://localhost:8500
).config/db_host
) and value (e.g., 192.168.1.100
).These commands and actions store critical configuration data that your services can access dynamically at runtime.
Once you’ve stored some KV pairs, you’ll want to retrieve them to ensure they’ve been stored correctly.
Using the CLI, retrieve a value using the following command:
Using the Web UI,
Retrieving the KV pairs is a necessary step to verify that your data is correctly stored and accessible.
By default, Docker containers are ephemeral, meaning that any data stored inside them will be lost if the container is removed. To persist your Consul KV data, you should use Docker volumes.
Now, check the containers and you should notice Consul container not running anymore.
2.Run a new Consul container with a Docker volume attached:
The -v consul_data:/consul/data
option mounts a Docker volume to the container, ensuring that your KV store persists across container restarts.
For production deployments, you might want to automate the startup of your Consul container using Docker Compose. Docker Compose simplifies multi-container Docker applications and makes it easy to manage services.
Create a docker-compose.yml
file with the following content:
Then, run:
This command starts Consul automatically and ensures it restarts if it fails, making it more robust for production use.
Once you’ve finished working with your Consul instance, you should clean up your Docker environment to free up resources.
Let’s stop and remove the Consul container:
If you’re done with Consul, you can also remove the Docker image:
Cleaning up helps maintain a tidy development environment and ensures that Docker resources are not unnecessarily consumed.
In this tutorial, you learned how to set up and configure Consul’s KV store using Docker. You’ve covered the installation of Docker, running the Consul container, configuring the KV store, persisting data with Docker volumes, and cleaning up your environment. With these steps, you can now use Consul to dynamically manage configuration data in your distributed systems, leveraging the power of Docker for easy deployment and management.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!