ETCD installation

Installed directly

Take the MAC system as an example, to describe two methods according to the first is very simple, is the MAC:

Install Homebrew. If you do not install Homebrew, you can view the installation package by using the following command

brew search etcd

# installation

brew install etcd

# check version

etcd --version

--enable-v2=true --enable-v2=true

etcd --enable-v2=true

Copy the code

However, the installation may fail in this way. When this fails, I will be reminded that the log does not have permission at present. Just execute the relevant command as prompted.

sudo chown -R $(whoami) /usr/local/var/log

Copy the code

The source code to install

I personally recommend the following installation method:

ETCD_VER = v3.4.14



# choose either URL

GOOGLE_URL=https://storage.googleapis.com/etcd

GITHUB_URL=https://github.com/etcd-io/etcd/releases/download

DOWNLOAD_URL=${GOOGLE_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



Print the ETCD version

/tmp/etcd-download-test/etcd --version

/tmp/etcd-download-test/etcdctl version



Copy etcd and etcdctl to bin directory

cp /tmp/etcd-download-test/etcd /usr/local/bin

cp /tmp/etcd-download-test/etcdctl /usr/local/bin

Then execute:

# installation etcd

sh etcd_install.sh

# check version

etcd --version

--enable-v2=true --enable-v2=true

etcd --enable-v2=true

Copy the code

Cluster deployment

Deployment process

The first script, which does not support pasting blocks outside Docs, reads as follows: https://www.cnblogs.com/linuxws/p/11194403.html) :

TOKEN=token-01

CLUSTER_STATE=new

NAME_1=etcd-01

NAME_2=etcd-02

NAME_3=etcd-03

HOST_1 = 127.0.0.1

HOST_2 = 127.0.0.1

HOST_3 = 127.0.0.1

PORT_API_1=2379

PORT_PEER_1=2380

PORT_API_2=2479

PORT_PEER_2=2480

PORT_API_3=2579

PORT_PEER_3=2580



CLUSTER=${NAME_1}=http://${HOST_1}:${PORT_PEER_1}.${NAME_2}=http://${HOST_2}:${PORT_PEER_2}.${NAME_3}=http://${HOST_3}:${PORT_PEER_3}



# For every machine

THIS_NAME=${NAME_1}

THIS_IP=${HOST_1}

THIS_PORT_API=${PORT_API_1}

THIS_PORT_PEER=${PORT_PEER_1}

# Used to kill processes

lsof -i:2379 | awk '{print $2}' | grep -v "PID" | uniq | xargs kill9 -



# --enable-v2 Supports the v2 interface and can be omitted

# --data-dir Data storage directory, can be omitted

# --name Specifies the node name

--initial-advertise-peer-urls Specifies the URL for data to interact with within the cluster

Listen-peer-urls Specifies the listening URL for communication between cluster nodes

# --advertise-client-urls This address is used by the client to communicate with this member

Listen for the url requested by the client

# --initial-cluster Specifies the initial cluster configuration

--initial-cluster-state Initializes the cluster state. The value can be new or existing

# --initial-cluster-token The cluster initializes the token and can be omitted

etcd --enable-v2=true --data-dir=data.${THIS_NAME} --name ${THIS_NAME} \

        --initial-advertise-peer-urls http://${THIS_IP}:${THIS_PORT_PEER} --listen-peer-urls http://${THIS_IP}:${THIS_PORT_PEER} \

        --advertise-client-urls http://${THIS_IP}:${THIS_PORT_API} --listen-client-urls http://${THIS_IP}:${THIS_PORT_API} \

        --initial-cluster ${CLUSTER} \

        --initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN}

Copy the code

The second script needs to replace the contents with the following:

# For every machine

THIS_NAME=${NAME_2}

THIS_IP=${HOST_2}

THIS_PORT_API=${PORT_API_2}

THIS_PORT_PEER=${PORT_PEER_2}

# Used to kill processes

lsof -i:2479 | awk '{print $2}' | grep -v "PID" | uniq | xargs kill9 -

Copy the code

The third script needs to be replaced with the following:

# For every machine

THIS_NAME=${NAME_3}

THIS_IP=${HOST_3}

THIS_PORT_API=${PORT_API_3}

THIS_PORT_PEER=${PORT_PEER_3}

# Used to kill processes

lsof -i:2579 | awk '{print $2}' | grep -v "PID" | uniq | xargs kill9 -

Copy the code

With these 3 scripts, 3 Windows were opened and executed respectively. The screenshot of service startup is as follows:


After all three scripts are started and the cluster deployment is complete, we check the health status of the three nodes:

The curl http://127.0.0.1:2379/health

The curl http://127.0.0.1:2479/health

The curl http://127.0.0.1:2579/health

Copy the code

If {“health”:”true”} is returned, the deployment is successful.

The curl http://127.0.0.1:2379/v2/members

Copy the code

The result is as follows, where peerURLs is the URL that the nodes communicate with each other and clientURLs is the URL that they access externally:

{

    "members": [

        {

            "id":"264ae6bc59e99892".

            "name":"etcd-01".

            "peerURLs": [

                "http://127.0.0.1:2380"

].

            "clientURLs": [

                "http://127.0.0.1:2379"

            ]

        },

        {

            "id":"dbafe5ad6b652eda".

            "name":"etcd-02".

            "peerURLs": [

                "http://127.0.0.1:2480"

].

            "clientURLs": [

                "http://127.0.0.1:2479"

            ]

        },

        {

            "id":"f570ae41f524bdcb".

            "name":"etcd-03".

            "peerURLs": [

                "http://127.0.0.1:2580"

].

            "clientURLs": [

                "http://127.0.0.1:2579"

            ]

        }

    ]

}

Copy the code

Have a problem

Question 1: service starts, cannot use v2 interface, such as the execution “curl http://127.0.0.1:2379/v2/members,” tip “404 page not found”

Cause: V3.4 uses V3 ports by default and does not support V2

Solution: Add –enable-v2=true when you start etCD to force you to use the V2 interface

Question 2: Failed to start the service. Conflicting environment variable “ETCD_ENABLE_V2″ is Shadowed by corresponding command-line flag (either unset Environment variable or disable flag)”

Cause: The “–enable-v2=true” parameter is used when the ETCD is started. V3.4 will read the configuration, and a message is displayed indicating that the configuration is repeated.

Solution: Do not delete this parameter, otherwise it will introduce other problems, I just close all Windows and restart etCD.

Fault 3: When a node is started, a message is displayed indicating that member already exists

Cause: Because the node has been started before, the member already exists and cannot be initialized. Only existing members can be added

Solution: You need to change the startup script “CLUSTER_STATE=new” to “CLUSTER_STATE=existing”

Normal operation

Cluster management

Here’s a quick summary of the methods we use to deploy a cluster:

// Version check, output {"etcdserver":"3.4.14"."etcdcluster":"3.4.0"}

The curl http://127.0.0.1:2379/version

// Health check, output {"health":"true"}

The curl http://127.0.0.1:2379/health

// View cluster nodes

The curl http://127.0.0.1:2379/v2/members

Copy the code

The key value actions

Set the key value:

The curl http://127.0.0.1:2379/v2/keys/message - XPUT - value = d"hello world"

Copy the code

Return result:

{

    "action":"set".

    "node": {

        "key":"/message".

        "value":"hello world".

        "modifiedIndex": 43.

        "createdIndex"43:

    }

}

Copy the code

Read key values:

The curl http://127.0.0.1:2379/v2/keys/message

Copy the code

Return result:

{

    "action":"get".

    "node": {

        "key":"/message".

        "value":"hello world".

        "modifiedIndex": 43.

        "createdIndex"43:

    }

}

Copy the code

Set key timeout to 10s:

The curl http://127.0.0.1:2379/v2/keys/message - XPUT - value = d"hello world" -d ttl=10

Copy the code

Returns the result (prevNode is old) :

{

    "action":"set".

    "node": {

        "key":"/message".

        "value":"hello world".

        "expiration":"The 2021-01-21 T00: not. 777434 z".

        "ttl": 10,

        "modifiedIndex": 44,

        "createdIndex"44:

    },

    "prevNode": {

        "key":"/message".

        "value":"hello world".

        "modifiedIndex": 43.

        "createdIndex"43:

    }

}

Copy the code

Key not found = key not found = key not found


Watch notice

When the value of the key changes, the listening client will be notified. We will first listen on the key on client A:

The curl http://127.0.0.1:2379/v2/keys/message?wait=true

Copy the code

Then on client B, change the value of this key:

The curl http://127.0.0.1:2379/v2/keys/message - XPUT - value = d"hello world2"

Copy the code

Client A returns and exits with the result:

{

    "action":"set".

    "node": {

        "key":"/message".

        "value":"hello world2".

        "modifiedIndex": 48.

        "createdIndex"48:

    }

}

Copy the code

If you want client A to continue listening without exiting, you can add the stream=true argument:

curl "Http://127.0.0.1:2379/v2/keys/message? wait=true&stream=true"

Copy the code

When the following is executed on client B:

The curl http://127.0.0.1:2379/v2/keys/message - XPUT - value = d"hello world" -d ttl=10

Copy the code

Client A will listen to the return in real time. For example, when the key is set, or when the key expires, client A will listen to: