ETCD
分布式鍵值存儲系統,可用于服務注冊發現
下載etcd安裝包
訪問git下載安裝包,地址:https://github.com/coreos/etcd/releases
使用wget命令加上資源url下載文件
解壓
tar -zxvf etcd-v3.3.2-linux-amd64.tar.gz # z參數對應.gz壓縮格式
查看etcd信息
[root@localhost 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
[root@localhost etcd]# ./etcdctl put mykey "this is awesome"
No help topic for 'put' # etcdctl 需要設置環境變量 ETCDCTL_API=3,使用第三版的api,默認的api是2
[root@localhost etcd]# ETCDCTL_API=3 ./etcdctl put mykey "this is awesome"
OK
設置環境變量
root@localhost etcd]# echo 'export ETCDCTL_API=3' >> /etc/profile ## 環境變量添加 ETCDCTL_API=3
[root@localhost etcd]# source /etc/profile # 是profile中修改的文件生效
[root@localhost 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
- 查看版本信息
[root@localhost etcd]# curl http://127.0.0.1:2379/version
{"etcdserver":"3.3.2","etcdcluster":"3.3.0"}
默認2379和2380端口只在127.0.0.1機器上監聽,即本機
[root@localhost 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都可以訪問
[root@localhost etcd]# curl http://192.168.72.31:2379/version # 通過機器IP訪問
{"etcdserver":"3.3.2","etcdcluster":"3.3.0"}