< Return to Video

Getting started with LXC containers

  • 0:00 - 0:03
    Hello viewers. Welcome to this video.
  • 0:03 - 0:08
    I'm Vagrant, and this is Just Me and Opensource channel. Right. In this video,
  • 0:08 - 0:11
    I'm going to talk about LXD containers.
  • 0:11 - 0:15
    What the LXD and LXC containers are, and how
  • 0:15 - 0:18
    to use them. I'll also show you a quick
  • 0:18 - 0:20
    demo of how you can get started with
  • 0:20 - 0:24
    using the LXD containers. As usual,
  • 0:24 - 0:27
    I've got my notes here to explain things
  • 0:27 - 0:32
    a little better. Okay. So we've got virtual machines, and the LXD
  • 0:32 - 0:38
    containers, and the application containers--basically Docker, RKT,
  • 0:38 - 0:42
    and so on. Okay. So how do LXD machine containers differ from a
  • 0:42 - 0:48
    virtual machine or application containers? So, basically, when you use
  • 0:48 - 0:51
    virtual machines, you've got your bare metal, and you've
  • 0:51 - 0:53
    got an operating system running on top
  • 0:53 - 0:55
    of it. It could be Linux, Windows, or macOS.
  • 0:55 - 0:58
    And you install a hypervisor on your
  • 0:58 - 1:04
    operating system. A typical example would be VirtualBox, and you create
  • 1:04 - 1:07
    virtual machines on top of it. So that's
  • 1:07 - 1:10
    the hypervisor bit, and each of the guest
  • 1:10 - 1:12
    operating systems is a complete operating
  • 1:12 - 1:18
    system. All the hardware attached to these guest virtual machines is
  • 1:18 - 1:24
    virtualized hardware. So the hypervisor is responsible for giving the
  • 1:24 - 1:30
    guest OS the hardware from your bare metal in a virtualized fashion.
  • 1:30 - 1:35
    So that's virtual machines, and we all are familiar
  • 1:35 - 1:37
    with Docker containers, or any
  • 1:37 - 1:42
    container platform. There is RKT and Docker. So similar to virtual machines,
  • 1:42 - 1:46
    you've got your bare metal, you've got your operating system installed--
  • 1:46 - 1:51
    Linux, Windows, or macOS--and you install a container runtime. It could be
  • 1:51 - 1:56
    Docker, RKT, or anything. And then you create these container images.
  • 1:56 - 1:58
    So these are not virtual machines.
  • 1:58 - 2:00
    You're not running a complete virtual machine.
  • 2:00 - 2:03
    It's just an application container. You grab
  • 2:03 - 2:06
    the libraries and the dependencies that your
  • 2:06 - 2:10
    application needs, package them as a container, and you start containers.
  • 2:10 - 2:13
    So containers are very lightweight compared to virtual machines.
  • 2:13 - 2:19
    You're not running an entire operating system, so it's just a set of files
  • 2:19 - 2:22
    your application requires.
  • 2:22 - 2:25
    You package them as a container, and the
  • 2:25 - 2:29
    container runtime is responsible for managing multiple containers.
  • 2:29 - 2:31
    So that's lightweight. You can have multiple
  • 2:31 - 2:35
    containers. They're all very lightweight.
  • 2:35 - 2:37
    So where do the machine containers sit?
  • 2:37 - 2:39
    They sit between the virtual machines
  • 2:39 - 2:41
    and the application containers.
  • 2:41 - 2:46
    They take the best of both worlds. So how is it
  • 2:46 - 2:52
    compared to virtual machines? The LXD containers that you create are a
  • 2:52 - 2:57
    complete operating system, not just an application container like Docker.
  • 2:57 - 2:59
    So it's a complete operating system similar
  • 2:59 - 3:04
    to virtual machines. And how is it compared to Docker?
  • 3:04 - 3:11
    It's very lightweight. It doesn't need a hypervisor or a container runtime.
  • 3:11 - 3:19
    It sits on top of the Linux OS. It uses the LXC libraries, and LXD is the REST
  • 3:19 - 3:25
    API. Your command line tool interacts with the LXD API to access the LXC
  • 3:25 - 3:30
    libraries, and you create your guest operating systems. It's a complete
  • 3:30 - 3:33
    operating system, and all the hardware--
  • 3:33 - 3:39
    whether it's CPU, memory, USB ports, and so on--is available to these
  • 3:39 - 3:43
    guest OS. It can start the container in a
  • 3:43 - 3:45
    matter of seconds, a complete operating
  • 3:45 - 3:51
    system. It's a very quick one. I've been recently using virtual
  • 3:51 - 3:53
    machines, and I used Vagrant to provision
  • 3:53 - 3:56
    virtual machines. But recently, I started
  • 3:56 - 3:59
    using...because of their simplicity and
  • 3:59 - 4:02
    lightweight nature. I started using
  • 4:02 - 4:07
    LXD machine containers. Okay. So you
  • 4:07 - 4:11
    can deploy a container--I mean, a machine
  • 4:11 - 4:13
    container--in a particular host machine,
  • 4:13 - 4:18
    or you could have a cluster of host machines running LXD on each of them.
  • 4:18 - 4:24
    And basically, that's a cluster. For this video, I'm not going to go into the
  • 4:24 - 4:27
    cluster mode. I'm just going to use a single
  • 4:27 - 4:31
    virtual machine to do that. Okay. The next
  • 4:31 - 4:35
    thing is comparing two virtual machines.
  • 4:35 - 4:38
    Say, for example, if you've got VirtualBox
  • 4:38 - 4:42
    or VMware or any hypervisor installed--
  • 4:42 - 4:45
    say, for example, your host memory has 16
  • 4:45 - 4:49
    GB of memory--and you're creating a couple
  • 4:49 - 4:52
    of virtual machines. So one is--each of
  • 4:52 - 4:57
    these virtual machines--you specify 4 GB.
  • 4:57 - 5:00
    So, basically, you're taking 8 GB
  • 5:00 - 5:02
    out of the 16 GB of your host memory.
  • 5:02 - 5:08
    So your host machine is left with just 8 GB. When you allocate 4 GB to a
  • 5:08 - 5:11
    virtual machine, you're allocating the
  • 5:11 - 5:13
    entire 4 GB to the machine. That's it.
  • 5:13 - 5:15
    So it's not available to your host machine.
  • 5:15 - 5:22
    Whereas in the LXC and LXD containers, you don't specify anything.
  • 5:22 - 5:24
    Although you can limit how much memory a
  • 5:24 - 5:28
    machine container can take, if you don't
  • 5:28 - 5:31
    specify anything, the entire host memory
  • 5:31 - 5:35
    is available for every single machine container that you create.
  • 5:35 - 5:40
    So when your host memory is 16 GB, inside your
  • 5:40 - 5:45
    container, if you look at your memory, the whole 16 GB is available for each
  • 5:45 - 5:50
    of these containers. But I'll show you in the demo how we can limit
  • 5:50 - 5:58
    the amount of memory that each container can take--limits.memory, limits.cpu--you can set limits.
  • 6:00 - 6:02
    Okay. So what we'll be doing in this quick
  • 6:02 - 6:04
    demo--I’ll show you some commonly used
  • 6:04 - 6:06
    commands, how to start and stop containers,
  • 6:06 - 6:11
    how to pull images, do a snapshot, apply certain profiles, and so on.
  • 6:11 - 6:18
    And also, there is a possibility to do nested containers. Say, you create a
  • 6:18 - 6:21
    machine--you create an LXD machine--and
  • 6:21 - 6:23
    inside that LXD machine, you create
  • 6:23 - 6:25
    another LXD environment and another LXD.
  • 6:25 - 6:28
    So basically, you can nest multiple levels.
  • 6:28 - 6:32
    Okay. So let's get into the business.
  • 6:32 - 6:38
    I've got an Ubuntu virtual machine. My host machine is Arch Linux,
  • 6:38 - 6:40
    and I've got a virtual machine.
  • 6:40 - 6:42
    Because most people are using Ubuntu, I thought
  • 6:42 - 6:48
    it would be useful to show this demo on an Ubuntu machine.
  • 6:48 - 6:53
    I've also got an LXD environment set up on my host machine, OS Linux.
  • 6:53 - 6:55
    If you're not aware, I'm also doing a series
  • 6:55 - 7:02
    on Kubernetes. So I'm using the LXD containers for Kubernetes. That's from
  • 7:02 - 7:04
    my host machine. But for this demo,
  • 7:04 - 7:08
    I'm going to use the Ubuntu 18.04 virtual machine.
  • 7:08 - 7:17
    lsb_release -a, which is 18.04. And this virtual machine is run
  • 7:17 - 7:20
    from VirtualBox. Okay. So that doesn't
  • 7:20 - 7:25
    matter. Okay. How do you install LXD?
  • 7:25 - 7:29
    In Ubuntu 18.04--I'm not sure about 16.04;
  • 7:29 - 7:35
    I haven't checked actually--but in 18.04, LXC and LXD are already installed.
  • 7:35 - 7:43
    If you do which lxc and which lxd, you'll find LXC and LXD installed.
  • 7:43 - 7:46
    So dpkg -l, you can check whether a
  • 7:46 - 7:51
    package is installed. grep lxd. So I've
  • 7:51 - 7:54
    got the LXD package installed. If not,
  • 7:54 - 8:02
    the command to install is sudo apt-get install lxd. That's it. Okay.
  • 8:03 - 8:07
    So once it's installed, there's a service, but it's not started.
  • 8:07 - 8:12
    If you look at the LXD service, that's the
  • 8:12 - 8:15
    service that needs to be running, and you
  • 8:15 - 8:17
    can see it's enabled, indirect. It's not
  • 8:17 - 8:19
    running at the moment. So the first thing
  • 8:19 - 8:27
    to do is to start the LXD service. sudo systemctl enable--no, that's already
  • 8:27 - 8:41
    enabled. Let's just start it--start lxd. So sudo systemctl status lxd.
  • 8:41 - 8:43
    It's running now. And the next thing we're going to
  • 8:43 - 8:49
    do is--there's a group called LXD.
  • 8:49 - 8:54
    And if you want to run LXC commands without prefixing sudo,
  • 8:54 - 8:57
    you need to add your user account to the LXD
  • 8:57 - 9:02
    group. I'm using Vagrant, so my user account is Vagrant. So I'm going to add
  • 9:02 - 9:05
    Vagrant to the LXD group. The command
  • 9:05 - 9:22
    to add is sudo gpasswd -a vagrant lxd. getent group lxd--
  • 9:22 - 9:25
    so now Vagrant is a member of the LXD
  • 9:25 - 9:29
    group. For the changes to take effect, you
  • 9:29 - 9:36
    can either do newgrp lxd, or you can just log out and log back in.
  • 9:37 - 9:44
    vagrant ssh. Okay. groups--so I'm part of the LXD group.
  • 9:44 - 9:48
    So now what I can do is, once you start the
  • 9:48 - 9:51
    service, the first thing to do is to initialize the LXD environment.
  • 9:51 - 9:56
    So the command to do this is lxd init.
  • 9:57 - 9:59
    Would you like to use LXD clustering? As I said, you
  • 9:59 - 10:01
    can set up an LXD cluster, but for
  • 10:01 - 10:03
    this video, I'm just going to install it
  • 10:03 - 10:07
    on one machine. So the default is no.
  • 10:07 - 10:09
    Do you want to configure a new storage pool?
  • 10:09 - 10:11
    So all the machine containers will use
  • 10:11 - 10:15
    this storage pool for storing the file
  • 10:15 - 10:18
    systems. Yes, definitely. Because this is a
  • 10:18 - 10:20
    plain installation, it won't have any
  • 10:20 - 10:24
    storage pool created. So let's create one.
  • 10:24 - 10:26
    Name of the new storage pool--it can be
  • 10:26 - 10:30
    any name. Let's leave it as default.
  • 10:30 - 10:32
    Name of the storage backend to use.
  • 10:32 - 10:37
    The default is Btrfs. It comes with lots of advantages.
  • 10:37 - 10:38
    You can do a snapshot.
  • 10:38 - 10:41
    You can do lots of volume management.
  • 10:41 - 10:44
    But for simplicity’s sake, I'm going to use the dir,
  • 10:44 - 10:48
    which is a directory on your host machine.
  • 10:48 - 10:51
    Would you like to connect to MAAS server,
  • 10:51 - 10:55
    which is Metal as a Service server? No.
  • 10:55 - 10:57
    Would you like to create a new local
  • 10:57 - 11:01
    network bridge? Yes. So again, this is the
  • 11:01 - 11:09
    same machine. If I do, the IP address is 172.24.24.101.
  • 11:09 - 11:14
    So that's the network interface--the active network interface--of the host
  • 11:14 - 11:19
    machine. And we need to create a network bridge so that all the machine
  • 11:19 - 11:23
    containers that we are going to create will use that network bridge to
  • 11:23 - 11:26
    communicate with the outside world.
  • 11:26 - 11:28
    So let's create the network bridge.
  • 11:28 - 11:30
    What should the network bridge be called?
  • 11:30 - 11:34
    By default, it's lxdbr0. You can call it any name.
  • 11:34 - 11:41
    Let's leave it to the default. IP address--what range do you want?
  • 11:41 - 11:45
    Let's leave it to default. Automatically assign IPv4, IPv6.
  • 11:45 - 11:49
    Would you like LXD to be available over the network?
  • 11:49 - 11:52
    No. I just want to access it from this machine.
  • 11:52 - 11:56
    I don't want to access it from other machines. Would you like stale cached
  • 11:56 - 11:58
    images to be updated automatically?
  • 11:58 - 12:01
    So when you first launch any of the
  • 12:01 - 12:03
    machine containers, it's going to pull
  • 12:03 - 12:06
    the machine image from a remote repository.
  • 12:06 - 12:10
    So once you pull that, every time you
  • 12:10 - 12:11
    start a machine, it's going to use the
  • 12:11 - 12:15
    image that it has pulled to your local machine.
  • 12:15 - 12:21
    This setting tells it to periodically check whether the local image is an
  • 12:21 - 12:26
    up-to-date image. If it's not, it will sync the image from the remote
  • 12:26 - 12:30
    repository. So let's leave it to default. Yes.
  • 12:30 - 12:34
    Would you like YAML init preseed to be printed out?
  • 12:34 - 12:39
    Basically, all these options that we just entered--do you want them to be printed out?
  • 12:39 - 12:44
    Default is no. That's all the questions. So we are all good to go.
  • 12:44 - 12:53
    And if you do lxc version--3.0.3--server version 3.0.3. Okay.
  • 12:53 - 12:57
    So it also gives you a command to start your container and do
  • 12:57 - 12:59
    an lxc launch. So before that, I want to
  • 12:59 - 13:01
    show you a few other things.
  • 13:01 - 13:06
    To show a list of available commands, do an lxc help.
  • 13:06 - 13:13
    lxc help | less. So there are lots of handy commands
  • 13:13 - 13:16
    that you can use, and I'll show you some
  • 13:16 - 13:18
    basic commands--most commonly used commands.
  • 13:18 - 13:20
    If you want to know more about a
  • 13:20 - 13:22
    particular command, you can use the help
  • 13:22 - 13:25
    option. For example, for storage, if you
  • 13:25 - 13:32
    want to look at an example for storage--lxc help <command>.
  • 13:32 - 13:37
    Okay. So you can create, delete, edit, get info, list, and so on.
  • 13:37 - 13:42
    lxc storage list--so that's the default storage.
  • 13:42 - 13:46
    The driver is directory--remember we did that during the lxc init
  • 13:46 - 13:50
    command. And that's the directory where
  • 13:50 - 13:53
    it's going to store all the machine images.
  • 13:55 - 13:58
    Okay. So where is it going to pull the machine
  • 13:58 - 14:03
    images from? lxc remote list.
  • 14:04 - 14:06
    So there are a couple of repositories that have
  • 14:06 - 14:10
    been added. So images:--that's the one.
  • 14:10 - 14:13
    And there are Ubuntu releases, Ubuntu containers.
  • 14:13 - 14:19
    So there are three repositories added. When you launch a
  • 14:19 - 14:25
    new machine, it's going to pull from these repositories. And local is just
  • 14:25 - 14:29
    the local repository. Whenever you pull it from the other three servers,
  • 14:29 - 14:33
    it's going to store it locally on this machine.
  • 14:33 - 14:38
    Okay. So let's try and search for a container.
  • 14:38 - 14:43
    Say, for example, if I want to start a CentOS machine--
  • 14:43 - 14:45
    how would you start it, or how would you find if
  • 14:45 - 14:53
    there is a machine image available for CentOS? lxc image list--
  • 14:53 - 14:57
    so that’s the list on your local machine.
  • 14:57 - 14:59
    I don’t have any images on my local machine. Okay.
  • 14:59 - 15:09
    lxc image list images:--if you just do images:, it's going to list all the
  • 15:09 - 15:16
    images available on that particular repository. There are lots of images--
  • 15:16 - 15:19
    Debian, Alpine, CentOS, and so on.
  • 15:19 - 15:21
    And if you want to search for a specific one,
  • 15:21 - 15:29
    you can start typing a few letters of it. So if I just do cent, you can see
  • 15:29 - 15:33
    CentOS 7, CentOS 7, and there should be lots.
  • 15:33 - 15:40
    If I pipe that to less, you can see CentOS 6, i386 version, CentOS 7,
  • 15:40 - 15:44
    ARM64, and so on. Okay. If I want to search
  • 15:44 - 15:53
    for Debian--there is Debian here: Debian 10, 9, and so on.
  • 15:53 - 15:56
    Okay. So that's how you search for the machine
  • 15:56 - 16:02
    images. So how would you start it? I want to start an image now. Okay.
  • 16:02 - 16:10
    So in this host machine, if I do uname, the kernel is 4.15.
  • 16:10 - 16:13
    So this kernel is for Ubuntu 18.04, and all the
  • 16:13 - 16:14
    machine containers--whether it's CentOS,
  • 16:14 - 16:16
    Debian, Alpine, or whatever--
  • 16:16 - 16:22
    are going to use the host kernel.
  • 16:22 - 16:26
    It's not the OS-specific kernel. It's whatever the host
  • 16:26 - 16:29
    kernel is, it’s going to use it. Okay.
  • 16:29 - 16:34
    So let's try and start an Ubuntu 16.04 container.
  • 16:35 - 16:41
    lxc launch is the command. Launch and
  • 16:41 - 16:47
    the container name--Ubuntu 16.04.
  • 16:47 - 16:49
    So if you're using any of the Ubuntu images,
  • 16:49 - 16:53
    you don’t have to prefix the remote repository part.
  • 16:54 - 17:05
    If I show you lxc remote list--So when you do ubuntu:16.04,
  • 17:05 - 17:09
    it's going to pull the image from this repository. So you don't have
  • 17:09 - 17:16
    to prefix ubuntu, but if it's anything else, you need to prefix images:. Okay.
  • 17:16 - 17:28
    So let's try and create an Ubuntu 16.04 image and a CentOS 7 image. lxc launch ubuntu:16.04
  • 17:28 - 17:32
    And if you don't specify a name, it's going to give you a
  • 17:32 - 17:36
    random name. So let's try--but before
  • 17:36 - 17:38
    that, if you want to see what machines
  • 17:38 - 17:40
    are available, what machines you've got,
  • 17:40 - 17:45
    you can do an lxc list. So at the moment, we don't have any machines--
  • 17:45 - 17:54
    we don’t have any containers started. lxc launch ubuntu:16.04
  • 17:56 - 18:00
    It's going to take--for the first time--a little while, maybe a couple of
  • 18:00 - 18:02
    minutes depending on your Internet speed.
  • 18:02 - 18:08
    So basically, it's pulling the file system to your local machine.
  • 18:08 - 18:12
    Once that's done, that's the base image, and all the other containers you're
  • 18:12 - 18:14
    going to start will start from the local
  • 18:14 - 18:18
    image. So the next time you launch a new container,
  • 18:18 - 18:22
    it will take less than 3 or 4 seconds to start it.
  • 18:22 - 18:28
    Right, it has started the container. Now if I do lxc
  • 18:28 - 18:33
    list, and there you go. The state is RUNNING, and it has got its own IP
  • 18:33 - 18:41
    address. And after we did the lxc init command, if I look at the network
  • 18:41 - 18:44
    interfaces, you will see lxdbr0.
  • 18:44 - 18:46
    So that's the network bridge for all the
  • 18:46 - 18:48
    machine containers this host is going to
  • 18:48 - 18:56
    create. It has got the IP address 10.149.213.1, and all the machines will get
  • 18:56 - 19:01
    the IP address from this bridge--10.149.213.165.
  • 19:01 - 19:05
    So that's in the range of this IP address. And for
  • 19:05 - 19:08
    each machine container you create, you're
  • 19:08 - 19:10
    going to see a virtual Ethernet interface
  • 19:10 - 19:16
    on your host machine. So that one is for this machine. I forgot to give the
  • 19:16 - 19:20
    machine a name, so it defined a random name.
  • 19:20 - 19:30
    Okay. Let's delete it. lxc delete <container-name>--horse.
  • 19:30 - 19:33
    You'll see an error here. It won't be able to
  • 19:33 - 19:36
    delete it while the container is running.
  • 19:36 - 19:40
    If you want to delete it, you need to stop the container before deleting it.
  • 19:40 - 19:46
    lxc stop sweet-horse. Or, if you want,
  • 19:46 - 19:50
    you can pass the--force option--that will stop the container for you and delete it.
  • 19:50 - 19:58
    So let's do the stop. lxc list--so the container is stopped.
  • 19:58 - 20:12
    And if you want to start the container: lxc start <container-name>, lxc start sweet-horse.
  • 20:12 - 20:20
    lxc list--see how quick that is? lxc list. Okay, the container is running.
  • 20:20 - 20:24
    Let's delete it. Let's delete it with the force option:
  • 20:24 - 20:31
    lxc delete--force sweet-horse.
  • 20:33 - 20:34
    Okay. That's deleted.
  • 20:34 - 20:40
    lxc list, that's gone. lxc image list--so that's the local
  • 20:40 - 20:44
    image that we downloaded: Ubuntu 16.04.
  • 20:44 - 20:48
    And the total size that I downloaded is 158 MB.
  • 20:48 - 20:54
    It's so lightweight. And now if I do lxc launch, if I launch a new Ubuntu
  • 20:54 - 20:57
    16.04 container, it's going to be very quick
  • 20:57 - 21:01
    because we've got an image already on our local machine.
  • 21:01 - 21:06
    lxc launch ubuntu:16.04 and give it a name--
  • 21:06 - 21:15
    myubuntu. Creating myubuntu, and then it will start it. Okay.
  • 21:15 - 21:21
    The container has been created. lxc list. Okay, there you go.
  • 21:21 - 21:26
    It has got a different IP address now. Okay, cool.
  • 21:26 - 21:34
    lxc list. If you want to copy this machine container, you can do lxc copy
  • 21:34 - 21:43
    myubuntu myanotherubuntu. lxc list.
  • 21:43 - 21:45
    So now we have got another machine,
  • 21:45 - 21:53
    which is stopped. You can start it with lxc start myanotherubuntu.
  • 21:53 - 22:05
    Okay, that's started. lxc list. I can see both these VMs--both these, not VMs, both these machine containers--are running.
  • 22:05 - 22:11
    They each got their own IP address. Right. lxc list.
  • 22:11 - 22:19
    So that's copy. Let's delete it. lxc delete --force myanotherubuntu.
  • 22:19 - 22:21
    Okay. Before deleting it, I just want to
  • 22:21 - 22:25
    show you how you can reach one container from another container.
  • 22:25 - 22:29
    Okay, let's keep that for a moment. And if you
  • 22:29 - 22:31
    want to rename a container or if you
  • 22:31 - 22:32
    want to move a container--say if you've
  • 22:32 - 22:38
    got an LXD cluster (obviously we don't
  • 22:38 - 22:39
    have a cluster at the moment)--but if
  • 22:39 - 22:42
    you've got a cluster, you can use the lxc move
  • 22:42 - 22:44
    command to move the container from one
  • 22:44 - 22:53
    host to another host. And to do that, lxc move is the command--myubuntu --
  • 22:53 - 22:57
    and give it another name. If it's on another host machine, you need to give
  • 22:57 - 23:01
    the hostname followed by a colon and the path, but
  • 23:01 - 23:04
    I'm going to move it within this host.
  • 23:04 - 23:08
    So I'm going to give it another name--myubuntu to myvm.
  • 23:08 - 23:12
    Okay. Renaming a container is not allowed while it's running.
  • 23:12 - 23:21
    lxc stop myubuntu, and then we do the lxc move command. That's done.
  • 23:22 - 23:31
    lxc list, and you can see myvm. It's been moved to myvm. lxc start myvm.
  • 23:32 - 23:38
    Okay, that started. lxc list. Good. So we've
  • 23:38 - 23:40
    got two VMs. Okay, so we've got the machine
  • 23:40 - 23:43
    containers. How would we log in to the
  • 23:43 - 23:45
    container, or how can we use the container?
  • 23:45 - 23:50
    So if you have used the Ubuntu container, there is a default Ubuntu user
  • 23:50 - 23:53
    account in these machine images.
  • 23:53 - 24:04
    But if you want to log in as root, you can do lxc exec <container-name> -- bash, e.g., lxc exec myvm -- bash.
  • 24:04 - 24:07
    Okay, so I'm inside the myvm container.
  • 24:07 - 24:09
    If I do hostname, the hostname
  • 24:09 - 24:17
    is myvm. That's cool. lsb_release--so that's Ubuntu 16.04, the machine that
  • 24:17 - 24:19
    we started. It's a complete operating
  • 24:19 - 24:21
    system--a very quick way to spin up a
  • 24:21 - 24:25
    virtual machine. And if I do free -m,
  • 24:25 - 24:28
    so it's 2 GB. Basically, my host machine
  • 24:28 - 24:33
    (I mean the virtual machine) has 2 GB, and
  • 24:33 - 24:36
    the container can see the complete
  • 24:36 - 24:41
    memory. So I've got one CPU, and one CPU
  • 24:41 - 24:46
    here. Okay. And if I do uname -r--4.15.0-45--
  • 24:46 - 24:51
    which is the host kernel on my Ubuntu 18.04.
  • 24:53 - 24:59
    Right. And you can ping the machine containers. You can ping
  • 24:59 - 25:02
    between the machine containers using the
  • 25:02 - 25:05
    DNS name. So when you create the LXD
  • 25:05 - 25:10
    bridge, you can ping the other containers
  • 25:10 - 25:13
    using .lxd naming convention.
  • 25:13 - 25:20
    So our other container was--I need to log out and log back in.
  • 25:20 - 25:25
    lxc list. myanotherubuntu. So if I want to ping it:
  • 25:25 - 25:32
    ping myanotherubuntu.lxd. Okay, so I'm
  • 25:32 - 25:34
    getting ping responses back from the
  • 25:34 - 25:45
    container. And if I want to log in to myanotherubuntu as user ubuntu, what
  • 25:45 - 25:56
    you can do is: su - ubuntu. Okay. hostname, I'm inside my Ubuntu virtual machine.
  • 25:56 - 26:06
    lsb_release--there, okay, 16.04. ping myvm.lxd--I'm getting ping response.
  • 26:06 - 26:12
    Okay, cool. So what else can we try? We did an image.
  • 26:12 - 26:15
    We did launch, start, stop. We did delete,
  • 26:15 - 26:19
    list, exec. So we've completed all these
  • 26:19 - 26:22
    commands. So let's go to these sets of commands.
  • 26:22 - 26:29
    So, config info. If you want to look at the details of a machine
  • 26:29 - 26:35
    container, you can do lxc info <container-name>--lxc info myvm.
  • 26:35 - 26:39
    Let me pipe that to less. So you can see remote (I mean
  • 26:39 - 26:43
    where it's running from). It will give you the path of the machine the
  • 26:43 - 26:46
    container is running, when it was created,
  • 26:46 - 26:50
    what architecture, and the current status of the machine--it's running.
  • 26:50 - 26:54
    Profiles--I’ll come to profiles in a minute.
  • 26:54 - 27:06
    PID is 7629. If I search for ps -p 7629, okay--so that's
  • 27:06 - 27:12
    the process isolation. Ethernet interface,
  • 27:12 - 27:15
    the address of the Ethernet interface,
  • 27:15 - 27:18
    how many processes it’s running--28 processes,
  • 27:18 - 27:21
    CPU usage, memory usage--it’s only using
  • 27:21 - 27:29
    about 50 MB of memory. Network bytes and some stats--so that's lxc info.
  • 27:29 - 27:32
    And there is also machine configuration if you want to
  • 27:32 - 27:36
    look at the machine configuration--lxc
  • 27:36 - 27:42
    config show myvm. So that’s the complete
  • 27:42 - 27:50
    configuration of the machine. Right? And it's using the default profile.
  • 27:50 - 27:55
    Okay, so I’ll come to the profile in a minute.
  • 27:59 - 28:04
    We've also done the copy and the move command. Okay, let's go to the profile.
  • 28:04 - 28:09
    lxc profile list--so we've got only one profile, and it's used by two
  • 28:09 - 28:11
    containers. So as we've got two machine
  • 28:11 - 28:15
    containers and we didn't assign any custom profile, by default, it uses the
  • 28:15 - 28:22
    default profile. So what's in the default profile? lxc profile show default.
  • 28:23 - 28:30
    So it hasn’t got any configuration--it's just a description and name, and the
  • 28:30 - 28:34
    storage pool is the default storage pool,
  • 28:34 - 28:39
    which we created during our lxd init command. And the type is disk.
  • 28:39 - 28:43
    What’s the ethernet interface? And what's the network bridge?
  • 28:43 - 28:48
    We're going to use lxdbr0. So that's the default profile.
  • 28:49 - 28:52
    And if you want to create a new profile, you can
  • 28:52 - 29:03
    copy the existing profile--lxc profile copy default custom. And if I do
  • 29:03 - 29:09
    lxc profile list, you can see the custom profile, and it's used by zero
  • 29:09 - 29:12
    containers. And if you want to launch a
  • 29:12 - 29:14
    container and use this profile, you have
  • 29:14 - 29:32
    to use: lxc launch <container-image> <name> --profile custom. For example: lxc launch ubuntu:16.04 myvm2 --profile custom.
  • 29:32 - 29:35
    So at the moment, the custom profile is the same as the default profile, but
  • 29:35 - 29:38
    I want to show you some options that you
  • 29:38 - 29:42
    can set in the profile. Okay, before that--
  • 29:42 - 29:51
    how can you restrict the memory? (It's going to be the same for CPU.)
  • 29:51 - 29:53
    Okay, let me log in to one of the machines:
  • 29:53 - 30:02
    lxc exec myvm -- bash. Okay, I'm in here--free -m.
  • 30:02 - 30:07
    So I'm seeing the complete 2 GB of memory that was on my host machine,
  • 30:07 - 30:13
    inside this container. So if I want this machine to use not more than 512 MB--
  • 30:13 - 30:18
    half a gig of RAM--because if you don't
  • 30:18 - 30:23
    set any limits, there are possibilities that any process inside your machine
  • 30:23 - 30:25
    might be resource intensive, and it could
  • 30:25 - 30:30
    end up using all your host memory, leaving your host
  • 30:30 - 30:32
    system in danger. So it's always a good
  • 30:32 - 30:35
    practice to limit the amount of memory and
  • 30:35 - 30:38
    number of CPUs on your machine
  • 30:38 - 30:41
    containers so that they won’t be able to use
  • 30:41 - 30:47
    more than the amount that you specify. This is all done using cgroups.
  • 30:47 - 30:55
    Okay, so free--now it has got 2 GB. Let's set it to half a gig.
  • 30:55 - 31:00
    You could do it dynamically or you can set it in the profile.
  • 31:00 - 31:02
    So I’ll show you both ways.
  • 31:02 - 31:15
    lxc config set myvm limits.memory 512MB.
  • 31:15 - 31:18
    Permission denied.
  • 31:18 - 31:22
    Okay. Let me log out and log back in.
  • 31:24 - 31:34
    lxc list, lxc config set myVM--so I'm going to set the configuration for my
  • 31:34 - 31:38
    VM, which is dynamic. The container is running. I haven't stopped the
  • 31:38 - 31:40
    container, so whatever change I'm going to
  • 31:40 - 31:51
    do is dynamic. lxc config set myVM limits.memory 512MB--that's done.
  • 31:51 - 32:01
    And if I do lxc config show myVM | less, you can see the configuration is
  • 32:01 - 32:03
    set here: limits.memory is 512.
  • 32:03 - 32:07
    Okay, so back inside the machine container, if
  • 32:07 - 32:11
    I do free -m now, you can see the memory
  • 32:11 - 32:13
    that it can use is only half a gig.
  • 32:13 - 32:16
    It can't go more than half a gig.
  • 32:16 - 32:25
    And I can change it again to one gig, 1GB.
  • 32:25 - 32:31
    And if you do free -m, you can see the change taking place, taking effect.
  • 32:31 - 32:39
    So that's dynamic. What if you want to set it in a profile?
  • 32:39 - 32:54
    Okay. Let's delete that VM. lxc delete --force myVM, and let's edit the custom profile that we just created:
  • 32:54 - 33:05
    lxc profile edit custom. Okay. So in the config section,
  • 33:05 - 33:16
    just remove the curly braces and type in limits.memory 512MB.
  • 33:16 - 33:24
    Save it. That's the profile. Now, if you try and create a virtual machine
  • 33:24 - 33:29
    applying that profile, then you will get those configuration details from the
  • 33:29 - 33:42
    profile: lxc launch ubuntu:16.04 myVM2 --profile custom.
  • 33:43 - 33:46
    Creating myVM2, and it's going to use the custom
  • 33:46 - 33:51
    profile that we created. And the only change we made was to set a default
  • 33:51 - 33:56
    memory limit of 512MB, half a gig.
  • 33:56 - 34:06
    Okay. lxc exec myVM2 bash--we are inside the container.
  • 34:06 - 34:17
    hostname--okay. ip a--the IP address is .1. free -m--now you can see it's half a gig of memory.
  • 34:17 - 34:23
    So, setting the CPU is going to be the same. Since I'm on
  • 34:23 - 34:24
    my virtual machine, I forgot to set the
  • 34:24 - 34:27
    number of CPUs. So my virtual machine has
  • 34:27 - 34:35
    got just one CPU. If I had, like, two or more CPUs, I could set in the profile
  • 34:35 - 34:46
    limits.cpu to 1. For example: lxc config set myVM2 limits.cpu 1.
  • 34:46 - 34:52
    If you've got, like, two CPUs and you want to assign just one CPU to the
  • 34:52 - 34:54
    machine container, you can use that command.
  • 34:55 - 35:02
    Okay. We've covered all these. Let's go to the
  • 35:02 - 35:05
    snapshot, restore, and file operations.
  • 35:05 - 35:08
    Okay. So if you want to push a file to the
  • 35:08 - 35:10
    container or if you want to pull a file
  • 35:10 - 35:16
    from the container to your host machine, you can use the file command.
  • 35:16 - 35:24
    Okay. lxc exec myVM2 bash--I don't have any files in there.
  • 35:24 - 35:29
    Okay. On my host machine, I'm going to create a file:
  • 35:29 - 35:35
    echo hello there > myfile.
  • 35:35 - 35:36
    And if you want to push this file into the
  • 35:36 - 35:48
    container, into that machine, I'm going to use: lxc file push myfile myVM2/root/.
  • 35:48 - 35:53
    So the container name is going to be myVM2, and it's not
  • 35:53 - 35:58
    semicolon or colon--it has to be in this format:
  • 35:58 - 36:01
    name slash and the part inside the
  • 36:01 - 36:08
    container name, slash, and the path inside the container, whether it's /etc, blah, blah, blah, or /root. Okay?
  • 36:08 - 36:14
    And if I do ls, you've got the file there. cat myfile--okay.
  • 36:14 - 36:18
    So the file is in the container. I'm going to delete the file
  • 36:18 - 36:23
    on my host machine--I don't have it here.
  • 36:23 - 36:26
    Now let's pull the file from the container to the host machine.
  • 36:26 - 36:28
    It's just the other way around.
  • 36:28 - 36:41
    lxc file pull myVM2/root/myfile ./ cat myfile--okay?
  • 36:41 - 36:47
    So that's how you use the file commands.
  • 36:47 - 37:07
    Okay. So let's quickly do the snapshot and restore capabilities. Let's snapshot the machine first. Okay.
  • 37:07 - 37:25
    I'm going to create some directories: for i in $(seq 5); do mkdir $i; done.
  • 37:25 - 37:32
    Okay. So I've created five directories named 1, 2, 3, 4, 5, and let's
  • 37:32 - 37:34
    exit out of the container. I'm going to
  • 37:34 - 37:37
    create a snapshot of this container.
  • 37:37 - 37:48
    The command to do it is: lxc snapshot myVM2 snap1.
  • 37:48 - 37:53
    So basically, it's going to copy the entire file system. As you saw
  • 37:53 - 37:59
    earlier, the size of the image is 158MB.
  • 37:59 - 38:03
    So when I say it's going to snapshot it, it's going to copy the
  • 38:03 - 38:06
    entire file system. Okay, so that's done.
  • 38:06 - 38:12
    And if I log into the VM and delete all
  • 38:12 - 38:20
    these directories: rm -rf * --I haven't got any directories.
  • 38:20 - 38:23
    Say, for example, I accidentally deleted all these
  • 38:23 - 38:34
    files--what I could do is: lxc restore myVM2 snap1.
  • 38:34 - 38:37
    If you want to see what snapshots are available, if I do lxc
  • 38:37 - 38:41
    list, in the snapshots column you can see there is one snapshot for this
  • 38:41 - 38:51
    container. lxc help snapshot--lxc snapshot and the name--creates snapshots.
  • 38:51 - 38:58
    I'm just seeing if there is a way to find out the name of the snapshots.
  • 38:58 - 39:19
    lxc help restore--snapshot. Okay, less--restore containers from the snapshot. lxc help snapshot--yes.
  • 39:21 - 39:25
    Okay. When you create a snapshot, you can also pass --stateful.
  • 39:25 - 39:29
    That saves the running state of the container as well.
  • 39:29 - 39:33
    I don't think we've got an option to list the snapshots. I don't know.
  • 39:33 - 39:40
    Okay. Let's not worry about it. The command to restore a snapshot is:
  • 39:40 - 39:46
    lxc restore myVM2 snap1.
  • 39:46 - 39:48
    So it's restoring the snapshot that we
  • 39:48 - 39:58
    created earlier. lxc exec myVM2 bash--and we can see all those directories there.
  • 39:58 - 40:00
    So basically, we've restored the
  • 40:00 - 40:05
    machine from the snapshot. Okay, cool.
  • 40:05 - 40:08
    And what else am I going to show you? Nested
  • 40:08 - 40:12
    containers. Let's do a quick demo on nested containers.
  • 40:13 - 40:18
    So, we've got a virtual machine--forget about the virtual machine--
  • 40:18 - 40:24
    so it's a machine, and you have the LXD machine containers.
  • 40:24 - 40:29
    I'm going to log in to one of the machine containers:
  • 40:29 - 40:34
    lxc exec myVM2 bash. So I'm inside the LXD container.
  • 40:34 - 40:38
    Inside this container, I'm going to create another LXD environment.
  • 40:38 - 40:44
    So what we do is see if there are lxc and lxd packages installed
  • 40:44 - 40:54
    which lxd--it's there. systemctl status lxd--it's not started, so let's
  • 40:54 - 41:10
    start it: systemctl start lxd. systemctl status lxd--so that's running.
  • 41:10 - 41:16
    The other thing to note is security.privileged and security.nesting.
  • 41:16 - 41:20
    We haven't set those things up for the myVM2 machine
  • 41:20 - 41:25
    container. So if you want to create a container inside a
  • 41:25 - 41:33
    container, you need to set the security.nesting configuration as well as security.privileged.
  • 41:33 - 41:37
    I'll show you what happens if you don't set those options.
  • 41:37 - 41:42
    Okay. So the LXD is running. It's not a privileged container--it's an
  • 41:42 - 41:48
    unprivileged machine container, myVM2, and it's going to show us some warnings.
  • 41:48 - 41:50
    I'm going to show you what it looks like.
  • 41:50 - 41:55
    lxd init. Do you want to configure a new
  • 41:55 - 42:01
    storage pool? Yes. Name of the storage pool--use default as dir.
  • 42:01 - 42:04
    Okay. And that's the warning: "We detected that you are
  • 42:04 - 42:08
    running inside an unprivileged container. There might be some issues."
  • 42:08 - 42:16
    Okay, Ctrl+C, Ctrl+D. Let's stop the container and set these options.
  • 42:16 - 42:24
    Okay? So, these are not dynamic like limits.memory, limits.cpu, etc.
  • 42:24 - 42:27
    So we need to stop the container, make the
  • 42:27 - 42:30
    configuration change, and then start the container again.
  • 42:30 - 42:38
    lxc list. lxc stop myVM2. Okay.
  • 42:38 - 42:56
    lxc config set myVM2 security.privileged true. lxc config set myVM2 security.nesting true.
  • 42:56 - 43:08
    By the way, you can also do lxc profile edit custom and set these there:
  • 43:08 - 43:21
    security.privileged true. security.nesting true. Save that.
  • 43:21 - 43:26
    And for any--okay, there's some syntax error.
  • 43:26 - 43:33
    Yep, I missed the colon bit. Ctrl+X.
  • 43:33 - 43:37
    Okay. So that's done. And for any new
  • 43:37 - 43:44
    containers you start, if you use this custom profile, it will be a privileged container.
  • 43:44 - 43:52
    Okay. lxc list. Let's start it: lxc start myVM2. See how quick it was?
  • 43:52 - 43:58
    It just took less than a couple of seconds to start the container.
  • 43:58 - 44:06
    lxc exec myVM2 bash. lxd init.
  • 44:06 - 44:10
    Do you want to configure a new storage pool? Yes.
  • 44:10 - 44:14
    Backend: directory. A new network bridge? Yes.
  • 44:14 - 44:20
    Would you like to set up a network bridge? Yes.
  • 44:21 - 44:36
    The name of the interface--everything is going to be default. Okay. Okay. Okay. Okay.
  • 44:36 - 44:42
    So that's all done. And if I do--I'm inside the machine
  • 44:42 - 44:45
    container, and there is another bridge inside that container.
  • 44:45 - 44:51
    And if I do lxc list, you can see that.
  • 44:51 - 44:53
    There you go. So that's my machine container.
  • 44:53 - 45:00
    And apart from the 88, we've also got the network bridge, because
  • 45:00 - 45:03
    basically, this is an LXC host now. It can also
  • 45:03 - 45:08
    host nested containers within it.
  • 45:08 - 45:14
    lxd --version--LXD. So that's the LXD version.
  • 45:14 - 45:21
    On my Ubuntu 18.04 machine, if you noticed, the LXD version was 3.0.3.
  • 45:21 - 45:26
    And on Ubuntu 16.04, the LXC version is 20.11.
  • 45:26 - 45:33
    Okay. lxc remote list--generating a client certificate. This may
  • 45:33 - 45:37
    take a minute or so. Okay. So that's the basic
  • 45:37 - 45:41
    difference between 16.04 and 18.04.
  • 45:41 - 45:45
    And you see all those--remote list.
  • 45:45 - 45:47
    lxc list--you don't have any machines.
  • 45:47 - 46:01
    If you want, you can start: lxc launch images:centos/7 x86_64 or i386, or whatever you want.
  • 46:01 - 46:08
    So basically, that is the nested containers--container within container within container.
  • 46:09 - 46:14
    Okay. I think that's all I wanted to show you.
  • 46:14 - 46:28
    Okay. Yep. lxc list--right. Okay. Right. I think I haven't missed anything.
  • 46:29 - 46:33
    Okay, cool. Thank you so much for your time watching this video.
  • 46:33 - 46:35
    I hope you found it useful.
  • 46:35 - 46:37
    Please share it with your friends.
  • 46:37 - 46:39
    And if you haven't already subscribed to my
  • 46:39 - 46:42
    channel, please, please subscribe to my channel.
  • 46:42 - 46:48
    And if you don't understand anything, or if you're having problems--if
  • 46:48 - 46:50
    you're having some issues following this
  • 46:50 - 46:53
    tutorial--please leave me a comment.
  • 46:53 - 46:54
    I should be able to help you.
  • 46:54 - 47:00
    And if you're trying this out on a different distribution, I can help you out as well.
  • 47:00 - 47:05
    Okay. Let me exit out of the virtual machine.
  • 47:05 - 47:07
    So that's my host machine, which
  • 47:07 - 47:11
    is Manjaro, Arch Linux-based.
  • 47:11 - 47:22
    And if you want to install LXC in your Arch Linux, the command to do is:
    sudo pacman -S lxc--to install the LXC libraries.
  • 47:22 - 47:28
    And to install LXD, I have to install LXD from the community
  • 47:28 - 47:39
    repository--AUR, Arch User Repository. So yay -S--usually it will be yay -S.
  • 47:39 - 47:44
    Previously, I've got a package called yay--
  • 47:44 - 47:51
    yet another AUR helper, I think. Yeah, yay -S lxd. So lxd is available from the
  • 47:51 - 47:56
    community repository. lxc--from the default Pacman repository.
  • 47:57 - 48:00
    Cool. And I'll see you in my next video.
  • 48:00 - 48:03
    Thank you so much for watching this video. Bye bye.
Title:
Getting started with LXC containers
Description:

more » « less
Video Language:
English
Duration:
48:04

English subtitles

Revisions Compare revisions