Yes, in an LXD clustered environment, you can list containers filtered by the host (cluster member) they are running on using the --columns option or by leveraging the lxc cluster list and lxc list commands in combination. However, there isn't a direct single command like lxc ls <hostname> to filter purely by host, but you can achieve this with a few approaches.
Here are some methods to list containers by host in an LXD cluster:
1. Using lxc list with Column Filtering
The lxc list command allows you to display specific columns, including the host (cluster member) a container is running on. You can use grep to filter the output by a specific host.
For example, to list all containers running on a specific host (e.g., host1):
lxc list --columns n,l | grep host1
n shows the container name.
l (lowercase L) shows the location (host) of the container.
grep host1 filters the output to show only containers on host1.
Example output:
+-------------------+--------+
| NAME | LOCATION |
+-------------------+--------+
| int-dev-container | host1 |
| web-app | host1 |
+-------------------+--------+
2. Using lxc list with a Specific Cluster Member
If you want to list all containers on a specific cluster member, you can use the --target option to query a specific host directly. For example:
lxc list --target host1
This will list all containers running on host1, regardless of their names.
Example output:
+--------------------+---------+----------------------+------+-----------+-----------+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+--------------------+---------+----------------------+------+-----------+-----------+
| int-dev-container | RUNNING | 192.168.1.101 (eth0) | | CONTAINER | 0 |
| web-app | RUNNING | 192.168.1.102 (eth0) | | CONTAINER | 0 |
+--------------------+---------+----------------------+------+-----------+-----------+
3. Combining with Container Name Filtering
If you want to filter by both host and container name (e.g., containers starting with int-dev on host1), you can combine the --target option with your name filter:
lxc list --target host1 int-dev
This will list all containers on host1 whose names start with int-dev.
4. List All Cluster Members and Their Containers
If you want to see an overview of all cluster members and their containers, you can first list the cluster members using:
lxc cluster list
Example output:
+-------+--------------------------+---------------+--------+
| NAME | URL | ROLES | STATUS |
+-------+--------------------------+---------------+--------+
| host1 | https://192.168.1.10:8443 | database | ONLINE |
| host2 | https://192.168.1.11:8443 | database | ONLINE |
| host3 | https://192.168.1.12:8443 | database | ONLINE |
+-------+--------------------------+---------------+--------+
Then, for each host, you can run lxc list --target <hostname> to see the containers on that host.
5. Scripting for Automation
If you frequently need to list containers by host, you can create a simple script to automate the process. For example, the following Bash script lists containers for each cluster member:
#!/bin/bash
# Get the list of cluster members
hosts=$(lxc cluster list --format csv | cut -d, -f1)
# Loop through each host and list its containers
for host in $hosts; do
echo "Containers on $host:"
lxc list --target "$host"
echo "-------------------"
done
Save this script (e.g., list_by_host.sh), make it executable (chmod +x list_by_host.sh), and run it (./list_by_host.sh). It will display the containers on each host in your cluster.
Summary
- Use
lxc list --columns n,l | grep <hostname> to filter by host in the output.
- Use
lxc list --target <hostname> to list containers directly on a specific host.
- Combine
--target with name filtering (e.g., lxc list --target host1 int-dev) for more specific queries.
- Use scripting for automation if you need to frequently list containers by host.
These methods should help you efficiently manage and query your LXD cluster!