Linux Containers
LXC
Open Source Camp Moldova 2018
$ whoami
Vladimir Melnic
Developer
DevOps
https://github.com/vmelnic
https://fb.com/melnic.vladimir
2
$ man lxc
LXC (Linux Containers) is an operating-system-level virtualization method for
running multiple isolated Linux systems (containers) on a control host using a
single Linux kernel. https://en.wikipedia.org/wiki/LXC
LGPL-2.1
C 88.9% Shell 7.4% M4 2.5% Other 1.2%
Initial release: August 6, 2008
Stable release: 3.0.0, 28 May 2018
3
$ man lxd
LXD is a next generation system container manager. It offers an user experience
similar to virtual machines but using Linux containers instead.
https://linuxcontainers.org/lxd/introduction/
Apache-2.0
Go 91.0%, Shell 7.9%, Other 1.1%
Stable release: 3.0.0, 3 Apr 2018
4
Features
● cgroups - CPU, memory, block I/O, network, etc.
● namespaces - Process IDs, hostnames, user IDs, file names etc.
● unprivileged containers
● snapshot
● migrations
● LXD - Rest API
● LXCFS - Userspace (FUSE) filesystem
https://linuxcontainers.org/lxcfs/introduction/
5
Install
# Ubuntu:
$ sudo apt install lxd lxd-client
# Centos/RHEL 7:
$ sudo yum install yum-plugin-copr epel-release
$ sudo yum copr enable ngompa/snapcore-el7
$ sudo yum install snapd
$ sudo snap install lxd
https://discuss.linuxcontainers.org/t/lxd-on-centos-7/1250
6
Configure
$ sudo lxd init
> Do you want to configure a new storage pool (yes/no) [default=yes]? yes
> Name of the storage backend to use (dir or zfs) [default=dir]: 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]: all
> Port to bind LXD to [default=8443]: 8443
> Trust password for new clients: *
> Again: *
> Do you want to configure the LXD bridge (yes/no) [default=yes]? yes
7
Configuring - network 1/11
8
Configuring - network 2/11
9
Configuring - network 3/11
10
Configuring - network 4/11
11
Configuring - network 5/11
12
Configuring - network 6/11
13
Configuring - network 7/11
14
Configuring - network 8/11
15
Configuring - network 9/11
16
Configuring - network 10/11
17
Configuring - network 11/11
18
$ sudo lxd init
> Do you want to configure a new storage pool (yes/no) [default=yes]?yes
> Name of the storage backend to use (dir or zfs) [default=dir]: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]:all
> Port to bind LXD to [default=8443]: 8443
> Trust password for new clients: *
> Again: *
> Do you want to configure the LXD bridge (yes/no) [default=yes]?yes
> LXD has been successfully configured.
$
19
Creating and starting
# Creating a container without starting it:
$ sudo lxc init ubuntu:16.04 <container>
# Creating and starting a new container:
$ sudo lxc launch ubuntu:16.04 <container>
https://linuxcontainers.org/lxd/getting-started-cli/
20
21
List containers
$ sudo lxc list
22
23
Start, stop and restart containers
# Start container:
$ sudo lxc start <container>
# Stop container:
$ sudo lxc stop <container>
# Restart container:
$ sudo lxc restart <container>
24
25
Delete containers
$ sudo lxc delete <container>
26
27
Exec
# Get a shell inside container:
$ sudo lxc exec <container> -- /bin/bash
# Run command inside container:
$ sudo lxc exec <container> -- apt-get upgrade -y
28
29
30
Images 1/2
# Built-in image remotes:
● ubuntu: (for stable Ubuntu images)
● ubuntu-daily: (for daily Ubuntu images)
● images: (for a bunch of other distros)
# Start a container from image:
$ sudo lxc launch ubuntu:14.04 my-ubuntu
$ sudo lxc launch ubuntu-daily:16.04 my-ubuntu-dev
$ sudo lxc launch images:centos/6/amd64 my-centos
31
Images 2/2
# Using a remote LXD as an image server:
$ sudo lxc remote add my-images 1.2.3.4
$ sudo lxc launch my-images:image-name your-container
32
List images
# Local images:
$ sudo lxc image list
# Built-in image remotes:
$ sudo lxc image list ubuntu:
$ sudo lxc image list ubuntu-daily:
$ sudo lxc image list images:
https://stgraber.org/2016/03/30/lxd-2-0-image-management-512
33
34
35
Container info
# Getting detailed information from a container:
$ sudo lxc info <container>
36
37
Snapshot management
# Creating a snapshot:
$ sudo lxc snapshot <container> <snapshot>
# Listing snapshots:
$ sudo lxc info <container>
# Restoring a snapshot:
$ sudo lxc restore <container> <snapshot>
# Creating a new container from a snapshot:
$ sudo lxc copy <source container>/<snapshot> <destination container>
# Deleting a snapshot:
$ sudo lxc delete <container>/<snapshot>
38
39
40
Cloning and renaming
# Copying a container:
$ sudo lxc copy <source container> <destination container>
# Moving a container:
$ sudo lxc move <old name> <new name>
41
42
43
Resource control
● Disk (only for ZFS or btrfs storage backend)
● CPU
● Memory
● Network I/O (only supported for “bridged” and “p2p” type interfaces)
● Block I/O
https://stgraber.org/2016/03/26/lxd-2-0-resource-control-412
44
Applying some limits
# Container-wide limits:
$ sudo lxc config set <container> <key> <value>
# Profile:
$ sudo lxc profile set <profile> <key> <value>
# Device-specific:
$ sudo lxc config device set <container> <device> <key> <value>
45
CPU
# Limit a container to 1 CPU:
$ sudo lxc config set c1 limits.cpu 1
46
47
Memory
# Limit a container memory (RAM) to 256 MB:
$ sudo lxc config set c1 limits.memory 256MB
48
49
Questions?
50

Linux Containers (LXC)

  • 1.
  • 2.
  • 3.
    $ man lxc LXC(Linux Containers) is an operating-system-level virtualization method for running multiple isolated Linux systems (containers) on a control host using a single Linux kernel. https://en.wikipedia.org/wiki/LXC LGPL-2.1 C 88.9% Shell 7.4% M4 2.5% Other 1.2% Initial release: August 6, 2008 Stable release: 3.0.0, 28 May 2018 3
  • 4.
    $ man lxd LXDis a next generation system container manager. It offers an user experience similar to virtual machines but using Linux containers instead. https://linuxcontainers.org/lxd/introduction/ Apache-2.0 Go 91.0%, Shell 7.9%, Other 1.1% Stable release: 3.0.0, 3 Apr 2018 4
  • 5.
    Features ● cgroups -CPU, memory, block I/O, network, etc. ● namespaces - Process IDs, hostnames, user IDs, file names etc. ● unprivileged containers ● snapshot ● migrations ● LXD - Rest API ● LXCFS - Userspace (FUSE) filesystem https://linuxcontainers.org/lxcfs/introduction/ 5
  • 6.
    Install # Ubuntu: $ sudoapt install lxd lxd-client # Centos/RHEL 7: $ sudo yum install yum-plugin-copr epel-release $ sudo yum copr enable ngompa/snapcore-el7 $ sudo yum install snapd $ sudo snap install lxd https://discuss.linuxcontainers.org/t/lxd-on-centos-7/1250 6
  • 7.
    Configure $ sudo lxdinit > Do you want to configure a new storage pool (yes/no) [default=yes]? yes > Name of the storage backend to use (dir or zfs) [default=dir]: 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]: all > Port to bind LXD to [default=8443]: 8443 > Trust password for new clients: * > Again: * > Do you want to configure the LXD bridge (yes/no) [default=yes]? yes 7
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
    $ sudo lxdinit > Do you want to configure a new storage pool (yes/no) [default=yes]?yes > Name of the storage backend to use (dir or zfs) [default=dir]: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]:all > Port to bind LXD to [default=8443]: 8443 > Trust password for new clients: * > Again: * > Do you want to configure the LXD bridge (yes/no) [default=yes]?yes > LXD has been successfully configured. $ 19
  • 20.
    Creating and starting #Creating a container without starting it: $ sudo lxc init ubuntu:16.04 <container> # Creating and starting a new container: $ sudo lxc launch ubuntu:16.04 <container> https://linuxcontainers.org/lxd/getting-started-cli/ 20
  • 21.
  • 22.
  • 23.
  • 24.
    Start, stop andrestart containers # Start container: $ sudo lxc start <container> # Stop container: $ sudo lxc stop <container> # Restart container: $ sudo lxc restart <container> 24
  • 25.
  • 26.
    Delete containers $ sudolxc delete <container> 26
  • 27.
  • 28.
    Exec # Get ashell inside container: $ sudo lxc exec <container> -- /bin/bash # Run command inside container: $ sudo lxc exec <container> -- apt-get upgrade -y 28
  • 29.
  • 30.
  • 31.
    Images 1/2 # Built-inimage remotes: ● ubuntu: (for stable Ubuntu images) ● ubuntu-daily: (for daily Ubuntu images) ● images: (for a bunch of other distros) # Start a container from image: $ sudo lxc launch ubuntu:14.04 my-ubuntu $ sudo lxc launch ubuntu-daily:16.04 my-ubuntu-dev $ sudo lxc launch images:centos/6/amd64 my-centos 31
  • 32.
    Images 2/2 # Usinga remote LXD as an image server: $ sudo lxc remote add my-images 1.2.3.4 $ sudo lxc launch my-images:image-name your-container 32
  • 33.
    List images # Localimages: $ sudo lxc image list # Built-in image remotes: $ sudo lxc image list ubuntu: $ sudo lxc image list ubuntu-daily: $ sudo lxc image list images: https://stgraber.org/2016/03/30/lxd-2-0-image-management-512 33
  • 34.
  • 35.
  • 36.
    Container info # Gettingdetailed information from a container: $ sudo lxc info <container> 36
  • 37.
  • 38.
    Snapshot management # Creatinga snapshot: $ sudo lxc snapshot <container> <snapshot> # Listing snapshots: $ sudo lxc info <container> # Restoring a snapshot: $ sudo lxc restore <container> <snapshot> # Creating a new container from a snapshot: $ sudo lxc copy <source container>/<snapshot> <destination container> # Deleting a snapshot: $ sudo lxc delete <container>/<snapshot> 38
  • 39.
  • 40.
  • 41.
    Cloning and renaming #Copying a container: $ sudo lxc copy <source container> <destination container> # Moving a container: $ sudo lxc move <old name> <new name> 41
  • 42.
  • 43.
  • 44.
    Resource control ● Disk(only for ZFS or btrfs storage backend) ● CPU ● Memory ● Network I/O (only supported for “bridged” and “p2p” type interfaces) ● Block I/O https://stgraber.org/2016/03/26/lxd-2-0-resource-control-412 44
  • 45.
    Applying some limits #Container-wide limits: $ sudo lxc config set <container> <key> <value> # Profile: $ sudo lxc profile set <profile> <key> <value> # Device-specific: $ sudo lxc config device set <container> <device> <key> <value> 45
  • 46.
    CPU # Limit acontainer to 1 CPU: $ sudo lxc config set c1 limits.cpu 1 46
  • 47.
  • 48.
    Memory # Limit acontainer memory (RAM) to 256 MB: $ sudo lxc config set c1 limits.memory 256MB 48
  • 49.
  • 50.