etcd
etcd2

【etcd】とりあえずetcd(v2) コンテナを動かす

More than 1 year has passed since last update.

etcdのv2(最新はv3)のコンテナを起動し使ってみるところまで。

Vagrantfile

使用するVagrantfile。
Dockerのインストール&quay.io/coreos/etcd:v2.3.7イメージをpullする。

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  # If true, then any SSH connections made will enable agent forwarding.
  # Default value: false
  config.ssh.forward_agent = true

  config.vm.define "host" do |d|
    d.vm.box = "ubuntu/trusty64"
    d.vm.network :private_network, ip: "192.168.33.10", virtualbox__intnet: "intnet"

    d.vm.provision "docker" do |d|
       d.pull_images "quay.io/coreos/etcd:v2.3.7"
    end
  end
end
$ docker images 
REPOSITORY            TAG                 IMAGE ID            CREATED             SIZE
quay.io/coreos/etcd   v2.3.7              e81032a59e55        5 months ago        32.29 MB

etcdコンテナの起動

$ docker run -d --name etcd -p 2379:2379 -p 4001:4001 quay.io/coreos/etcd:v2.3.7 --advertise-client-urls 'http://192.168.33.10:2379,http://192.168.33.10:4001' --listen-client-urls 'http://0.0.0.0:2379,http://0.0.0.0:4001'

$ docker ps 
CONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS              PORTS                                                                NAMES
4e71ac2989c1        quay.io/coreos/etcd:v2.3.7   "/etcd --advertise-cl"   52 seconds ago      Up 51 seconds       0.0.0.0:2379->2379/tcp, 2380/tcp, 0.0.0.0:4001->4001/tcp, 7001/tcp   etcd

v2 API

APIをいくつか実行してみる

Version

/versionで動作確認。

$ curl http://127.0.0.1:2379/version | jq . 
{
  "etcdcluster": "2.3.0",
  "etcdserver": "2.3.7"
}

Key/Valueのセット

Let's set the first key-value pair in the datastore. In this case the key is /message and the value is Hello world.

/messageキーに"Hello world"をセットする

$ curl http://127.0.0.1:2379/v2/keys/message -XPUT -d value="Hello world" | jq . 
{
  "node": {
    "createdIndex": 4,
    "modifiedIndex": 4,
    "value": "Hello world",
    "key": "/message"
  },
  "action": "set"
}

KeyからValueを取得

We can get the value that we just set in /message by issuing a GET request:

同じエンドポイントに対しGETリクエストでValueを取得。

$ curl http://127.0.0.1:2379/v2/keys/message | jq . 
{
  "node": {
    "createdIndex": 4,
    "modifiedIndex": 4,
    "value": "Hello world",
    "key": "/message"
  },
  "action": "get"
}

etcdctl

インストール

$ curl -L  https://github.com/coreos/etcd/releases/download/v2.3.7/etcd-v2.3.7-linux-amd64.tar.gz -o etcd-v2.3.7-linux-amd64.tar.gz
$ tar xzvf etcd-v2.3.7-linux-amd64.tar.gz
$ cd etcd-v2.3.7-linux-amd64
$ ./etcdctl cluster-health
member ce2a822cea30bfca is healthy: got healthy result from http://0.0.0.0:2379
cluster is healthy

つかう

$ ./etcdctl --endpoint="http://127.0.0.1:2379" --no-sync get  message 
Hello world
$ ./etcdctl --endpoint="http://127.0.0.1:2379" --no-sync set some-key some-value
some-value
$ ./etcdctl --endpoint="http://127.0.0.1:2379" --no-sync get some-key 
some-value

endpointはデフォルトでhttp://127.0.0.1:2379なのでいらないけどいちおう。

参考

http://stackoverflow.com/questions/33134126/getting-connection-refused-when-trying-to-access-etcd-from-within-a-docker-con

https://coreos.com/etcd/docs/latest/docker_guide.html

https://coreos.com/etcd/docs/latest/v2/api.html#getting-the-etcd-version

https://github.com/coreos/etcd/releases?after=v3.0.0

https://github.com/coreos/etcd/blob/master/etcdctl/READMEv2.md