etcd is a strongly consistent, distributed key-value store that provides a reliable way to store data that needs to be accessed by a distributed system or cluster of machines. It is an open-source project maintained by CNCF. Most notably, it manages the configuration data, state data, and metadata for Kubernetes, the popular container orchestration platform.
etcd has its roots with the developers at CoreOS. The name comes from the idea of distributing the Unix "/etc" directory, where many configuration files live, across multiple machines – /etc distributed thus as etcd. It uses a consensus algorithm called RAFT. It is the ‘go-to’ algorithm that developers use to write systems that agree on the state of values. In etcd's case, those values represent a hierarchical keyspace with directories and keys.
Features of ETCD
- Simple Interface: Values can be read and written using standard HTTP tools such as curl.
- Key-value storage: Data can be stored in hierarchically organized directories as a standard filesystem.
- Watch for changes: Watch specific keys or directories for changes and react to changes in values.
- Provide SSL client certificate authentication and TTLs for key expiration.
- Provides etcdctl, a command-line tool to interact with etcd, the server which is written in Go programming language.
Further, we will be doing hands-on with ETCD, for this firstly install etcd and then see its basic implementation to learn more about it and after this, we will be seeing on how to set up a local 3-node etcd cluster.
- To get started with etcd, first, let’s install etcd by running the script file.
- Check the etcd and etcdctl version.
Now we will be seeing how to interact with the etcd server by performing some basic operations on it.
- Start the etcd server
Interact with the etcd server through the etcdctl command-line tool.
Basic Interaction with ETCD
Here, we will be seeing how to interact with the etcd server with these different commands.
Create a key
etcdctl put key1 bar1
This will make a new key-pair with key as key1 and value as a bar1
etcdctl put key2 bar2
This will make a new key-pair with key as key2 and value as a bar2
etcdctl put color blue
This will make another key-pair with key as color and value as blue
etcdctl get key1
This will give the key-value pair of key key1
etcdctl get key1 --hex
This will return the key-value pair in hexadecimal format.
etcdctl get key1 --print-value-only
This will only print the value of key1
Delete a key
etcdctl del color
This will delete the specified key i.e. color and return the number of keys deleted.
etcdctl get --prefix key
This will return all the keys which have prefixes as key along with their values like key1-bar1 and key2-bar2 key-value pair.
Putting lease on key
etcdctl lease grant 100
This returns a
LEASE_ID (alphanumeric) which will be used to put a lease(timer) on a key for 100 seconds.
This is generally used to manage temporary keys, for example, to keep a watch on Kubernetes events.
Stop the ETCD Server
- Now stop the etcd server through
Setting up a local ETCD cluster
To set up a three-node local etcd cluster, first, install a goreman which manages procfile-based applications.
Procfile is used for declaring a set of commands that can be run by an application at the start and is currently present at the root directory.
- Install goreman with the below command
tar xzvf goreman_v0.3.7_linux_amd64.tar.gz
mv goreman_v0.3.7_linux_amd64/goreman /usr/local/bin
- And its installation can be checked by executing a
Interacting with local cluster via goreman
- After installing this generate a Procfile that has all the cluster details currently present in the root directory.
On observing you can see that there are three processes as etcd1 , etcd2 , etcd3 and each one of them have given names as node1, node2 , node3 respectively with their respective URLs and port numbers.
Along with it, the nodes have also information about other nodes for peer-to-peer communication.
- Now, run the goreman by running a command
- Now, print the nodes of the cluster in form of a table
etcdctl --write-out=table --endpoints=localhost:2379 member list
Fault tolerance testing to check the working of nodes in the cluster.
- Identify the process name of the member to be stopped. For example, consider the member with the process name, etcd2.
Stop the member:
goreman run stop etcd2
Store a key
etcdctl put location earth
Retrieve the key that is stored in the previous step:
etcdctl get location
Retrieve a key from the stopped member:
etcdctl --endpoints=localhost:22379 get location
Restart the stopped member:
goreman run restart etcd2
Get the key from the restarted member:
etcdctl --endpoints=localhost:22379 get location
And to know about how to configure etcd cluster with TLS certificate, click here
In this hands-on lab, we have learned about etcd with its basic commands and learned how to set up a three-node local etcd cluster and interact with it.