Consul Maintenance
Server
基本配置
# consul.hcl
datacenter = "sh-dc-1"
server = true
bootstrap_expect = 1
node_name = "sh-cloud-1"
client_addr = "0.0.0.0"
data_dir = "/opt/consul/data"
ui_config {
enabled = true
}
acl {
enabled = true
default_policy = "deny"
tokens {
# 最好用 acl bootstrap
master = "consul-master-token"
}
}
启动服务
consul agent -config-file=/opt/consul/consul.hcl -log-level=info
多节点
简单配置不可以应用到生产环境
datacenter = "sh-dc-1" # 直接加入现有数据中心
server = true
node_name = "sh-cloud-1"
client_addr = "0.0.0.0"
data_dir = "/opt/consul/data"
# 连接到lan端口 or wan: retry_join_wan
retry_join = ["sjc1.clusters.zeabur.com:8301"]
# 服务端也要有这个
encrypt = "Nmqtyhh87pvFVY4adCMiU9IrL1fR0OO3VbfkuIYzyls="
生成加密密钥
consul keygen
CLI
export CONSUL_HTTP_TOKEN="consul-master-token"
consul acl token read -self
API
agent
服务列表
curl --header "X-Consul-Token: $CONSUL_HTTP_TOKEN" http://localhost:8500/v1/agent/services
取消服务的注册
curl -X PUT --header "X-Consul-Token: $CONSUL_HTTP_TOKEN" \
http://localhost:8500/v1/agent/service/deregister/app-admin-192-168-8-119-8080
catalog
info
正常使用(注册发现/健康检查)不会用到catalog的API,如果你用到了,说明用的不对。
catalog 服务列表
curl --header "X-Consul-Token: $CONSUL_HTTP_TOKEN" http://localhost:8500/v1/catalog/services
or detail
curl --header "X-Consul-Token: $CONSUL_HTTP_TOKEN" http://localhost:8500/v1/catalog/service/records-service
取消注册 catalog service
curl --request PUT "X-Consul-Token: $CONSUL_HTTP_TOKEN" --data '{
"Datacenter": "dc-1",
"Node": "server-1",
"ServiceID": "records-service"
}' http://localhost:8500/v1/catalog/deregister