LoginSignup
14
13

More than 5 years have passed since last update.

etcdctlコマンドの使い方

Last updated at Posted at 2016-11-15

1. etcdのデータベース

etcdのデータベースは、/var/lib/etcd(デフォルト)配下に格納される。
下記2つのサブディレクトリから構成される。walとsnapは別の場所に置くことが可能。

ディレクトリの名前 用途
wal(write ahead log) write ahead log とは???
snap snapshotsが保存される。snapshotsとは何?

2. 環境

  • 2台構成(ホスト名はmaster1,master2)

3. 確認に使用したetcdcltコマンド版数

[root@master1 ~]# etcdctl --version
etcdctl version 2.3.7

4. etcdctlコマンドの使い方

4.1 ヘルプの使い方


etcdctlコマンドの後に-hオプションを付ける。
[root@master1 ~]# etcdctl -h
NAME:
   etcdctl - A simple command line client for etcd.

USAGE:
   etcdctl [global options] command [command options] [arguments...]

VERSION:
   2.3.7

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
   import               import a snapshot to a cluster
   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

以下、略

4.2 サブコマンドのヘルプの使い方

サブコマンドの後に-hオプションを付ける。
[root@master1 ~]# etcdctl ls -h
NAME:
   etcdctl ls - retrieve a directory

USAGE:
   etcdctl ls [command options] [key]

OPTIONS:
   --sort       returns result in sorted order
   --recursive  returns all key names recursively for the given path
   -p           append slash (/) to directories
   --quorum     require quorum for get request

[root@master1 ~]# etcdctl backup -h
NAME:
   etcdctl backup - backup an etcd directory

USAGE:
   etcdctl backup [command options]

OPTIONS:
   --data-dir           Path to the etcd data dir
   --wal-dir            Path to the etcd wal dir
   --backup-dir         Path to the backup dir
   --backup-wal-dir     Path to the backup wal dir
   --keep-cluster-id    Do not rewrite the cluster id
   --node-id            Use custom node id instead of a random value

4.3 ディレクトリの作成

ディレクトリを表示する(ls)
[root@master1 ~]# etcdctl ls
/scheduler
/registry
/controller

/a/bというディレクトリを作る(mkdir)
[root@master1 ~]# etcdctl mkdir /a/b

/a配下にbというディレクトリができたことがわかる。
[root@master1 ~]# etcdctl ls /a
/a/b

4.4 キー&バリューの設定

作成したディレクトリ(/a/b)にos=centos6を設定する。
[root@master1 ~]# etcdctl set /a/b/os centos6
centos6
[root@master1 ~]# etcdctl get /a/b/os
centos6

作成したディレクトリ(/a/b)にcar=GT-Rを設定する。
[root@master1 ~]# etcdctl set /a/b/car GT-R
GT-R
[root@master1 ~]# etcdctl get /a/b/car
GT-R

ディレクトリ(/a/b)配下を表示する。
[root@master1 ~]# etcdctl ls /a/b
/a/b/os
/a/b/car

/a配下を再帰的に表示する。
[root@master1 ~]# etcdctl ls /a --recursive
/a/b
/a/b/os
/a/b/car

master2でもディレクトリ(/a/b)配下を確認する。master1と同じ結果が表示された。
[root@master2 ~]# etcdctl ls /a/b
/a/b/os
/a/b/car

4.5 ディレクトリ、キー&バリューの削除

master2でos=centos6を削除する。
[root@master2 ~]# etcdctl rm /a/b/os
PrevNode.Value: centos6

削除できたかどうか確認する。os=centos6が削除できた。
[root@master2 ~]# etcdctl get /a/b/os
Error:  100: Key not found (/a/b/os) [888]

car=GT-Rを削除する。
[root@master2 ~]# etcdctl rm /a/b/car
PrevNode.Value: GT-R

削除できたかどうか確認する。car=GT-Rが削除できた。
[root@master2 ~]# etcdctl get /a/b/car
Error:  100: Key not found (/a/b/car) [902]

os=centos6,car=GT-Rが両方とも削除できた。
[root@master2 ~]# etcdctl ls /a/b
[root@master2 ~]#

bディレクトリを削除する。
[root@master2 ~]# etcdctl rmdir /a/b

a配下のbディレクトリが削除できた。
[root@master2 ~]# etcdctl ls /a
[root@master2 ~]#

aディレクトリも削除する。
[root@master2 ~]# etcdctl rmdir /a

aディレクトリが削除できた。
[root@master2 ~]# etcdctl ls
/scheduler
/registry
/controller

4.6 指定した値(value)にアップデート(updateオプション)する。

"os"というキーに"centos6"という値を設定する。
[root@master1 ~]# etcdctl set /a/b/os centos6
centos6

設定した値を確認する。
[root@master1 ~]# etcdctl get /a/b/os
centos6

osをcentos7にアップデートする。
[root@master1 ~]# etcdctl update /a/b/os centos7
centos7

設定した値を確認する。指定した値にアップデートできたことがわかる。
[root@master1 ~]# etcdctl get /a/b/os
centos7

4.7 ディレクトリを再帰的(recursiveオプション)に削除する。

[root@master1 ~]# etcdctl mkdir /a/b/c
[root@master1 ~]# etcdctl ls /a --recursive
/a/b
/a/b/c
[root@master1 ~]# etcdctl rm /a --recursive
[root@master1 ~]# etcdctl ls /a --recursive
Error:  100: Key not found (/a) [1367]
[root@master1 ~]#

4.8 ディレクトリに寿命(ttlオプション)を設定する。

10秒間だけ存在するディレクトリを作成する。
[root@master1 ~]# etcdctl mkdir /a --ttl 10;date
2016年 11月 15日 火曜日 21:00:57 JST

[root@master1 ~]# etcdctl ls;date
/registry
/controller
/a
/scheduler
2016年 11月 15日 火曜日 21:00:59 JST

-中略-

[root@master1 ~]# etcdctl ls;date
/scheduler
/registry
/controller
/a
2016年 11月 15日 火曜日 21:01:07 JST

12秒経過したときには、/aディレクトリは消えている。
[root@master1 ~]# etcdctl ls;date
/scheduler
/registry
/controller
2016年 11月 15日 火曜日 21:01:09 JST

4.9 キーに設定された値の変化(watchオプション)を監視する。

osにcentos6を設定する。
[root@master1 ~]# etcdctl mkdir /a/b
[root@master1 ~]# etcdctl set /a/b/os centos6
centos6

watchオプションで値の変化を監視する。
[root@master1 ~]# etcdctl watch /a/b/os

master2でosにcentos7を設定する。
[root@master2 ~]# etcdctl set /a/b/os centos7
centos7

osの値が変化したことで、etcdctlコマンドのブロックが解除された。
[root@master1 ~]# etcdctl watch /a/b/os
centos7

4.10 データベースをバックアップする。

データベースのディレクトリ(--data-dir)を確認する。
[root@master1 ~]# ps aux|grep etcd
etcd       1024  7.8  2.2  40112 22232 ?        Ssl  19:36   9:17 /usr/bin/etcd --name=master1 --data-dir=/var/lib/etcd/default.etcd --listen-client-urls=http://0.0.0.0:2379

バックアップ用のディレクトリを作成する。
[root@master1 ~]# mkdir /backup

データベースを/backup配下にバックアップする。
[root@master1 ~]# etcdctl backup --data-dir=/var/lib/etcd/default.etcd --backup-dir=/backup

バックアップしたデータベースを確認する。
[root@master1 ~]# ls -R /backup/
/backup/:
member

/backup/member:
snap  wal

/backup/member/snap:

/backup/member/wal:
0000000000000000-0000000000000000.wal

4.11 データベース(walのみ)をバックアップする。

バックアップ用のディレクトリを作成する。
[root@master1 ~]# mkdir /backup

データベースを/backup配下にバックアップする。
[root@master1 ~]# etcdctl backup --wal-dir=/var/lib/etcd/default.etcd/member/wal --backup-wal-dir=/backup

バックアップしたデータベースを確認する。
[root@master1 ~]# ls -R /backup/
/backup/:
0000000000000000-0000000000000000.wal

4.12 コマンド実行時の詳細情報(--debug)を出力する。

[root@master1 ~]# etcdctl --debug ls
start to sync cluster using endpoints(http://127.0.0.1:2379,http://127.0.0.1:4001)
cURL Command: curl -X GET http://127.0.0.1:2379/v2/members
got endpoints(http://192.168.0.20:2379,http://192.168.0.10:2379) after sync
Cluster-Endpoints: http://192.168.0.20:2379, http://192.168.0.10:2379
cURL Command: curl -X GET http://192.168.0.20:2379/v2/keys/?quorum=false&recursive=false&sorted=false
/registry
[root@master1 ~]#

4.13 ディレクトリに"/"を付けてわかりやすく表示する(-p)。

オプション(-p)を付けずに実行する。ディレクトリがどれだかわからない。
[root@master1 ~]# etcdctl ls --recursive
/registry
-中略-
/registry/minions
/registry/minions/master2
/registry/minions/master1

オプション(-p)を使うと、minionsはディレクトリだとわかる。master1,master2はディレクトリ内のキーだとわかる。
[root@master1 ~]# etcdctl ls -p --recursive
/registry/
-中略-
/registry/minions/
/registry/minions/master2
/registry/minions/master1

4.14 HTTPでアクセスする。


ディレクトリを表示する。現在は、/registryのみ存在する。
[root@master1 ~]# etcdctl ls
/registry

/直下にOS=linuxを設定する。
[root@master1 ~]# curl http://localhost:2379/v2/keys/os -XPUT -d "value=linux"
{"action":"set","node":{"key":"/os","value":"linux","modifiedIndex":8815,"createdIndex":8815}}

etcdctlコマンドでキーの値を表示する。
[root@master1 ~]# etcdctl ls
/os
/registry

etcdctlコマンドでバリューの値を表示する。osにlinuxが設定されていることが確認できる。
[root@master1 ~]# etcdctl get /os
linux

HTTPでosに設定されているバリューを読みだす。etcdctlコマンドと同じ結果が得られた。
[root@master1 ~]# curl http://localhost:2379/v2/keys/os -XGET
{"action":"get","node":{"key":"/os","value":"linux","modifiedIndex":8893,"createdIndex":8893}}

キーを削除する。
[root@master1 ~]# curl http://localhost:2379/v2/keys/os -XDELETE
{"action":"delete","node":{"key":"/os","modifiedIndex":8962,"createdIndex":8893},"prevNode":{"key":"/os","value":"linux","modifiedIndex":8893,"createdIndex":8893}}

[root@master1 ~]# etcdctl ls
/registry

5. 参考情報

Deployment Considerations for ETCD in Production Kubernetes Clusters

14
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
13