[Gandi] [Serveur] [IaaS] Installation de LXC

[Gandi] [Serveur] [IaaS] Installation de LXC

Localisation : Bissen, Luxembourg (LU-BI1)
Cœur(s) CPU : 2
RAM : 2048 Mo
Disque : 20 Go
Système d'exploitation : Debian 9
Nom du disque système : sysdisklxc
Nom (Hostname) : serverlxc
Identifiant administrateur : admin
Adresse ipv4 : 46.226.109.238

1/ Connexion au serveur Debian.

➜  ~ ssh admin@46.226.109.238
Linux server01lxc 4.9.0-4-amd64 #1 SMP Debian 4.9.51-1 (2017-09-28) x86_64
...
admin@server01lxc:~$

2/ Passage à l’utilisateur ‘root‘.

admin@server01lxc:~$ su
Password:
root@server01lxc:/home/admin#

3/ Initialisation des locales.

root@server01lxc:/home/admin# export LANGUAGE=fr_FR.UTF-8
root@server01lxc:/home/admin# export LANG=fr_FR.UTF-8
root@server01lxc:/home/admin# export LC_ALL=fr_FR.UTF-8
bash: warning: setlocale: LC_ALL: cannot change locale (fr_FR.UTF-8)

4/ Configuration des locales.

Ouvrir :

/etc/locale.gen

Chercher :

# fr_FR ISO-8859-1
# fr_FR.UTF-8 UTF-8

Remplacer par :

fr_FR ISO-8859-1
fr_FR.UTF-8 UTF-8

Action :

root@server01lxc:/home/admin# locale-gen
Generating locales (this might take a while)...
  fr_FR.ISO-8859-1... done
  fr_FR.UTF-8... done
Generation complete.

5/ Initialisation des locales.

root@server01lxc:/home/admin# export LANGUAGE=fr_FR.UTF-8
root@server01lxc:/home/admin# export LANG=fr_FR.UTF-8
root@server01lxc:/home/admin# export LC_ALL=fr_FR.UTF-8
bash: warning: setlocale: LC_ALL: cannot change locale (fr_FR.UTF-8)

6/ Mise-à-jour du système d’exploitation.

root@server01lxc:/home/admin# apt-get update && apt-get upgrade

7/ Installation des paquets de base.

root@server01lxc:/home/admin# apt-get install vim screen htop xz-utils mc

8/ Installation de LXC.

root@server01lxc:/home/admin# apt install lxc xz-utils bridge-utils dnsmasq-base iptables  debootstrap

9/ Configuration du bridge.

Ouvrir :

/etc/lxc/default.conf

Chercher :

lxc.network.type = empty

Remplacer par :

lxc.network.type = veth
lxc.network.link = lxcbr0
lxc.network.flags = up
lxc.network.hwaddr = 00:16:3e:xx:xx:xx

Ouvrir :

/etc/default/lxc-net

Ajouter :

USE_LXC_BRIDGE="true"

10/ Redémarrage du service lxc-net.

root@server01lxc:/home/admin# systemctl restart lxc-net
root@server01lxc:/home/admin# systemctl status lxc-net
● lxc-net.service - LXC network bridge setup
   Loaded: loaded (/lib/systemd/system/lxc-net.service; enabled; vendor preset: enabled)
   Active: active (exited) since Wed 2018-02-21 19:51:05 CET; 4s ago
  Process: 6928 ExecStop=/usr/lib/x86_64-linux-gnu/lxc/lxc-net stop (code=exited, status=0/SUCCESS)
  Process: 6950 ExecStart=/usr/lib/x86_64-linux-gnu/lxc/lxc-net start (code=exited, status=0/SUCCESS)
 Main PID: 6950 (code=exited, status=0/SUCCESS)
    Tasks: 1 (limit: 4915)
   Memory: 908.0K
      CPU: 74ms
   CGroup: /system.slice/lxc-net.service
           └─6987 dnsmasq -u dnsmasq --strict-order --bind-interfaces --pid-file=/run/lxc/dnsmasq.pid --listen-address 10.0.3.1 --dhcp-range 10.0.3.2,10.0.3.254 --
févr. 21 19:51:05 server01lxc dnsmasq[6987]: started, version 2.76 cachesize 150
févr. 21 19:51:05 server01lxc dnsmasq[6987]: compile time options: IPv6 GNU-getopt DBus i18n IDN DHCP DHCPv6 no-Lua TFTP conntrack ipset auth DNSSEC loop-detect in
févr. 21 19:51:05 server01lxc dnsmasq-dhcp[6987]: DHCP, IP range 10.0.3.2 -- 10.0.3.254, lease time 1h
févr. 21 19:51:05 server01lxc dnsmasq-dhcp[6987]: DHCP, sockets bound exclusively to interface lxcbr0
févr. 21 19:51:05 server01lxc dnsmasq[6987]: reading /etc/resolv.conf
févr. 21 19:51:05 server01lxc dnsmasq[6987]: using nameserver 217.70.186.194#53
févr. 21 19:51:05 server01lxc dnsmasq[6987]: using nameserver 217.70.186.193#53
févr. 21 19:51:05 server01lxc dnsmasq[6987]: using nameserver 2001:4b98:dc2:49::193#53
févr. 21 19:51:05 server01lxc dnsmasq[6987]: read /etc/hosts - 5 addresses
févr. 21 19:51:05 server01lxc systemd[1]: Started LXC network bridge setup.

11/ Vérification.

root@server01lxc:/home/admin# ip -4 -o a show lxcbr0
4: lxcbr0    inet 10.0.3.1/24 scope global lxcbr0\       valid_lft forever preferred_lft forever

12/ Création de machine virtuelle.

root@server01lxc:/home/admin# lxc-create -t debian -n vm01
debootstrap est /usr/sbin/debootstrap
Checking cache download in /var/cache/lxc/debian/rootfs-stable-amd64 ...
Downloading debian minimal ...
I: Retrieving InRelease
I: Retrieving Release
I: Retrieving Release.gpg
I: Checking Release signature
...
I: Unpacking libtinfo5:amd64...
I: Unpacking ncurses-base...
I: Unpacking ncurses-bin..
...
invoke-rc.d: policy-rc.d denied execution of start.
Current default time zone: 'Etc/UTC'
Local time is now:      Wed Feb 21 18:58:08 UTC 2018.
Universal Time is now:  Wed Feb 21 18:58:08 UTC 2018.
root@server01lxc:/home/admin#
root@server01lxc:/home/admin# lxc-create -t debian -n vm02
root@server01lxc:/home/admin# lxc-create -t debian -n vm03

13/ Démarrage des machines virtuelles.

root@server01lxc:/home/admin# lxc-start -n vm01
root@server01lxc:/home/admin# lxc-start -n vm02
root@server01lxc:/home/admin# lxc-start -n vm03

14/ Vérification.

root@server01lxc:/home/admin# lxc-ls --fancy
NAME STATE   AUTOSTART GROUPS IPV4       IPV6
vm01 RUNNING 0         -      10.0.3.94  -
vm02 RUNNING 0         -      10.0.3.101 -
vm03 RUNNING 0         -      10.0.3.100 -

15/ Test.

root@server01lxc:/home/admin# ping -c 1 10.0.3.94
PING 10.0.3.94 (10.0.3.94) 56(84) bytes of data.
64 bytes from 10.0.3.94: icmp_seq=1 ttl=64 time=0.074 ms
--- 10.0.3.94 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.074/0.074/0.074/0.000 ms
root@server01lxc:/home/admin# ping -c 1 10.0.3.101
PING 10.0.3.101 (10.0.3.101) 56(84) bytes of data.
64 bytes from 10.0.3.101: icmp_seq=1 ttl=64 time=0.099 ms
--- 10.0.3.101 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.099/0.099/0.099/0.000 ms
root@server01lxc:/home/admin# ping -c 1 10.0.3.100
PING 10.0.3.100 (10.0.3.100) 56(84) bytes of data.
64 bytes from 10.0.3.100: icmp_seq=1 ttl=64 time=0.110 ms
--- 10.0.3.100 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.110/0.110/0.110/0.000 ms
root@server01lxc:/home/admin#

16/ Attache un conteneur.

root@server01lxc:/home/admin# lxc-attach -n vm01
root@vm01:/#

17/ Vérification.

root@vm01:/# ip -4 -o a
1: lo    inet 127.0.0.1/8 scope host lo\       valid_lft forever preferred_lft forever
5: eth0    inet 10.0.3.94/24 brd 10.0.3.255 scope global eth0\       valid_lft forever preferred_lft forever

18/ Test.

root@vm01:/# apt install iputils-ping
root@vm01:/# ping -c 1 10.0.3.101
PING 10.0.3.101 (10.0.3.101) 56(84) bytes of data.
64 bytes from 10.0.3.101: icmp_seq=1 ttl=64 time=0.185 ms
--- 10.0.3.101 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.185/0.185/0.185/0.000 ms
root@vm01:/# ping -c 1 google.fr
PING google.fr (216.58.208.35) 56(84) bytes of data.
64 bytes from fra15s12-in-f35.1e100.net (216.58.208.35): icmp_seq=1 ttl=57 time=5.14 ms
--- google.fr ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 5.147/5.147/5.147/0.000 ms

19/ Sortie du conteneur.

root@vm01:/# exit
exit
root@server01lxc:/home/admin#

20/ Adresse ip statique.

Ouvrir :

/etc/lxc/dhcp.conf

Ajouter :

dhcp-host=vm01,10.0.3.11
dhcp-host=vm02,10.0.3.12
dhcp-host=vm03,10.0.3.13

Ouvrir :

/etc/default/lxc-net

Ajouter à la fin :

LXC_DHCP_CONFILE=/etc/lxc/dhcp.conf

21/ Redémarrer lxc-net.

root@server01lxc:/home/admin# systemctl restart lxc-net

22/ Redémarrer les conteneurs.

root@server01lxc:/home/admin# lxc-stop -n vm01 && lxc-start -n vm01
root@server01lxc:/home/admin# lxc-stop -n vm02 && lxc-start -n vm02
root@server01lxc:/home/admin# lxc-stop -n vm03 && lxc-start -n vm03

23/ Vérification.

root@server01lxc:/home/admin# lxc-ls --fancy
NAME STATE   AUTOSTART GROUPS IPV4      IPV6
vm01 RUNNING 0         -      10.0.3.11 -
vm02 RUNNING 0         -      10.0.3.12 -
vm03 RUNNING 0         -      10.0.3.13 -

24/ Routage vers un conteneur.

Commande :

iptables -t nat -A PREROUTING -i  -p tcp --dport  -j DNAT --to-destination :

Soit :

root@server01lxc:/home/admin# iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to-destination 10.0.3.11:8080

25/ Installation de Apache dans le conteneur vm01.

root@server01lxc:/home/admin# lxc-attach -n vm01
root@vm01:/# apt get install mc vim
root@vm01:/# apt install apache2
...
Traitement des actions différées (« triggers ») pour sgml-base (1.29) ...
Traitement des actions différées (« triggers ») pour systemd (232-25+deb9u1) ...
root@vm01:/#

Ouvrir :

/etc/apache2/ports.conf

Chercher :

Listen 80

Remplacer par :

Listen 8080

Action :

root@vm01:/# /etc/init.d/apache2 restart
[ ok ] Restarting apache2 (via systemctl): apache2.service.

26/ Test.

http://46.226.109.238/

27/ Lien.

https://angristan.xyz/setup-network-bridge-lxc-net/

Comments are closed, but trackbacks and pingbacks are open.