0%

etcd安装(Ubuntu 18.04)

etcd概述

etcd 是一种Go语言开发的、分布式、高可用的key-value数据库

  • 简单: 接口易用,读/写可通过标准的HTTP工具,例如curl,其背后API是gRPC技术
  • 安全: 支持TLS验证
  • 快: 官方给出的基准测试值为操作每秒可达10000次
  • 稳定可靠: 底层使用raft算法协议保障集群间状态、数据同步

安装 - 单实例

https://github.com/etcd-io/etcd/releases

etcd-linux-install.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ETCD_VER=v3.4.14

# choose either URL
GOOGLE_URL=https://storage.googleapis.com/etcd
GITHUB_URL=https://github.com/etcd-io/etcd/releases/download
DOWNLOAD_URL=${GOOGLE_URL}

rm -f /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
rm -rf /tmp/etcd-download-test && mkdir -p /tmp/etcd-download-test

curl -L ${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz -o /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
tar xzvf /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz -C /tmp/etcd-download-test --strip-components=1
rm -f /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz

/tmp/etcd-download-test/etcd --version
/tmp/etcd-download-test/etcdctl version

更多测试样例可参考README-etcdctl.md

端口使用(默认)

2379 接收客户端请求

2380 集群间通信

systemd

/lib/systemd/system/etcd.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[Unit]
Description=Etcd Service
After=syslog.target network.target

[Service]
Type=notify
ExecStart=/usr/local/bin/etcd --config-file=/etc/etcd/conf.yml

ExecReload=/bin/kill -s HUP $MAINPID
Environment=
#TimeoutSec=0
#RestartSec=2
#Restart=always

[Install]
WantedBy=multi-user.target

/etc/etcd/conf.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
name: test-etcd
data-dir: /var/lib/etcd
listen-client-urls: http://127.0.0.1:2379
advertise-client-urls: http://127.0.0.1:2379
listen-peer-urls: http://127.0.0.1:2380
initial-advertise-peer-urls: http://128.0.0.1:2380
initial-cluster: test-etcd=http://127.0.0.1:2380
initial-cluster-token: test-etcd-cluster-1
initial-cluster-state: new
cert-file: /etc/ssl/etcd/server.pem
key-file: /etc/ssl/etcd/server-key.pem
client-cert-auth: false
trusted-ca-file: /etc/ssl/etcd/ca.pem
auto-tls: false
logger: zap

systemctl测试和管理

1
2
3
4
systemctl daemon-reload
systemctl start etcd
systemctl enable etcd
systemctl status etcd

etcdctl 常用命令

etcdctl v2 v3切换; v2和v3有一定差异,以下ETCDCTL_API=3测试

1
export ETCDCTL_API=[2|3]

put

1
2
3
etcdctl put hello world
etcdctl put hello2 world2
etcdctl put test1 test1

get

1
2
3
etcdctl get hello
etcdctl get hello2
etcdctl get test1

相同前缀get

1
etcdctl get --prefix hello

get所有key-value

1
etcdctl get --prefix ''

get所有key

1
etcdctl get --prefix --keys-only ''

del

1
etcdctl del hello

租约lease,与redis等不同的是使用前需先创建

1
2
3
4
etcdctl lease grant 100 #创建一个lease
etcdctl lease list #罗列在生效范围内的lease
etcdctl put hello world --lease=1cc4761868a0a636 #1cc4761868a0a636要使用的租约id
etcdctl lease timetolive 1cc4761868a0a636 #查看租约剩余时间

更多etcdctl -h

监控

存活监控

1
curl -L http://127.0.0.1:2399/health

参考

https://etcd.io/
https://github.com/etcd-io/etcd
https://wiki.shileizcc.com/confluence/display/etcd/Etcd+Systemd