본문 바로가기

리눅스

[리눅스] consul kv

반응형

consul kv

consul 사용법

$ consul kv
Usage: consul kv <subcommand> [options] [args]

  This command has subcommands for interacting with Consul's key-value
  store. Here are some simple examples, and more detailed examples are
  available in the subcommands or the documentation.

  Create or update the key named "redis/config/connections" with the value "5":

      $ consul kv put redis/config/connections 5

  Read this value back:

      $ consul kv get redis/config/connections

  Or get detailed key information:

      $ consul kv get -detailed redis/config/connections

  Finally, delete the key:

      $ consul kv delete redis/config/connections

  For more examples, ask for subcommand help or view the documentation.

Subcommands:
    delete    Removes data from the KV store
    export    Exports a tree from the KV store as JSON
    get       Retrieves or lists data from the KV store
    import    Imports a tree stored as JSON to the KV store
    put       Sets or updates data in the KV store

데이터 추가(키 생성)

consul kv put web/server/config/webserv1-hostname webserv1
$ consul kv put web/server/config/webserv1-hostname webserv1
Success! Data written to: web/server/config/webserv1-hostname
consul kv put web/server/config/webserv1-ip 1.1.1.1
$ consul kv put web/server/config/webserv1-ip 1.1.1.1
Success! Data written to: web/server/config/webserv1-ip

쿼리 데이터(키 읽기)

consul kv get web/server/config/webserv1-hostname
$ consul kv get web/server/config/webserv1-hostname
webserv1
consul kv get web/server/config/webserv1-ip
$ consul kv get web/server/config/webserv1-ip
1.1.1.1

데이터 삭제(키 삭제)

consul kv delete web/server/config/webserv1-hostname
$ consul kv delete web/server/config/webserv1-hostname
Success! Deleted key: web/server/config/webserv1-hostname
consul kv delete web/server/config/webserv1-ip
$ consul kv delete web/server/config/webserv1-ip
Success! Deleted key: web/server/config/webserv1-ip

consul REST API

export CONSUL_CLIENT_ADDRESS=`ip route get 1.2.3.4 | awk '{ print $7 }' | egrep -v '^$'`
echo $CONSUL_CLIENT_ADDRESS

데이터 저장

curl --request PUT --data 'active' http://${CONSUL_CLIENT_ADDRESS}:8500/v1/kv/redis/state
$ curl --request PUT --data 'active' http://${CONSUL_CLIENT_ADDRESS}:8500/v1/kv/redis/state
true

데이터 호출

curl -s http://${CONSUL_CLIENT_ADDRESS}:8500/v1/kv/redis/state | jq
$ curl -s http://${CONSUL_CLIENT_ADDRESS}:8500/v1/kv/redis/state | jq                  
[
  {
    "LockIndex": 0,
    "Key": "redis/state",
    "Flags": 0,
    "Value": "YWN0aXZl",
    "CreateIndex": 74042,
    "ModifyIndex": 74042
  }
]

데이터 삭제

curl -XDELETE http://${CONSUL_CLIENT_ADDRESS}:8500/v1/kv/redis/state
$ curl -XDELETE http://${CONSUL_CLIENT_ADDRESS}:8500/v1/kv/redis/state
true

in.tpl 파일 생성

vim in.tpl
{{ key "foo" }}

consul-template 실행

consul-template -template "in.tpl:out.txt" -once

터미널 하나를 더 띄워 아래 명령을 실행합니다.

consul kv put foo bar
$ consul kv put foo bar
Success! Data written to: foo

out.txt 파일이 생성됩니다.

$ cat out.txt
bar
728x90
반응형