How to setup LXD Linux containers on Ubuntu 16.04 cloud machine?
Overview
Containers are a great tool for any scale, from a small dev environment set up on a cloud machine to a large and scalable applications infrastucture. The main benefits are the lightweight, fast and easy setup and the plenty of flexibility.
In this tutorial we’ll take a look at LXD containers – one of the most popular at the moment, especially good for its “virtual machine” like containerization.
Prerequisites
Ubuntu Server 16.04
Step 1: Installing lxd
Installation is simple and straight forward. You just simply install the lxd package from apt:
apt install lxd
Few seconds later you have lxd installed and you can proceed to initial configuration.
Step 2: Initial settings
To start using lxd, you will have to do some initial setting like setting up storage and networking for the containers. It is all done using a simple configuration wizard:
lxd init
Going through the steps you will be asked wether you are willing to provide networking to your containers and how to setup the bridge that will be used for connectivity:
Name of the storage backend to use (dir or zfs) [default=dir]:
Would you like LXD to be available over the network (yes/no) [default=no]? yes
Address to bind LXD to (not including port) [default=all]:
Port to bind LXD to [default=8443]:
Trust password for new clients:
Again:
Do you want to configure the LXD bridge (yes/no) [default=yes]?
Warning: Stopping lxd.service, but it can still be activated by:
lxd.socket
LXD has been successfully configured.
Once you’re done with the ‘init’ you’re ready to start your first container.
Step 3: Starting a container
LXD comes with a nice set of predefined images for various Linux distributions. You can check the list of images by:
lxc image list images:
To start a container simply chose an image and do:
lxc launch images:centos/7 container01
Where “container01” is simply a name for your container. If you skip it an automatically generated one will be set.
Step 4: Connect to your new container
The interesting part is to get into the container. You can execute direct commands by using ‘lxc exec’:
lxc exec container01 — /bin/bash
In the example we start ‘bash’ inside the contatiner and connect interactively to that shell.
root@cloud:~# lxc exec container01 — /bin/bash
[root@container01 ~]#
Step 5: Installing OpenSSH server in a container
Using the “lxc exec” is only good for a local setup, but to make a real use of LXD containers you will probably prefer to get SSH access. We need to install the OpenSSH server first, so while we’re in the ‘bash’ on the CentOS 7 example container we do:
yum install openssh-server
service sshd start
We’ll need to set a password:
passwd
And you’re ready to try the SSH. Exit the ‘bash’ to return back to the LXD host:
exit
Find the internal IP address of your container:
lxc list
+————-+———+———————+——+————+———–+
| NAME | STATE | IPV4 | IPV6 | TYPE | SNAPSHOTS |
+————-+———+———————+——+————+———–+
| container01 | RUNNING | 10.102.70.24 (eth0) | | PERSISTENT | 0 |
+————-+———+———————+——+————+———–+
Finally, from the LXD host machine you can use SSH directly to the internal IP:
ssh root@10.102.70.24
Step 6: Setting a port forward for your container
Connecting from the LXD host might be useful, but to get to being able to remotely access the container the last step you’ll need to setup is port forwarding.
This will map a TCP or UDP port from the host machine to the container. For example:
iptables -t nat -A PREROUTING -j DNAT -p tcp –dport 2001 –to-destination 10.102.70.24:22
Where “2001” is the ‘external’ port on your LXD host and “10.102.70.24:22” is the internal IP of the container with port 22 for SSH.
Now you can access your container from any other computer by simply opening an SSH connection to your LXD host external IP address and port 2001.
Final thoughts
LXD containers seem to be very powerful and easy to setup tool, so we assume you’ll like to play with it for a while.
If you want to stop a container it is simple as:
lxc stop container01
To remove it:
lxc delete container01
Have some fun and come back soon for more Linux containers articles on CloudBalkan.