etcd - key value store for distributed systems

17.12.2020Ricky Elfner
Cloud Kubernetes GNU/Linux Daemon Raft Distributed Web Storage

What is etcd?

etcd is a distributed and consistent key-value store. This offers the possibility of a central, fail-safe, fault-tolerant and secure storage location for distributions applications in which information changes again and again. Etcd was developed by the CoreOS team. etcd is written - like many other cloud applications - in Go. Furthermore, the Raft , a consensus algorithm, is used to manage a highly available replicated protocol.

The name is made up of two parts. The first part etc stands for the central directory /etc for all system-wide configuration files within GNU / Linux systems. While the second part, the d, an abbreviation for distributed is.

At the same time, etcd is very well known for its function as the primary data store for Kubernetes. There it is used for backup and replication of Kubernetes clusters. This is one reason why etcd must be very reliable in terms of configuration and management.

key points of etcd

Etcd has the following advantages:

  • simple: because it relies on very well-defined and user-oriented APIs (gRPC), which are based on REST and JSON
  • secure: it is automatically using by default a transmission via SSL / TLS. In addition, there is also the possibility to use client certificate authentication
  • fast: Benchmarks confirm up to 10,000 write operations per second
  • reliable: the raft algorithm always distributes the memory correctly
  • replicable: since the entire memory is available on each node of the cluster, it is fully replicable
  • high availability: individual sources of error are avoided
  • consistent: across multiple hosts, each read process delivers the last write process.

How it works

First, you have to know three important terms in connection with the management of memories, namely

  • Leader (Leader),
  • elections (Elections)
  • time periods (Terms)

At etcd there is always a leader for a certain period of time chosen. He is then responsible for all inquiries that require the cluster’s consent. All changes to saved data require the approval of the leader. However, if there are other inquiries that do not require consent, then these can also be sent and answersed by the other members. This includes, for example, read requests. Once the leader fails to answer because he has failed, new elections will be held immediately and a new leader will be appointed. The new elections are initiated by the timers of the individual nodes. Because that is how long it will be lasts until a new election is requested. With this requirement, the node also makes itself available as a candidate disposal. These times can differ depending on the node. This principle can guarantee that there is no interruption of the defined process.

Once the request is accepted by the leader, etcd ensures that the information about the Leader will be replicated to subsequent accounts. If they confirm, the leader can also do the socalled apply changes. Due to the raft algorithm, this change can only be made through the survey and majority of the knots. Individual nodes can cause stability and refusal protect functionality and avoid subsequent problems. To maintain this principle, one should look at the planning to make sure that the number of nodes is always odd and additionally the number of 7 pieces is not exceeds.

etcd and Kubernetes

etcd has been used at Kubernetes since 2014. There the etcd database serves as storage for configuration files, the status and metadata. Etcd ensures that everyone from the cluster can access the data to read and write. The status of the entire system is also monitored. Should the desired state differentiate with the current state, Kubernetes will make changes to fix this.

Basic commands of etcd

In the following part, you can see how the basic commands are carried out. For this you need an etcd server and a client. Use the command etcdctl to access the server from the client.

Adding a value

You need the following command to add a value to your key value store.

1
2
$ etcdctl put myKey myValue
> OK

If the command was correct and processed, you will receive a OK as a response.

Read out a key

Afterwards, of course, you want to check whether this value was added correctly. That’s where you get the key, as well as the corresponding value back. But if you only want the value, there is the option --print-value-only.

1
2
3
$ etcdctl get myKey
> myKey
> myValue
1
2
$ etcdctl get myKey --print-value-only
> myValue

Of course, there is also the option of reading out several keys at once. To demonstrate this, there are already the following key-value pairs which should be read out:

1
2
3
4
myKey1 = myValue1
myKey2 = myValue2
myKey3 = myValue3
myKey4 = myValue4

Here, too, you can only read out the values, if this is preferred. You must note that myValue4 is not read out. Instead, keys until the Value 4 key are read out.

1
2
3
4
$ etcdctl get myKey myKey4 --print-value-only
> myValue1
> myValue2
> myValue3

There is of course the option of working with a prefix with which you can read out all of our examples.

1
$ etcdctl get --prefix myKey

Delete a key

When deleting, you also have the option of deleting individual key-value pairs or a specific series. After this You will get back the number of deleted pairs after you have executed the command.

1
2
$ etcdctl del myKey
> 1

If you want to be sure that you have deleted the correct data, you can also use commands to output this --prev-kv.

1
2
3
4
$ etcdctl del --prev-kv myKey2 
> 1
> myKey2
> myValue2

Now you can also delete the last two pairs.

1
2
$ etcdctl del --prefix myKey
> 2

monitoring

Using etcd you can also monitor individual keys to see whether they have changed. To recreate this, you can enter the command to monitor into a terminal.

1
$ etcdctl watch myKey1

Now switch to a second terminal window and change the value of the key myKey1.

1
$ etcdctl put myKey1 myValue1

As a result of this change, you will receive a notification of a change in the first terminal window.

1
2
3
> PUT
> myKey1
> myValue2`

Of course, you can not only monitor individual values, but also a whole series.

1
$ etcdctl watch --prefix myKey

Another advantage of etcd is that you can also track older changes.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$ etcdctl watch --rev=2 myKey1
> PUT
> myKey1
> myValue1
> DELETE
> myKey1

> PUT
> myKey1
> myValue1

This text was automatically translated with our golang markdown translator.

Ricky Elfner

Ricky Elfner – Denker, Überlebenskünstler, Gadget-Sammler. Dabei ist er immer auf der Suche nach neuen Innovationen, sowie Tech News, um immer über aktuelle Themen schreiben zu können.