hashicorp에서 만든 Consul, 거기서 지정한 DevOps 관련 내용 중 Monitor에 해당하는 부분입니다.
Consul?
"Service Mesh Made Easy"
(Consul is a distributed service mesh to connect, secure, and configure services across any runtime platform and public or private cloud)
공식 홈페이지의 설명(?)으론 위와 같습니다. Service Mesh를 쉽게 해주는 툴이며 Service Mesh Architecture, 즉 클라우드 환경에서 MicroService Architecture 에서 서비스간 통신을 위한 메커니즘 구현을 쉽게 해준다는 이야기입니다.
Service Mesh(MicroService Architecture)에서 추구하는 기능들은 여러가지가 있습니다.
Service Discovery Load Balancing Dynamic Request Routing Circuit Breaking ...등등
이 중 Consul에서 눈에 띄는 부분들은 Health check + Service Discovery + Key/value 입니다. (제가 사용하고, 찾아본 부분들이 이정도라 그정도 시야인듯하네요)
정리하자면, Consul을 이용하면 Health check, Service Discovery 등 서비스간 통신 채널을 복잡하지 않고 깔끔하게 구현할 수 있다고 보입니다. (보통 클라우드쪽에서 많이 이야기나오는듯하네요)
How to Install?
Consul은 단일 바이너리로 동작하며 옵션을 통해 크게 Agent와 Server로 기능을 나누어 처리할 수 있습니다. 설치 자체는 뭐 크게 불편한점 없습니다. 바이너리의 경우 OS 버전에 맞는 바이너리 받아서 실행하면되고, 직접 빌드 시 git clone 후 make 해주시면 됩니다.빌드의 경우 Consul이 go로 만들어졌기 때문에 GOPATH 지정이 필요합니다.
[ Binary ]
https://www.consul.io/downloads.html
[ Build ]
#> mkdir -p $GOPATH/src/github.com/hashicorp&& cd $! #> git clone https://github.com/hashicorp/consul.git #> cd consul
빌드,
#> make tools
or
#> make dev
직접 바이너리를 실행하여 구동할 수 있습니다. /usr/bin 에 넣고 명령행처럼 사용해도 되고, 아님 바이너리 직접 호출해서 쓰셔도 무방합니다.
#> consul -v Usage: consul [--version] [--help] <command> [<args>] Available commands are: agent Runs a Consul agent catalog Interact with the catalog connect Interact with Consul Connect event Fire a new event exec Executes a command on Consul nodes force-leave Forces a member of the cluster to enter the "left" state info Provides debugging information for operators. intention Interact with Connect service intentions join Tell Consul agent to join cluster keygen Generates a new encryption key keyring Manages gossip layer encryption keys kv Interact with the key-value store leave Gracefully leaves the Consul cluster and shuts down lock Execute a command holding a lock maint Controls node or service maintenance mode members Lists the members of a Consul cluster monitor Stream logs from a Consul agent operator Provides cluster-level tools for Consul operators reload Triggers the agent to reload configuration files rtt Estimates network round trip time between nodes snapshot Saves, restores and inspects snapshots of Consul server state validate Validate config files/directories version Prints the Consul version watch Watch for changes in Consul
따로 정리할까 하다가 너무 많아서 그냥 링크 하나 남겨놓습니다. Agent, Server 모드 이외에도 현재 Health나 Discovery 정보를 얻어올 수 있는 옵션들이 많습니다.
Agent-Server로 구성하고 Cli 모드로 추가적인 정보를 가져와서 다른 Application에서 활용할 수 있겠네욤
[ Cli commands ]
https://www.consul.io/docs/commands/index.html
서비스 구동 방식
실제로 사용할떈 Client-Server 느낌으로 보이지만, Agent 또한 서버로 동작하며 이를 총 관리해주는 리더서버가 존재합니다.리더서버 : Consul Server
노드서버 : Consul Agent
Consul server
음 우선 리더서버인 Consul server는 Node(Consul Agent)들의 정보를 관리합니다. 데모 주소 보시면 어떤 느낌인지 감 오실겁니다.[ demo ]
https://demo.consul.io/ui/
우선 관리서버 Web UI를 위해 hashicorp 홈페이지에서 consul web ui를 다운받아 압축 해제합니다.
#> cd /consul-ui #> wget https://releases.hashicorp.com/consul/0.7.2/consul_0.7.2_web_ui.zip #> unzip consul_0.7.2_web_ui.zip
이제 Consul server를 동작할텐데요, 동작에 앞서 Key generate가 필요합니다. generate된 key를 각각 Agent의 설정 파일로 넘겨주어 통신을 성공시킬 수 있습니다.
#> consul key gen zRaQ1ZhnLB777nw0Lpoo2w==
해당 값은 복사해두셨다가 Agent config에 넣어주시면 됩니다.
다시 본론으로 와서, server의 config 파일을 작성합니다. 여기서 아까 압축 해제한 web ui 디렉토리 경로 또한 추가해줍니다. (ui_dir)
#>vim /etc/consul.d/server/config.json
{
"bootstrap": true,
"server": true,
"datacenter": "test",
"ui_dir": "/consul-ui",
"data_dir": "/data/consul",
"encrypt": "[key 생성 내용]",
"addresses": {
"http": "[Consul 서버 IP]"
}
}
그러고 consul agent를 --server 옵션을 주어 실행하여 서버로 동작시킵니다.
#> consul agent --server -config-dir=/etc/consul.d/server -bind=[Consul 서버 IP]
Consul Agent
Agent는 아까 생성한 키 값을 포함하여 설정 파일을 만들어줍니다. 서버 IP도 기록해줍니다.#> vim /etc/consul.d/agent/config.json
{
"server": false,
"datacenter": "test",
"data_dir": "/data/consul",
"encrypt": "[key 생성 내용]",
"start_join": ["[Consul 서버 IP]"]
}
그러고 agent 옵션에 --server 없이 명령행으로 전달하면 Consul Agent(node)가 생성되고 통신을 시도하게 됩니다.
#> consul agent -config-dir /etc/consul.d/agent -bind=[Consul agent IP]
Consul은 실행(Server/Agent) 시 8500 포트가 올라가며 이를 통해 서비스 등록/제거를 할 수 있습니다.
http://127.0.0.1:8500/v1/agent/service/register
http://127.0.0.1:8500/v1/agent/service/deregister/[id]
등록 시 JSON 포맷으로 개별 서비스의 설정 정보를 같이 전달해줍니다.
{
"service": {
"name": "HAHWUL-Redis",
"tags": ["New"],
"address": "",
"port": 8000,
"enableTagOverride": false,
"checks": [
{
# "script": "/data/redis.rb", # 일반 스크립트도 가능합니다.
"http" : "http://127.0.0.1:4000/health", # 10초마다 /health 페이지를 접근하여 체크합니다.
"interval": "10s"
}
]
}
}
이후 Consul에서 Cli든 API던 서비스 정보를 받아올 수 있어 이를 이용하여 서비스에 필요한 로직을 추가로 구현하면 됩니당 :)
![]() |
HAHWULSecurity engineer, Gopher and H4cker! |
0 개의 댓글:
Post a Comment