Skip to content

Deploying Traefik v3 as a dynamic edge proxy on AlmaLinux 9 (production-ready)

Stand up a hardened Traefik v3 edge proxy on AlmaLinux 9 — file + Docker providers, wildcard Let's Encrypt via DNS-01, dashboard basic-auth, access log.

You stand up Traefik to “let containers register themselves” and it works. The first time a developer deploys a service with traefik.enable=true and a Host rule, TLS provisions, traffic flows, the dashboard is on :8080 so you bookmark it. Six months later the dashboard is still on :8080 exposed to the internet, the ACME account email is the personal Gmail of the engineer who left, the access log is missing entirely because nobody enabled it, and the next time a wildcard cert needs to renew, the DNS-01 challenge fails because the API token was scoped to a now-deleted zone.

What’s happening

A Traefik install that survives more than one rotation needs: a file provider for the static, security-critical entrypoints (HTTPS redirect, default TLS options, dashboard) and a Docker provider for the dynamic side (apps registering themselves via labels); the dashboard bound to a private interface or behind basic-auth with TLS, never on :8080 open to the internet; an access log in CommonLog or JSON format that you can grep and ship; ACME resolvers using DNS-01 for any wildcard, with an API token scoped tightly to the zone and rotation tracked; Prometheus metrics exposed on a separate localhost-only entrypoint; sane connect/read/write/idle timeouts at the entrypoint level, and a default TLS option that disables TLS 1.0/1.1 outright. The first install is fine; what fails is the second engineer inheriting the system without these guardrails.

How to verify

After install, the listeners should look like this:

ss -lntp | grep -E ':(80|443|8082|9100)\b'
# 0.0.0.0:80   traefik
# 0.0.0.0:443  traefik
# 127.0.0.1:8082  traefik  (dashboard, localhost only)
# 127.0.0.1:9100  traefik  (metrics, localhost only)

Static config must parse before reload:

docker exec traefik traefik healthcheck

A curl -I against a configured host should show HSTS and the cert chain should resolve via Let’s Encrypt production (not staging):

curl -sI https://app.example.com/ | grep -iE 'strict-transport|server'
openssl s_client -connect app.example.com:443 -servername app.example.com </dev/null 2>/dev/null \
  | openssl x509 -noout -issuer -dates

If the dashboard answers on 0.0.0.0:8080, or the issuer says (STAGING), or there is no access log under /var/log/traefik/, the install is not done yet.

The fix

Save the script below as /root/setup-traefik-alma9.sh, then run it. It performs the full installer flow on a fresh AlmaLinux 9 VM: base update + EPEL, installs Docker CE and the compose plugin, hardens SSH while keeping the root key, configures firewalld for 22/80/443 only, applies sysctl tuning relevant to a high-concurrency edge proxy, writes a traefik.yml (static config: entrypoints, certificate resolvers via Cloudflare DNS-01, metrics + access log) plus a dynamic.yml (default TLS options, dashboard router with basic-auth, mTLS toggle stub), spins up Traefik through docker compose with LimitNOFILE=200000, exposes the dashboard only on 127.0.0.1:8082, validates with traefik healthcheck, and waits for both public listeners before continuing.

#!/usr/bin/env bash
set -euo pipefail

### AlmaLinux 9 Traefik v3 edge proxy installer
### TLS via Let's Encrypt DNS-01 (Cloudflare)
### Dashboard bound to 127.0.0.1:8082 behind basic-auth
### Metrics bound to 127.0.0.1:9100
### No local backups; assumes VM-level backup
### Reboot remains manual

if [[ "$(id -u)" -ne 0 ]]; then
  echo "Run as root."
  exit 1
fi

# Required environment for the script run:
: "${ACME_EMAIL:?Set ACME_EMAIL=ops@example.com before running}"
: "${CF_DNS_API_TOKEN:?Set CF_DNS_API_TOKEN=... (zone-scoped) before running}"
: "${DASHBOARD_HOST:?Set DASHBOARD_HOST=traefik.example.com before running}"
: "${DASHBOARD_USER:?Set DASHBOARD_USER=admin before running}"
: "${DASHBOARD_PASSWORD:?Set DASHBOARD_PASSWORD=... before running}"

echo
echo "========================================"
echo "Traefik v3 AlmaLinux 9 Setup"
echo "========================================"
echo

echo "==> Updating base OS"
dnf -y update
dnf -y install epel-release
dnf -y update

echo "==> Installing base packages"
dnf -y install \
  curl \
  wget \
  ca-certificates \
  gnupg2 \
  chrony \
  fail2ban \
  dnf-automatic \
  policycoreutils-python-utils \
  lsof \
  tcpdump \
  bind-utils \
  iproute \
  net-tools \
  jq \
  httpd-tools

echo "==> Enabling time sync"
systemctl enable --now chronyd
timedatectl set-ntp true

echo "==> Configuring SELinux enforcing"
if [[ -f /etc/selinux/config ]]; then
  sed -i 's/^SELINUX=.*/SELINUX=enforcing/' /etc/selinux/config
fi
setenforce 1 2>/dev/null || true

echo "==> Applying sysctl tuning for edge proxy"
cat > /etc/sysctl.d/99-traefik.conf <<'SYSCTL'
net.core.somaxconn = 32768
net.core.netdev_max_backlog = 250000
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_fin_timeout = 15
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 10240 65535

net.core.default_qdisc = fq
net.ipv4.tcp_congestion_control = bbr

net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0

fs.file-max = 2097152
SYSCTL

modprobe tcp_bbr || true
echo tcp_bbr > /etc/modules-load.d/bbr.conf
sysctl --system

echo "==> Setting file limits"
cat > /etc/security/limits.d/99-traefik.conf <<'LIMITS'
* soft nofile 200000
* hard nofile 200000
root soft nofile 200000
root hard nofile 200000
LIMITS

echo "==> Hardening SSH while keeping root SSH key login enabled"
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak.$(date +%F-%H%M%S)

set_sshd_option() {
  local key="$1"
  local value="$2"

  if grep -qE "^[#[:space:]]*${key}[[:space:]]+" /etc/ssh/sshd_config; then
    sed -i "s|^[#[:space:]]*${key}[[:space:]].*|${key} ${value}|" /etc/ssh/sshd_config
  else
    echo "${key} ${value}" >> /etc/ssh/sshd_config
  fi
}

set_sshd_option PermitRootLogin prohibit-password
set_sshd_option PasswordAuthentication no
set_sshd_option PubkeyAuthentication yes
set_sshd_option KbdInteractiveAuthentication no
set_sshd_option ChallengeResponseAuthentication no
set_sshd_option X11Forwarding no
set_sshd_option ClientAliveInterval 300
set_sshd_option ClientAliveCountMax 2
set_sshd_option MaxAuthTries 4

sshd -t
systemctl restart sshd

echo "==> Configuring firewalld (22, 80, 443 only)"
systemctl enable --now firewalld
firewall-cmd --permanent --remove-service=cockpit 2>/dev/null || true
firewall-cmd --permanent --add-service=ssh
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

echo "==> Configuring fail2ban for SSH"
mkdir -p /etc/fail2ban/jail.d
cat > /etc/fail2ban/jail.d/sshd.local <<'F2B'
[sshd]
enabled = true
port = ssh
backend = systemd
maxretry = 4
findtime = 10m
bantime = 1h
F2B
systemctl enable --now fail2ban

echo "==> Configuring automatic security updates"
cat > /etc/dnf/automatic.conf <<'DNFAUTO'
[commands]
upgrade_type = security
random_sleep = 900
download_updates = yes
apply_updates = yes

[emitters]
emit_via = stdio

[base]
debuglevel = 1
DNFAUTO
systemctl enable --now dnf-automatic.timer

echo "==> Installing Docker CE + compose plugin"
dnf config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
dnf -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
systemctl enable --now docker

echo "==> Creating Traefik layout"
mkdir -p /opt/traefik/{conf,dynamic,acme,logs}
chmod 700 /opt/traefik/acme
touch /opt/traefik/acme/acme.json
chmod 600 /opt/traefik/acme/acme.json

echo "==> Generating basic-auth hash for dashboard"
DASH_HASH=$(htpasswd -nbB "${DASHBOARD_USER}" "${DASHBOARD_PASSWORD}" | sed -e 's/\$/\$\$/g')

echo "==> Writing static config /opt/traefik/conf/traefik.yml"
cat > /opt/traefik/conf/traefik.yml <<'TRAEFIK_YML'
global:
  checkNewVersion: false
  sendAnonymousUsage: false

log:
  level: INFO
  filePath: /var/log/traefik/traefik.log
  format: json

accessLog:
  filePath: /var/log/traefik/access.log
  format: json
  bufferingSize: 100
  fields:
    headers:
      defaultMode: keep
      names:
        Authorization: drop
        Cookie: redact

api:
  dashboard: true
  insecure: false

metrics:
  prometheus:
    entryPoint: metrics
    addEntryPointsLabels: true
    addServicesLabels: true

entryPoints:
  web:
    address: ":80"
    http:
      redirections:
        entryPoint:
          to: websecure
          scheme: https
          permanent: true
    transport:
      respondingTimeouts:
        readTimeout: 30s
        writeTimeout: 30s
        idleTimeout: 120s

  websecure:
    address: ":443"
    http:
      tls:
        certResolver: letsencrypt
    transport:
      respondingTimeouts:
        readTimeout: 30s
        writeTimeout: 30s
        idleTimeout: 120s

  dashboard:
    address: "127.0.0.1:8082"

  metrics:
    address: "127.0.0.1:9100"

providers:
  file:
    directory: /etc/traefik/dynamic
    watch: true
  docker:
    endpoint: "unix:///var/run/docker.sock"
    exposedByDefault: false
    network: edge

certificatesResolvers:
  letsencrypt:
    acme:
      email: REPLACE_ACME_EMAIL
      storage: /etc/traefik/acme/acme.json
      keyType: EC256
      dnsChallenge:
        provider: cloudflare
        resolvers:
          - "1.1.1.1:53"
          - "1.0.0.1:53"
TRAEFIK_YML

sed -i "s|REPLACE_ACME_EMAIL|${ACME_EMAIL}|" /opt/traefik/conf/traefik.yml

echo "==> Writing dynamic config /opt/traefik/dynamic/dynamic.yml"
cat > /opt/traefik/dynamic/dynamic.yml <<DYNAMIC_YML
tls:
  options:
    default:
      minVersion: VersionTLS12
      cipherSuites:
        - TLS_AES_128_GCM_SHA256
        - TLS_AES_256_GCM_SHA384
        - TLS_CHACHA20_POLY1305_SHA256
        - TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
        - TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
        - TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384
        - TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
      curvePreferences:
        - X25519
        - secp384r1
      sniStrict: true

http:
  middlewares:
    secure-headers:
      headers:
        stsSeconds: 31536000
        stsIncludeSubdomains: true
        contentTypeNosniff: true
        referrerPolicy: "strict-origin-when-cross-origin"
        customResponseHeaders:
          Server: ""
    rate-limit:
      rateLimit:
        average: 200
        burst: 400
    dashboard-auth:
      basicAuth:
        users:
          - "${DASH_HASH}"

  routers:
    dashboard:
      rule: "Host(\`${DASHBOARD_HOST}\`)"
      entryPoints:
        - websecure
      service: api@internal
      middlewares:
        - dashboard-auth
        - secure-headers
      tls:
        certResolver: letsencrypt
DYNAMIC_YML

echo "==> Writing docker-compose.yml"
cat > /opt/traefik/docker-compose.yml <<COMPOSE
services:
  traefik:
    image: traefik:v3.1
    container_name: traefik
    restart: always
    environment:
      - CF_DNS_API_TOKEN=${CF_DNS_API_TOKEN}
    ports:
      - "80:80"
      - "443:443"
      - "127.0.0.1:8082:8082"
      - "127.0.0.1:9100:9100"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - /opt/traefik/conf/traefik.yml:/etc/traefik/traefik.yml:ro
      - /opt/traefik/dynamic:/etc/traefik/dynamic:ro
      - /opt/traefik/acme:/etc/traefik/acme
      - /opt/traefik/logs:/var/log/traefik
    networks:
      - edge
    ulimits:
      nofile:
        soft: 200000
        hard: 200000
    healthcheck:
      test: ["CMD", "traefik", "healthcheck", "--ping"]
      interval: 10s
      timeout: 3s
      retries: 5

networks:
  edge:
    name: edge
COMPOSE

echo "==> Starting Traefik"
cd /opt/traefik
docker compose pull
docker compose up -d

echo "==> Waiting for Traefik listeners"
for i in {1..60}; do
  if ss -lntp | grep -q ':443' && ss -lntp | grep -q ':80'; then
    break
  fi
  sleep 1
done

if ! ss -lntp | grep -q ':443'; then
  echo "Traefik did not bind :443"
  docker compose logs --tail=200 traefik || true
  exit 1
fi

echo "==> Healthcheck"
docker exec traefik traefik healthcheck || true

echo
echo "========================================"
echo "DONE"
echo "========================================"
echo
echo "Kernel:"
uname -r
echo
echo "Listeners:"
ss -lntp | grep -E ':(80|443|8082|9100)\b' || true
echo
echo "Services:"
systemctl is-active docker firewalld fail2ban chronyd || true
echo
echo "Containers:"
docker compose -f /opt/traefik/docker-compose.yml ps
echo
echo "Config: /opt/traefik/conf/traefik.yml"
echo "Dynamic: /opt/traefik/dynamic/dynamic.yml"
echo "ACME storage: /opt/traefik/acme/acme.json (mode 600)"
echo "Access log: /opt/traefik/logs/access.log"
echo
echo "Dashboard: https://${DASHBOARD_HOST}/ (user: ${DASHBOARD_USER})"
echo "First test request:"
echo "  curl -I https://${DASHBOARD_HOST}/"

Make it executable and run it (export the five required env vars first):

chmod 700 /root/setup-traefik-alma9.sh
export ACME_EMAIL=ops@example.com
export CF_DNS_API_TOKEN=...
export DASHBOARD_HOST=traefik.example.com
export DASHBOARD_USER=admin
export DASHBOARD_PASSWORD=...
/root/setup-traefik-alma9.sh

The dashboard answers on https://traefik.example.com/ (the basic-auth credentials you exported), and only on that hostname — there is no :8080 open to the internet. Apps register themselves by adding traefik.enable=true and a Host(...) rule on the edge Docker network. Rotate the Cloudflare API token annually and confirm the wildcard renews via the dry-run path in the Traefik logs.

When this proxy fronts a PaaS surface and an IaaS surface together, the Stack Harbor Cloud PaaS team owns the renewal cadence and the dashboard ACL — book a review.