CoAI LogoCoAI.Dev

Docker Swarm Deployment

Deploy CoAI.Dev using Docker Swarm for multi-node clustering with built-in orchestration and load balancing

Docker Swarm Deployment

Docker Swarm provides native clustering capabilities for Docker, making it easier to deploy and manage CoAI.Dev across multiple nodes. It offers built-in load balancing, service discovery, and rolling updates with a simpler setup compared to Kubernetes.

When to Use Docker Swarm

Docker Swarm is ideal for medium-scale deployments where you need clustering capabilities but want simpler management than Kubernetes. Perfect for teams familiar with Docker who need high availability without the complexity.

Overview

Docker Swarm Benefits:

  • 🏗️ Native Docker Integration: Built into Docker Engine
  • ⚖️ Automatic Load Balancing: Built-in load balancing across nodes
  • 🔄 Rolling Updates: Zero-downtime deployments
  • 🌐 Service Discovery: Automatic service networking
  • 📈 Easy Scaling: Simple horizontal scaling commands
  • 🛡️ Built-in Security: Encrypted communication between nodes

Best For:

  • Medium-scale deployments (3-50 nodes)
  • Teams experienced with Docker
  • Applications requiring high availability
  • Environments needing simpler orchestration than Kubernetes

Prerequisites

System Requirements

Manager Nodes (Minimum 3 for HA):

  • CPU: 4 cores
  • RAM: 8GB
  • Storage: 50GB SSD
  • Network: 1Gbps+ between nodes

Worker Nodes:

  • CPU: 2+ cores
  • RAM: 4GB+
  • Storage: 20GB SSD
  • Network: 1Gbps+ between nodes

Network Requirements:

  • Cluster Communication: Port 2377/tcp
  • Node Communication: Port 7946/tcp and 7946/udp
  • Overlay Networks: Port 4789/udp
  • Application Ports: 80, 443, custom ports

Install Docker

Install Docker on all nodes:

# Update package index
sudo apt-get update
 
# Install required packages
sudo apt-get install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
 
# Add Docker's official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
 
# Add Docker repository
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
 
# Install Docker Engine
sudo apt-get update
sudo apt-get install -y docker-ce docker-ce-cli containerd.io
 
# Start and enable Docker
sudo systemctl start docker
sudo systemctl enable docker

Swarm Cluster Setup

Initialize Swarm Cluster

Initialize Manager Node

On your first manager node:

# Initialize swarm with advertise address
docker swarm init --advertise-addr <MANAGER-IP>
 
# Example:
docker swarm init --advertise-addr 192.168.1.10

Save the join tokens displayed after initialization:

  • Manager token: For adding additional manager nodes
  • Worker token: For adding worker nodes

Add Manager Nodes

On additional manager nodes:

# Join as manager (use token from init command)
docker swarm join --token <MANAGER-TOKEN> <MANAGER-IP>:2377
 
# Example:
docker swarm join --token SWMTKN-1-xxx-xxx 192.168.1.10:2377

Best Practices:

  • Use odd number of managers (3, 5, 7)
  • Maximum recommended: 7 manager nodes
  • Distribute managers across availability zones

Add Worker Nodes

On worker nodes:

# Join as worker (use worker token from init command)
docker swarm join --token <WORKER-TOKEN> <MANAGER-IP>:2377

Verify cluster status:

# Check node status (run on manager)
docker node ls
 
# View swarm info
docker info | grep Swarm

Network Setup

Create Overlay Network:

# Create attachable overlay network
docker network create \
  --driver overlay \
  --attachable \
  chatnio-network
 
# Verify network creation
docker network ls

CoAI.Dev Deployment

Prepare Stack Configuration

Create chatnio-stack.yml:

version: '3.8'
 
services:
  # MySQL Database
  mysql:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: chatnio123456
      MYSQL_DATABASE: chatnio
      MYSQL_USER: chatnio
      MYSQL_PASSWORD: chatnio123456
    volumes:
      - mysql_data:/var/lib/mysql
    networks:
      - chatnio-network
    deploy:
      replicas: 1
      placement:
        constraints:
          - node.role == manager
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s
      resources:
        limits:
          memory: 2G
        reservations:
          memory: 1G
 
  # Redis Cache
  redis:
    image: redis:7-alpine
    command: redis-server --appendonly yes
    volumes:
      - redis_data:/data
    networks:
      - chatnio-network
    deploy:
      replicas: 1
      placement:
        constraints:
          - node.role == manager
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s
      resources:
        limits:
          memory: 512M
        reservations:
          memory: 256M
 
  # CoAI.Dev Application
  chatnio:
    image: programzmh/chatnio:latest
    environment:
      MYSQL_HOST: mysql
      MYSQL_PORT: 3306
      MYSQL_DB: chatnio
      MYSQL_USER: chatnio
      MYSQL_PASSWORD: chatnio123456
      REDIS_HOST: redis
      REDIS_PORT: 6379
      SECRET: ${SECRET_KEY}
      SERVE_STATIC: "true"
    ports:
      - "80:8094"
      - "443:8094"
    volumes:
      - config_data:/config
      - logs_data:/logs
      - storage_data:/storage
    networks:
      - chatnio-network
    deploy:
      replicas: 3
      update_config:
        parallelism: 1
        delay: 10s
        failure_action: rollback
        order: start-first
      rollback_config:
        parallelism: 1
        delay: 5s
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s
      resources:
        limits:
          memory: 1G
        reservations:
          memory: 512M
    depends_on:
      - mysql
      - redis
 
  # Load Balancer (Optional)
  nginx:
    image: nginx:alpine
    ports:
      - "8080:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
    networks:
      - chatnio-network
    deploy:
      replicas: 2
      placement:
        preferences:
          - spread: node.id
      restart_policy:
        condition: on-failure
      resources:
        limits:
          memory: 256M
        reservations:
          memory: 128M
    depends_on:
      - chatnio
 
volumes:
  mysql_data:
    driver: local
  redis_data:
    driver: local
  config_data:
    driver: local
  logs_data:
    driver: local
  storage_data:
    driver: local
 
networks:
  chatnio-network:
    external: true

Environment Configuration

Create .env file:

# Security
SECRET_KEY=your-super-secret-key-here
 
# Database Configuration
MYSQL_ROOT_PASSWORD=chatnio123456
MYSQL_DATABASE=chatnio
MYSQL_USER=chatnio
MYSQL_PASSWORD=chatnio123456
 
# Application Configuration
CHATNIO_VERSION=latest
REPLICA_COUNT=3
 
# Load Balancer Configuration
NGINX_REPLICAS=2

Deploy Stack

Deploy Services

Deploy the complete stack:

# Deploy stack
docker stack deploy -c chatnio-stack.yml chatnio
 
# Verify deployment
docker stack ls
docker stack services chatnio
docker stack ps chatnio

Verify Deployment

Check service status:

# List all services
docker service ls
 
# Check specific service logs
docker service logs chatnio_chatnio
docker service logs chatnio_mysql
docker service logs chatnio_redis
 
# Check service details
docker service inspect chatnio_chatnio

Test Application

Access your CoAI.Dev deployment:

# Find service endpoint
docker service ls
 
# Test connectivity
curl http://localhost:80
curl http://[node-ip]:80

Default Login:

  • Username: root
  • Password: chatnio123456

Scaling and Management

Service Scaling

Scale CoAI.Dev instances:

# Scale up application
docker service scale chatnio_chatnio=5
 
# Scale down application
docker service scale chatnio_chatnio=2
 
# Scale multiple services
docker service scale \
  chatnio_chatnio=4 \
  chatnio_nginx=3

Rolling Updates

Update application version:

# Update to specific version
docker service update \
  --image programzmh/chatnio:v2.0.0 \
  chatnio_chatnio
 
# Update with custom configuration
docker service update \
  --env-add NEW_FEATURE=enabled \
  chatnio_chatnio
 
# Rollback to previous version
docker service rollback chatnio_chatnio

Load Balancing Configuration

Create nginx.conf for advanced load balancing:

events {
    worker_connections 1024;
}
 
http {
    upstream chatnio_backend {
        least_conn;
        server chatnio:8094 max_fails=3 fail_timeout=30s;
    }
 
    server {
        listen 80;
        
        location / {
            proxy_pass http://chatnio_backend;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            
            # WebSocket support
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            
            # Timeouts
            proxy_connect_timeout 60s;
            proxy_send_timeout 60s;
            proxy_read_timeout 60s;
        }
        
        # Health check endpoint
        location /health {
            access_log off;
            return 200 "healthy\n";
            add_header Content-Type text/plain;
        }
    }
}

Monitoring and Logging

Service Monitoring

Built-in Docker Swarm monitoring:

# Check cluster health
docker node ls
docker service ls
 
# Monitor resource usage
docker stats
 
# View service events
docker events --filter service=chatnio_chatnio

Centralized Logging

Deploy logging stack:

# Add to your stack file
  fluentd:
    image: fluent/fluentd:v1.14
    volumes:
      - /var/log:/fluentd/log
      - ./fluentd.conf:/fluentd/etc/fluent.conf
    networks:
      - chatnio-network
    deploy:
      mode: global
      
  elasticsearch:
    image: elasticsearch:7.17.0
    environment:
      - discovery.type=single-node
      - "ES_JAVA_OPTS=-Xms1g -Xmx1g"
    volumes:
      - elasticsearch_data:/usr/share/elasticsearch/data
    networks:
      - chatnio-network
    deploy:
      replicas: 1
      placement:
        constraints:
          - node.role == manager

Health Checks

Add health checks to services:

services:
  chatnio:
    # ... other configuration
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8094/health"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    deploy:
      # ... other deploy configuration
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s

Security Configuration

Network Security

Configure network encryption:

# Create encrypted network
docker network create \
  --driver overlay \
  --opt encrypted \
  --attachable \
  chatnio-secure-network

Secrets Management

Use Docker secrets for sensitive data:

# Create secrets
echo "chatnio123456" | docker secret create mysql_password -
echo "your-secret-key" | docker secret create app_secret -
 
# Use in stack file
services:
  mysql:
    secrets:
      - mysql_password
    environment:
      MYSQL_ROOT_PASSWORD_FILE: /run/secrets/mysql_password
      
secrets:
  mysql_password:
    external: true
  app_secret:
    external: true

Access Control

Configure node access and roles:

# Drain node for maintenance
docker node update --availability drain <node-id>
 
# Add node labels for placement
docker node update --label-add environment=production <node-id>
 
# Promote worker to manager
docker node promote <node-id>
 
# Demote manager to worker
docker node demote <node-id>

Backup and Recovery

Data Backup

Backup persistent volumes:

# Create backup container
docker service create \
  --name backup \
  --mount source=mysql_data,target=/backup/mysql \
  --mount source=redis_data,target=/backup/redis \
  --mount source=config_data,target=/backup/config \
  alpine:latest sleep 3600
 
# Perform backup
docker exec $(docker ps -q -f name=backup) tar czf /backup.tar.gz /backup/
 
# Copy backup from container
docker cp $(docker ps -q -f name=backup):/backup.tar.gz ./backup-$(date +%Y%m%d).tar.gz

Disaster Recovery

Recovery procedures:

# Restore from backup
docker service create \
  --name restore \
  --mount source=mysql_data,target=/restore/mysql \
  --mount type=bind,source=$(pwd)/backup.tar.gz,target=/backup.tar.gz \
  alpine:latest
 
# Extract backup
docker exec $(docker ps -q -f name=restore) tar xzf /backup.tar.gz -C /
 
# Restart services
docker stack rm chatnio
docker stack deploy -c chatnio-stack.yml chatnio

Troubleshooting

Common Issues

Service Not Starting:

# Check service logs
docker service logs chatnio_chatnio
 
# Check node status
docker node ls
 
# Inspect service
docker service inspect chatnio_chatnio

Network Connectivity Issues:

# Test network connectivity
docker network ls
docker network inspect chatnio-network
 
# Check port availability
netstat -tulpn | grep :2377

Performance Issues:

# Check resource usage
docker stats --no-stream
 
# Monitor service performance
docker service ps chatnio_chatnio --no-trunc

Production Readiness

Docker Swarm provides excellent orchestration capabilities for medium-scale deployments. For larger deployments or more complex requirements, consider migrating to Kubernetes.

Docker Swarm offers a balanced approach between simplicity and functionality, making it an excellent choice for teams that need orchestration capabilities without the complexity of Kubernetes.