LXD/LXC Basic Setup: Difference between revisions
m (→References) |
m (→References) |
||
Line 137: | Line 137: | ||
* Ask Ubuntu: [https://askubuntu.com/q/1398344/566421 '''apt-key deprecation warning when updating system'''] | * Ask Ubuntu: [https://askubuntu.com/q/1398344/566421 '''apt-key deprecation warning when updating system'''] | ||
* Ask Ubuntu: [https://askubuntu.com/a/971846/566421 At what point is the ~/.bashrc file created?] | * Ask Ubuntu: [https://askubuntu.com/a/971846/566421 At what point is the ~/.bashrc file created?] | ||
== LXC Basic operations == | |||
List available containers.<syntaxhighlight lang="shell" line="1"> | |||
lxc list | |||
lxc list -c ns4 | |||
</syntaxhighlight>Login to a container (note <code>''lxc-webserver''</code> is a container name).<syntaxhighlight lang="shell" line="1"> | |||
lxc shell lxc-webserver | |||
</syntaxhighlight>Execute a command against the container from the host.<syntaxhighlight lang="shell" line="1"> | |||
lxc exec lxc-webserver -- apt install apache2 | |||
</syntaxhighlight>Start, stop or delete container.<syntaxhighlight lang="shell" line="1"> | |||
lxc (start|stop|delete) container-name | |||
</syntaxhighlight>Create a snapshot.<syntaxhighlight lang="shell" line="1"> | |||
lxc snapshot lxc-webserver snapshot-name | |||
</syntaxhighlight>Delete a snapshot.<syntaxhighlight lang="shell" line="1"> | |||
lxc delete lxc-webserver/snapshot-name | |||
</syntaxhighlight>Restore a snapshot.<syntaxhighlight lang="shell" line="1"> | |||
lxc restore lxc-webserver snapshot-name | |||
</syntaxhighlight> | |||
Create a backup. | |||
<syntaxhighlight lang="shell"> | |||
lxc export lxc-webserver ./lxc-webserver-backup.tar.gz | |||
</syntaxhighlight> | |||
Restore a backup.<syntaxhighlight lang="shell"> | |||
lxc import ./lxc-webserver-backup.tar.gz | |||
</syntaxhighlight> | |||
Get info about the container (and its snapshots at the bottom).<syntaxhighlight lang="shell" line="1"> | |||
lxc info lxc-webserver | |||
</syntaxhighlight>Limit the container's memory usage.<syntaxhighlight lang="shell" line="1"> | |||
lxc config set lxc-webserver limits.memory 1GB | |||
</syntaxhighlight>'''Auto-start a container.'''<syntaxhighlight lang="shell" line="1"> | |||
lxc config set lxc-webserver boot.autostart 1 | |||
</syntaxhighlight>Set an auto-start delay for a container.<syntaxhighlight lang="shell" line="1"> | |||
lxc config set lxc-webserver boot.autostart.delay 30 | |||
</syntaxhighlight>Set an auto-start order number for a container.<syntaxhighlight lang="shell" line="1"> | |||
lxc config set lxc-database boot.autostart.order 2 | |||
lxc config set lxc-webserver boot.autostart.order 3 | |||
</syntaxhighlight>Disable IPv6 for the containers - [https://github.com/lxc/lxd/issues/3333#issuecomment-302933013 reference].<syntaxhighlight lang="shell" line="1"> | |||
lxc network set lxdbr0 ipv6.address none | |||
</syntaxhighlight> | |||
<noinclude> | <noinclude> |
Revision as of 14:28, 8 September 2022
LXD Initial Setup
sudo usermod -aG lxd <user>
Initialize LXD.
lxd init
Would you like to use LXD clustering? (yes/no) [default=no]: no
Do you want to configure a new storage pool? (yes/no) [default=yes]: yes
Name of the new storage pool [default=default]: default
Name of the storage backend to use (dir, lvm, zfs, ceph, btrfs) [default=zfs]: dir
Would you like to connect to a MAAS server? (yes/no) [default=no]: no
Would you like to create a new local network bridge? (yes/no) [default=yes]: yes
What should the new bridge be called? [default=lxdbr0]: lxdbr0
What IPv4 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: auto
What IPv6 address should be used? (CIDR subnet notation, “auto” or “none”) [default=auto]: auto
Would you like the LXD server to be available over the network? (yes/no) [default=no]: no
Would you like stale cached images to be updated automatically? (yes/no) [default=yes]: yes
Would you like a YAML "lxd init" preseed to be printed? (yes/no) [default=no]: yes
config: {}
networks:
- config:
ipv4.address: auto
ipv6.address: auto
description: ""
name: lxdbr0
type: ""
project: default
storage_pools:
- config: {}
description: ""
name: default
driver: dir
profiles:
- config: {}
description: ""
devices:
eth0:
name: eth0
network: lxdbr0
type: nic
root:
path: /
pool: default
type: disk
name: default
projects: []
cluster: null
List the available images by the command: lxc image list ubuntu:22.04 | grep x86_64
. Then install a certain image and name the container lxc-webserver
, that will be capable to run other containers inside, for more details read the article Nested containers in LXD.
lxc launch ubuntu:22.04 lxc-webserver -c security.nesting=true
Creating lxc-webserver
Starting lxc-webserver
In order to add (or remove) the nesting option to an existing LXC, use:
lxc config set lxc-webserver security.nesting true
lxc list --columns ns4 # name, state and IPv4
+---------------+---------+-----------------------+
| NAME | STATE | IPV4 |
+---------------+---------+-----------------------+
| lxc-webserver | RUNNING | 10.127.198.222 (eth0) |
+---------------+---------+-----------------------+
Configure the container to obtain a static IP assignment.
lxc config device override lxc-webserver eth0
Device eth0 overridden for lxc-webserver
lxc config device set lxc-webserver eth0 ipv4.address 10.127.198.222 # no output mean everything is fine
lxc restart lxc-webserver
lxc list --columns ns4 | grep eth0 # check the ip
| lxc-webserver | RUNNING | 10.127.198.222 (eth0) |
LXD Basic operation
Restart the service.
sudo snap restart lxd
Mount a host directory to a directory inside a container. Map the permissions.
printf "lxd:$(id -u):1\nroot:$(id -u):1\n" | sudo tee -a /etc/subuid # Allow LXD’s use of our user uid
printf "lxd:$(id -g):1\nroot:$(id -g):1\n" | sudo tee -a /etc/subgid # Allow LXD’s use of our user gid
sudo snap restart lxd # Restart LXD to have it load the new map
printf "uid $(id -u) 1000\ngid $(id -g) 1000" | lxc config set lxc-webserver raw.idmap - # Set a custom map for our container
lxc restart lxc-webserver # Restart the container to have the new map apply
lxc config device add lxc-webserver Git disk source=/home/<user>/Git path=/home/<user>/Git # Mount the directory
Do the actual share (mount) of some directories.
lxc config device add lxc-webserver Git disk source=/home/<user>/Git path=/home/<user>/Git
lxc config device add lxc-webserver VSC disk source=/home/<user>/.vscode-server path=/home/<user>/.vscode-server
References:
- Linux containers LXD: Server configuration settings
- Canonical Ubuntu Blog: Custom user mappings in LXD containers
- Canonical Ubuntu Blog: Mounting your home directory in LXD
- Ask Ubuntu: Adding a shared host directory to an LXC/LXD Container (for privileged container)
References
- DigitalOcean: How To Install and Configure LXD on Ubuntu 20.04
- Linux containers LXD: Getting Started with LXD
- Canonical Ubuntu Server: LXD
- LearnLinuxTV: Getting started with LXD Containerization (Full Guide!)
- Ask Ubuntu: apt-key deprecation warning when updating system
- Ask Ubuntu: At what point is the ~/.bashrc file created?
LXC Basic operations
List available containers.
lxc list
lxc list -c ns4
Login to a container (note lxc-webserver
is a container name).
lxc shell lxc-webserver
Execute a command against the container from the host.
lxc exec lxc-webserver -- apt install apache2
Start, stop or delete container.
lxc (start|stop|delete) container-name
Create a snapshot.
lxc snapshot lxc-webserver snapshot-name
Delete a snapshot.
lxc delete lxc-webserver/snapshot-name
Restore a snapshot.
lxc restore lxc-webserver snapshot-name
Create a backup.
lxc export lxc-webserver ./lxc-webserver-backup.tar.gz
Restore a backup.
lxc import ./lxc-webserver-backup.tar.gz
Get info about the container (and its snapshots at the bottom).
lxc info lxc-webserver
Limit the container's memory usage.
lxc config set lxc-webserver limits.memory 1GB
Auto-start a container.
lxc config set lxc-webserver boot.autostart 1
Set an auto-start delay for a container.
lxc config set lxc-webserver boot.autostart.delay 30
Set an auto-start order number for a container.
lxc config set lxc-database boot.autostart.order 2
lxc config set lxc-webserver boot.autostart.order 3
Disable IPv6 for the containers – reference.
lxc network set lxdbr0 ipv6.address none