LXD Grundbefehle

Aus Thomas-Krenn-Wiki
Zur Navigation springen Zur Suche springen

LXD ist ein neuer Hypervisor zur Verwaltung von LXC Containern. Basierend auf den früheren LXC-Tools fügt LXD eine Abstraktionsschicht ein, mit der über mehrere LXD-Hosts hinweg Container verwaltet werden können.[1] Der folgende Wiki-Artikel stellt grundlegende Befehle vor, mit dem Container verwaltet werden. Als Distribution wird bei den Bespielen Ubuntu 16.04 verwendet, LXD kommt dabei direkt aus den Ubuntu Repos.

Images

  • Remotes anzeigen
$ lxc remote list
+-----------------+------------------------------------------+---------------+--------+--------+
|      NAME       |                   URL                    |   PROTOCOL    | PUBLIC | STATIC |
+-----------------+------------------------------------------+---------------+--------+--------+
| images          | https://images.linuxcontainers.org       | lxd           | YES    | NO     |
+-----------------+------------------------------------------+---------------+--------+--------+
| local (default) | unix://                                  | lxd           | NO     | YES    |
+-----------------+------------------------------------------+---------------+--------+--------+
| lxd2            | https://192.168.56.103:8090              | lxd           | NO     | NO     |
+-----------------+------------------------------------------+---------------+--------+--------+
| ubuntu          | https://cloud-images.ubuntu.com/releases | simplestreams | YES    | YES    |
+-----------------+------------------------------------------+---------------+--------+--------+
| ubuntu-daily    | https://cloud-images.ubuntu.com/daily    | simplestreams | YES    | YES    |
+-----------------+------------------------------------------+---------------+--------+--------+
  • Remote Images anzeigen
$ lxc image list ubuntu:
  • Image von Remote in lokalen Speicher kopieren
$ lxc image copy ubuntu:14.04 local: --alias trusty
Image copied successfully!

Remotes

  • Remote LXD-Server zur lokalen Verwaltung hinzufügen:
$ lxc remote add lxd2 192.168.56.103:8090
Certificate fingerprint: b253acc45147d6eb9ad0e862c79b7e8e9d9efb7ce1b81d68cb7a9661bc3950c1
ok (y/n)? y
Admin password for lxd2: 
Client certificate stored at server:  lxd2

Container

  • Container initialisieren (ohne ihn zu starten)
$ lxc init trusty container1
Creating container1
  • Container aus Image starten
$ time lxc launch trusty container1
Creating container1
Starting container1

real	0m6.637s
  • Shell in einem Container ausführen
$ lxc exec container1 -- /bin/bash
  • Container anzeigen
$ lxc list
+------------+---------+-------------------+------+------------+-----------+
|    NAME    |  STATE  |       IPV4        | IPV6 |    TYPE    | SNAPSHOTS |
+------------+---------+-------------------+------+------------+-----------+
| container0 | RUNNING | 10.0.3.227 (eth0) |      | PERSISTENT | 0         |
+------------+---------+-------------------+------+------------+-----------+
| container1 | RUNNING | 10.0.3.10 (eth0)  |      | PERSISTENT | 0         |
+------------+---------+-------------------+------+------------+-----------+
  • Container auf Remote LXD-Server anzeigen
$ lxc list lxd2:
+--------+---------+------+------+------------+-----------+
|  NAME  |  STATE  | IPV4 | IPV6 |    TYPE    | SNAPSHOTS |
+--------+---------+------+------+------------+-----------+
| trusty | STOPPED |      |      | PERSISTENT | 1         |
+--------+---------+------+------+------------+-----------+
  • Container Ressourcen beschränken
$ lxc config set container1 limits.memory 64MB
$ lxc exec container1 -- /bin/bash
root@container1:~# free -m
             total       used       free     shared    buffers     cached
Mem:            64         18         45          1          0         10
-/+ buffers/cache:          7         56
Swap:          508          1        507
  • Container Snapshot erstellen
$ lxc snapshot trusty 
$ lxc list 
+--------+---------+-------------------+------+------------+-----------+ 
|  NAME  |  STATE  |       IPV4        | IPV6 |    TYPE    | SNAPSHOTS | 
+--------+---------+-------------------+------+------------+-----------+ 
| trusty | RUNNING | 10.0.3.170 (eth0) |      | PERSISTENT | 1         | 
+--------+---------+-------------------+------+------------+-----------+
  • Snapshot wiederherstellen
$ lxc restore trusty snap0

Profile

  • Verfügbare Profile anzeigen
$ lxc profile list
default
docker
  • Details zu einem Profil anzeigen
$ lxc profile show default 
name: default 
config: {} 
description: "" 
devices: 
  eth0: 
    name: eth0 
    nictype: bridged 
    parent: lxcbr0 
    type: nic 
  • Profil Container zuweisen
$ lxc profile apply container0 default 
Profile default applied to container0 

REST API

  • Python Script, zum Auflisten von Containern
#!/usr/bin/env python
import os.path
import requests

conf_dir = os.path.expanduser('~/.config/lxc')
crt = os.path.join(conf_dir, 'client.crt')
key = os.path.join(conf_dir, 'client.key')

print(requests.get('https://192.168.56.105:8090/1.0/containers', verify=False, cert=(crt, key)).text)
  • Script aufrufen
$ lxc config trust add client.crt
$ python -W ignore lxd.py | jq
{
  "type": "sync",
  "status": "Success",
  "status_code": 200,
  "metadata": [
    "/1.0/containers/container0",
    "/1.0/containers/container1"
  ]
}

Statische IP-Adressen

$ sudo apt-get install dnsmasq 
$ sudo vi /etc/default/lxc-net
LXC_DHCP_CONFILE=/etc/lxc/dnsmasq.conf
LXC_DOMAIN="lxc"
$ echo 'server=/lxc/10.0.3.1' | sudo tee -a /etc/dnsmasq.d/lxc 
server=/lxc/10.0.3.1 
$ sudo vi /etc/lxc/dnsmasq.conf
dhcp-host=container0,10.0.3.100 
dhcp-host=container1,10.0.3.101 
$ sudo service lxc-net restart 
$ sudo service dnsmasq restart 
$ sudo killall -s SIGHUP dnsmasq 

Einzelnachweise

  1. LXD Getting started (linuxcontainers.org)


Foto Georg Schönberger.jpg

Autor: Georg Schönberger

Georg Schönberger, Abteilung DevOps bei der XORTEX eBusiness GmbH, absolvierte an der FH OÖ am Campus Hagenberg sein Studium zum Bachelor Computer- und Mediensicherheit, Studium Master Sichere Informationssysteme. Seit 2015 ist Georg bei XORTEX beschäftigt und arbeitet sehr lösungsorientiert und hat keine Angst vor schwierigen Aufgaben. Zu seinen Hobbys zählt neben Linux auch Tennis, Klettern und Reisen.


Das könnte Sie auch interessieren

Cgroup Werte von LXC Linux Containern
Linux Containers LXC