Installazione di una Virtual Machine LXC in Debian Jessie

From RVM Wiki
Jump to navigation Jump to search

Lo scopo è installare in Jessi una vm LXC con debian Buster, per avviare un webserver con PHP 7.3 da debian buster

Networking

Si utilizza un bridge su una dummy network card per creare una rete interna, da cui si uscirà in NAT e si entrerà con dei portmap

apt-get install bridge-utils
vi /etc/network/interfaces
auto eth0
iface eth0 inet static
    address 10.11.12.109
    #....

auto br0
iface br0 inet static
    address 192.168.33.254
    netmask 255.255.255.0
    broadcast 192.168.33.255
    bridge_ports dummy0
    bridge_fd 2.0
    bridge_maxwait 1
ifup br0
ifconfig br0
br0       Link encap:Ethernet  HWaddr ea:a2:46:ce:7c:0f  
          inet addr:192.168.33.254  Bcast:192.168.33.255  Mask:255.255.255.0
  • Attivare l'ip forwarding:
echo "net.ipv4.ip_forward=1" >  /etc/sysctl.d/10-net.ipv4.ip_forward.conf
sysctl -p --system
  • Impostare le regole per il NAT in uscita:
apt-get install iptables-persistent
iptables -P FORWARD  ACCEPT
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
  • Impostare il portmap della porta 80 verso la futura VM che avrà ip .100:
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT  --to-destination 192.168.33.100:80
  • Abilitare il restore delle regole:
iptables-save > /etc/iptables/rules.v4 
ip6tables-save > /etc/iptables/rules.v6

LXC

  • Installare LXC
apt-get install xz-utils lxc
  • Creare il primo container, selezionando debian buster amd64:
lxc-create -n buster64 -t download
---

Distribution: 
debian
Release: 
buster
Architecture: 
amd64
  • Modificare la configurazione per attivare il networking aggiungendo:
vi /var/lib/lxc/buster64/config
lxc.kmsg = 0
lxc.tty = 6
lxc.start.auto = 1

lxc.network.type = veth
lxc.network.name = veth0
lxc.network.flags = up
lxc.network.link = br0


  • Se si installa su Debian Buster:
# Network configuration
#lxc.net.0.type = empty

lxc.start.auto = 1

lxc.net.0.type = veth
lxc.net.0.name = veth0
lxc.net.0.flags = up
lxc.net.0.link = br0
  • Settare la rete nella VM:
cd /var/lib/lxc/buster64/rootfs/
chroot .
  • DNS
vi /etc/resolv.conf
nameserver 8.8.8.8
  • IP
 vi /etc/network/interfaces
auto veth0
iface veth0 inet static
        address 192.168.33.100
        netmask 255.255.255.0
        gateway 192.168.33.254
        dns-nameservers 8.8.8.8
  • Installare i pacchetti minimali
apt-get update
apt-get install inetutils-ping vim bash-completion ssh less bzip2 rsync screen sudo
exit
  • Aggiungere la propria chiave pubblica alla vm
mkdir root/.ssh
chmod 0700 root/.ssh
cp /root/.ssh/authorized_keys root/.ssh/

Gestione container

  • Avviarlo in foreground
lxc-start -n buster64 
  • Impostare la password per fare login (il container non ha password e quindi nbon ci si può loggare in foregorund)
lxc-attach -n buster64 passwd
  • Provare a loggarsi dal foreground e terminare l'installazione di ssh
apt-get install -f
  • Provare ad entrare in ssh
ssh root@192.168.33.100
exit
  • Fermare il container:
lxc-stop -n buster64
  • Avviarlo in background:
lxc-start -n buster64 -d
  • Provare ad attaccare la console (attendere e premere invio più volte, per uscire se si è in screen ctrl-a a q)
lxc-console -n buster64 -t0
  • Provare a fare login da attach (non va per root)
lxc-attach -n buster64 -- login
  • Riavviare e vedere se il container è attivo
lxc-ls
  • Verficare se ci sono le rgole di nat
iptables -L -n -t nat
  • Entrare ed installare apache
ssh 192.168.33.100
apt-get install apache2
  • Cambiare index.html:
echo "this is lxc" > /var/www/html/index.htm

  • Verifcare se chiamndo sull'ip pubblico di eth0 della macchina ospitante, risponde l'apache lxc
curl http://10.11.12.109

this is lxc


Riferimenti