概要
在微服务中服务发现是必不可少的,此时ETCD中间件就是一种可选项,其实ETCD除了服务发现功能,还有:
- 元数据存储,比如存储服务配置等数据,以实现配置中心化,进行统一的,有版本记录的变更管理;
- 分布式锁;
- 选主,master-slave的软件架构中可以通过etcd快速实现选主的功能。
ETCD 采用 raft 算法,实现分布式数据的一致性和高可用,遵循CP原则,支持10 k/s 的并发读写操作。
一、安装
安装的方法有很多:
- 二进制安装;
- yum一键安装;
- docker安装;
安装模式有单点和集群两种;
1.1 单点
直接yum下最简单了:
yum install etcd
如果服务器上已经装有docker了,可以docker安装:
docker pull bitnami/etcd:latest
docker network create app-tier --driver bridge
docker run -d --name etcd-server \
--network app-tier \
--publish 2379:2379 \
--publish 2380:2380 \
--env ALLOW_NONE_AUTHENTICATION=yes \
--env ETCD_ADVERTISE_CLIENT_URLS=http://etcd-server:2379 \
bitnami/etcd:latest
1.2 集群
这里采用二进制的方式,首先到github下载自己所要的版本,本人选的v3.5.11。
ETCD集群部署有三种模式:
- 静态配置;
- ETCD 动态发现;
- DNS发现。
由于是测试使用,所以在一台Linux服务器上基于静态配置搭建了个三节点的集群。
配置项指标见官网
节点1:
mkdir -p /usr/local/etcd-v3.5.11/etcd1/data
vim /usr/local/etcd-v3.5.11/etcd1/conf.yml
name: etcd1
data-dir: /usr/local/etcd-v3.5.11/etcd1/data
initial-advertise-peer-urls: http://127.0.0.1:2380
listen-peer-urls: http://127.0.0.1:2380
listen-client-urls: http://172.20.101.222:2379,http://127.0.0.1:2379
advertise-client-urls: http://127.0.0.1:2379
initial-cluster-token: test-etcd-cluster
initial-cluster: etcd1=http://127.0.0.1:2380,etcd2=http://127.0.0.1:2370,etcd3=http://127.0.0.1:2360
initial-cluster-state: new
节点2:
mkdir -p /usr/local/etcd-v3.5.11/etcd2/data
vim /usr/local/etcd-v3.5.11/etcd2/conf.yml
name: etcd2
data-dir: /usr/local/etcd-v3.5.11/etcd2/data
initial-advertise-peer-urls: http://127.0.0.1:2370
listen-peer-urls: http://127.0.0.1:2370
listen-client-urls: http://172.20.101.222:2369,http://127.0.0.1:2369
advertise-client-urls: http://127.0.0.1:2369
initial-cluster-token: test-etcd-cluster
initial-cluster: etcd1=http://127.0.0.1:2380,etcd2=http://127.0.0.1:2370,etcd3=http://127.0.0.1:2360
initial-cluster-state: new
节点3:
mkdir -p /usr/local/etcd-v3.5.11/etcd3/data
vim /usr/local/etcd-v3.5.11/etcd3/conf.yml
name: etcd2
data-dir: /usr/local/etcd-v3.5.11/etcd3/data
initial-advertise-peer-urls: http://127.0.0.1:2360
listen-peer-urls: http://127.0.0.1:2360
listen-client-urls: http://172.20.101.222:2359,http://127.0.0.1:2359
advertise-client-urls: http://127.0.0.1:2359
initial-cluster-token: test-etcd-cluster
initial-cluster: etcd1=http://127.0.0.1:2380,etcd2=http://127.0.0.1:2370,etcd3=http://127.0.0.1:2360
initial-cluster-state: new
启动:
/usr/local/etcd-v3.5.11/etcd --config-file /usr/local/etcd-v3.5.11/etcd1/conf.yml
/usr/local/etcd-v3.5.11/etcd --config-file /usr/local/etcd-v3.5.11/etcd2/conf.yml
/usr/local/etcd-v3.5.11/etcd --config-file /usr/local/etcd-v3.5.11/etcd2/conf.yml
默认的etcdctrl使用的是v2版本的API,我们需要设置环境变量来使用v3版本的API:
#window
set ETCDCTL_API=3
#linux
export ETCDCTL_API=3
验证:文章来源:https://www.toymoban.com/news/detail-822592.html
#查看集群状态
[root@test etcd-v3.5.11]# /usr/local/etcd-v3.5.11/etcdctl endpoint status --endpoints=127.0.0.1:2379,127.0.0.1:2369,127.0.0.1:2359 -w table
+----------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
+----------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| 127.0.0.1:2379 | 7637173a60d743e5 | 3.5.11 | 21 MB | true | false | 15 | 10843 | 10843 | |
| 127.0.0.1:2369 | ce6b0efed2935560 | 3.5.11 | 21 MB | false | false | 15 | 10843 | 10843 | |
| 127.0.0.1:2359 | 6ea1523f71bf92f0 | 3.5.11 | 21 MB | false | false | 15 | 10843 | 10843 | |
+----------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
#插入数据
[root@test etcd-v3.5.11]# /usr/local/etcd-v3.5.11/etcdctl put /test/hello helloworld
OK
#查看数据
[root@test etcd-v3.5.11]# /usr/local/etcd-v3.5.11/ get /test/hello
/test/hello
helloworld
至此就可以用于测试使用了,当然还支持配置校验,证书等功能。文章来源地址https://www.toymoban.com/news/detail-822592.html
到了这里,关于一起学习ETCD系列——简单安装的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!