You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过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.

Using pylxd for LXC Container Cross-Host Migration

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")

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)
Alternative: Use the Raw LXD REST API

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 GET request to https://source-host-ip:8443/1.0/containers/{container-name}/export to get the container’s tar stream (authenticate with your client certs)
  • Send a POST request to https://target-host-ip:8443/1.0/containers with the tar stream in the request body, plus JSON params like {"name": "your-container-name"}
  • Handle the asynchronous operation status to confirm completion
Key Notes
  • 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:49:55