Etcd单机搭建入门

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

Etcd 使用入门

从零开始搭建etcd分布式存储系统+web管理界面

Centos7安装etcd

etcdctl命令工具-V2

etcdctl命令工具-V3

etcdctl的使用[v3版本]

搭建 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

306

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

307

308

创建键值对 [ybs:优碧胜]

etcdctl --endpoints=192.168.133.115:2379 set /test/ybs "优碧胜"

获取键值 (由于使用的是 v2 的版本,所以路径需要带上v2并且 keys是固定写法)

305

结果:

309

附:

etcd 可视化工具:etcdkeeper

docker run -it -d --name etcdkeeper \
-p 8080:8080 \
deltaprojects/etcdkeeper
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值