Manage Infrastructure Setup

Managing Containers, Images, Storage Pools, and Backups (CLI + Web UI)

This document provides a comprehensive guide on managing LXC (Linux Containers) using both the Command-Line Interface (CLI) and the LXC Web UI. It covers creating tech-stacked images, copying containers and images between remotes, managing storage pools, and performing backups and restores.


1. Creating Tech-Stacked Images

Using CLI:

Tech-stacked images are pre-configured container images that include specific software stacks, libraries, and configurations tailored for a particular use case. Here’s how to create them:

Steps:

  1. Create a Base Container: Start by launching a base container using an existing image:

    lxc launch images:ubuntu/22.04 pssb1aldevlxc
    
  2. Install Required Software: Access the container and install the desired software stack:

    lxc exec pssb1aldevlxc -- apt update
    lxc exec pssb1aldevlxc -- apt install -y nginx mysql-server php-fpm
    
  3. Configure the Environment: Customize the container as needed (e.g., configure services, add scripts, or modify configuration files).

  4. Publish the Container as an Image: Once the container is fully configured, publish it as a reusable image:

    lxc stop pssb1aldevlxc
    lxc publish pssb1aldevlxc --alias=my-tech-stack-image
    
  5. Verify the Image: Check if the image is available locally:

    lxc image list
    
  6. Push the Image to a Remote (Optional): If you want to share the image with other systems, push it to a remote server:

    lxc image copy my-tech-stack-image remote: --copy-aliases
    

Using LXC Web UI:

  1. Launch a New Container:

    • Navigate to the “Containers” section in the Web UI.
    • Click “Add Container” and select an image (e.g., Ubuntu 22.04).
    • Provide a name for the container (e.g., pssb1aldevlxc001).
  2. Access the Container:

    • Open the terminal for the container from the Web UI.
    • Install the required software stack using commands like:
      apt update && apt install -y nginx mysql-server php-fpm
      
  3. Publish the Image:

    • Stop the container from the Web UI.
    • Go to the “Images” section and click “Create Image from Container.”
    • Select the container (pssb1aldevlxc) and provide an alias (e.g., tech-stacked-pssb-image).
  4. Push to Remote:

    • In the “Images” section, select the image and click “Copy to Remote.”
    • Choose the remote server and ensure “Copy Aliases” is enabled.

2. Copying Containers and Images Between Remotes

Using CLI:

To copy containers and images between local and remote systems:

Copying Containers:

lxc copy ps1adevlxc001 remote:my-container

Copying Images:

lxc image copy my-image remote: --copy-aliases

Using LXC Web UI:

  1. Copy Containers:

    • Navigate to the “Containers” section.
    • Select the container you want to copy.
    • Click “Copy” and choose the destination remote.
  2. Copy Images:

    • Go to the “Images” section.
    • Select the image you want to copy.
    • Click “Copy to Remote” and specify the destination.

3. Managing Storage Pools

Using CLI:

Storage pools in LXC manage disk space for containers and images.

Common Operations:

  1. List Existing Storage Pools:

    lxc storage list
    
  2. Create a New Storage Pool: Create a new storage pool (e.g., ZFS or DIR):

    lxc storage create my-pool dir
    
  3. Set Default Storage Pool: Set the default storage pool for new containers:

    lxc profile device add default root disk path=/ pool=my-pool
    
  4. Resize a Storage Pool: For ZFS pools, resize the underlying dataset:

    zfs set quota=50G my-pool
    
  5. Delete a Storage Pool: Remove a storage pool (ensure no containers are using it):

    lxc storage delete my-pool
    

Using LXC Web UI:

  1. List Storage Pools:

    • Navigate to the “Storage” section in the Web UI.
    • View all available storage pools.
  2. Create a New Storage Pool:

    • Click “Add Storage Pool.”
    • Specify the name, driver (e.g., dir, zfs), and configuration options.
  3. Set Default Storage Pool:

    • Go to the “Profiles” section.
    • Edit the default profile and set the root device to the desired storage pool.
  4. Resize/Delete Storage Pools:

    • Use the Web UI to edit or delete storage pools directly.

4. Backup and Export Containers as .tar.gz Files

Using CLI:

Exporting containers as .tar.gz files creates portable backups.

Steps:

  1. Stop the Container (Optional): Stopping the container ensures data consistency:

    lxc stop ps1adevlxc002
    
  2. Export the Container: Export the container to a compressed .tar.gz file:

    lxc export ps1adevlxc002 ps1adevlxc002-backup.tar.gz
    
  3. Import the Backup: To restore the container from the backup:

    lxc import ps1adevlxc002-backup.tar.gz
    

Using LXC Web UI:

  1. Export a Container:

    • Navigate to the “Containers” section.
    • Select the container and click “Export.”
    • Choose the format (.tar.gz) and download the backup.
  2. Import a Container:

    • Go to the “Containers” section.
    • Click “Import” and upload the .tar.gz file.

5. Backup and Restore Snapshots

Using CLI:

Snapshots are point-in-time copies of a container’s state.

Creating Snapshots:

  1. Take a Snapshot: Create a snapshot of a container:

    lxc snapshot ps1adevlxc002 ps1adevlxc002-ss01
    
  2. List Snapshots: View all snapshots of a container:

    lxc info ps1adevlxc002
    

Restoring Snapshots:

  1. Restore a Snapshot: Revert back the container to a specific snapshot:

    lxc restore ps1adevlxc003 ps1adevlxc002-ss01
    
  2. Export a Snapshot: Export a snapshot as a standalone container:

    lxc copy ps1adevlxc002/ps1adevlxc002-ss01 ps1adevlxc003
    

Deleting Snapshots:

Remove a snapshot when it’s no longer needed:

lxc delete ps1adevlxc002/ps1adevlxc002-ss01

Using LXC Web UI:

  1. Create a Snapshot:

    • Navigate to the “Containers” section.
    • Select the container and click “Snapshots.”
    • Click “Create Snapshot” and provide a name.
  2. Restore a Snapshot:

    • In the “Snapshots” tab, select the snapshot and click “Restore.”
  3. Export a Snapshot:

    • Select the snapshot and click “Export” to download it as a .tar.gz file.
  4. Delete a Snapshot:

    • Select the snapshot and click “Delete.”

Conclusion

This documentation outlines the key operations for managing LXC containers using both the CLI and the LXC Web UI. By following these steps, you can efficiently manage your containerized infrastructure and ensure data integrity and availability.

For further details, refer to the official LXC documentation.