如何通过pylxd或其他API实现LXC容器跨主机迁移
Hey there! Since you already have CLI-based LXC cross-host migration down, moving to the pylxd API (or even the raw LXD REST API) should be a smooth transition. Let’s walk through exactly how to implement this for your project.
Pre-Requisites
First, make sure you have these bases covered:
- Install pylxd on your control machine (the one running your project code):
pip install pylxd - Both source and target LXD hosts are configured for remote access (same as you set up for CLI migration: trusted certificates, open port 8443 if using HTTPS, etc.)
- Your client certificate is trusted by both LXD hosts
Step-by-Step Implementation
1. Connect to Source and Target LXD Hosts
Create pylxd Client instances to interact with both hosts. For local hosts, you can skip the endpoint (uses Unix socket by default); for remote hosts, use the HTTPS endpoint with your client certs:
from pylxd import Client # Connect to source LXD host source_client = Client( endpoint="https://source-host-ip:8443", cert=("/path/to/your-client.crt", "/path/to/your-client.key") ) # Connect to target LXD host target_client = Client( endpoint="https://target-host-ip:8443", cert=("/path/to/your-client.crt", "/path/to/your-client.key") )
2. Fetch the Container to Migrate
Grab the container object from the source host using its name:
source_container = source_client.containers.get("your-container-name")
3. Prepare the Container for Migration (Optional but Recommended)
To ensure data consistency, pause the container before exporting (just like lxc stop in CLI):
# Stop the container and wait for it to fully halt source_container.stop(wait=True) # Export the container as a byte stream (avoids writing to local disk) container_export = source_container.export()
4. Import the Container to the Target Host
Use the target client to import the exported byte stream, then start the container:
# Import the container to the target host (use the same name or a new one) target_client.containers.import_container(container_export, name="your-container-name") # Fetch the imported container and start it target_container = target_client.containers.get("your-container-name") target_container.start(wait=True)
5. Clean Up the Source Container (Optional)
If you don’t need the original container anymore, delete it from the source host:
source_container.delete(wait=True)
pylxd is just a wrapper around LXD’s native REST API. If you prefer direct HTTP calls (e.g., using requests in Python), here’s the gist:
- Send a
GETrequest tohttps://source-host-ip:8443/1.0/containers/{container-name}/exportto get the container’s tar stream (authenticate with your client certs) - Send a
POSTrequest tohttps://target-host-ip:8443/1.0/containerswith the tar stream in the request body, plus JSON params like{"name": "your-container-name"} - Handle the asynchronous operation status to confirm completion
- Keep source and target LXD versions as close as possible to avoid compatibility issues
- Always back up containers before migration (even if you’re confident!)
- If your container uses custom storage pools or network profiles, ensure those exist on the target host before importing
内容的提问来源于stack exchange,提问作者Jonathan Kumar

