Writing Skills
writing-skills
效率与协作技能,来自 obra/superpowers,查看原文获取完整用途和操作说明。
consul【部署与运维】Use this skill when the user says 'consul', 'hashicorp consul', 'consul agent', 'consul server', 'consul client', 'consul cluster', 'consul datacenter', 'consul service discovery', 'consul DNS', 'consul catalog', 'consul health check', 'consul KV', 'consul sessions', 'consul ACL', 'consul token', 'consul policy', 'consul role', 'consul connect', 'con…
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
Deploy and operate HashiCorp Consul for service discovery, health checking, KV configuration, multi-datacenter federation, and the Connect service mesh with mTLS, intentions, and Envoy proxying.
Exact user phrases: "consul", "consul agent", "consul server", "consul client", "consul cluster", "consul datacenter", "consul service discovery", "consul DNS", "consul catalog", "consul health check", "consul KV", "consul sessions", "consul ACL", "consul token", "consul connect", "consul service mesh", "consul intention", "consul envoy", "consul mesh gateway", "consul snapshot", "consul federation", "consul admin partition", "consul namespace", "consul k8s", "consul helm", "consul terraform", "consul-template", "consul ESM", "consul NIA", "consul terraform sync", "service discovery".
Consul agent configuration (HCL), service registration definitions, ACL policy/token files, intention rules, or Terraform/Kubernetes manifests.
Consul HCL/JSON configuration, CLI commands, or Kubernetes manifests. No preamble. No explanations unless asked.
dig <service>.service.consul).400 lines.
consul agent -dev → register a service (consul services register svc.hcl or consul service register) → consul catalog services → dig @127.0.0.1 -p 8600 web.service.consul → enable ACLs with consul acl bootstrap → consul connect envoy -sidecar-for web for the mesh.
| Topology | Use Case | Agents | Federation |
|---|---|---|---|
| Single DC | Most applications, one region | 3–5 servers + clients | None |
| Multi-DC (WAN gossip) | Global services, low ACL/plan needs | 3–5 per DC, clients everywhere | WAN gossip + mesh gateways |
| Multi-DC (mesh gateways only) | Isolate gossip, strict security | Same, gateways on boundary | Mesh gateways, no WAN gossip |
| Dev | Local testing | 1 dev agent | None |
# /etc/consul.d/server.hcl
server = true
bootstrap_expect = 3
ui = true
data_dir = "/opt/consul"
log_level = "INFO"
bind_addr = "{{ GetPrivateInterfaces | attr \"address\" }}"
client_addr = "0.0.0.0"
retry_join = ["10.0.1.11", "10.0.1.12", "10.0.1.13"]
ports {
dns = 8600
http = 8500
https = -1
grpc = 8502
serf_lan = 8301
serf_wan = 8302
server = 8300
}
# Encrypt gossip and RPC
encrypt = "A4F4+oAxyOvAThY4c3Kt0sMN0+SIpxEwbjxZTr/BRh0="
verify_server_hostname = true
verify_incoming = true
verify_outgoing = true
ca_file = "/etc/consul.d/tls/consul-agent-ca.pem"
# ACLs
acl = {
enabled = true
default_policy = "deny"
enable_token_persistence = true
tokens = {
master = "a34adf98-9b1d-4be5-8f6a-5c1f2a3b4c5d"
}
}
performance {
raft_multiplier = 1
}
# /etc/consul.d/client.hcl
server = false
data_dir = "/opt/consul"
client_addr = "0.0.0.0"
bind_addr = "{{ GetPrivateInterfaces | attr \"address\" }}"
retry_join = ["10.0.1.11:8301", "10.0.1.12:8301", "10.0.1.13:8301"]
encrypt = "A4F4+oAxyOvAThY4c3Kt0sMN0+SIpxEwbjxZTr/BRh0="
# Client agents proxy traffic and serve DNS locally
# /etc/consul.d/conf.d/web.hcl
service {
name = "web"
id = "web-01"
address = "10.0.1.50"
port = 8080
tags = ["primary", "v1"]
checks = [
{
name = "web-http"
type = "http"
path = "/healthz"
interval = "10s"
timeout = "2s"
status = "passing"
},
{
name = "web-memory"
type = "script"
args = ["/usr/local/bin/check-web.sh"]
interval = "30s"
}
]
enable_tag_override = false
meta {
version = "1.2.0"
}
}
consul services register /etc/consul.d/conf.d/web.hcl
consul catalog services # list unique services
consul catalog nodes -service web # nodes running web
dig @127.0.0.1 -p 8600 web.service.consul +short
dig @127.0.0.1 -p 8600 web.service.consul SRV +short # with host:port
dig @127.0.0.1 -p 8600 web.service.consul A +short # prepared query
# RFC 2782 SRV: _web._tcp.service.consul
Configure /etc/resolv.conf to point at 127.0.0.1:8600 with search consul. for zero-config service resolution inside the cluster.
consul catalog health -service web # health of all web instances
consul health state -passing # all passing
# Automatic deregistration: critical + critical deregistration in check config
consul services deregister /etc/consul.d/conf.d/web.hcl
consul kv put config/webapp/feature_flag enabled
consul kv put config/webapp/db_host db.internal
consul kv get config/webapp/feature_flag
consul kv get -recurse config/webapp
consul kv delete config/webapp/db_host
consul kv put -flags=42 config/webapp/version 2
# consul-template patterns
{{ key "config/webapp/db_host" }}
{{ range key "config/webapp/" }}
Refresh is automatic in consul-template:
template {
contents = "db_host = {{ key \"config/webapp/db_host\" }}"
destination = "/etc/webapp.conf"
command = "systemctl reload webapp"
}
consul session create -name webapp-session -ttl 60s
# Leader election pattern: try to acquire on a KV key
consul kv put -acquire -session=<session-id> leader/webapp $(hostname)
consul kv get -recurse leader/
consul kv put -release -session=<session-id> leader/webapp $(hostname)
consul session info <session-id>
Sessions are used for leader election, distributed locks, and fencing. Set lock-delay to protocol with stale lock holders.
consul acl bootstrap # creates management token (SAVE IT!)
# Policy file
cat > web-policy.hcl <<'EOF'
node_prefix "web-*" {
policy = "read"
}
service_prefix "web" {
policy = "write"
}
key_prefix "config/webapp/" {
policy = "write"
}
session_prefix "" {
policy = "read"
}
EOF
consul acl policy create -name web-app -rules @web-policy.hcl
consul acl token create -policy web-app -description "Web app token"
consul acl token set -id <token-id> -policy web-app -description "update policy"
consul acl token read -self
consul acl token list
# Roles bundle multiple policies for easier management
consul acl role create -name web-team -policy-name web-app -policy-name ops-read
consul acl token create -role web-team
# Service with Envoy sidecar
service {
name = "web"
port = 8080
connect {
sidecar_service {
proxy {
upstreams = [
{
destination_name = "api"
local_bind_port = 7070
},
{
destination_name = "db"
local_bind_port = 5432
}
]
}
}
}
}
consul connect envoy -sidecar-for web -admin-bind 127.0.0.1:19000
# Intentions (allow web -> api)
consul intention create -deny '*' '*' # default deny
consul intention create -allow web api
consul intention create -deny web internal # block web -> internal
consul intentions list
# Transparent proxy — no app changes needed
consul connect envoy -sidecar-for web -transparent-proxy
# Mesh gateway service registration (each DC)
service {
name = "mesh-gateway"
port = 8443
kind = "mesh-gateway"
}
consul connect envoy -mesh-gateway -register -address 10.0.1.70:8443
# Join WAN gossip (server config) or rely on mesh gateways only
consul join -wan 10.0.2.11
consul members -wan
# Requires TLS on WAN; gateways cross the boundary
Admin partitions isolate teams; namespaces organize within a DC.
curl localhost:8500/v1/agent/metrics | jq '.Gauges[] | select(.Name|test("raft|serf|acl|connect"))'
Key metrics:
consul.raft.leader — 1 if leaderconsul.raft.peers / consul.raft.stateconsul.serf.members / consul.serf.eventsconsul.catalog.service.register.countconsul.memberlist.msg.suspectconsul.acl.reject — ACL silencesconsul.connect.proxy.config.executable.versionconsul.health.failing.count (response: "The remote datacenter has no leader")Alert on: no leader, consul.raft.peers < 3, gossip suspect storms, failed checks, consul.acl.reject ratio rising.
consul keygen) and TLS (verify_incoming/verify_outgoing).default_policy = "deny" before joining production workloads.enable_tag_override = false unless the consul agent owns tags.-lock-delay for safe fencing.consul snapshot save and restore with consul snapshot restore; test restores.raft_multiplier carefully; only tune with profiling data.-config-dir for fragments: one service per file eases CI/CD.bootstrap_expect + retry_join for autoclustering; never use -bootstrap in prod.reconnect_timeout/reconnect_timeout_wan tuned for flapping network nodes.autopilot for automatic Raft peer removal: autopilot { cleanup_dead_servers = true }.max_replicas in autopilot for server count enforcement.URI SAN for cross-DC mTLS.consul ESM if using external nodes / instance health health-checks.consul k8s (Helm chart) for K8s bootstrap; consul terraform-sync (NIA) to program LB/firewalls.consul.raft.numPeers and consul.raft.leader.lastContact for quorum health.consul snapshot save + offsite copy; restore into a fresh DC for DR drill.consul operator raft list-peers and consul operator autopilot get-config for ops.-bootstrap in production — two agents can split brain.-reload without staged cert/ACL rotation — partial DC failures.consul connect with default-deny disabled — no mTLS enforcement.devops-nomad for job scheduling with Consul service discovery and Consul Connect.devops-vault for Vault secrets, dynamic credentials, and Vault+Consul integration.devops-terraform for Consul cluster provisioning with Terraform.devops-monitoring for Prometheus/Grafana observability of Consul metrics.devops-kubernetes for consul-k8s Helm chart and service mesh on K8s.