postgres-service/proxmox-lxc-setup.md

9.3 KiB

Setting Up an LXC Container for Docker in Proxmox

This guide will walk you through creating and configuring an LXC container in Proxmox that's optimized for running Docker and our PostgreSQL service.

Step 1: Download the Ubuntu 22.04 LTS Template

If you don't already have the Ubuntu 22.04 LTS template in your Proxmox server, you'll need to download it first:

  1. Log in to your Proxmox web interface
  2. Select your node in the server view
  3. Go to the "Local" storage (or any storage configured for CT templates)
  4. Click on the "Templates" button
  5. In the template list, find "ubuntu-22.04-standard" in the list
    • If you don't see it, click on "Templates" and then search for "ubuntu-22.04"
    • If the template list is empty or doesn't show Ubuntu 22.04, you may need to refresh the list by clicking "Refresh"
  6. Click on the template and then click "Download"
  7. Wait for the download to complete

Step 2: Create a new LXC Container in Proxmox

  1. Log in to your Proxmox web interface

  2. Select your node in the server view

  3. Click "Create CT" to create a new container

  4. Configure the basic settings:

    • General:

      • Node: (your Proxmox node)
      • CT ID: (choose an available ID, e.g., 102)
      • Hostname: postgres-docker
      • Unprivileged container: Yes (checked)
      • Password: (set a secure password)
      • SSH Public Key: (optionally add your SSH key)
    • Template:

      • Best choice: Ubuntu 22.04 LTS (ubuntu-22.04-standard)
      • Reasons:
        • Excellent Docker compatibility
        • Long-term support until 2027
        • Best documentation and community support for Docker
        • Most stable kernel features needed for containerization
        • Regular security updates
    • Disks:

      • Storage: (select your storage)
      • Disk size: At least 20GB (recommended 40GB+ for production)
    • CPU:

      • Cores: At least 2 (recommended 4+ for production)
    • Memory:

      • Memory: At least 4GB (recommended 8GB+ for production)
      • Swap: 2GB
    • Network:

      • Name: eth0
      • Bridge: vmbr0 (or your preferred bridge)
      • IP address: DHCP or static IP
      • IP version: IPv4
  5. Click "Finish" to create the container

Step 3: Configure the LXC Container for Docker

After creating the container, you need to modify its configuration to support Docker:

  1. Stop the container if it's running
  2. From the Proxmox shell, run these commands to modify the container configuration:
# Enable nesting and other required features
pct set <container-id> -features nesting=1,keyctl=1
  1. Edit the container configuration file directly:
nano /etc/pve/lxc/<container-id>.conf
  1. Add these lines to the configuration file:
lxc.apparmor.profile: unconfined
lxc.cgroup.devices.allow: a
lxc.cap.drop: 
lxc.mount.auto: proc:rw sys:rw
  1. Start the container

Step 4: Configure Network and Install Docker

  1. Start the container and access its shell:
pct start <container-id>
pct enter <container-id>
  1. IMPORTANT: Check if your network interface has an IP address:
ip a

If your eth0 interface doesn't show an IPv4 address (like 192.168.x.x), you need to configure it first:

# For Proxmox LXC containers, configure networking from the Proxmox web interface:

1. Exit the container first with 'exit' command
2. In the Proxmox web interface, select your container from the left sidebar
3. Click 'Stop' to stop the container if it's running
4. Go to the 'Network' tab
5. If there's no network interface, click 'Create' to add one:
   - Name: eth0
   - Bridge: vmbr0 (or your preferred bridge)
   - IPv4: DHCP (or Static with your preferred IP configuration)
   - IPv4/CIDR: (if using static IP, enter something like 192.168.1.100/24)
   - Gateway: (if using static IP, enter your gateway, e.g., 192.168.1.1)
6. If there's already a network interface, click 'Edit' and update the configuration
7. Click 'OK' to save the changes
8. Go back to the 'Summary' tab and click 'Start' to start the container
9. Click 'Console' to access the container

# Alternatively, use the command line on the Proxmox host:

# Stop the container
pct stop <container-id>

# Configure networking (DHCP)
pct set <container-id> -net0 name=eth0,bridge=vmbr0,ip=dhcp

# Or configure with static IP (replace with your network details)
pct set <container-id> -net0 name=eth0,bridge=vmbr0,ip=192.168.1.100/24,gw=192.168.1.1

# Start the container again
pct start <container-id>
pct enter <container-id>

# Verify you now have an IP address
ip a
  1. Fix network connectivity issues:
# First, check if you can ping IP addresses
ping -c 4 8.8.8.8

# If you can't ping IPs, check your network interface
ip a

# Check your container's network configuration
cat /etc/network/interfaces

# Check DNS configuration
cat /etc/resolv.conf

# Fix DNS by adding these entries to resolv.conf
echo "nameserver 8.8.8.8
nameserver 8.8.4.4" > /etc/resolv.conf

# Make the DNS changes persistent by editing the systemd-resolved configuration
mkdir -p /etc/systemd/resolved.conf.d/
cat > /etc/systemd/resolved.conf.d/dns_servers.conf << EOF
[Resolve]
DNS=8.8.8.8 8.8.4.4
FallbackDNS=1.1.1.1
EOF

# Restart networking and DNS services
systemctl restart systemd-networkd
systemctl restart systemd-resolved

# Test DNS resolution
host archive.ubuntu.com
  1. If DNS is still not working, try adding entries to /etc/hosts:
# Add essential Ubuntu repositories to /etc/hosts
cat >> /etc/hosts << EOF
185.125.190.36 archive.ubuntu.com
185.125.190.36 security.ubuntu.com
EOF

# Test if it works
ping -c 2 archive.ubuntu.com
  1. Install basic tools:
apt update
apt install -y curl wget apt-transport-https ca-certificates gnupg lsb-release
  1. Update the system:
apt update && apt upgrade -y
  1. Install Docker using the official installation script:
# Download the Docker installation script
curl -fsSL https://get.docker.com -o get-docker.sh

# Review the script (optional but recommended)
less get-docker.sh

# Run the installation script
sh get-docker.sh

This script automatically detects your OS, adds the appropriate repositories, and installs Docker and its dependencies.

  1. Install Docker Compose:
curl -L "https://github.com/docker/compose/releases/download/v2.20.3/docker-compose-linux-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
  1. Verify the installations:
docker --version
docker-compose --version
  1. Enable Docker to start on boot:
systemctl enable docker

Step 5: Deploy PostgreSQL using Docker Compose

  1. Create a directory for your PostgreSQL service:
mkdir -p /opt/postgres-service
cd /opt/postgres-service
  1. Clone your Git repository:
git clone ssh://git@gitea.mehmetkaratay.com.tr:222/evyos-center-server/postgres-service.git .
  1. Create a proper .env file with secure credentials:
cp environments/.env.production .env
nano .env
  1. Make the scripts executable:
chmod +x scripts/*.sh
  1. Create the Docker network for PostgreSQL:
docker network create postgres_network
  1. Start the PostgreSQL service:
docker-compose up --build -d
  1. Verify that the container is running:
docker-compose ps

If you're using a firewall on your Proxmox host, make sure to allow traffic to port 5432:

# For UFW
ufw allow 5432/tcp

# For iptables
iptables -A INPUT -p tcp --dport 5432 -j ACCEPT

Step 7: Test the Connection

From your external machine, test the connection to PostgreSQL:

  1. Using psql or another PostgreSQL client, connect to:

    psql -h your-server-ip -p 5432 -U postgres -d postgres
    
  2. Or using a connection string:

    postgresql://postgres:your_password@your-server-ip:5432/postgres
    

Troubleshooting

If you encounter issues:

  1. Check container logs:

    docker-compose logs
    
  2. Verify network connectivity:

    telnet your-server-ip 5432
    
  3. Check Docker service status:

    systemctl status docker
    
  4. Ensure the container has proper resources:

    docker stats
    
  5. Check PostgreSQL logs:

    docker-compose logs postgres
    

Maintenance

Backup and Restore

  1. Create a backup of your PostgreSQL database:

    cd /opt/postgres-service
    ./scripts/backup.sh
    
  2. Restore from a backup when needed:

    cd /opt/postgres-service
    ./scripts/restore.sh backups/postgres_backup_YYYYMMDD_HHMMSS.sql.gz
    

Monitoring

  1. Monitor PostgreSQL performance:

    cd /opt/postgres-service
    ./scripts/monitor.sh
    
  2. Check PostgreSQL logs:

    docker-compose logs postgres
    
  3. Monitor system resources:

    htop
    
  4. Check disk usage:

    df -h
    

Updates and Maintenance

  1. Update PostgreSQL container:

    cd /opt/postgres-service
    docker-compose pull
    docker-compose down
    docker-compose up -d
    
  2. Restart PostgreSQL service:

    cd /opt/postgres-service
    docker-compose restart
    
  3. Stop PostgreSQL service:

    cd /opt/postgres-service
    docker-compose down
    
  4. Start PostgreSQL service:

    cd /opt/postgres-service
    docker-compose up -d