Etcd教程 — 第一章 Etcd简介、Etcd单机安装
一、Etcd介绍
1.1 介绍
etcd 是使用Go语言开发的一个开源的、高可用的分布式key-value存储系统,可以用于配置共享和服务的注册和发现。
1.2 etcd特点
etcd 具有以下特点:
完全复制:集群中的每个节点都可以使用完整的存档
高可用性:Etcd可用于避免硬件的单点故障或网络问题
一致性:每次读取都会返回跨多主机的最新写入
简单:包括一个定义良好、面向用户的API(gRPC)
安全:实现了带有可选的客户端证书身份验证的自动化TLS
快速:每秒10000次写入的基准速度
可靠:使用Raft算法实现了强一致、高可用的服务存储目录
二、 Etcd单机安装
2.1 安装方式
2.1.1 yum install etcd
2.1.2 通过系统工具安装etcd【版本比较滞后】
2.1.3 二进制包、源码编译、Docker容器【推荐方式】
下载安装包地址:https://github.com/etcd-io/etcd/tags
选择最新版本,同时里面还有Linux、macOS (Darwin)、Docker这三种安装方式的步骤以及安装包。
注:2.2到2.4的安装方式在github上都有写,详见:https://github.com/etcd-io/etcd/releases/tag/v3.5.4
2.2 Linux安装Etcd
2.2.1 创建执行脚本
在 linux的 /tmp 目录下创建一个脚本文件:
vim etcd
2.2.2 脚本内容
ETCD_VER=v3.5.4 # 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 /tmp/etcd-download-test/etcdctl version /tmp/etcd-download-test/etcdutl version
2.2.3 执行脚本里的命令
在 /tmp 执行:
./etcd
如果在执行时提示权限不够,则需要授权,然后再次执行即可:
chmod 777 etcd
2.2.4 查看Etcd安装是否成功
2.2.4.1 查看Etcd服务端安装是否成功
/tmp/etcd-download-test/etcd --version
如下显示则安装成功:
etcd Version: 3.5.4 Git SHA: 08407ff76 Go Version: go1.16.15 Go OS/Arch: linux/amd64
注意:需要将etcd加入到环境变量中,否则会在 3.3.3 执行 goreman启动命令 步骤时报 /bin/sh: etcd: 未找到命令 。
vim ~/.bashrc
将etcd环境变量添加到文件末尾。
export GOROOT=/usr/local/go #GOROOT是系统上安装Go软件包的位置。 export GOPATH=/root/go/GOPATH #GOPATH是工作目录的位置。这个是自己创建的,想放在哪都行 export ETCD=/tmp/etcd-download-test #ETCD #export PATH=$GOPATH/bin:$GOROOT/bin:$PATH export PATH=$GOPATH/bin:$GOROOT/bin:$ETCD:$PATH export GOPROXY="https://goproxy.cn"
加完后按esc键退出编辑模式,输入:wq保存退出。
source ~/.bashrc 更新配置文件。
2.2.4.2 查看Etcd客户端安装是否成功
/tmp/etcd-download-test/etcdctl version
如下显示则安装成功:
etcdctl version: 3.5.4 API version: 3.5
2.2.5 启动Etcd
# start a local etcd server /tmp/etcd-download-test/etcd
2.2.6 测试
# write,read to etcd /tmp/etcd-download-test/etcdctl --endpoints=localhost:2379 put foo bar /tmp/etcd-download-test/etcdctl --endpoints=localhost:2379 get foo
2.3 macOS (Darwin)安装Etcd
ETCD_VER=v3.5.4 # 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 /tmp/etcd-${ETCD_VER}-darwin-amd64 /tmp/etcd-download-test/etcd --version /tmp/etcd-download-test/etcdctl version /tmp/etcd-download-test/etcdutl version
2.4 Docker安装Etcd
Docker安装Etcd前,需要先在Linux上安装Docker,Docker的安装步骤参见:centos7安装docker
2.4.1安装Etcd的命令(1)
rm -rf /tmp/etcd-data.tmp && mkdir -p /tmp/etcd-data.tmp && \ docker rmi gcr.io/etcd-development/etcd:v3.5.4 || true && \ docker run \ -p 2379:2379 \ -p 2380:2380 \ --mount type=bind,source=/tmp/etcd-data.tmp,destination=/etcd-data \ --name etcd-gcr-v3.5.4 \ gcr.io/etcd-development/etcd:v3.5.4 \ /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
安装中遇到的一些问题:
Q1: 提示仓库里没有 gcr.io/etcd-development/etcd:v3.5.4
Error: No such image: gcr.io/etcd-development/etcd:v3.5.4 Unable to find image 'gcr.io/etcd-development/etcd:v3.5.4' locally docker: Error response from daemon: Get "https://gcr.io/v2/": net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers). See 'docker run --help'.
使用 docker search etcd 搜索仓库里的etcd:
NAME DESCRIPTION STARS OFFICIAL AUTOMATED bitnami/etcd Bitnami etcd Docker Image 126 [OK] elcolio/etcd Tiny Etcd Container with TLS support and etc… 88 [OK] microbox/etcd Trusted Automated etcd image (17MB size) 38 [OK] appcelerator/etcd etcd 13 [OK] xieyanze/etcd3 etcd v3.0.9 image 12 [OK] monsantoco/etcd-aws-cluster 12 [OK] nikfoundas/etcd-viewer etcd key-value store viewer and editor 8 [OK] rancher/etcd 4 ibmcom/etcd Docker Image for IBM Cloud private-CE (Commu… 2 pachyderm/etcd 1 kubesphere/etcd 1 ibmcom/etcd-ppc64le Docker Image for IBM Cloud Private-CE (Commu… 1 mesosphere/etcd-mesos etcd framework for mesos 1 openshift/etcd-20-centos7 OpenShift etcd v2.0 with clustering support 1 gasparekatapy/etcd Updated version of splazit/etcd 0 kope/etcd 0 docker/desktop-kubernetes-etcd Mirrors some tags from k8s.gcr.io/etcd 0 etcdigital/node Node 10 Prepared 0 ibmcom/etcd-s390x 0 etcdev/fork-geth 0 anldisr/etcdctl Etcdctl 0 splazit/etcd-swarm Etcd cluster in docker swarm 0 [OK] ibmcom/etcd-amd64 0 rancher/etcd-tools 0 razorpay/etcd-backup Container to take backups for etcd cluster w… 0
这里使用 第一个 bitnami/etcd 来替换 gcr.io/etcd-development/etcd:v3.5.4 :
安装Etcd的命令(2)
rm -rf /tmp/etcd-data.tmp && mkdir -p /tmp/etcd-data.tmp && \ docker rmi bitnami/etcd:3.5.4 || true && \ docker run \ -p 2379:2379 \ -p 2380:2380 \ --mount type=bind,source=/tmp/etcd-data.tmp,destination=/etcd-data \ --name etcd-bitnami-v3.5.4 \ bitnami/etcd:3.5.4 \ /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
Q2:又报 /opt/bitnami/scripts/etcd/entrypoint.sh: line 26: /usr/local/bin/etcd: No such file or directory ,暂未找到解决办法
etcd 11:49:09.66 Welcome to the Bitnami etcd container etcd 11:49:09.66 Subscribe to project updates by watching https://github.com/bitnami/bitnami-docker-etcd etcd 11:49:09.66 Submit issues and feature requests at https://github.com/bitnami/bitnami-docker-etcd/issues etcd 11:49:09.66 /opt/bitnami/scripts/etcd/entrypoint.sh: line 26: /usr/local/bin/etcd: No such file or directory
Docker安装Etcd通过以上的方式均安装失败,目前尚未找到解决办法,所以目前暂时搁置Docker安装Etcd。
2.4.2 Docker查看etcd版本及使用
docker exec etcd-gcr-v3.5.4 /bin/sh -c "/usr/local/bin/etcd --version" docker exec etcd-gcr-v3.5.4 /bin/sh -c "/usr/local/bin/etcdctl version" docker exec etcd-gcr-v3.5.4 /bin/sh -c "/usr/local/bin/etcdctl endpoint health" docker exec etcd-gcr-v3.5.4 /bin/sh -c "/usr/local/bin/etcdctl put foo bar" docker exec etcd-gcr-v3.5.4 /bin/sh -c "/usr/local/bin/etcdctl get foo" docker exec etcd-gcr-v3.5.4 /bin/sh -c "/usr/local/bin/etcdutl version"