Geavanceerd
Zoeken inInstallatiegidsen
Zo zoek je
  • Plak een foutmelding tussen aanhalingstekens, bijv. "permission denied", om de oplossingstabel te vinden.
  • Kies een gebied en een deel van de gids (installeren, eerste instellingen, dagelijks gebruik, beveiliging, oplossingen) om direct naar de juiste stap te gaan.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

Gids 100Consul · Network

Install and set up Consul (service discovery and configuration)

HashiCorp Consul answers "where is the service, and is it healthy?" across VMs, containers and Kubernetes: services register with health checks, and clients find healthy instances by DNS (web.service.consul) or the HTTP API. It also has a key/value store for configuration, and a service mesh (Consul Connect) with mTLS. This guide builds a 3-server Consul cluster on RHEL or Ubuntu with gossip encryption, TLS and ACLs, adds a client agent with a service and health check, uses DNS and KV, and covers backups and fixes.

  • 11 Lessen
  • 9 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

100.1

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).

ItemValue
Packageconsul (HashiCorp repository)
Serviceconsul.service
Config/etc/consul.d/*.hcl
Data/opt/consul
Ports8300 (server RPC), 8301 tcp+udp (LAN gossip), 8302 (WAN), 8500 (HTTP), 8501 (HTTPS), 8600 tcp+udp (DNS)
This guideservers 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.

100.2

Install on RHEL, Rocky, Alma and Fedora

dnf -y install dnf-plugins-core
dnf config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
dnf -y install consul
consul version

Commando's in deze les

100.3

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 consul

100.4

Prepare 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 dc1

Copy 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.

100.5

First configuration: the server cluster

/etc/consul.d/consul.hcl (consul1)
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
}
SettingWhy
bootstrap_expect = 3The cluster starts electing a leader when 3 servers are there.
retry_joinServers (and later clients) find each other by themselves.
encryptGossip 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 = 8501No 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-peers

Let 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.

100.6

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:

/etc/consul.d/web.hcl
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 SRV

DNS 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).

Commando's in deze les

100.7

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 list

Real 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-template to rewrite config files on change).
  • Vault's storage/service discovery in older setups; Consul Connect mTLS between services.

Commando's in deze les

100.8

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).

100.9

Update and remove

Back up first, then upgrade one server at a time (followers first, the leader last), checking consul operator raft list-peers after each:

consul snapshot save /backup/consul-before-upgrade.snap
dnf -y upgrade consul
apt-get install --only-upgrade -y consul
systemctl restart consul
consul members

100.10

Fix common problems

ProblemCauseFix
No cluster leaderFewer servers than bootstrap_expect, or gossip blocked.Start all servers; open 8301 tcp and udp; check consul members.
Permission denied / ACL not foundNo 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 messageDifferent gossip encrypt key.Use the same key on every agent.
x509: certificate signed by unknown authority / bad certificateWrong 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.consulAll instances unhealthy, or DNS not forwarded.consul catalog services; check health checks; query port 8600 directly.
Service check is criticalThe health URL fails or times out.curl the check URL locally; fix the app or the check.

Commando's in deze les

100.11

Next steps

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.

Alle commando's in dit hoofdstuk