etcd を起動 (インストール) してみた

◆ Live配信スケジュール ◆
サイオステクノロジーでは、Microsoft MVPの武井による「わかりみの深いシリーズ」など、定期的なLive配信を行っています。
⇒ 詳細スケジュールはこちらから
⇒ 見逃してしまった方はYoutubeチャンネルをご覧ください
【4/18開催】VSCode Dev Containersで楽々開発環境構築祭り〜Python/Reactなどなど〜
Visual Studio Codeの拡張機能であるDev Containersを使ってReactとかPythonとかSpring Bootとかの開発環境をラクチンで構築する方法を紹介するイベントです。
https://tech-lab.connpass.com/event/311864/

こんにちは。サイオステクノロジー OSS サポート担当 Y です。

今回は、分散 KVS である etcd について検証してみました。(※以下の内容は etcd 3.3.11/CentOS 7.6 にて検証しています。)

■はじめに

etcd は最近流行りの Kubernetes でも利用されている KVS であり、名前を聞いたことがある方も多いのではないでしょうか。

また、昨年の 12月に CNCF にプロジェクトが寄与されたことが記憶に新しい製品であると思います。

ということで、今回はその etcd を単体で動かしてみました。

■検証

それではさっそく検証してみます。

まずはインストールですが、etcd は Go 言語で書かれているので、基本的にはバイナリをダウンロードすればコンパイル等をしなくてもそのまま利用できます。

[root@etcd ~]# ETCD_VER=v3.3.11
[root@etcd ~]# 
[root@etcd ~]# DOWNLOAD_URL=https://storage.googleapis.com/etcd
[root@etcd ~]# 
[root@etcd ~]# curl -L ${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz -o /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 10.8M  100 10.8M    0     0  2656k      0  0:00:04  0:00:04 --:--:-- 2656k
[root@etcd ~]# 
[root@etcd ~]# tar xzf /tmp/etcd-v3.3.11-linux-amd64.tar.gz -C /tmp/
[root@etcd ~]# 
[root@etcd ~]# ls -l /tmp/etcd-v3.3.11-linux-amd64/
total 34296
drwxr-xr-x 11 1000 1000     4096 Jan 11 20:33 Documentation
-rw-r--r--  1 1000 1000    38864 Jan 11 20:33 README-etcdctl.md
-rw-r--r--  1 1000 1000     7262 Jan 11 20:33 README.md
-rw-r--r--  1 1000 1000     7855 Jan 11 20:33 READMEv2-etcdctl.md
-rwxr-xr-x  1 1000 1000 19237536 Jan 11 20:33 etcd
-rwxr-xr-x  1 1000 1000 15817472 Jan 11 20:33 etcdctl
[root@etcd ~]# 
[root@etcd ~]# cp /tmp/etcd-v3.3.11-linux-amd64/etcd* /usr/local/bin/
[root@etcd ~]# 
[root@etcd ~]# ls -l /usr/local/bin/
total 34236
-rwxr-xr-x 1 root root 19237536 Feb  5 05:46 etcd
-rwxr-xr-x 1 root root 15817472 Feb  5 05:46 etcdctl
[root@etcd ~]# 
[root@etcd ~]# etcd --version
etcd Version: 3.3.11
Git SHA: 2cf9e51d2
Go Version: go1.10.7
Go OS/Arch: linux/amd64
[root@etcd ~]# 
[root@etcd ~]# etcdctl --version
etcdctl version: 3.3.11
API version: 2

では、ダウンロードしたバイナリを使って etcd を起動してみます。(バイナリを実行するとフォアグラウンドで動作するため、以降の作業は別ターミナルから行なっています。)

[root@etcd ~]# mkdir /tmp/etcd-data-dir
[root@etcd ~]# 
[root@etcd ~]# cd /tmp/etcd-data-dir/
[root@etcd etcd-data-dir]# 
[root@etcd etcd-data-dir]# etcd
2019-02-05 06:03:43.232802 I | etcdmain: etcd Version: 3.3.11
2019-02-05 06:03:43.232886 I | etcdmain: Git SHA: 2cf9e51d2
2019-02-05 06:03:43.232895 I | etcdmain: Go Version: go1.10.7
2019-02-05 06:03:43.232902 I | etcdmain: Go OS/Arch: linux/amd64
2019-02-05 06:03:43.232910 I | etcdmain: setting maximum number of CPUs to 4, total number of available CPUs is 4
2019-02-05 06:03:43.232936 W | etcdmain: no data-dir provided, using default data-dir ./default.etcd
2019-02-05 06:03:43.233516 I | embed: listening for peers on https://localhost:2380
2019-02-05 06:03:43.233686 I | embed: listening for client requests on localhost:2379
2019-02-05 06:03:43.235272 I | etcdserver: name = default
2019-02-05 06:03:43.235293 I | etcdserver: data dir = default.etcd
2019-02-05 06:03:43.235303 I | etcdserver: member dir = default.etcd/member
2019-02-05 06:03:43.235310 I | etcdserver: heartbeat = 100ms
2019-02-05 06:03:43.235317 I | etcdserver: election = 1000ms
2019-02-05 06:03:43.235328 I | etcdserver: snapshot count = 100000
2019-02-05 06:03:43.235342 I | etcdserver: advertise client URLs = https://localhost:2379
2019-02-05 06:03:43.235351 I | etcdserver: initial advertise peer URLs = https://localhost:2380
2019-02-05 06:03:43.235363 I | etcdserver: initial cluster = default=https://localhost:2380
2019-02-05 06:03:43.248709 I | etcdserver: starting member 8e9e05c52164694d in cluster cdf818194e3a8c32
2019-02-05 06:03:43.248757 I | raft: 8e9e05c52164694d became follower at term 0
2019-02-05 06:03:43.248802 I | raft: newRaft 8e9e05c52164694d [peers: [], term: 0, commit: 0, applied: 0, lastindex: 0, lastterm: 0]
2019-02-05 06:03:43.248815 I | raft: 8e9e05c52164694d became follower at term 1
2019-02-05 06:03:43.252739 W | auth: simple token is not cryptographically signed
2019-02-05 06:03:43.254072 I | etcdserver: starting server... [version: 3.3.11, cluster version: to_be_decided]
2019-02-05 06:03:43.254634 I | etcdserver: 8e9e05c52164694d as single-node; fast-forwarding 9 ticks (election ticks 10)
2019-02-05 06:03:43.255168 I | etcdserver/membership: added member 8e9e05c52164694d [https://localhost:2380] to cluster cdf818194e3a8c32
2019-02-05 06:03:43.349265 I | raft: 8e9e05c52164694d is starting a new election at term 1
2019-02-05 06:03:43.349317 I | raft: 8e9e05c52164694d became candidate at term 2
2019-02-05 06:03:43.349380 I | raft: 8e9e05c52164694d received MsgVoteResp from 8e9e05c52164694d at term 2
2019-02-05 06:03:43.349404 I | raft: 8e9e05c52164694d became leader at term 2
2019-02-05 06:03:43.349417 I | raft: raft.node: 8e9e05c52164694d elected leader 8e9e05c52164694d at term 2
2019-02-05 06:03:43.349793 I | etcdserver: setting up the initial cluster version to 3.3
2019-02-05 06:03:43.350544 N | etcdserver/membership: set the initial cluster version to 3.3
2019-02-05 06:03:43.350606 I | etcdserver: published {Name:default ClientURLs:[https://localhost:2379]} to cluster cdf818194e3a8c32
2019-02-05 06:03:43.350653 I | etcdserver/api: enabled capabilities for version 3.3
2019-02-05 06:03:43.350742 I | embed: ready to serve client requests
2019-02-05 06:03:43.351541 N | embed: serving insecure client requests on 127.0.0.1:2379, this is strongly discouraged!

特にオプション等を指定しない場合は、カレントディレクトリ配下に default.etcd というディレクトリが作成され、その配下に etcd のデータ (関連する各ファイル) が格納されるようです。

[root@etcd ~]# ls -lR /tmp/etcd-data-dir/default.etcd/
/tmp/etcd-data-dir/default.etcd/:
total 0
drwx------ 4 root root 29 Feb  5 06:03 member

/tmp/etcd-data-dir/default.etcd/member:
total 0
drwx------ 2 root root 16 Feb  5 06:03 snap
drwx------ 2 root root 64 Feb  5 06:03 wal

/tmp/etcd-data-dir/default.etcd/member/snap:
total 20
-rw------- 1 root root 20480 Feb  5 06:03 db

/tmp/etcd-data-dir/default.etcd/member/wal:
total 125000
-rw------- 1 root root 64000000 Feb  5 06:03 0.tmp
-rw------- 1 root root 64000000 Feb  5 06:45 0000000000000000-0000000000000000.wal
[root@etcd ~]# 

それでは、標準で提供されているクライアントツールである etcdctl コマンドを使い、起動した etcd に対してデータの作成/参照/更新/削除を実施してみます。

[root@etcd ~]# etcdctl mk test_key "test value"
test value
[root@etcd ~]# 
[root@etcd ~]# etcdctl ls
/test_key
[root@etcd ~]# 
[root@etcd ~]# etcdctl get test_key
test value
[root@etcd ~]# 
[root@etcd ~]# etcdctl set test_key "new value"
new value
[root@etcd ~]# 
[root@etcd ~]# etcdctl get test_key
new value
[root@etcd ~]# 
[root@etcd ~]# etcdctl rm test_key
PrevNode.Value: new value
[root@etcd ~]# 
[root@etcd ~]# etcdctl ls
[root@etcd ~]# 

各サブコマンドの詳細等は割愛しますが、etcdctl コマンドの利用方法につては etcdctl help コマンドで確認することができます。

[root@etcd /]# etcdctl help
NAME:
   etcdctl - A simple command line client for etcd.

WARNING:
   Environment variable ETCDCTL_API is not set; defaults to etcdctl v2.
   Set environment variable ETCDCTL_API=3 to use v3 API or ETCDCTL_API=2 to use v2 API.

USAGE:
   etcdctl [global options] command [command options] [arguments...]
   
VERSION:
   3.3.11
   
COMMANDS:
     backup          backup an etcd directory
     cluster-health  check the health of the etcd cluster
     mk              make a new key with a given value
     mkdir           make a new directory
     rm              remove a key or a directory
     rmdir           removes the key if it is an empty directory or a key-value pair
     get             retrieve the value of a key
     ls              retrieve a directory
     set             set the value of a key
     setdir          create a new directory or update an existing directory TTL
     update          update an existing key with a given value
     updatedir       update an existing directory
     watch           watch a key for changes
     exec-watch      watch a key for changes and exec an executable
     member          member add, remove and list subcommands
     user            user add, grant and revoke subcommands
     role            role add, grant and revoke subcommands
     auth            overall auth controls
     help, h         Shows a list of commands or help for one command

~(以下略)~

また、etcd ではディレクトリの作成及び、パス形式での key の指定が可能です。etcdctl ls –recursive コマンドで、再起的にディレクトリ及び key の一覧を確認することも可能です。

[root@etcd ~]# etcdctl mk key1 "test value1"
test value1
[root@etcd ~]# 
[root@etcd ~]# etcdctl get key1
test value1
[root@etcd ~]# 
[root@etcd ~]# etcdctl mkdir /hoge/fuga
[root@etcd ~]# 
[root@etcd ~]# etcdctl mk /hoge/key2 "test value2"
test value2
[root@etcd ~]# 
[root@etcd ~]# etcdctl mk /hoge/fuga/key3 "test value3"
test value3
[root@etcd ~]# 
[root@etcd ~]# etcdctl ls -p --recursive
/key1
/hoge/
/hoge/fuga/
/hoge/fuga/key3
/hoge/key2
[root@etcd ~]# 
[root@etcd ~]# etcdctl get /hoge/key2
test value2
[root@etcd ~]# 
[root@etcd ~]# etcdctl get /hoge/fuga/key3
test value3

ディレクトリを削除する場合は etcdctl rmdir を実行します。

[root@etcd ~]# etcdctl rm key1
PrevNode.Value: test value1
[root@etcd ~]# 
[root@etcd ~]# etcdctl ls -p --recursive
/hoge/
/hoge/fuga/
/hoge/fuga/key3
/hoge/key2
[root@etcd ~]# 
[root@etcd ~]# etcdctl rm /hoge/fuga/key3
PrevNode.Value: test value3
[root@etcd ~]# 
[root@etcd ~]# etcdctl ls -p --recursive
/hoge/
/hoge/fuga/
/hoge/key2
[root@etcd ~]# 
[root@etcd ~]# etcdctl rmdir /hoge/fuga
[root@etcd ~]# 
[root@etcd ~]# etcdctl ls -p --recursive
/hoge/
/hoge/key2
[root@etcd ~]# 

また、基本的に削除するディレクトリは空 (key がない) である必要があり、ディレクトリ内に key が残っていると以下の様なエラーになります。

[root@etcd ~]# etcdctl rmdir /hoge
Error:  108: Directory not empty (/hoge) [13]

etcdctl rm –recursive を利用すると再帰的かつディレクトリ内に含まれる key もまとめて削除することができます。(Linux でいうところの rm -rf コマンドの様な処理になるので、慎重に実行した方が良さそうです…)

[root@etcd ~]# etcdctl ls -p --recursive
/hoge/
/hoge/key2
[root@etcd ~]# 
[root@etcd ~]# etcdctl rm --recursive /hoge
[root@etcd ~]# 
[root@etcd ~]# etcdctl ls -p --recursive
[root@etcd ~]# 

■まとめ

今回は etcd を単体で起動し、基本的な DB 操作を実行してみました。

今回の様に単体で動作させることも可能ですが、最初に記載した通り etcd は “分散 KVS” であるため、etcd 自体にクラスタリングの機能が備わっています。

次回は etcd のクラスタリングの動作検証を実施してみようと思います。

アバター画像
About サイオステクノロジーの中の人 41 Articles
サイオステクノロジーで働く中の人です。
ご覧いただきありがとうございます! この投稿はお役に立ちましたか?

役に立った 役に立たなかった

1人がこの投稿は役に立ったと言っています。


ご覧いただきありがとうございます。
ブログの最新情報はSNSでも発信しております。
ぜひTwitterのフォロー&Facebookページにいいねをお願い致します!



>> 雑誌等の執筆依頼を受付しております。
   ご希望の方はお気軽にお問い合わせください!

Be the first to comment

Leave a Reply

Your email address will not be published.


*


質問はこちら 閉じる