Skip to content

Instantly share code, notes, and snippets.

@f4rx
Last active August 31, 2018 12:07
Show Gist options
  • Select an option

  • Save f4rx/1e68ef6eece23f154c25ac36a2d29fa9 to your computer and use it in GitHub Desktop.

Select an option

Save f4rx/1e68ef6eece23f154c25ac36a2d29fa9 to your computer and use it in GitHub Desktop.
LXC usage

Установка:

apt install lxc1 lxc1-templates lxc2

Темплейты расположены в /usr/share/lxc/templates/, представляют собой скрипты.

ls -l /usr/share/lxc/templates/

В первой версии подход к образам отличается от последующих. В первой используются темплейты-скрипты, во втором есть сущность образы как в опенстеке.

/usr/share/lxc/templates/lxc-download --help
Required arguments:
[ -d | --dist <distribution> ]: The name of the distribution
[ -r | --release <release> ]: Release name/version
[ -a | --arch <architecture> ]: Architecture of the container

Доступные ОС:

/usr/share/lxc/templates/lxc-download -l

Создать из темплейта, информация о темплейте будет запрашиться в интерактивном режиме

lxc-create --template download -n c1

Или передать аргументами

lxc-create --template download -n c1 -- -d ubuntu -r xenial -a amd64

Просмотр всех ВМ

lxc-ls -f

Запуск контейнера

lxc-start -n c1

Подключиться к контейнеру

lxc-attach -n c1

Выполнить команду внутри контейнера

lxc-attach -n c1 -- hostname -f

Иследуем ФС

ls -l /var/lib/lxc
ls -l /var/lib/lxc/c1

less /var/lib/lxc/c1/config
ls -l /var/lib/lxc/c1/rootfs/

LXD

lxc list

Создать новый контейнер

lxc launch ubuntu:16.04 u1

Подключиться к контейнеру

lxc exec u1 -- bash

Конфигурация сети

less /etc/default/lxd-bridge

Переконфигурировать значения сети через виззард.

dpkg-reconfigure -p medium lxd

Множество хостов Утилита командной строки "lxc" может связываться со множеством серверов LXD. По умолчанию она связана с локальным по локальному UNIX socket.

Удаленные операции требуют запуска двух команд на удаленном сервере:

lxc config set core.https_address "[::]"
lxc config set core.trust_password some-password

Первая указывает LXD слушать все адреса на порту 8443. Вторая устанавливает надежный пароль для связи с сервером.

Теперь для связи с удаленным LXD, вы можете просто добавить его:

lxc remote add host-a <ip address or DNS>

Это попросит вас подтвердить отпечаток удаленного сервера а затем спросит пароль.

И после этого, используйте те же команды, что и раньше, но предваряя из именами контейнера и образа:

lxc exec host-a:first -- apt-get update
root@lxc-host:~# lxc list host-a:
+------+---------+------+------+------------+-----------+
| NAME |  STATE  | IPV4 | IPV6 |    TYPE    | SNAPSHOTS |
+------+---------+------+------+------------+-----------+
| u1   | STOPPED |      |      | PERSISTENT | 0         |
+------+---------+------+------+------------+-----------+

root@lxc-host:~# lxc start host-a:u1

root@lxc-host:~# lxc list host-a:
+------+---------+------+------+------------+-----------+
| NAME |  STATE  | IPV4 | IPV6 |    TYPE    | SNAPSHOTS |
+------+---------+------+------+------------+-----------+
| u1   | RUNNING |      |      | PERSISTENT | 0         |
+------+---------+------+------+------------+-----------+
root@lxc-host:~# lxc remote list
+-----------------+------------------------------------------+---------------+--------+--------+
|      NAME       |                   URL                    |   PROTOCOL    | PUBLIC | STATIC |
+-----------------+------------------------------------------+---------------+--------+--------+
| host-a          | https://192.168.0.5:8443                 | lxd           | NO     | NO     |
+-----------------+------------------------------------------+---------------+--------+--------+
| images          | https://images.linuxcontainers.org       | simplestreams | YES    | NO     |
+-----------------+------------------------------------------+---------------+--------+--------+
| local (default) | unix://                                  | lxd           | NO     | YES    |
+-----------------+------------------------------------------+---------------+--------+--------+
| ubuntu          | https://cloud-images.ubuntu.com/releases | simplestreams | YES    | YES    |
+-----------------+------------------------------------------+---------------+--------+--------+
| ubuntu-daily    | https://cloud-images.ubuntu.com/daily    | simplestreams | YES    | YES    |
+-----------------+------------------------------------------+---------------+--------+--------+
lxc config edit u1

https://linuxcontainers.org/ru/lxd/getting-started-cli/
https://stgraber.org/2016/04/12/lxd-2-0-remote-hosts-and-container-migration-612/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment