Linux Containers (LXC)

382 views 50 slides Oct 15, 2018
Slide 1
Slide 1 of 50
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50

About This Presentation

Linux Containers (LXC) @Open Source Camp Moldova 2018

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


Slide Content

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