Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
Know it before you install
Servers (3 or 5) store the catalog and KV with Raft. Client agents run on every node, register local services and run their health checks, and talk to the servers. Agents find each other with gossip (Serf). ACLs decide who may read or write what (tokens).
| Item | Value |
|---|---|
| Package | consul (HashiCorp repository) |
| Service | consul.service |
| Config | /etc/consul.d/*.hcl |
| Data | /opt/consul |
| Ports | 8300 (server RPC), 8301 tcp+udp (LAN gossip), 8302 (WAN), 8500 (HTTP), 8501 (HTTPS), 8600 tcp+udp (DNS) |
| This guide | servers consul1–3 (10.0.4.11–13), datacenter dc1 |
Notitie
Consul uses the BSL license (since 1.17). For Kubernetes-only service discovery, Kubernetes DNS is enough; Consul helps when VMs and several platforms must find each other.
Install on RHEL, Rocky, Alma and Fedora
Install on Ubuntu and Debian
curl -fsSL https://apt.releases.hashicorp.com/gpg | gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" > /etc/apt/sources.list.d/hashicorp.list
apt-get update
apt-get install -y consulPrepare keys and certificates (once)
On one server:
consul keygen
pUqJrVyVRj5jsiYEkM/tFQYfWyJIv4s3XkvDwy7Cu5s=
consul tls ca create
consul tls cert create -server -dc dc1
consul tls cert create -server -dc dc1
consul tls cert create -server -dc dc1Copy consul-agent-ca.pem and one server cert/key pair (dc1-server-consul-N.pem, …-key.pem) to each server's /etc/consul.d/, owner consul, keys 600. Keep consul-agent-ca-key.pem offline.
First configuration: the server cluster
datacenter = "dc1"
data_dir = "/opt/consul"
node_name = "consul1"
bind_addr = "10.0.4.11"
client_addr = "127.0.0.1 10.0.4.11"
server = true
bootstrap_expect = 3
retry_join = ["10.0.4.11", "10.0.4.12", "10.0.4.13"]
ui_config { enabled = true }
encrypt = "<the key from consul keygen>"
tls {
defaults {
ca_file = "/etc/consul.d/consul-agent-ca.pem"
cert_file = "/etc/consul.d/dc1-server-consul-0.pem"
key_file = "/etc/consul.d/dc1-server-consul-0-key.pem"
verify_incoming = true
verify_outgoing = true
}
internal_rpc { verify_server_hostname = true }
}
ports {
http = -1
https = 8501
}
acl {
enabled = true
default_policy = "deny"
enable_token_persistence = true
}| Setting | Why |
|---|---|
bootstrap_expect = 3 | The cluster starts electing a leader when 3 servers are there. |
retry_join | Servers (and later clients) find each other by themselves. |
encrypt | Gossip traffic is encrypted (same key on every agent). |
tls with verify_* | Only agents with certificates from your CA may talk RPC; HTTPS API. |
http = -1, https = 8501 | No plain HTTP API. |
acl … default_policy = "deny" | Nothing is allowed without a token. |
Open the ports between Consul nodes, start all three, bootstrap ACLs once:
firewall-cmd --permanent --add-port={8300,8301,8302,8501,8600}/tcp --add-port={8301,8302,8600}/udp
firewall-cmd --reload
systemctl enable --now consul
export CONSUL_HTTP_ADDR=https://127.0.0.1:8501 CONSUL_CACERT=/etc/consul.d/consul-agent-ca.pem
consul acl bootstrap
SecretID: 7c1f2a…
export CONSUL_HTTP_TOKEN=7c1f2a…
consul members
Node Address Status Type DC
consul1 10.0.4.11:8301 alive server dc1
consul2 10.0.4.12:8301 alive server dc1
consul3 10.0.4.13:8301 alive server dc1
consul operator raft list-peersLet op
Save the bootstrap token (it is the root token) in your password manager. Create smaller tokens for agents and apps; use the root token only for admin work.
A client agent with a service and health check
On an app server: the same datacenter, encrypt, CA file and retry_join, but server = false, plus an agent token (made with a policy that allows node write for this node). Then register the service:
service {
name = "web"
port = 8080
tags = ["prod", "v1.4.0"]
check {
id = "web-http"
name = "HTTP health"
http = "http://127.0.0.1:8080/health"
interval = "10s"
timeout = "2s"
}
}consul validate /etc/consul.d/
consul reload
consul catalog services
dig @127.0.0.1 -p 8600 web.service.consul +short
10.0.1.11
10.0.1.12
dig @127.0.0.1 -p 8600 prod.web.service.consul SRVDNS returns only healthy instances. To use *.consul names from every program, forward the consul domain from the system resolver to 127.0.0.1:8600 (systemd-resolved, dnsmasq or BIND forward zone).
Daily use: KV, snapshots and the real uses
consul kv put config/shop/feature_new_checkout true
consul kv get config/shop/feature_new_checkout
consul kv get -recurse config/shop/
consul watch -type=key -key=config/shop/feature_new_checkout ./reload.sh
consul snapshot save /backup/consul-$(date +%F).snap
consul monitor -log-level=warn
consul acl token listReal uses at work:
- Service discovery for VMs and mixed platforms (load balancers like HAProxy/Nginx/Traefik read Consul to find backends).
- Dynamic configuration (KV +
consul-templateto rewrite config files on change). - Vault's storage/service discovery in older setups; Consul Connect mTLS between services.
Make it safe
Security checklist:
- 3 or 5 servers; gossip encryption; TLS with verification; HTTPS-only API.
- ACLs with
default_policy = "deny"; small tokens per agent/app; bootstrap token stored safely. - Firewall Consul ports to cluster networks only; the UI only for admins.
- Regular
consul snapshot save(encrypted, off the servers) and restore tests. - Keep Consul updated (one minor version at a time, servers one by one).
Update and remove
Fix common problems
| Problem | Cause | Fix |
|---|---|---|
No cluster leader | Fewer servers than bootstrap_expect, or gossip blocked. | Start all servers; open 8301 tcp and udp; check consul members. |
Permission denied / ACL not found | No token or a token without rights. | Set CONSUL_HTTP_TOKEN; give the token a policy for that action. |
Agent can not join: No installed keys could decrypt the message | Different gossip encrypt key. | Use the same key on every agent. |
x509: certificate signed by unknown authority / bad certificate | Wrong CA or cert, or verify_server_hostname with a wrong cert. | Use certs from consul tls cert create for the right DC; same CA file everywhere. |
DNS gives no answer for web.service.consul | All instances unhealthy, or DNS not forwarded. | consul catalog services; check health checks; query port 8600 directly. |
| Service check is critical | The health URL fails or times out. | curl the check URL locally; fix the app or the check. |
Next steps
- Same company, same ideas: Vault, Terraform, Packer.
- DNS forwarding for
.consul: BIND. Load balancers that read Consul: HAProxy, Traefik. - Service mesh in Kubernetes: Istio.
1Why does Consul DNS return only some of the registered instances?
It returns only instances whose health checks pass.
2What does default_policy = "deny" mean for ACLs?
Nothing is allowed without a token that has a policy for it.