etcd 两种本机搭建集群环境方式 操作记录

etcd

发布地址

etcd官网

git release

etcd是使用Go语言开发的一个开源的、高可用的分布式key-value存储系统,可以用于配置共享和服务的注册和发现。

类似项目有zookeeper和consul。

etcd具有以下特点:

完全复制:集群中的每个节点都可以使用完整的存档
高可用性:Etcd可用于避免硬件的单点故障或网络问题
一致性:每次读取都会返回跨多主机的最新写入
简单:包括一个定义良好、面向用户的API(gRPC)
安全:实现了带有可选的客户端证书身份验证的自动化TLS
快速:每秒10000次写入的基准速度
可靠:使用Raft算法实现了强一致、高可用的服务存储目录

官网Linux安装方法

ETCD_VER=v3.5.1

# 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
# start a local etcd server
/tmp/etcd-download-test/etcd

# 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

官网docker安装

rm -rf /tmp/etcd-data.tmp && mkdir -p /tmp/etcd-data.tmp && \
  docker rmi gcr.io/etcd-development/etcd:v3.5.1 || 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.1 \
  gcr.io/etcd-development/etcd:v3.5.1 \
  /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

docker exec etcd-gcr-v3.5.1 /bin/sh -c "/usr/local/bin/etcd --version"
docker exec etcd-gcr-v3.5.1 /bin/sh -c "/usr/local/bin/etcdctl version"
docker exec etcd-gcr-v3.5.1 /bin/sh -c "/usr/local/bin/etcdctl endpoint health"
docker exec etcd-gcr-v3.5.1 /bin/sh -c "/usr/local/bin/etcdctl put foo bar"
docker exec etcd-gcr-v3.5.1 /bin/sh -c "/usr/local/bin/etcdctl get foo"
docker exec etcd-gcr-v3.5.1 /bin/sh -c "/usr/local/bin/etcdutl version"

方案一

使用 goreman 本机启动多进程

etcd地址:https://github.com/etcd-io/etcd

goreman: https://github.com/mattn/goreman

安装goreman后 在 $GOPATH/workspace/bin 中根据以下配置创建 Procfile 文件

etcd1: etcd --name infra1 --listen-client-urls http://127.0.0.1:2379 --advertise-client-urls http://127.0.0.1:2379 --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-state new --enable-pprof --logger=zap --log-outputs=stderr

etcd2: etcd --name infra2 --listen-client-urls http://127.0.0.1:22379 --advertise-client-urls http://127.0.0.1:22379 --listen-peer-urls http://127.0.0.1:22380 --initial-advertise-peer-urls http://127.0.0.1:22380 --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-state new --enable-pprof --logger=zap --log-outputs=stderr

etcd3: etcd --name infra3 --listen-client-urls http://127.0.0.1:32379 --advertise-client-urls http://127.0.0.1:32379 --listen-peer-urls http://127.0.0.1:32380 --initial-advertise-peer-urls http://127.0.0.1:32380 --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-state new --enable-pprof --logger=zap --log-outputs=stderr
#proxy: etcd grpc-proxy start --endpoints=127.0.0.1:2379,127.0.0.1:22379,127.0.0.1:32379 --listen-addr=127.0.0.1:23790 --advertise-client-url=127.0.0.1:23790 --enable-pprof

启动goreman 即可

goreman start

其他操作

goreman run stop etcd1 停止某个节点
goreman run restart etcd2 重启某个节点
goreman -f Procfile start 启动集群 手动指定文件

测试

[root@vmCentos etcd-v3.5.1-linux-amd64]# ./etcdctl put 123 1
OK
[root@vmCentos etcd-v3.5.1-linux-amd64]# ./etcdctl get 123
123
1
[root@vmCentos etcd-v3.5.1-linux-amd64]# ./etcdctl --endpoints=http://127.0.0.1:12380  get 123
123
1
[root@vmCentos etcd-v3.5.1-linux-amd64]# ./etcdctl put 123 456
OK
[root@vmCentos etcd-v3.5.1-linux-amd64]# ./etcdctl --endpoints=http://127.0.0.1:12380  get 123
123
456

方案二

使用Docker创建

  1. 创建docker 网络
docker network create app-etcd
  1. 创建etcdj节点数据目录
mkdir /root/etcd-n1
mkdir /root/etcd-n2
mkdir /root/etcd-n3
  1. 拉取docker镜像
docker pull quay.io/coreos/etcd:v3.5.1
  1. 创建节点1
docker run -d \
  -p 10379:2379 \
  -p 10380:2380 \
  --net app-etcd \
  --volume=/root/etcd-n1:/etcd-data \
  --name etcd-n1 \
  quay.io/coreos/etcd:v3.5.1 \
  /usr/local/bin/etcd \
  --name etcd-n1 \
  --data-dir /etcd-data \
  --listen-client-urls http://0.0.0.0:2379 \
  --advertise-client-urls http://etcd-n1:2379 \
  --listen-peer-urls http://0.0.0.0:2380 \
  --initial-advertise-peer-urls http://etcd-n1:2380 \
  --initial-cluster etcd-n1=http://etcd-n1:2380,etcd-n2=http://etcd-n2:2380,etcd-n3=http://etcd-n3:2380 \
  --initial-cluster-token myetcdToken \
  --initial-cluster-state new \
  --log-level info \
  --logger zap \
  --log-outputs stderr
  1. 创建节点2
 docker run -d \
  -p 20379:2379 \
  -p 20380:2380 \
  --net app-etcd \
  --volume=/root/etcd-n2:/etcd-data \
  --name etcd-n2 \
  quay.io/coreos/etcd:v3.5.1 \
  /usr/local/bin/etcd \
  --name etcd-n2 \
  --data-dir /etcd-data \
  --listen-client-urls http://0.0.0.0:2379 \
  --advertise-client-urls http://etcd-n2:2379 \
  --listen-peer-urls http://0.0.0.0:2380 \
  --initial-advertise-peer-urls http://etcd-n2:2380 \
  --initial-cluster etcd-n1=http://etcd-n1:2380,etcd-n2=http://etcd-n2:2380,etcd-n3=http://etcd-n3:2380 \
  --initial-cluster-token myetcdToken \
  --initial-cluster-state new \
  --log-level info \
  --logger zap \
  --log-outputs stderr
  1. 创建节点3
 docker run -d \
  -p 30379:2379 \
  -p 30380:2380 \
  --net app-etcd \
  --volume=/root/etcd-n3:/etcd-data \
  --name etcd-n3 \
  quay.io/coreos/etcd:v3.5.1 \
  /usr/local/bin/etcd \
  --name etcd-n3 \
  --data-dir /etcd-data \
  --listen-client-urls http://0.0.0.0:2379 \
  --advertise-client-urls http://etcd-n3:2379 \
  --listen-peer-urls http://0.0.0.0:2380 \
  --initial-advertise-peer-urls http://etcd-n3:2380 \
  --initial-cluster etcd-n1=http://etcd-n1:2380,etcd-n2=http://etcd-n2:2380,etcd-n3=http://etcd-n3:2380 \
  --initial-cluster-token myetcdToken \
  --initial-cluster-state new \
  --log-level info \
  --logger zap \
  --log-outputs stderr

测试

#搭建好的docker 服务列表
[root@vmCentos ~]# docker ps
CONTAINER ID   IMAGE                        COMMAND                  CREATED              STATUS              PORTS                                                                                      NAMES
9195d985bc07   quay.io/coreos/etcd:v3.5.1   "/usr/local/bin/etcd…"   12 seconds ago       Up 11 seconds       0.0.0.0:20379->2379/tcp, :::20379->2379/tcp, 0.0.0.0:20380->2380/tcp, :::20380->2380/tcp   etcd-n2
09c91092fa51   quay.io/coreos/etcd:v3.5.1   "/usr/local/bin/etcd…"   17 seconds ago       Up 16 seconds       0.0.0.0:30379->2379/tcp, :::30379->2379/tcp, 0.0.0.0:30380->2380/tcp, :::30380->2380/tcp   etcd-n3
8a376c0ed897   quay.io/coreos/etcd:v3.5.1   "/usr/local/bin/etcd…"   About a minute ago   Up About a minute   0.0.0.0:10379->2379/tcp, :::10379->2379/tcp, 0.0.0.0:10380->2380/tcp, :::10380->2380/tcp   etcd-n1
# 执行客户端调用测试
[root@vmCentos etcd-v3.5.1-linux-amd64]# ./etcdctl --endpoints=http://127.0.0.1:20380 put 123 key
OK
[root@vmCentos etcd-v3.5.1-linux-amd64]# ./etcdctl --endpoints=http://127.0.0.1:30380 put 123 key1
OK
[root@vmCentos etcd-v3.5.1-linux-amd64]# ./etcdctl --endpoints=http://127.0.0.1:10380 get 123
123
key1
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论抢沙发

请登录后发表评论