redis-service/proxmox-lxc-setup.md

8.6 KiB

Setting Up an LXC Container for Docker in Proxmox (Redis Service)

This guide will walk you through creating and configuring an LXC container in Proxmox that's optimized for running Docker and our Redis 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: redis-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 10GB (recommended 20GB+ for production)
    • CPU:

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

      • Memory: At least 2GB (recommended 4GB+ for production)
      • Swap: 1GB
    • 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 Redis using Docker Compose

  1. Create a directory for your Redis service:
mkdir -p /opt/redis-service
cd /opt/redis-service
  1. Clone your Git repository:
git clone <your-git-repository-url> .
# Or manually create the files as described in the README.md
  1. Create a proper .env file with secure credentials:
cp .env.example .env
nano .env
  1. Start the Redis service:
docker-compose up -d
  1. Verify that the container is running:
docker-compose ps
docker logs redis-server

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

# For UFW
ufw allow 6379/tcp

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

Step 7: Test the Connection

From your external machine, test the connection to Redis:

  1. Using redis-cli:

    redis-cli -h <your-server-ip> -p 6379 -a <your-redis-password>
    
  2. Once connected, test with a simple command:

    PING
    

    You should receive "PONG" as a response.

Troubleshooting

If you encounter issues:

  1. Check container logs:

    docker-compose logs
    
  2. Verify network connectivity:

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

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

    docker stats
    
  5. Check Redis configuration:

    docker exec -it redis-server redis-cli -a <your-redis-password> CONFIG GET *
    

Maintenance

  1. Backup your data regularly:

    # Create a backup directory
    mkdir -p /opt/redis-backups
    
    # Run the backup command
    docker exec redis-server redis-cli -a <your-redis-password> SAVE
    docker cp redis-server:/data/dump.rdb /opt/redis-backups/redis-backup-$(date +%Y%m%d%H%M%S).rdb
    
  2. Update your container:

    docker-compose pull
    docker-compose down
    docker-compose up -d
    
  3. Monitor your system resources:

    htop
    
  4. Check Redis metrics:

    docker exec -it redis-server redis-cli -a <your-redis-password> INFO