Skip to content

Commit c4cfba0

Browse files
JskobosGuessWhoSamFoo
authored andcommitted
[NEW] How to Use LXD Containers on Linode (#1493)
* [NEW] How to use LXD containers on Linode * how to use block storage * how to repartition the Linode disk * common use of LXD and practical example * [UPDATE] How to use LXD containers on Linode (#1469) * trim whitespace at end of lines * Tech edit * Initial copy edit commit * Updated introductory paragraph
1 parent 59eabc9 commit c4cfba0

8 files changed

+236
-0
lines changed
Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
---
2+
author:
3+
name: Linode
4+
5+
description: 'LXD is a container hypervisor that manages Linux Containers. Compared to other uses of Linux Containers, LXD manages machine containers which each work just like typical servers. This guide will show how to run Apache inside LXD and direct traffic to the container.'
6+
keywords: ["container", "lxd", "lxc", "virtual machine"]
7+
license: '[CC BY-ND 4.0](https://creativecommons.org/licenses/by-nd/4.0)'
8+
published: 2018-02-12
9+
modified: 2018-02-12
10+
modified_by:
11+
name: Linode
12+
title: 'How to use LXD containers on Linode'
13+
contributor:
14+
name: Simos Xenitellis
15+
link: https://blog.simos.info/
16+
external_resources:
17+
- '[LXD Introduction](https://linuxcontainers.org/lxd/)'
18+
- '[Blog post series on LXD 2.0](https://stgraber.org/2016/03/11/lxd-2-0-blog-post-series-012/)'
19+
- '[LXD support community](https://discuss.linuxcontainers.org/)'
20+
- '[Try LXD Online](https://linuxcontainers.org/lxd/try-it/)'
21+
---
22+
23+
## What is LXD?
24+
25+
[LXD](https://linuxcontainers.org/lxd/) (pronounced "Lex-Dee") is a system container manager build on top of LXC (Linux Containers) that is currently supported by Canonical. The goal of LXD is to provide an experience similar to a virtual machine but through containerization rather than virtualization. Compared to Docker for delivering applications, LXD offers nearly full OS functionality with additional features such as snapshots, live migrations, storage management, and more.
26+
27+
The main benefits of LXD are the high density of containers that it can support and the performance it delivers compared to virtual machines. A computer with 2GB RAM can adequately support half a dozen containers. In addition, LXD officially supports the [container images of major Linux distributions](https://us.images.linuxcontainers.org/). We can choose the Linux distribution and version to run in the container.
28+
29+
This guide covers how to setup a Linode to work with LXD, how LXD works in practice, and how to troubleshoot common issues.
30+
31+
{{< note >}}
32+
For simplicity, the term *container* is used throughout this guide to describe the LXD containers.
33+
{{< /note >}}
34+
35+
## Before You Begin
36+
37+
1. Complete the [Getting Started](/docs/getting-started) guide. If you are using a Block Storage Volume, **select** the image `Ubuntu 16.04 LTS` from the drop-down menu according to the instructions.
38+
39+
2. This guide will use `sudo` wherever possible. Follow the [Securing Your Server](/docs/security/securing-your-server/) guide to create a standard user account, harden SSH access, and remove unnecessary network services.
40+
41+
3. Update your system:
42+
43+
sudo apt update && sudo apt upgrade
44+
45+
## Mount Storage Volume
46+
47+
When setting up LXD, you can either store container data in an [external volume](#block-storage-volume) (such as a Block Storage Volume) or in a [Disk](#disk) mounted to your Linode.
48+
49+
### Block Storage Volume
50+
51+
1. Follow the [How to Use Block Storage with Your Linode](/docs/platform/how-to-use-block-storage-with-your-linode/) guide and create a block storage volume with size *at least 20GB* and attach it to your Linode. Make a note of the device name and the path to the Volume.
52+
53+
{{< caution >}}
54+
**Do not** format the volume and do not add it to `/etc/fstab`.
55+
{{< /caution >}}
56+
57+
![Add a volume for Disk Storage](/docs/assets/lxd/add-volume-for-disk-storage.png "Add a volume for Disk Storage")
58+
59+
2. Edit your Configuration Profile and under **Boot Settings** select **GRUB 2** as your kernel. See [Run a Distribution-Supplied Kernel on a KVM Linode](/docs/tools-reference/custom-kernels-distros/run-a-distribution-supplied-kernel-with-kvm/) for more information.
60+
61+
3. Reboot your Linode from the Linode Manager.
62+
63+
### Disk
64+
65+
1. In the Linode Manager, find the **Disks** section and click **Create a new disk**.
66+
67+
![Create a Linode Disk](/docs/assets/lxd/create_new_disk.png)
68+
69+
{{< note >}}
70+
If your Linode's distribution disk already has 100% of the available disk space allocated to it, you will need to resize the disk before you can create a storage disk. See [Resizing a Disk](/docs/platform/disk-images/disk-images-and-configuration-profiles/#resizing-a-disk) for more information.
71+
{{< /note >}}
72+
73+
2. Edit your Linode's Configuration Profile. Under **Block Device Assignment**, assign your new disk to `/dev/sdc`. Make a note of this path, which you will need when configuring LXD in the next section.
74+
75+
![Block Device Assignement](/docs/assets/lxd/block-device-assignment.png "Block Device Assignment")
76+
77+
3. Under **Boot Settings** select **GRUB 2** as your kernel.
78+
79+
4. Reboot your Linode from the Linode Manager.
80+
81+
## Initialize LXD
82+
83+
1. Install the packages `lxd` and `zfsutils-linux`:
84+
85+
sudo apt install lxd zfsutils-linux
86+
87+
2. Add your Unix user to the `lxd` group:
88+
89+
sudo usermod -a -G lxd username
90+
91+
3. Start a new SSH session for this change to take effect:
92+
93+
4. Run `lxd init` to initialize LXD:
94+
95+
sudo lxd init
96+
97+
You will be prompted several times during the initialization process. Choose the defaults for all options **except** `Use existing block device?` For this option, select **yes** and then enter the path to the storage volume added in the previous section.
98+
99+
## LXD Commands
100+
101+
1. List all containers:
102+
103+
lxc list
104+
105+
{{< output >}}
106+
Generating a client certificate. This may take a minute...
107+
If this is your first time using LXD, you should also run: sudo lxd init
108+
To start your first container, try: lxc launch ubuntu:16.04
109+
110+
+------+-------+------+------+------+-----------+
111+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
112+
+------+-------+------+------+------+-----------+
113+
{{< /output >}}
114+
115+
2. List all available container images:
116+
117+
lxc image list images:
118+
119+
{{< output >}}
120+
+---------------------------------+--------------+--------+------------------------------------------+---------+----------+-------------------------------+
121+
| ALIAS | FINGERPRINT | PUBLIC | DESCRIPTION | ARCH | SIZE | UPLOAD DATE |
122+
+---------------------------------+--------------+--------+------------------------------------------+---------+----------+-------------------------------+
123+
| alpine/3.4 (3 more) | 39a3bf44c9d8 | yes | Alpine 3.4 amd64 (20180126_17:50) | x86_64 | 2.04MB | Jan 26, 2018 at 12:00am (UTC) |
124+
+---------------------------------+--------------+--------+------------------------------------------+---------+----------+-------------------------------+
125+
| alpine/3.4/armhf (1 more) | 9fe7c201924c | yes | Alpine 3.4 armhf (20170111_20:27) | armv7l | 1.58MB | Jan 11, 2017 at 12:00am (UTC) |
126+
+---------------------------------+--------------+--------+------------------------------------------+---------+----------+-------------------------------+
127+
| alpine/3.4/i386 (1 more) | d39f2f2ba547 | yes | Alpine 3.4 i386 (20180126_17:50) | i686 | 1.88MB | Jan 26, 2018 at 12:00am (UTC) |
128+
+---------------------------------+--------------+--------+------------------------------------------+---------+----------+-------------------------------+
129+
| alpine/3.5 (3 more) | 5533a5247551 | yes | Alpine 3.5 amd64 (20180126_17:50) | x86_64 | 1.70MB | Jan 26, 2018 at 12:00am (UTC) |
130+
+---------------------------------+--------------+--------+------------------------------------------+---------+----------+-------------------------------+
131+
| alpine/3.5/i386 (1 more) | 5e93d5f4cae1 | yes | Alpine 3.5 i386 (20180126_17:50) | i686 | 1.73MB | Jan 26, 2018 at 12:00am (UTC) |
132+
+---------------------------------+--------------+--------+------------------------------------------+---------+----------+-------------------------------+
133+
| alpine/3.6 (3 more) | 5010616d9a24 | yes | Alpine 3.6 amd64 (20180126_17:50) | x86_64 | 1.73MB | Jan 26, 2018 at 12:00am (UTC) |
134+
+---------------------------------+--------------+--------+------------------------------------------+---------+----------+-------------------------------+
135+
.....................................................................
136+
{{< /output >}}
137+
138+
{{< note >}}
139+
The first two columns for the alias and fingerprint provide an identifier that can be used to specify the container image when launching it.
140+
{{< /note >}}
141+
142+
3. Launch a new container with the name `mycontainer`:
143+
144+
lxc launch ubuntu:16.04 mycontainer
145+
146+
{{< output >}}
147+
Creating mycontainer
148+
Starting mycontainer
149+
{{< /output >}}
150+
151+
152+
4. Check the list of containers to make sure the new container is running:
153+
154+
lxc list
155+
156+
{{< output >}}
157+
+-------------+---------+-----------------------+---------------------------+------------+-----------+
158+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
159+
+-------------+---------+-----------------------+---------------------------+------------+-----------+
160+
| mycontainer | RUNNING | 10.142.148.244 (eth0) | fde5:5d27:...:1371 (eth0) | PERSISTENT | 0 |
161+
+-------------+---------+-----------------------+---------------------------+------------+-----------+
162+
{{< /output >}}
163+
164+
165+
5. Execute basic commands in `mycontainer`:
166+
167+
lxc exec mycontainer -- apt update
168+
lxc exec mycontainer -- apt upgrade
169+
170+
{{< note >}}
171+
The characters `--` instruct the `lxc` command not to parse any more command-line parameters.
172+
{{< /note >}}
173+
174+
6. Open a shell session within `mycontainer`:
175+
176+
lxc exec mycontainer -- sudo --login --user ubuntu
177+
178+
{{< output >}}
179+
To run a command as administrator (user "root"), use "sudo <command>".
180+
See "man sudo_root" for details.
181+
182+
ubuntu@mycontainer:~$
183+
{{< /output >}}
184+
185+
{{< note >}}
186+
The Ubuntu container images have by default a non-root account with username `ubuntu`. This account can use `sudo` and does not require a password to perform administrative tasks.
187+
188+
The `sudo` command provides a login to the existing account `ubuntu`.
189+
{{< /note >}}
190+
191+
7. View the container logs:
192+
193+
lxc info mycontainer --show-log
194+
195+
8. Stop the container:
196+
197+
lxc stop mycontainer
198+
199+
9. Remove the container:
200+
201+
lxc delete mycontainer
202+
203+
204+
## Apache Web Server with LXD
205+
206+
This section will create a container, install the Apache web server, and add the appropriate `iptables` rules in order to expose post 80.
207+
208+
1. Launch a new container:
209+
210+
lxc launch ubuntu:16.04 web
211+
212+
2. Update the package list in the container.
213+
214+
lxc exec web -- apt update
215+
216+
3. Install the Apache in the LXD container.
217+
218+
lxc exec web -- apt install apache2
219+
220+
4. Add the `iptables` rule to expose the port 80. When someone connects to port 80 through the public IP address, this rule redirects them to port 80 of the container.
221+
222+
You will need to replace `your_public_ip` and `your_container_ip` with your public IP and container IP respectively in this command.
223+
224+
PORT=80 PUBLIC_IP=your_public_ip CONTAINER_IP=your_container_ip sudo -E bash -c 'iptables -t nat -I PREROUTING -i eth0 -p TCP -d $PUBLIC_IP --dport $PORT -j DNAT --to-destination $CONTAINER_IP:$PORT -m comment --comment "forward to the Apache2 container"'
225+
226+
5. Make the `iptables` rule persist on reboot by installing `iptables-persistent`. When prompted to save the IPv4 and IPv6 rules, click **Yes** in order to save them.
227+
228+
sudo apt install iptables-persistent
229+
230+
6. From your local computer, navigate to your Linode's public IP address in a web browser. You should see the default Apache page:
231+
232+
[![Web page of Apache server running in a container](/docs/assets/lxd/apache-server-running-in-lxd-container.png)](/docs/assets/lxd/apache-server-running-in-lxd-container.png "Web page of Apache server running in a container.")
233+
234+
## Next Steps
235+
236+
If you plan to use a single website, then a single `iptables` rule to the website container will suffice. If you plan to use multiple websites, you need to set up [a reverse proxy like NGINX](https://linode.com/docs/web-servers/nginx/nginx-reverse-proxy/) in a container. The `iptables` rule would then redirect to this container.
22 KB
Loading
67.9 KB
Loading
20.3 KB
Loading
33.4 KB
Loading
36.7 KB
Loading
37.1 KB
Loading
11.9 KB
Loading

0 commit comments

Comments
 (0)