How to use etcd (multi-machine cluster basic mode) in Ubuntu?


Recently, I have found interesting solution. it is called as etcd. It is Distributed reliable key-value store. What is the key-value store?. It is similar with dictionary in Python. This etcd make dictionary items can be put and get on any hosts. 


1. Prerequesite


The etcd is used for cluster environment. Therefore, I will prepare 3 servers with ubuntu.


infra0     172.22.0.96 infra0.example.com

infra1     172.22.0.33 infra1.example.com

infra2     172.22.0.133 infra2.example.com 


2. Get etcd release 


I have ubuntu server. I need the etcd release for ubuntu. In this documentation. There is installation guide for Linux.


ETCD_VER=v3.3.10


# 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}-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

ETCDCTL_API=3 /tmp/etcd-download-test/etcdctl version 


I will run this command on every hosts. "/tmp" directory is used in command above. However, In my case, I will change the directory "/home/ubunut"


# mkdir -p /home/ubuntu/etcd-download-test

# curl -L ${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz -o 

# /home/ubuntu/etcd-${ETCD_VER}-linux-amd64.tar.gz

# tar xzvf /home/ubuntu/etcd-${ETCD_VER}-linux-amd64.tar.gz -C /home/ubuntu/etcd-download-test --strip-components=1

# rm -f /home/ubuntu/etcd-${ETCD_VER}-linux-amd64.tar.gz 


I can check the installation version with both of commands.


/home/ubuntu/etcd-download-test/etcd --version

ETCDCTL_API=3 /home/ubuntu/etcd-download-test/etcdctl version 


# /home/ubuntu/etcd-download-test/etcd --version

etcd Version: 3.3.10

Git SHA: 27fc7e2

Go Version: go1.10.4

Go OS/Arch: linux/amd64


More efficient, I will export the directory in PATH environment.


# export PATH=$PATH:/home/ubuntu/etcd-download-test


# vi /root/.bashrc
PATH=$PATH:/home/ubuntu/etcd-download-test

# source /root/.bashrc


3. Running in multi-machine cluster basic mode


I can run in single standalone mode. The command line usage is here. At first, run in single mode


etcd


I can put and get data. "ETCDCTL_API=3" is important parameter. It should be exist.


# ETCDCTL_API=3 etcdctl put mykey "this is awesome"

OK

# ETCDCTL_API=3 etcdctl get mykey
mykey
this is awesome


Also I can see the result with JSON format like below. If I want to find more example of command usage, look this documentation.


# export ETCDCTL_API=3
# etcdctl --endpoints=localhost:2379 put foo "Hello World!"
OK

# etcdctl --endpoints=localhost:2379 get foo --write-out="json"
{"header":{"cluster_id":14841639068965178418,"member_id":10276657743932975437,"revision":3,"raft_term":2},"kvs":[{"key":"Zm9v","create_revision":3,"mod_revision":3,"version":1,"value":"SGVsbG8gV29ybGQh"}],"count":1}

# etcdctl --endpoints=localhost:2379 get foo
foo
Hello World!


Even if this single mode works, but etcd is more powerful in mult-machin cluster mode. Look this documentation. To run cluster mode, there are 2 things important. IP address to listen and announce and IP address for peer hosts


etcd --name infra0 --initial-advertise-peer-urls http://172.22.0.96:2380 \

  --listen-peer-urls http://172.22.0.96:2380 \

  --listen-client-urls http://172.22.0.96:2379,http://127.0.0.1:2379 \

  --advertise-client-urls http://172.22.0.96:2379 \

  --initial-cluster-token etcd-cluster-1 \

  --initial-cluster infra0=http://172.22.0.96:2380,infra1=http://172.22.0.33:2380,infra2=http://172.22.0.133:2380 \

  --initial-cluster-state new

etcd --name infra1 --initial-advertise-peer-urls http://172.22.0.33:2380 \

  --listen-peer-urls http://172.22.0.33:2380 \

  --listen-client-urls http://172.22.0.33:2379,http://127.0.0.1:2379 \

  --advertise-client-urls http://172.22.0.33:2379 \

  --initial-cluster-token etcd-cluster-1 \

  --initial-cluster infra0=http://172.22.0.96:2380,infra1=http://172.22.0.33:2380,infra2=http://172.22.0.133:2380 \

  --initial-cluster-state new

etcd --name infra2 --initial-advertise-peer-urls http://172.22.0.133:2380 \

  --listen-peer-urls http://172.22.0.133:2380 \

  --listen-client-urls http://172.22.0.133:2379,http://127.0.0.1:2379 \

  --advertise-client-urls http://172.22.0.133:2379 \

  --initial-cluster-token etcd-cluster-1 \

  --initial-cluster infra0=http://172.22.0.96:2380,infra1=http://172.22.0.33:2380,infra2=http://172.22.0.133:2380 \

  --initial-cluster-state new

  

For peer communicate, the 2380 port is used. For data communication, the 2379 port is used. Therefore, "127.0.0.1:2379" is necessary to request to localhost. However, this is not necessary to announce. These command above should be run on each host. After running, I can get the status of clustering.


# ETCDCTL_API=3 etcdctl --endpoints=172.22.0.96:2379,172.22.0.33:2379,172.22.0.133:2379 endpoint health

172.22.0.96:2379 is healthy: successfully committed proposal: took = 2.385219ms

172.22.0.133:2379 is healthy: successfully committed proposal: took = 2.847419ms

172.22.0.33:2379 is healthy: successfully committed proposal: took = 2.292504ms


Now, I have done everything to cluster multi-hosts. In multi-hosts environments, I put data into anywhere, I can get data from anywhere.


ETCDCTL_API=3 etcdctl --endpoints=172.22.0.96:2379 put foo "Hello World!"

OK


ETCDCTL_API=3 etcdctl --endpoints=172.22.0.133:2379 get foo

foo

Hello World!


Finally, I can use the "etcd" little.


 Reference


[ 1 ] https://github.com/etcd-io/etcd

[ 2 ] https://github.com/etcd-io/etcd/releases

[ 3 ] https://github.com/etcd-io/etcd/blob/master/Documentation/op-guide/clustering.md

[ 4 ] https://coreos.com/etcd/docs/latest/demo.html

+ Recent posts