Etcd

From air
Jump to navigation Jump to search

https://coreos.com/using-coreos/etcd/

etcd is an open-source distributed key value store that provides the backbone of CoreOS clusters and the etcd client runs on each machine in a cluster. etcd gracefully handles master election during network partitions and the loss of the current master.

Your applications can read and write data into etcd. Common examples are storing database connection details, cache settings, feature flags, and more. Features

  • Simple, curl-able API (HTTP + JSON)
  • Optional SSL client cert authentication
  • Benchmarked 1000s of writes/s per instance
  • Properly distributed using Raft protocol
  • Keys support TTL
  • Atomic test and set
  • Easily listen for changes to a prefix via HTTP long-polling

In etcd 2.0

  • etcdctl backup was added to make recovering from cluster failure easier
  • etcdctl member list/add/remove commands for easily managing a cluster

Related: CoreOS

First steps

core@core-01 ~ $ etcdctl -h                               
NAME:
   etcdctl - A simple command line client for etcd.

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

VERSION:
   0.4.6

COMMANDS:
   mk		make a new key with a given value
   mkdir	make a new directory
   rm		remove a key
   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 or existing directory
   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
   help, h	Shows a list of commands or help for one command
   
GLOBAL OPTIONS:
   --debug					output cURL commands which can be used to reproduce the request
   --no-sync					don't synchronize cluster information before sending request
   --output, -o 'simple'			output response in the given format (`simple` or `json`)
   --peers, -C '--peers option --peers option'	a comma-delimited list of machine addresses in the cluster (default: "127.0.0.1:4001")
   --version, -v				print the version
   --help, -h					show help
core@core-01 ~ $ etcdctl set /message Hello
Hello
core@core-01 ~ $ etcdctl rm /message

core@core-01 ~ $ etcdctl set /message Hello
Hello
core@core-01 ~ $ etcdctl get /message      
Titi
core@core-01 ~ $ etcdctl rm /message

core@core-01 ~ $ etcdctl set /message Hello
Hello
core@core-01 ~ $ etcdctl ls                
/coreos.com
/first-etcd-key
core@core-01 ~ $ etcdctl ls /
/first-etcd-key
/coreos.com
core@core-01 ~ $ etcdctl ls /message
Error:  100: Key not found (/message) [2977]
core@core-01 ~ $ etcdctl set /message Hello
Hello
core@core-01 ~ $ etcdctl ls /
/coreos.com
/first-etcd-key
/message
core@core-01 ~ $ etcdctl watch /message --recursive
Titi
core@core-01 ~ $ etcdctl set /message "Hi" --swap-with-value "Hello"
Hi
core@core-01 ~ $ etcdctl set /message "Expiring Soon" --ttl 20
Expiring Soon
core@core-02 ~ $ etcdctl get /message
Hello
core@core-02 ~ $ etcdctl get /message
Error:  100: Key not found (/message) [2830]
core@core-02 ~ $ etcdctl get /message
Hello
core@core-02 ~ $ etcdctl set /message Titi
Titi
core@core-02 ~ $ etcdctl get /message
Titi
core@core-02 ~ $ etcdctl get /message
Error:  100: Key not found (/message) [2883]

core@core-02 ~ $ curl -L -X DELETE http://127.0.0.1:4001/v2/keys/message
{"errorCode":100,"message":"Key not found","cause":"/message","index":2902}

core@core-02 ~ $ curl -L -X DELETE http://127.0.0.1:4001/v2/keys/message
{"action":"delete","node":{"key":"/message","modifiedIndex":2909,"createdIndex":2906},"prevNode":{"key":"/message","value":"Hello","modifiedIndex":2906,"createdIndex":2906}}

core@core-02 ~ $ curl -L -X DELETE http://127.0.0.1:4001/v2/keys/me

core@core-02 ~ $ etcdctl set /message Titi
Titi
core@core-02 ~ $ etcdctl set /message Titi
Titi
core@core-02 ~ $ etcdctl set /message Hi  
Hi
core@core-02 ~ $ etcdctl set /message Hello
Hello
core@core-02 ~ $ etcdctl watch /message --recursive
Hi
core@core-02 ~ $ etcdctl get /message    
Expiring Soon
core@core-02 ~ $ etcdctl get /message
Expiring Soon
core@core-02 ~ $ etcdctl get /message
Expiring Soon
core@core-02 ~ $ etcdctl get /message
Expiring Soon
core@core-02 ~ $ etcdctl get /message
Expiring Soon
core@core-02 ~ $ etcdctl get /message
Error:  100: Key not found (/message) [3112]