欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ETCD 安装、简介、使用REST API

程序员文章站 2022-07-13 22:45:16
...

ETCD

分布式键值存储系统,可用于服务注册发现

下载etcd安装包

访问git下载安装包,地址https://github.com/coreos/etcd/releases
使用wget命令加上资源url下载文件

解压

tar -zxvf etcd-v3.3.2-linux-amd64.tar.gz # z参数对应.gz压缩格式

查看etcd信息

[[email protected] etcd]# ./etcd -version
etcd Version: 3.3.2
Git SHA: c9d46ab37
Go Version: go1.9.4
Go OS/Arch: linux/amd64

启动etcd(后台进程启动)

nohup ./etcd >/tmp/etcd.log 2>&1 & # 日志文件输出到/tmp/etcd.log目录 

./etcd 可以直接前台运行etcd

添加key

[[email protected] etcd]# ./etcdctl put mykey "this is awesome"
No help topic for 'put' # etcdctl 需要设置环境变量 ETCDCTL_API=3,使用第三版的api,默认的api是2
[[email protected] etcd]# ETCDCTL_API=3 ./etcdctl put mykey "this is awesome"
OK

设置环境变量

[email protected] etcd]# echo 'export ETCDCTL_API=3' >> /etc/profile ## 环境变量添加 ETCDCTL_API=3
[[email protected] etcd]# source /etc/profile # 是profile中修改的文件生效
[[email protected] etcd]# ./etcdctl get mykey # 可以直接使用./etcdctl get key 命令了
mykey
this is awesome # 刚添加的内容 

etcd的README.md文件中有etcd的介绍和命令
使用 ETCDCTL_API=2 ./etcdctl 可以查看 api2的命令
使用 ETCDCTL_API=3 ./etcdctl 可以查看 api3的命令

REST API

  • 查看版本信息
[[email protected] etcd]# curl http://127.0.0.1:2379/version 
{"etcdserver":"3.3.2","etcdcluster":"3.3.0"}

默认2379和2380端口只在127.0.0.1机器上监听,即本机

[[email protected] etcd]# ss -tnl
State      Recv-Q Send-Q                       Local Address:Port                                      Peer Address:Port              
LISTEN     0      128                              127.0.0.1:2379                                                 *:*                  
LISTEN     0      128                              127.0.0.1:2380                                                 *:*                  
LISTEN     0      128                                      *:22                                                   *:*                  
LISTEN     0      100                              127.0.0.1:25                                                   *:*                  
LISTEN     0      128                                     :::80                                                  :::*                  
LISTEN     0      128                                     :::22                                                  :::*                  
LISTEN     0      100                                    ::1:25                                                  :::* 

若想通过机器IP访问则需要启动时指定IP

./etcd --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
# 后台启动
nohup ./etcd --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 > /tmp/etcd.log 2>&1 &

0.0.0.0:2379 表示任何ip都可以访问

[[email protected] etcd]# curl http://192.168.72.31:2379/version # 通过机器IP访问 
{"etcdserver":"3.3.2","etcdcluster":"3.3.0"}