Oto pliki dla uruchomienia aplikacji w Docker Swarm:
docker-compose.yml
version: '3.8'
services:
haproxy:
image: haproxy:2.8-alpine
ports:
- "80:80"
- "443:443"
- "8404:8404" # HAProxy stats
configs:
- source: haproxy_config
target: /usr/local/etc/haproxy/haproxy.cfg
deploy:
replicas: 2
placement:
constraints:
- node.role == manager
update_config:
parallelism: 1
delay: 10s
restart_policy:
condition: on-failure
networks:
- frontend
- backend
web:
image: nginx:alpine
# image: your-web-app:latest # Zastąp swoim obrazem
deploy:
replicas: 3
update_config:
parallelism: 1
delay: 10s
restart_policy:
condition: on-failure
resources:
limits:
memory: 512M
reservations:
memory: 256M
networks:
- frontend
# volumes:
# - web_data:/usr/share/nginx/html
api:
image: node:18-alpine
# image: your-api:latest # Zastąp swoim obrazem API
environment:
- NODE_ENV=production
- REDIS_URL=redis://redis:6379
deploy:
replicas: 4
update_config:
parallelism: 2
delay: 10s
restart_policy:
condition: on-failure
resources:
limits:
memory: 1G
reservations:
memory: 512M
networks:
- backend
- frontend
depends_on:
- redis
redis:
image: redis:7-alpine
command: redis-server --appendonly yes
deploy:
replicas: 1
placement:
constraints:
- node.role == manager
restart_policy:
condition: on-failure
resources:
limits:
memory: 512M
reservations:
memory: 256M
networks:
- backend
volumes:
- redis_data:/data
tasks:
image: python:3.11-alpine
# image: your-tasks:latest # Zastąp swoim obrazem worker'a
environment:
- REDIS_URL=redis://redis:6379
deploy:
replicas: 2
update_config:
parallelism: 1
delay: 10s
restart_policy:
condition: on-failure
resources:
limits:
memory: 512M
reservations:
memory: 256M
networks:
- backend
depends_on:
- redis
networks:
frontend:
driver: overlay
attachable: true
backend:
driver: overlay
internal: true
volumes:
redis_data:
driver: local
configs:
haproxy_config:
file: ./haproxy.cfg
haproxy.cfg
global
daemon
maxconn 4096
log stdout local0
# SSL/TLS settings
tune.ssl.default-dh-param 2048
ssl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384
ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets
defaults
mode http
log global
option httplog
option dontlognull
option redispatch
option forwardfor
option http-server-close
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
errorfile 400 /usr/local/etc/haproxy/errors/400.http
errorfile 403 /usr/local/etc/haproxy/errors/403.http
errorfile 408 /usr/local/etc/haproxy/errors/408.http
errorfile 500 /usr/local/etc/haproxy/errors/500.http
errorfile 502 /usr/local/etc/haproxy/errors/502.http
errorfile 503 /usr/local/etc/haproxy/errors/503.http
errorfile 504 /usr/local/etc/haproxy/errors/504.http
# HAProxy Statistics
frontend stats
bind *:8404
stats enable
stats uri /stats
stats refresh 30s
stats admin if TRUE
# Frontend for HTTP traffic
frontend web_frontend
bind *:80
# bind *:443 ssl crt /path/to/certificate.pem # Uncomment for HTTPS
# Redirect HTTP to HTTPS (uncomment if using SSL)
# redirect scheme https if !{ ssl_fc }
# Health check endpoint
acl health_check path_beg /health
http-request return status 200 content-type text/plain string "OK" if health_check
# Route API requests
acl api_request path_beg /api/
use_backend api_backend if api_request
# Default to web backend
default_backend web_backend
# Backend for Web application
backend web_backend
balance roundrobin
option httpchk GET /
http-check expect status 200
# Web service discovery through Docker Swarm DNS
server-template web- 10 web:80 check resolvers docker init-addr none
# Backend for API
backend api_backend
balance roundrobin
option httpchk GET /api/health
http-check expect status 200
# Remove /api prefix when forwarding to backend
http-request replace-path ^/api/(.*) /\1
# API service discovery through Docker Swarm DNS
server-template api- 10 api:3000 check resolvers docker init-addr none
# DNS resolver for Docker Swarm service discovery
resolvers docker
nameserver dns1 127.0.0.11:53
resolve_retries 3
timeout resolve 1s
timeout retry 1s
hold other 10s
hold refused 10s
hold nx 10s
hold timeout 10s
hold valid 10s
hold obsolete 10s
Instrukcje uruchomienia:
- Zainicjalizuj Docker Swarm (jeśli jeszcze nie zrobiłeś):
docker swarm init
- Uruchom stack:
docker stack deploy -c docker-compose.yml myapp
- Sprawdź status:
docker stack services myapp
docker stack ps myapp
- Dostęp do aplikacji:
Kluczowe cechy konfiguracji:
Docker Swarm:
- Overlay networks dla izolacji ruchu
- Service discovery przez DNS
- Health checks i restart policies
- Resource limits
- Rolling updates
HAProxy:
- Load balancing dla web i API
- Service discovery przez Docker DNS
- Health checks
- Statistics dashboard
- Gotowość na SSL/HTTPS
Pamiętaj o zastąpieniu przykładowych obrazów Docker swoimi rzeczywistymi obrazami aplikacji i dostosowaniu portów oraz ścieżek według potrzeb swojej aplikacji.