Skip to main content

Load-Balancer et Initialisation (Keepalived et HAProxy)

Installation et Configuration de HAProxy :

Sur chaque master node

Installation

sudo apt -y install haproxy
sudo systemctl enable --now haproxy

Configuration :

mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.bak && sudo nano /etc/haproxy/haproxy.cfg

global
  maxconn 4096
  log /dev/log local0
  daemon

defaults
  mode tcp
  log global
  option tcplog
  timeout connect 5s
  timeout client  60s
  timeout server  60s

# API Kubernetes : 6443 en TCP (TLS passthrough)
frontend k8s_api
  bind 0.0.0.0:6443
  default_backend k8s_api_back

backend k8s_api_back
  mode tcp
  balance roundrobin
  option tcp-check
  # Remplace par les IPs réelles de tes masters
  server cp1 192.168.20.240:6443 check
  server cp2 192.168.20.241:6443 check
  server cp3 192.168.20.242:6443 check

Redémarrage de HAProxy pour appliquer les changements sudo systemctl restart haproxy

Test si on écoute bien sur le port 6443 sudo ss -lntp | grep 6443

Installation et Configuration de Keepalived :

Sur chaque master node

Installation

sudo apt -y install keepalived

Configuration

Pensez a adapter la configuration en fonction de l'hote en baissant la priorité et en modifiant le nom de l'interface si nécéssaire.

sudo nano /etc/keepalived/keepalived.conf

vrrp_script chk_haproxy {
  script "pidof haproxy"
  interval 2
  weight -50
}

vrrp_instance VI_API {
  state BACKUP
  interface ens18               # <-- remplace par ton NIC
  virtual_router_id 51
  priority 150                  # M1:150, M2:120, M3:100
  advert_int 1
  authentication {
    auth_type PASS
    auth_pass changeMe
  }
  virtual_ipaddress {
    192.168.20.100/24 dev ens18 # <-- ton VIP
  }
  track_script { chk_haproxy }

  # Bascules ARP propres (options documentées dans le man keepalived)
  garp_master_delay 1
  garp_master_repeat 5
  garp_lower_prio_no_advert
}

Activation et vérification de l'installation :

sudo systemctl enable --now keepalived
journalctl -u keepalived -n 50 --no-pager
ip a | grep -A2 192.168.20.100

Ont peut également vérifié que le port fonctionne bien :

nc -vz 192.168.20.100 6443