搭建 etcd,我是一路踩着坑过来的 TAT,还好最终成功了。我才接触etcd一天,所以这篇文章肯定存在许多漏洞,请各位大佬不惜吝教!
1.简介
etcd 作为 k8s 的高可用的分布式键值(key-value)数据库。etcd内部采用raft协议作为一致性算法,etcd基于Go语言实现。
etcd比较多的应用场景是用于服务发现,服务发现(Service Discovery)要解决的是分布式系统中最常见的问题之一,即在同一个分布式集群中的进程或服务如何才能找到对方并建立连接。
2.安装
etcd 的安装包我是从 华为开源镜像站 下载的,这里速度较快
解压安装后将 启动文件 和 命令管理文件 拷贝到 /usr/local/bin 下
cp etcd /usr/local/bin
cp etcdctl /usr/local/bin
启动参数的解释
--name
etcd集群中的节点名,这里可以随意,可区分且不重复就行
--listen-peer-urls
监听的用于节点之间通信的url,可监听多个,集群内部将通过这些url进行数据交互(如选举,数据同步等)
--initial-advertise-peer-urls
建议用于节点之间通信的url,节点间将以该值进行通信。
--listen-client-urls
监听的用于客户端通信的url,同样可以监听多个。
--advertise-client-urls
建议使用的客户端通信url,该值用于etcd代理或etcd成员与etcd节点通信。
--initial-cluster-token etcd-cluster-1
节点的token值,设置该值后集群将生成唯一id,并为每个节点也生成唯一id,当使用相同配置文件再启动一个集群时,只要该token值不一样,etcd集群就不会相互影响。
--initial-cluster
也就是集群中所有的initial-advertise-peer-urls 的合集
--initial-cluster-state new
新建集群的标志
启动
etcd默认监听的是localhost的2379端口,既只监听了lo设备,这样会导致启动后集群中的其他机器无法访问
因此我们可以在启动的时候将默认的localhost改成0.0.0.0,确保etcd监听了所有网卡。
nohup etcd --listen-client-urls="http://0.0.0.0:2379" --advertise-client-urls="http://0.0.0.0:2379" &
注意:etcd有要求,如果–listen-client-urls被设置了,那么就必须同时设置–advertise-client-urls,所以即使设置和默认相同,也必须显式设置
我们来使用curl来测试一下,是否可以远程访问,这里我的机器IP是 192.168.133.115

etcd 基本使用
etcdctl 是一个命令行工具,它能提供一些简洁的命令,供用户直接跟 etcd 服务交互,而无需基于 Http API 方式。可以方便我们对服务进行测试或者手动修改数据库内容。
它有 V2 和 V3 的版本,这两个版本所支持的命令有所不同。
首先使用 etcdctl v2版本时,需要设置环境变量 ETCDCTL_API=3
export ETCDCTL_API=3
或者在`/etc/profile`文件中添加环境变量
vi /etc/profile
...
ETCDCTL_API=3
...
source /etc/profile
然后可以查看当前版本支持哪些命令
[root@localhost etcd-v3.1.5-linux-amd64]# etcdctl -h
NAME:
etcdctl - A simple command line client for etcd.
USAGE:
etcdctl [global options] command [command options] [arguments...]
VERSION:
3.1.5
COMMANDS:
backup backup an etcd directory
cluster-health check the health of the etcd cluster
mk make a new key with a given value
mkdir make a new directory
rm remove a key or a directory
rmdir removes the key if it is an empty directory or a key-value pair
get retrieve the value of a key
ls retrieve a directory
set set the value of a key
setdir create a new directory or update an existing directory TTL
update update an existing key with a given value
updatedir update an existing directory
watch watch a key for changes
exec-watch watch a key for changes and exec an executable
member member add, remove and list subcommands
user user add, grant and revoke subcommands
role role add, grant and revoke subcommands
auth overall auth controls
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:
--debug output cURL commands which can be used to reproduce the request
--no-sync don't synchronize cluster information before sending request
--output simple, -o simple output response in the given format (simple, `extended` or `json`) (default: "simple")
--discovery-srv value, -D value domain name to query for SRV records describing cluster endpoints
--insecure-discovery accept insecure SRV records describing cluster endpoints
--peers value, -C value DEPRECATED - "--endpoints" should be used instead
--endpoint value DEPRECATED - "--endpoints" should be used instead
--endpoints value a comma-delimited list of machine addresses in the cluster (default: "http://127.0.0.1:2379,http://127.0.0.1:4001")
--cert-file value identify HTTPS client using this SSL certificate file
--key-file value identify HTTPS client using this SSL key file
--ca-file value verify certificates of HTTPS-enabled servers using this CA bundle
--username value, -u value provide username[:password] and prompt if password is not supplied.
--timeout value connection timeout per request (default: 2s)
--total-timeout value timeout for the command execution (except watch) (default: 5s) --help, -h show help
--version, -v print the version
注:这里都是踩过的坑啊…,由于之前使用的时 V3 的版本,所以 mkdir 命令一直无法使用,后面切换为 V2 版本就好了。
创建目录 /test
etcdctl --endpoints=192.168.133.115:2379 mkdir /test


创建键值对 [ybs:优碧胜]
etcdctl --endpoints=192.168.133.115:2379 set /test/ybs "优碧胜"
获取键值 (由于使用的是 v2 的版本,所以路径需要带上v2并且 keys是固定写法)

结果:

附:
etcd 可视化工具:etcdkeeper
docker run -it -d --name etcdkeeper \
-p 8080:8080 \
deltaprojects/etcdkeeper
本文详细介绍etcd的安装过程及常见问题解决方案,涵盖etcd在Centos7上的部署、etcdctl命令工具的使用,以及如何搭建etcd分布式存储系统。深入解析etcd在服务发现中的应用,并提供etcd可视化工具etcdkeeper的使用指导。
725

被折叠的 条评论
为什么被折叠?



