feat: Working config for HAProxy mit mapping via `X-Shard` (casesensitive!)
authorKai Moritz <kai@juplo.de>
Sun, 15 Jan 2023 16:27:10 +0000 (17:27 +0100)
committerKai Moritz <kai@juplo.de>
Sun, 15 Jan 2023 18:15:02 +0000 (19:15 +0100)
docker-compose.yml
haproxy.cfg
sharding.map [new file with mode: 0644]

index 6d09370..28fca8b 100644 (file)
@@ -88,18 +88,26 @@ services:
     image: haproxy:2.7
     volumes:
       - ./haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
+      - ./sharding.map:/usr/local/etc/haproxy/sharding.map:ro
     sysctls:
       net.ipv4.ip_unprivileged_port_start: 0
     ports:
-      - 8080:80
+      - 8080:8400
+      - 8401:8401
       - 8404:8404
 
-  web1:
-    image: nginx:1.17
-  web2:
-    image: nginx:1.17
-  web3:
-    image: nginx:1.17
+  backend-1:
+    image: jmalloc/echo-server:latest
+    ports:
+      - 8081:8080
+  backend-2:
+    image: jmalloc/echo-server:latest
+    ports:
+      - 8082:8080
+  backend-3:
+    image: jmalloc/echo-server:latest
+    ports:
+      - 8083:8080
 
   setup:
     image: juplo/toolbox
index f334457..0dd9ac5 100644 (file)
@@ -7,6 +7,10 @@ defaults
   timeout http-request 10s
   log global
 
+global
+  stats socket ipv4@:8401 level admin
+  stats timeout 2m
+
 frontend stats
   bind *:8404
   stats enable
@@ -14,10 +18,15 @@ frontend stats
   stats refresh 10s
 
 frontend frontend
-  bind :80
-  default_backend webservers
+  bind :8400
+  use_backend %[req.hdr(X-Shard),map(/usr/local/etc/haproxy/sharding.map)]
+
+backend backend_1
+  server b1 backend-1:8080 check
+
+backend backend_2
+  server b2 backend-2:8080 check
+
+backend backend_3
+  server b3 backend-3:8080 check
 
-backend webservers
-  server s1 web1:80 check
-  server s2 web2:80 check
-  server s3 web3:80 check
diff --git a/sharding.map b/sharding.map
new file mode 100644 (file)
index 0000000..ffdbb6c
--- /dev/null
@@ -0,0 +1,10 @@
+0 backend_3
+1 backend_1
+2 backend_2
+3 backend_3
+4 backend_1
+5 backend_2
+6 backend_3
+7 backend_1
+8 backend_2
+9 backend_3