ETCD集群安装实验

时间:2024-03-16 19:13:55

目录


【1、下载二进制程序】
【2、安装etcd集群】
【3、查询集群状态】
【4、存入读取数据】
【5、注意事项】
【6、参考链接】


简介:
     Etcd的官网文档及其在GitHub上的文档,已经非常全了,而且简洁明了,无论是二进制安装、还是Docker下运行、命令操作,都有详细的说明。但直接复制安装还是会出错的,只有亲手做一遍,才能发现有哪些问题。


【1、下载二进制程序】

#分别在三台主机上下载并安装etcd和etcdctl程序
#设定ETCD的版本

ETCD_VER=v3.3.8

# 设置下载地址URL
GITHUB_URL=https://github.com/coreos/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下载etcd二进制文件
#地址为
https://github.com/coreos/etcd/releases/download/v3.3.8/etcd-v3.3.8-linux-amd64.tar.gz
#解包后删除压缩包
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

#将etcd 和 etcdctl可执行程序移动到系统路径中,方便直接运行程序
sudo mv /tmp/etcd-download-test/etcd* /usr/local/bin

#查询etcd和etcdctl版本,
#检查是否能正常运行程序。

etcd --version
ETCDCTL_API=3 etcdctl version


【2、安装etcd集群】

#在节点node1上执行

TOKEN=token-01
CLUSTER_STATE=new
NAME_1=node1
NAME_2=node2
NAME_3=node3
HOST_1=192.168.3.161
HOST_2=192.168.3.162
HOST_3=192.168.3.163
CLUSTER=${NAME_1}=http://${HOST_1}:2380,${NAME_2}=http://${HOST_2}:2380,${NAME_3}=http://${HOST_3}:2380

THIS_NAME=${NAME_1}
THIS_IP=${HOST_1}
etcd --data-dir=data.etcd --name ${THIS_NAME} \
--initial-advertise-peer-urls http://${THIS_IP}:2380 --listen-peer-urls http://${THIS_IP}:2380 \
--advertise-client-urls http://${THIS_IP}:2379 --listen-client-urls http://${THIS_IP}:2379 \
--initial-cluster ${CLUSTER} \
--initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN}

#此处注意,直接复制github网站上的代码,执行时会报错
#2018-06-29 16:57:54.048863 E | etcdmain: error verifying flags, '.--initial-advertise-peer-urls' is not a valid flag. See 'etcd --help'.
#
https://github.com/coreos/etcd/blob/master/Documentation/demo.md
#需要清除掉--短横线之前的空格字符


#在节点node2上执行

TOKEN=token-01
CLUSTER_STATE=new
NAME_1=node1
NAME_2=node2
NAME_3=node3
HOST_1=192.168.3.161
HOST_2=192.168.3.162
HOST_3=192.168.3.163
CLUSTER=${NAME_1}=http://${HOST_1}:2380,${NAME_2}=http://${HOST_2}:2380,${NAME_3}=http://${HOST_3}:2380

THIS_NAME=${NAME_2}
THIS_IP=${HOST_2}
etcd --data-dir=data.etcd --name ${THIS_NAME} \
--initial-advertise-peer-urls http://${THIS_IP}:2380 --listen-peer-urls http://${THIS_IP}:2380 \
--advertise-client-urls http://${THIS_IP}:2379 --listen-client-urls http://${THIS_IP}:2379 \
--initial-cluster ${CLUSTER} \
--initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN}


#在节点node3上执行

TOKEN=token-01
CLUSTER_STATE=new
NAME_1=node1
NAME_2=node2
NAME_3=node3
HOST_1=192.168.3.161
HOST_2=192.168.3.162
HOST_3=192.168.3.163
CLUSTER=${NAME_1}=http://${HOST_1}:2380,${NAME_2}=http://${HOST_2}:2380,${NAME_3}=http://${HOST_3}:2380

THIS_NAME=${NAME_3}
THIS_IP=${HOST_3}
etcd --data-dir=data.etcd --name ${THIS_NAME} \
--initial-advertise-peer-urls http://${THIS_IP}:2380 --listen-peer-urls http://${THIS_IP}:2380 \
--advertise-client-urls http://${THIS_IP}:2379 --listen-client-urls http://${THIS_IP}:2379 \
--initial-cluster ${CLUSTER} \
--initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN}


【3、查询集群状态】
export ETCDCTL_API=3
HOST_1=192.168.3.161
HOST_2=192.168.3.162
HOST_3=192.168.3.163
ENDPOINTS=$HOST_1:2379,$HOST_2:2379,$HOST_3:2379

etcdctl --endpoints=$ENDPOINTS --write-out=table member list
etcdctl --endpoints=$ENDPOINTS --write-out=table endpoint status
etcdctl --endpoints=$ENDPOINTS --write-out=table endpoint health

图1查询集群的状态

ETCD集群安装实验


【4、存入读取数据】
#在同一网段的其他机器上,下载并解包etcdctl,
#使用etcdctl命令访问etcd的集群

export ETCDCTL_API=3
HOST_1=192.168.3.161
HOST_2=192.168.3.162
HOST_3=192.168.3.163
ENDPOINTS=$HOST_1:2379,$HOST_2:2379,$HOST_3:2379

etcdctl --endpoints=$ENDPOINTS put myurl www.sina.com.cn
etcdctl --endpoints=$ENDPOINTS get myurl

图2使用etcdctl 存入和读取数据

ETCD集群安装实验


#或者使用curl 访问etcd集群,
#通过集群中的任何一个节点,都可以访问到数据

HOST_1=192.168.3.161
HOST_2=192.168.3.162
HOST_3=192.168.3.163
curl -X PUT http://${HOST_1}:2379/v2/keys/myurl -d value=www.china.com
curl -X GET http://${HOST_1}:2379/v2/keys/myurl
curl -X GET http://${HOST_2}:2379/v2/keys/myurl
curl -X GET http://${HOST_3}:2379/v2/keys/myurl
curl http://192.168.3.161:2379/v2/keys

图3使用curl 存入和读取数据

ETCD集群安装实验


【5、注意事项】
1、直接复制粘贴github上的代码会出错,需要清除etcd命令中--短横线之前的空格
2018-06-29 16:57:54.048863 E | etcdmain: error verifying flags, '.--initial-advertise-peer-urls' is not a valid flag. See 'etcd --help'.
2、主机的时间应该同步,可以安装NTP服务,否则会告警:
2018-06-29 18:04:29.328082 W | rafthttp: the clock difference against peer 5d5a6e579c03726b is too high [3.777632896s > 1s]


【6、参考链接】
etcd官方网址
https://coreos.com/etcd/docs/latest/demo.html
https://github.com/coreos/etcd/releases/
https://github.com/coreos/etcd/blob/master/Documentation/demo.md

etcd rest API基本操作
https://segmentfault.com/a/1190000005649865