etcd
https://raw.githubusercontent.com/etcd-io/etcd/main/logos/etcd-horizontal-color.svg https://raw.githubusercontent.com/etcd-io/etcd/main/logos/etcd-horizontal-color.svg
etcd
etcd is a strongly consistent, distributed key-value store that provides a reliable way to store data that needs to be accessed by a distributed system or cluster of machines. It gracefully handles leader elections during network partitions and can tolerate machine failure, even in the leader node.
OSS の分散 KVS
Sample
Dockerで試す
etcd用のnetworkの作成
code:bash
docker network create etcd-network --subnet=172.16.0.0/24 --ip-range=172.16.0.0/24
etcdコンテナの作成
code:bash
rm -rf /tmp/etcd-data1.tmp && mkdir -p /tmp/etcd-data1.tmp && \
docker run -d \
--network etcd-network \
--ip 172.16.0.4 \
-p 2379:2379 \
-p 2380:2380 \
--mount type=bind,source=/tmp/etcd-data1.tmp,destination=/etcd-data \
--name etcd1 \
gcr.io/etcd-development/etcd:v3.4.5 \
/usr/local/bin/etcd \
--name etcd1 \
--data-dir /etcd-data \
--listen-client-urls http://0.0.0.0:2379 \
--advertise-client-urls http://0.0.0.0:2379 \
--listen-peer-urls http://172.16.0.4:2380 \
--initial-advertise-peer-urls http://172.16.0.4:2380 \
--initial-cluster etcd1=http://172.16.0.4:2380,etcd2=http://172.16.0.5:2382 \
--initial-cluster-token tkn \
--initial-cluster-state new
rm -rf /tmp/etcd-data2.tmp && mkdir -p /tmp/etcd-data2.tmp && \
docker run -d \
--network etcd-network \
--ip 172.16.0.5 \
-p 2381:2381 \
-p 2382:2382 \
--mount type=bind,source=/tmp/etcd-data2.tmp,destination=/etcd-data \
--name etcd2 \
gcr.io/etcd-development/etcd:v3.4.5 \
/usr/local/bin/etcd \
--name etcd2 \
--data-dir /etcd-data \
--listen-client-urls http://0.0.0.0:2381 \
--advertise-client-urls http://0.0.0.0:2381 \
--listen-peer-urls http://172.16.0.5:2382 \
--initial-advertise-peer-urls http://172.16.0.5:2382 \
--initial-cluster etcd1=http://172.16.0.4:2380,etcd2=http://172.16.0.5:2382 \
--initial-cluster-token tkn \
--initial-cluster-state new
etcdコンテナの確認
code:bash
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c5d576185df4 gcr.io/etcd-development/etcd:v3.4.5 "/usr/local/bin/etcd…" 3 minutes ago Up 3 minutes 2379-2380/tcp, 0.0.0.0:2381-2382->2381-2382/tcp etcd2
dbf2bff87e48 gcr.io/etcd-development/etcd:v3.4.5 "/usr/local/bin/etcd…" 4 minutes ago Up 4 minutes 0.0.0.0:2379-2380->2379-2380/tcp etcd1
データ投入と確認
code:bash
curl -X POST -d '{"key": "hoge","value": "fuga"}' http://localhost:2379/v3/kv/put
# 両方で追加されていることを確認
curl -L http://localhost:2379/v3/kv/range -X POST -d '{"key": "hoge"}'
curl -L http://localhost:2381/v3/kv/range -X POST -d '{"key": "hoge"}'
backup
code: (bash)
ETCDCTL_API=3 etcdctl \
--endpoints=https://127.0.0.1:2379 \
--cert=/etc/kubernetes/pki/etcd/ca.crt \
--cacert=/etc/kubernetes/pki/etcd/server.crt \
--key=/etc/kubernetes/pki/etcd/ca.key \
snapshot save /opt/snapshot-pre-boot.db
restore
code: (bash)
ETCDCTL_API=3 etcdctl \
--endpoints=https://[127.0.0.1]:2379 \
--cert=/etc/kubernetes/pki/etcd/server.crt \
--cacert=/etc/kubernetes/pki/etcd/ca.crt \
--key=/etc/kubernetes/pki/etcd/server.key \
--name=master \
--data-dir /var/lib/etcd-from-backup \
--initial-cluster=master=https://127.0.0.1:2380 \
--initial-cluster-token etcd-cluster-1 \
--initial-advertise-peer-urls=https://127.0.0.1:2380 \
snapshot restore /opt/snapshot-pre-boot.db
code: /etc/kubernetes/manifests/etcd.yaml
volumes:
- hostPath:
path: /var/lib/etcd-from-backup
type: DirectoryOrCreate
name: etcd-data
Reference
分散KVS「etcd」の環境を世界最速で構築!! | SIOS Tech. Lab
kubernetes内のetcdをバックアップ・リストアする - goodbyegangsterのブログ