botserver/docs/src/chapter-07-gbapp/containers.md

140 lines
3.3 KiB
Markdown
Raw Normal View History

2025-11-21 12:13:48 -03:00
# Container Deployment (LXC)
2025-12-01 02:22:35 -03:00
BotServer uses LXC (Linux Containers) for isolated component deployment with system-level containerization.
2025-11-21 12:13:48 -03:00
## What is LXC?
2025-12-01 02:22:35 -03:00
- **System containers** - Full Linux userspace (lightweight VMs)
2025-11-21 12:13:48 -03:00
- **Shared kernel** - More efficient than virtual machines
2025-12-01 02:22:35 -03:00
- **Isolation** - Separate processes, networking, filesystems
- **Resource control** - CPU, memory, I/O limits
2025-11-21 12:13:48 -03:00
2025-12-01 02:22:35 -03:00
## Automatic Setup
2025-11-21 12:13:48 -03:00
```bash
./botserver --container
```
2025-12-01 02:22:35 -03:00
This automatically:
1. Detects LXC/LXD availability
2. Initializes LXD if needed
3. Creates Debian 12 containers per component
4. Mounts directories for persistent data
5. Configures networking and ports
6. Installs and starts services
2025-11-21 12:13:48 -03:00
## Container Architecture
### Container Naming
```
2025-12-01 02:22:35 -03:00
{tenant}-tables → PostgreSQL
{tenant}-drive → S3-compatible storage
{tenant}-cache → Valkey cache
2025-11-21 12:13:48 -03:00
{tenant}-llm → LLM server (optional)
2025-12-01 02:22:35 -03:00
{tenant}-email → Mail server (optional)
2025-11-21 12:13:48 -03:00
```
2025-12-01 02:22:35 -03:00
Default tenant: `default``default-tables`, `default-drive`, etc.
2025-11-21 12:13:48 -03:00
### Directory Mounting
```
2025-12-01 02:22:35 -03:00
Host: botserver-stack/tables/data/ → Container: /opt/gbo/data/
Host: botserver-stack/tables/conf/ → Container: /opt/gbo/conf/
Host: botserver-stack/tables/logs/ → Container: /opt/gbo/logs/
2025-11-21 12:13:48 -03:00
```
2025-12-01 02:22:35 -03:00
Data persists even if containers are deleted.
2025-11-21 12:13:48 -03:00
### Port Forwarding
2025-12-01 02:22:35 -03:00
| Container Port | Host Port | Service |
|----------------|-----------|---------|
| 5432 | 5432 | PostgreSQL |
| 9000 | 9000 | Drive API |
| 9001 | 9001 | Drive Console |
| 6379 | 6379 | Cache |
2025-11-21 12:13:48 -03:00
2025-12-01 02:22:35 -03:00
## Common Operations
2025-11-21 12:13:48 -03:00
```bash
2025-12-01 02:22:35 -03:00
# List containers
2025-11-21 12:13:48 -03:00
lxc list
2025-12-01 02:22:35 -03:00
# Execute command in container
2025-11-21 12:13:48 -03:00
lxc exec default-tables -- psql -U gbuser botserver
2025-12-01 02:22:35 -03:00
# View logs
2025-11-21 12:13:48 -03:00
lxc exec default-tables -- journalctl -u tables
2025-12-01 02:22:35 -03:00
# Stop/Start
2025-11-21 12:13:48 -03:00
lxc stop default-tables
lxc start default-tables
2025-12-01 02:22:35 -03:00
# Delete (data in mounts persists)
2025-11-21 12:13:48 -03:00
lxc delete default-tables --force
```
2025-12-01 02:22:35 -03:00
## Resource Limits
2025-11-21 12:13:48 -03:00
```bash
lxc config set default-tables limits.cpu 2
lxc config set default-tables limits.memory 4GB
```
2025-12-01 02:22:35 -03:00
## Snapshots
2025-11-21 12:13:48 -03:00
```bash
2025-12-01 02:22:35 -03:00
# Create
2025-11-21 12:13:48 -03:00
lxc snapshot default-tables backup-2024-01-15
2025-12-01 02:22:35 -03:00
# List
2025-11-21 12:13:48 -03:00
lxc info default-tables
2025-12-01 02:22:35 -03:00
# Restore
2025-11-21 12:13:48 -03:00
lxc restore default-tables backup-2024-01-15
```
## Troubleshooting
2025-12-01 02:22:35 -03:00
| Issue | Solution |
|-------|----------|
| LXC not installed | `sudo snap install lxd && sudo lxd init --auto` |
| Permission denied | `sudo usermod -aG lxd $USER && newgrp lxd` |
| Container won't start | `lxc console default-tables --show-log` |
| Port in use | `sudo netstat -tulpn \| grep PORT` |
2025-11-21 12:13:48 -03:00
2025-12-01 02:22:35 -03:00
## Container vs Local
2025-11-21 12:13:48 -03:00
2025-12-01 02:22:35 -03:00
| Use Containers When | Use Local When |
|---------------------|----------------|
| Clean isolation needed | Maximum performance |
| Multiple instances | LXC not available |
| Easy cleanup/reinstall | Simple deployment |
| Security isolation | Direct service access |
2025-11-21 12:13:48 -03:00
## Migration
2025-12-01 02:22:35 -03:00
### Local → Container
2025-11-21 12:13:48 -03:00
```bash
pg_dump botserver > backup.sql
./botserver --container
lxc exec default-tables -- psql -U gbuser botserver < backup.sql
```
2025-12-01 02:22:35 -03:00
### Container → Local
2025-11-21 12:13:48 -03:00
```bash
lxc exec default-tables -- pg_dump -U gbuser botserver > backup.sql
./botserver uninstall tables
./botserver install tables --local
psql -U gbuser botserver < backup.sql
```
2025-12-01 02:22:35 -03:00
## See Also
2025-11-21 12:13:48 -03:00
2025-12-01 02:22:35 -03:00
- [Installation](../chapter-01/installation.md) - Local setup
- [Docker Deployment](./docker-deployment.md) - Docker alternative
- [Architecture](./architecture.md) - System design