Changes between Initial Version and Version 1 of TipAndDoc/Linux/LXC/Ubuntu


Ignore:
Timestamp:
Apr 23, 2016 4:32:31 PM (8 years ago)
Author:
mitty
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • TipAndDoc/Linux/LXC/Ubuntu

    v1 v1  
     1[[PageOutline]] 
     2 
     3 = Ubuntu 12.04 = 
     4 * http://manpages.ubuntu.com/manpages/precise/man5/lxc.conf.5.html 
     5 
     6 * mitty@precise:~$ sudo aptitude install lxc 
     7{{{ 
     8The following NEW packages will be installed: 
     9  bridge-utils{a} cgroup-lite{a} cloud-utils{a} debootstrap{a} 
     10  dnsmasq-base{a} euca2ools{a} libapparmor1{a} libcap2-bin{a} libgmp10{a} 
     11  libnetfilter-conntrack3{a} libpam-cap{a} libyaml-0-2{a} lxc 
     12  python-boto{a} python-crypto{a} python-m2crypto{a} python-paramiko{a} 
     13  python-yaml{a} 
     140 packages upgraded, 18 newly installed, 0 to remove and 0 not upgraded. 
     15Need to get 2,873 kB of archives. After unpacking 16.1 MB will be used. 
     16}}} 
     17 
     18 == lxcbr0 == 
     19 * mitty@precise:~$ ifconfig lxcbr0 
     20{{{ 
     21lxcbr0    Link encap:Ethernet  HWaddr 12:5e:23:12:4a:0f 
     22          inet addr:10.0.3.1  Bcast:10.0.3.255  Mask:255.255.255.0 
     23          inet6 addr: fe80::105e:23ff:fe12:4a0f/64 Scope:Link 
     24          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1 
     25          RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
     26          TX packets:7 errors:0 dropped:0 overruns:0 carrier:0 
     27          collisions:0 txqueuelen:0 
     28          RX bytes:0 (0.0 B)  TX bytes:554 (554.0 B) 
     29}}} 
     30  * /etc/default/lxc 
     31{{{ 
     32 
     33USE_LXC_BRIDGE="true" 
     34 
     35 
     36LXC_BRIDGE="lxcbr0" 
     37LXC_ADDR="10.0.3.1" 
     38LXC_NETMASK="255.255.255.0" 
     39LXC_NETWORK="10.0.3.0/24" 
     40LXC_DHCP_RANGE="10.0.3.2,10.0.3.254" 
     41LXC_DHCP_MAX="253" 
     42}}} 
     43  * /etc/lxc/lxc.conf 
     44{{{ 
     45lxc.network.type=veth 
     46lxc.network.link=lxcbr0 
     47lxc.network.flags=up 
     48}}} 
     49  * /etc/dnsmasq.d/lxc 
     50{{{ 
     51bind-interfaces 
     52except-interface=lxcbr0 
     53}}} 
     54 
     55 * /etc/init/lxc-net.conf も参考になる 
     56 
     57 = USE_LXC_BRIDGE="false" = 
     58 * 手動でLXCコンテナのNAT設定を行う 
     59 * eth0 -> br0にブリッジされていて、KVMで使用 
     60  * LXCとKVMの共存のテスト 
     61 
     62 == ip forwarding == 
     63 * mitty@precise:~$ cat /etc/sysctl.d/60-ip_forward.conf 
     64{{{ 
     65net.ipv4.ip_forward=1 
     66net.ipv6.conf.all.forwarding=1 
     67}}} 
     68 
     69 * mitty@precise:~$ cat /etc/network/interfaces 
     70{{{ 
     71# This file describes the network interfaces available on your system 
     72# and how to activate them. For more information, see interfaces(5). 
     73 
     74# The loopback network interface 
     75auto lo 
     76iface lo inet loopback 
     77 
     78# The primary network interface 
     79auto eth0 
     80iface eth0 inet manual 
     81 
     82auto br0 
     83iface br0 inet dhcp 
     84        bridge_ports eth0 eth0 
     85        bridge_maxwait 0 
     86 
     87auto lxcbr0 
     88iface lxcbr0 inet static 
     89        bridge_ports none 
     90        bridge_maxwait 0 
     91        address 10.0.0.254 
     92        netmask 255.255.255.0 
     93        post-up iptables -A POSTROUTING -s 10.0.0.0/24 -t nat -j MASQUERADE 
     94        pre-down iptables -D POSTROUTING -s 10.0.0.0/24 -t nat -j MASQUERADE 
     95}}} 
     96 
     97 == change apt repository mirror and disable auto start lxcbr0 == 
     98 * デフォルトのミラーが遅いので、ftp.tsukubaに変更 
     99 * lxcの起動スクリプトによるlxcbr0の作成を抑制 
     100 
     101 * mitty@precise:~$ cat /etc/default/lxc 
     102{{{ 
     103MIRROR="http://ftp.tsukuba.wide.ad.jp/Linux/ubuntu" 
     104 
     105USE_LXC_BRIDGE="false" 
     106}}} 
     107 
     108 == host settings == 
     109 
     110 * mitty@precise:~$ ifconfig -a 
     111{{{ 
     112br0       Link encap:Ethernet  HWaddr 52:54:00:bc:53:bc 
     113          inet addr:192.168.10.172  Bcast:192.168.10.255  Mask:255.255.255.0 
     114          inet6 addr: fe80::5054:ff:febc:53bc/64 Scope:Link 
     115          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1 
     116          RX packets:1340 errors:0 dropped:0 overruns:0 frame:0 
     117          TX packets:948 errors:0 dropped:0 overruns:0 carrier:0 
     118          collisions:0 txqueuelen:0 
     119          RX bytes:116870 (116.8 KB)  TX bytes:111171 (111.1 KB) 
     120 
     121eth0      Link encap:Ethernet  HWaddr 52:54:00:bc:53:bc 
     122          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1 
     123          RX packets:1400 errors:0 dropped:0 overruns:0 frame:0 
     124          TX packets:945 errors:0 dropped:0 overruns:0 carrier:0 
     125          collisions:0 txqueuelen:1000 
     126          RX bytes:150975 (150.9 KB)  TX bytes:110725 (110.7 KB) 
     127 
     128lo        Link encap:Local Loopback 
     129          inet addr:127.0.0.1  Mask:255.0.0.0 
     130          inet6 addr: ::1/128 Scope:Host 
     131          UP LOOPBACK RUNNING  MTU:16436  Metric:1 
     132          RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
     133          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 
     134          collisions:0 txqueuelen:0 
     135          RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B) 
     136 
     137lxcbr0    Link encap:Ethernet  HWaddr 4a:5a:12:a4:0a:ac 
     138          inet addr:10.0.0.254  Bcast:10.0.0.255  Mask:255.255.255.0 
     139          inet6 addr: fe80::485a:12ff:fea4:aac/64 Scope:Link 
     140          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1 
     141          RX packets:0 errors:0 dropped:0 overruns:0 frame:0 
     142          TX packets:4 errors:0 dropped:0 overruns:0 carrier:0 
     143          collisions:0 txqueuelen:0 
     144          RX bytes:0 (0.0 B)  TX bytes:408 (408.0 B) 
     145}}} 
     146 
     147 * mitty@precise:~$ ip route 
     148{{{ 
     149default via 192.168.10.254 dev br0  metric 100 
     15010.0.0.0/24 dev lxcbr0  proto kernel  scope link  src 10.0.0.254 
     151192.168.10.0/24 dev br0  proto kernel  scope link  src 192.168.10.172 
     152}}} 
     153 
     154 * mitty@precise:~$ brctl show 
     155{{{ 
     156bridge name     bridge id               STP enabled     interfaces 
     157br0             8000.525400bc53bc       no              eth0 
     158lxcbr0          8000.000000000000       no 
     159}}} 
     160 
     161 === iptables on host === 
     162 * mitty@precise:~$ sudo iptables -L -t nat -vx 
     163{{{ 
     164Chain PREROUTING (policy ACCEPT 30 packets, 10827 bytes) 
     165    pkts      bytes target     prot opt in     out     source               destination 
     166 
     167Chain INPUT (policy ACCEPT 4 packets, 323 bytes) 
     168    pkts      bytes target     prot opt in     out     source               destination 
     169 
     170Chain OUTPUT (policy ACCEPT 83 packets, 5999 bytes) 
     171    pkts      bytes target     prot opt in     out     source               destination 
     172 
     173Chain POSTROUTING (policy ACCEPT 83 packets, 5999 bytes) 
     174    pkts      bytes target     prot opt in     out     source               destination 
     175       0        0 MASQUERADE  all  --  any    any     10.0.0.0/24          anywhere 
     176}}} 
     177 
     178 * MASQUERADEが正しく設定されている 
     179 
     180 == make LXC container with ubuntu template == 
     181 * mitty@precise:~$ sudo lxc-create -t ubuntu -n lxc-test 
     182{{{ 
     183 
     184No config file specified, using the default config 
     185debootstrap is /usr/sbin/debootstrap 
     186Checking cache download in /var/cache/lxc/precise/rootfs-amd64 ... 
     187installing packages: vim,ssh 
     188Downloading ubuntu precise minimal ... 
     189I: Retrieving Release 
     190I: Retrieving Release.gpg 
     191I: Checking Release signature 
     192 
     193.... 
     194 
     195I: Checking component main on http://ftp.tsukuba.wide.ad.jp/Linux/ubuntu... 
     196 
     197.... 
     198 
     199Processing triggers for initramfs-tools ... 
     200Download complete 
     201Copy /var/cache/lxc/precise/rootfs-amd64 to /var/lib/lxc/lxc-test/rootfs ... 
     202Copying rootfs to /var/lib/lxc/lxc-test/rootfs ... 
     203 
     204## 
     205# The default user is 'ubuntu' with password 'ubuntu'! 
     206# Use the 'sudo' command to run tasks as root in the container. 
     207## 
     208 
     209'ubuntu' template installed 
     210'lxc-test' created 
     211}}} 
     212 
     213 === set container IP with LXC/config === 
     214 * LXCコンテナのconfigファイルからIPアドレスを指定する 
     215 * 結論としては、デフォルトゲートウェイなどを設定できないので、不便 
     216 
     217 * mitty@precise:~$ sudo vim /var/lib/lxc/lxc-test/config 
     218{{{ 
     219lxc.network.ipv4 = 10.0.0.10/24 
     220}}} 
     221 
     222 * mitty@precise:~$ sudo lxc-start -n lxc-test -d 
     223 * mitty@precise:~$ ping 10.0.0.10 -c 1 
     224{{{ 
     225PING 10.0.0.10 (10.0.0.10) 56(84) bytes of data. 
     22664 bytes from 10.0.0.10: icmp_req=1 ttl=64 time=0.060 ms 
     227 
     228--- 10.0.0.10 ping statistics --- 
     2291 packets transmitted, 1 received, 0% packet loss, time 0ms 
     230rtt min/avg/max/mdev = 0.060/0.060/0.060/0.000 ms 
     231}}} 
     232 
     233 * mitty@precise:~$ ssh 10.0.0.10 -l ubuntu 
     234  * ubuntu@lxc-test:~$ ifconfig 
     235{{{ 
     236eth0      Link encap:Ethernet  HWaddr 00:16:3e:ba:3e:ef 
     237          inet addr:10.0.0.10  Bcast:10.0.0.255  Mask:255.255.255.0 
     238}}} 
     239  * ubuntu@lxc-test:~$ ip route 
     240{{{ 
     24110.0.0.0/24 dev eth0  proto kernel  scope link  src 10.0.0.10 
     242}}} 
     243  * ubuntu@lxc-test:~$ ping 8.8.8.8 
     244{{{ 
     245connect: Network is unreachable 
     246}}} 
     247 
     248 === set container IP with interfaces === 
     249 * ホストOS上から、あらかじめコンテナのinterfacesファイルを設定して、起動する 
     250 * 間違いが無く、かつ楽そう 
     251 
     252 * mitty@precise:~$ sudo vim /var/lib/lxc/lxc-test/rootfs/etc/network/interfaces 
     253{{{ 
     254auto eth0 
     255iface eth0 inet static 
     256        address 10.0.0.1 
     257        netmask 255.255.255.0 
     258        gateway 10.0.0.254 
     259}}} 
     260 
     261 * mitty@precise:~$ ssh 10.0.0.1 -l ubuntu 
     262  * ubuntu@lxc-test:~$ ifconfig 
     263{{{ 
     264eth0      Link encap:Ethernet  HWaddr 00:16:3e:ba:3e:ef 
     265          inet addr:10.0.0.1  Bcast:10.0.0.255  Mask:255.255.255.0 
     266}}} 
     267  * ubuntu@lxc-test:~$ ip route 
     268{{{ 
     269default via 10.0.0.254 dev eth0  metric 100 
     27010.0.0.0/24 dev eth0  proto kernel  scope link  src 10.0.0.1 
     271}}} 
     272  * ubuntu@lxc-test:~$ ping 8.8.8.8 -c 1 
     273{{{ 
     274PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 
     27564 bytes from 8.8.8.8: icmp_req=1 ttl=52 time=7.66 ms 
     276 
     277--- 8.8.8.8 ping statistics --- 
     2781 packets transmitted, 1 received, 0% packet loss, time 0ms 
     279rtt min/avg/max/mdev = 7.667/7.667/7.667/0.000 ms 
     280}}} 
     281