“This is the 17th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”

Etcd single-node deployment

CentOS 7 etcd install CentOS 7 etcd yum 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.

Binary installation

The latest ETCD API version is V3.4, and we practice based on 3.4.4. The API version is the same as the latest version. 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 versionCopy the code

Etcd download may be slow. After the etCD is executed, the following result is displayed:

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

As you can see from the above results, we have successfully installed on Linux, and the binary installation of macOS is similar, so we will not repeat the demonstration here. The installation of Windows system is relatively simple. After downloading the installation package, execute it directly. Where etcd.exe is the server and etcdctl.exe is the client, as shown below:

The source code to install

To use source installation, first you need to make sure you have a local Go locale. If no, see golang.org/doc/install to install the Go language environment. We need Go version 1.13+ to build the latest version of ETCD. If you want to try the latest version, you can also build etCD from the Master branch.

First check out our local version of Go:

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

The local Go version meets the requirements. Clone the created version of the ETCD project to local, build the ETCD in the project folder. Once built, execute the test command to make sure etcd compiled and installed successfully:

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

After the above steps, we have successfully installed ETCD through source compilation.

Alternatively, you can install using a Docker container, which is simpler, but has one drawback: etCD ports are exposed on startup.

Etcd cluster installation and deployment

We have just described a stand-alone installation of ETCD, but in a real production environment, etCD is often deployed in a cluster to avoid a single point of failure for high availability of the entire cluster. Let’s take a look at etCD cluster deployment.

There are three ways to boot the ETCD cluster:

  • Static starting
  • Etcd dynamic discovery
  • DNS found

Statically starting the ETCD cluster requires that each member know about the other members of the cluster. However, in many scenarios, the IP addresses of cluster members may not be known. Therefore, you need to boot the ETCD cluster with a discovery service. Each of these approaches will be described below.

Start the ETCD cluster statically

If we want to practice etCD cluster building on a single machine, we can use the Goreman tool.

Goreman is a multi-process management tool written in the Go language, which is a rewrite of the widely used Ruby Foreman. Let’s use Goreman to demonstrate statically starting an ETCD cluster on a physical machine.

Previously we have confirmed the installation environment of Go language, now we can 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.

To write the Procfile script, we start three etcds as follows:

Infra1 in the Procfile script starts 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 - the state new --enable-pprof --logger=zap --log-outputs=stderrCopy the code

Infra2 and Infra3 have similar start orders. Let’s take a look at the description of each configuration item.

  • –name: specifies the node name in the etcd cluster.
  • Listen-client-urls: Listen for urls used for client communication, and also listen for multiple urls.
  • –advertise-client-urls: Recommended client communication URL for etCD agents or ETCD members to communicate with ETCD nodes.
  • Listen-peer-urls: Listen for urls used to communicate between nodes. You can listen for multiple urls through which data interactions (such as elections, data synchronization, etc.) will occur within the cluster.
  • — Initial-advertise-peer-urls: Recommended URL for communication between nodes, which will communicate with each other.
  • — Initial-cluster-token: etcd-cluster-1: token value of a node. After this value is set, the cluster will generate a unique ID and a unique ID for each node. When another cluster is started using the same configuration file, the ETCD clusters do not affect each other as long as the token values are 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 with the goreman command:

goreman -f /opt/procfile start
Copy the code

After the startup is complete, view the members in 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, False fd422379fda50e48, started, infra3, http://127.0.0.1:32380, http://127.0.0.1:32379, falseCopy the code

Now our ETCD cluster has been set up successfully. Note that when the cluster is started, we specify the members of the cluster statically. However, the IP addresses of cluster members may not be known in advance. In this case, dynamic discovery is required.