0 Album Overview

Etcd is an important foundation component in the cloud native architecture and is incubated and hosted by CNCF. Etcd can be used not only as service registration and discovery, but also as middleware for key-value storage in microservices and Kubernates clusters.

“Thoroughly Understand ETCD series of articles” will be from the basic function of ETCD practice, API interface, implementation principle, source code analysis, as well as the implementation of the pit experience and other aspects of the specific introduction of ETCD. It is expected that there will be about 20 articles, I will continue to update every week, welcome to pay attention to.

1 EtCD installation and use

In the previous chapter, we introduced the concepts and usage scenarios of ETCD. This chapter mainly introduces various installation and use methods of ETCD.

1.1 ETCD Concept vocabulary

Before we start the etCD installation, let’s look at the etCD concept vocabulary to make it easier to understand.

  • Raft: An algorithm used by ETCD to ensure strong consistency in distributed systems.
  • Node: A Raft state machine instance.
  • Member: an etCD instance. It manages a Node and can service client requests.
  • Cluster: An ETCD Cluster consisting of multiple members that can work together.
  • Peer: indicates the name of another Member in the same ETCD cluster.
  • Client: the Client that sends HTTP requests to the ETCD cluster.
  • WAL: write-ahead log format. Etcd is used for persistent storage.
  • Snapshot: etcd A snapshot created to prevent excessive WAL files and store ETCD data status.
  • Proxy: An ETCD mode that provides reverse Proxy services for ETCD clusters.
  • Leader: The node created by campaigning in the Raft algorithm to process all data commits.
  • Follower: The failed node serves as a subordinate node in Raft to ensure the consistency of the algorithm.
  • Candidate: When the Follower cannot receive the heartbeat from the Leader for a certain period of time, the Follower becomes the Candidate and starts the campaign.
  • Term: a node becomes the Leader until the next election, called a Term.
  • Index: indicates the number of a data item. Term and Index are used to locate data in Raft.

2 EtCD single-node installation and deployment

Centos 7 and MacOS 10.15 are used as examples. You can install etcd using yum install etcd and brew install etcd.

However, the etCD version installed by system tools is lagging behind. If we need to install the latest version of ETCD, we can install it by binary package, source code compilation and Docker container.

2.1 Binary Installation

The latest version of ETCD is 3.4.4. On macOS, execute the following script:

ETCD_VER = v3.4.4 GITHUB_URL=https://github.com/etcd-io/etcd/releases/download DOWNLOAD_URL =${GITHUB_URL}

rm -f /tmp/etcd-${ETCD_VER}-darwin-amd64.zip
rm -rf /tmp/etcd-download-test && mkdir -p /tmp/etcd-download-test

curl -L ${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-darwin-amd64.zip -o /tmp/etcd-${ETCD_VER}-darwin-amd64.zip
unzip /tmp/etcd-${ETCD_VER}-darwin-amd64.zip -d /tmp && rm -f /tmp/etcd-${ETCD_VER}-darwin-amd64.zip
mv /tmp/etcd-${ETCD_VER}-darwin-amd64/* /tmp/etcd-download-test && rm -rf mv /tmp/etcd-${ETCD_VER}-darwin-amd64

/tmp/etcd-download-test/etcd --version
/tmp/etcd-download-test/etcdctl version
Copy the code

The following output is displayed:

Etcd Version: 3.4.4 Git SHA: c65a9e2dd Go Version: go1.12.12 Go OS/Arch: Darwin /amd64Copy the code

As you can see, we have successfully wrapped ETCD 3.4.4, which is based on Go version 1.12.12.

Similarly, we use the following script to install Centos 7:

ETCD_VER = v3.4.4 GITHUB_URL=https://github.com/etcd-io/etcd/releases/download DOWNLOAD_URL =${GITHUB_URL}

rm -f /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
rm -rf /tmp/etcd-download-test && mkdir -p /tmp/etcd-download-test

curl -L ${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz -o /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
tar xzvf /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz -C /tmp/etcd-download-test --strip-components=1
rm -f /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz

/tmp/etcd-download-test/etcd --version
/tmp/etcd-download-test/etcdctl version
Copy the code

The download may be slow, and after execution, the result is as follows:

Etcd Version: 3.4.4 Git SHA: e784ba73c Go Version: go1.12.12 Go OS/Arch: Linux/AMD64Copy the code

In this way, we have successfully installed macOS and Linux. The installation of Windows system is relatively simple. After downloading the installation package, we can directly execute it. Etcd. exe is the server and etcdctl.exe is the client.

2.2 Installing Source Code

To use source installation, you first need to ensure that you have a local Go locale. If not installed, please refer to golang.org/doc/install…

For those who want to try the latest version, etCD can be built from the Master branch. Go version 1.13+ is required to build the latest version of ETCD. The local Go version is:

$go Version $Go Version go1.13.6 Linux/AMD64Copy the code

Build etCD based on the master branch with the following script:

git clone https://github.com/etcd-io/etcd.git
cd etcd
./build
Copy the code

Execute the test command to make sure etCD is successfully compiled and installed:

$./etcdctl version Etcdctl version: 3.4.4 API version: 3.4Copy the code

After the above steps, etCD has been successfully installed through source compilation.

2.3 Docker Container Installation

IO/ETCd-development/ETCD as the container repository source, while quay. IO/CoreOS/ETCD as the secondary container registry.

REGISTRY=quay.io/coreos/etcd
# available from v3.2.5REGISTRY=gcr.io/etcd-development/etcd rm -rf /tmp/etcd-data.tmp && mkdir -p /tmp/etcd-data.tmp && \ docker rmi GCR. IO/etcd - development/etcd: v3.4.7 | |true && \
  docker run \
  -p 2379:2379 \
  -p 2380:2380 \
  --mount type=bind.source/ TMP /etcd-data. TMP,destination=/etcd-data \ --name etcd-gcr-v3.4.7 \ gcr. IO /etcd-development/etcd:v3.4.7 \ /usr/local/bin/etcd \
  --name s1 \
  --data-dir /etcd-data \
  --listen-client-urls http://0.0.0.0:2379 \
  --advertise-client-urls http://0.0.0.0:2379 \
  --listen-peer-urls http://0.0.0.0:2380 \
  --initial-advertise-peer-urls http://0.0.0.0:2380 \
  --initial-cluster s1=http://0.0.0.0:2380 \
  --initial-cluster-token tkn \
  --initial-cluster-state new \
  --log-level info \
  --logger zap \
  --log-outputs stderr

docker execEtcd - GCR - v3.4.7 / bin/sh -c"/usr/local/bin/etcd --version"
docker execEtcd - GCR - v3.4.7 / bin/sh -c"/usr/local/bin/etcdctl version"
docker execEtcd - GCR - v3.4.7 / bin/sh -c"/usr/local/bin/etcdctl endpoint health"
docker execEtcd - GCR - v3.4.7 / bin/sh -c"/usr/local/bin/etcdctl put foo bar"
docker execEtcd - GCR - v3.4.7 / bin/sh -c"/usr/local/bin/etcdctl get foo"
Copy the code

Run the following command to verify the status of the container installed ETCD:

$etcdctl -- endPoints =http://localhost:2379 version etcdctl version: 3.4.4 API version: 3.4Copy the code

3. Etcd cluster deployment

In a production environment, etCD is normally deployed in a cluster to ensure high availability of the entire cluster, avoiding a single point of failure. This section describes how to deploy an ETCD cluster.

3.1 Run an ETCD cluster locally

If you want to practice setting up etCD clusters on a single machine, you can use the Goreman tool.

Goreman is a multi-process management tool written in the Go language. It is a rewrite of the widely used Ruby Version of Foreman (foreman also implemented a Go version, forego, which is not as useful as Goreman).

We have confirmed the Go installation environment before, and directly execute:

go get github.com/mattn/goreman
Copy the code

The compiled file is stored in $GOPATH/bin. The $GOPATH/bin directory has been added to the system $PATH, so we can easily execute the goreman command. Here is the Procfile script. We start three etcds as follows:

HostName ip Client interaction port Peer communication port
infra1 127.0.0.1 12379 12380
infra2 127.0.0.1 22379 22380
infra3 127.0.0.1 32379 32380

The Procfile script is as follows:

etcd1: etcd --name infra1 --listen-client-urls http://127.0.0.1:12379 --advertise-client-urls http://127.0.0.1:12379 --listen-peer-urls http://127.0.0.1:12380 --initial-advertise-peer-urls http://127.0.0.1:12380 --initial-cluster-token etcd-cluster-1 --initial-cluster 'infra1 = http://127.0.0.1:12380, infra2 = http://127.0.0.1:22380, infra3 = http://127.0.0.1:32380' --initial-cluster-state new --enable-pprof --logger=zap --log-outputs=stderr
etcd2: etcd --name infra2 --listen-client-urls http://127.0.0.1:22379 --advertise-client-urls http://127.0.0.1:22379 --listen-peer-urls http://127.0.0.1:22380 --initial-advertise-peer-urls http://127.0.0.1:22380 --initial-cluster-token etcd-cluster-1 --initial-cluster 'infra1 = http://127.0.0.1:12380, infra2 = http://127.0.0.1:22380, infra3 = http://127.0.0.1:32380' --initial-cluster-state new --enable-pprof --logger=zap --log-outputs=stderr
etcd3: etcd --name infra3 --listen-client-urls http://127.0.0.1:32379 --advertise-client-urls http://127.0.0.1:32379 --listen-peer-urls http://127.0.0.1:32380 --initial-advertise-peer-urls http://127.0.0.1:32380 --initial-cluster-token etcd-cluster-1 --initial-cluster 'infra1 = http://127.0.0.1:12380, infra2 = http://127.0.0.1:22380, infra3 = http://127.0.0.1:32380' --initial-cluster-state new --enable-pprof --logger=zap --log-outputs=stderr
Copy the code

Description of configuration items:

  • –name: the name of the node in the etcd cluster, which can be distinguished and not repeated
  • Listen-peer-urls: listening urls that are used to communicate between nodes, multiple urls that will be used for data interaction within the cluster (such as elections, data synchronization, etc.)
  • — Initial-advertise-peer-urls: Recommended URL for communication between nodes, which will communicate with each other.
  • Listen-client-urls: Listening urls used for client communication, also listening for more than one.
  • –advertise-client-urls: Recommended client communication URL for etCD agents or ETCD members to communicate with ETCD nodes.
  • – initial cluster – token: Etcd-cluster-1 is the token value of a node. After the token value is set, the cluster will generate a unique ID and a unique ID for each node. When the same configuration file is used to start another cluster, the ETcd clusters will not affect each other as long as the token value is different.
  • Initial-cluster: a collection of all initial-advertise-peer-urls in a cluster.
  • — Initial-cluster-state: new: indicates the flag of a new cluster

Note In the preceding script, you need to configure the etcd command based on the actual local installation address. Let’s start the ETCD cluster.

goreman -f /opt/procfile start
Copy the code

Use the preceding command to start the ETCD cluster and view the members of the cluster.

$etcdctl --endpoints=http://localhost:22379 member list 8211f1d0f64f3269, started, infra1, http://127.0.0.1:12380, http://127.0.0.1:12379,false
91bc3c398fb3c146, started, infra2, http://127.0.0.1:22380, http://127.0.0.1:22379, falseFd422379fda50e48, started, infra3, http://127.0.0.1:32380, http://127.0.0.1:32379,false
Copy the code

We successfully set up the pseudo cluster in a single machine. It should be noted that when the cluster is started, we specify the members of the cluster statically. In the actual environment, the IP addresses of the cluster members may not be known in advance. This is where dynamic discovery is needed.

4. Summary

This chapter is relatively simple, inclined to practice, readers had better be able to do it themselves. This paper mainly introduces various installation methods of ETCD: ETCD single machine and ETCD cluster. The installation of etCD clusters, along with the dynamic mechanism of service discovery, will be explained in the next article.

Subscribe to the latest articles, welcome to follow my official account

Recommended reading

  1. Comparison between ETCD and other K-V components such as Zookeeper and Consul
  2. Thoroughly understand ETCD series article (1) : first know ETCD

reference

etcd docs