etcd
是一个golang
编写的分布式、高可用的一致性键值存储系统,用于提供可靠的分布式键值(key-value)存储、配置共享和服务发现等功能。etcd
可以用于存储关键数据和实现分布式调度,在现代化的集群运行中能够起到关键性的作用。
etcd
基于raft
协议,通过复制日志文件的方式来保证数据的强一致性。在etcd
之前,常用的是基于paxos
协议的zookeeper
。
安装etcd
CentOS
yum install -y etcd
启动
# 设置开机自启动
$ sudo systemctl enable etcd
# 启动etcd
$ sudo systemctl start etcd
# 查看etcd运行状态
$ sudo systemctl status etcd
● etcd.service - Etcd Server
Loaded: loaded (/usr/lib/systemd/system/etcd.service; disabled; vendor preset: disabled)
Active: active (running) since 二 2019-06-18 19:36:16 CST; 3s ago
Main PID: 3188591 (etcd)
Tasks: 33
Memory: 12.9M
CGroup: /system.slice/etcd.service
└─3188591 /usr/bin/etcd --name=default --data-dir=/var/lib/etcd/default.etcd --listen-client-urls=http://localhost:2379
6月 18 19:36:16 103-18-41-sh-100-D10.yidian.com etcd[3188591]: 8e9e05c52164694d received MsgVoteResp from 8e9e05c52164694d at term 2
6月 18 19:36:16 103-18-41-sh-100-D10.yidian.com etcd[3188591]: 8e9e05c52164694d became leader at term 2
6月 18 19:36:16 103-18-41-sh-100-D10.yidian.com etcd[3188591]: raft.node: 8e9e05c52164694d elected leader 8e9e05c52164694d at term 2
6月 18 19:36:16 103-18-41-sh-100-D10.yidian.com etcd[3188591]: setting up the initial cluster version to 3.3
6月 18 19:36:16 103-18-41-sh-100-D10.yidian.com etcd[3188591]: set the initial cluster version to 3.3
6月 18 19:36:16 103-18-41-sh-100-D10.yidian.com etcd[3188591]: enabled capabilities for version 3.3
6月 18 19:36:16 103-18-41-sh-100-D10.yidian.com etcd[3188591]: ready to serve client requests
6月 18 19:36:16 103-18-41-sh-100-D10.yidian.com etcd[3188591]: published {Name:default ClientURLs:[http://localhost:2379]} to cluster cdf818194e3a8c32
6月 18 19:36:16 103-18-41-sh-100-D10.yidian.com systemd[1]: Started Etcd Server.
6月 18 19:36:16 103-18-41-sh-100-D10.yidian.com etcd[3188591]: serving insecure client requests on 127.0.0.1:2379, this is strongly discouraged!
systemd配置
从systemctl status etcd
命令的输出可以看到,etcd的 systemd配置文件位于/usr/lib/systemd/system/etcd.service
,该配置文件的内容如下:
$ cat /usr/lib/systemd/system/etcd.service
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
Type=notify
WorkingDirectory=/var/lib/etcd/
EnvironmentFile=-/etc/etcd/etcd.conf
User=etcd
# set GOMAXPROCS to number of processors
ExecStart=/bin/bash -c "GOMAXPROCS=$(nproc) /usr/bin/etcd --name=\"${ETCD_NAME}\" --data-dir=\"${ETCD_DATA_DIR}\" --listen-client-urls=\"${ETCD_LISTEN_CLIENT_URLS}\""
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
从上面的配置中可以看到,etcd的配置文件位于/etc/etcd/etcd.conf
,如果我们想要修改某些配置项,可以编辑该文件。
MacOS
brew install etcd
etcdctl
etcdctl 是官方提供的客户端程序。我们可以通过 etcdctl 相关命令实现如下操作:
1. 添加key-value并设置ttl
$ etcdctl set --ttl 5 key 'Hello World'
Hello World
$ etcdctl get key
Hello World
$ etcdctl get key
Error: 100: Key not found (/key) [7]
2.添加key-value
$ etcdctl set key 'Hello World'
Hello World
$ etcdctl update key 'Hello World2'
Hello World2
3.获取key-value
$ etcdctl get key
Hello World2
4.删除key-value
$ etcdctl rm key
PrevNode.Value: Hello World2
$ etcdctl get key
Error: 100: Key not found (/key) [10]
安装etcd webui
记得启动Etcd服务。
先安装node,git环境,然后clone。
安装node可以到 http://nodejs.cn/download/ 下载对应的 node 版本,之后双击安装即可。
git clone https://github.com/henszey/etcd-browser.git
cd etcd-browser/
vim server.js
编辑server.js,修改内容如下:
var etcdHost = process.env.ETCD_HOST || '127.0.0.1'; # etcd 主机IP
var etcdPort = process.env.ETCD_PORT || 4001; # etcd 主机端口
var serverPort = process.env.SERVER_PORT || 8000; # etcd-browser 监听端口
然后启动
node server.js
访问:http://127.0.0.1:8000/
有疑问加站长微信联系(非本文作者)