| 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 | {{{ |
| 8 | The 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} |
| 14 | 0 packages upgraded, 18 newly installed, 0 to remove and 0 not upgraded. |
| 15 | Need 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 | {{{ |
| 21 | lxcbr0 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 | |
| 33 | USE_LXC_BRIDGE="true" |
| 34 | |
| 35 | |
| 36 | LXC_BRIDGE="lxcbr0" |
| 37 | LXC_ADDR="10.0.3.1" |
| 38 | LXC_NETMASK="255.255.255.0" |
| 39 | LXC_NETWORK="10.0.3.0/24" |
| 40 | LXC_DHCP_RANGE="10.0.3.2,10.0.3.254" |
| 41 | LXC_DHCP_MAX="253" |
| 42 | }}} |
| 43 | * /etc/lxc/lxc.conf |
| 44 | {{{ |
| 45 | lxc.network.type=veth |
| 46 | lxc.network.link=lxcbr0 |
| 47 | lxc.network.flags=up |
| 48 | }}} |
| 49 | * /etc/dnsmasq.d/lxc |
| 50 | {{{ |
| 51 | bind-interfaces |
| 52 | except-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 | {{{ |
| 65 | net.ipv4.ip_forward=1 |
| 66 | net.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 |
| 75 | auto lo |
| 76 | iface lo inet loopback |
| 77 | |
| 78 | # The primary network interface |
| 79 | auto eth0 |
| 80 | iface eth0 inet manual |
| 81 | |
| 82 | auto br0 |
| 83 | iface br0 inet dhcp |
| 84 | bridge_ports eth0 eth0 |
| 85 | bridge_maxwait 0 |
| 86 | |
| 87 | auto lxcbr0 |
| 88 | iface 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 | {{{ |
| 103 | MIRROR="http://ftp.tsukuba.wide.ad.jp/Linux/ubuntu" |
| 104 | |
| 105 | USE_LXC_BRIDGE="false" |
| 106 | }}} |
| 107 | |
| 108 | == host settings == |
| 109 | |
| 110 | * mitty@precise:~$ ifconfig -a |
| 111 | {{{ |
| 112 | br0 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 | |
| 121 | eth0 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 | |
| 128 | lo 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 | |
| 137 | lxcbr0 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 | {{{ |
| 149 | default via 192.168.10.254 dev br0 metric 100 |
| 150 | 10.0.0.0/24 dev lxcbr0 proto kernel scope link src 10.0.0.254 |
| 151 | 192.168.10.0/24 dev br0 proto kernel scope link src 192.168.10.172 |
| 152 | }}} |
| 153 | |
| 154 | * mitty@precise:~$ brctl show |
| 155 | {{{ |
| 156 | bridge name bridge id STP enabled interfaces |
| 157 | br0 8000.525400bc53bc no eth0 |
| 158 | lxcbr0 8000.000000000000 no |
| 159 | }}} |
| 160 | |
| 161 | === iptables on host === |
| 162 | * mitty@precise:~$ sudo iptables -L -t nat -vx |
| 163 | {{{ |
| 164 | Chain PREROUTING (policy ACCEPT 30 packets, 10827 bytes) |
| 165 | pkts bytes target prot opt in out source destination |
| 166 | |
| 167 | Chain INPUT (policy ACCEPT 4 packets, 323 bytes) |
| 168 | pkts bytes target prot opt in out source destination |
| 169 | |
| 170 | Chain OUTPUT (policy ACCEPT 83 packets, 5999 bytes) |
| 171 | pkts bytes target prot opt in out source destination |
| 172 | |
| 173 | Chain 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 | |
| 184 | No config file specified, using the default config |
| 185 | debootstrap is /usr/sbin/debootstrap |
| 186 | Checking cache download in /var/cache/lxc/precise/rootfs-amd64 ... |
| 187 | installing packages: vim,ssh |
| 188 | Downloading ubuntu precise minimal ... |
| 189 | I: Retrieving Release |
| 190 | I: Retrieving Release.gpg |
| 191 | I: Checking Release signature |
| 192 | |
| 193 | .... |
| 194 | |
| 195 | I: Checking component main on http://ftp.tsukuba.wide.ad.jp/Linux/ubuntu... |
| 196 | |
| 197 | .... |
| 198 | |
| 199 | Processing triggers for initramfs-tools ... |
| 200 | Download complete |
| 201 | Copy /var/cache/lxc/precise/rootfs-amd64 to /var/lib/lxc/lxc-test/rootfs ... |
| 202 | Copying 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 | {{{ |
| 219 | lxc.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 | {{{ |
| 225 | PING 10.0.0.10 (10.0.0.10) 56(84) bytes of data. |
| 226 | 64 bytes from 10.0.0.10: icmp_req=1 ttl=64 time=0.060 ms |
| 227 | |
| 228 | --- 10.0.0.10 ping statistics --- |
| 229 | 1 packets transmitted, 1 received, 0% packet loss, time 0ms |
| 230 | rtt 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 | {{{ |
| 236 | eth0 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 | {{{ |
| 241 | 10.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 | {{{ |
| 245 | connect: 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 | {{{ |
| 254 | auto eth0 |
| 255 | iface 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 | {{{ |
| 264 | eth0 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 | {{{ |
| 269 | default via 10.0.0.254 dev eth0 metric 100 |
| 270 | 10.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 | {{{ |
| 274 | PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. |
| 275 | 64 bytes from 8.8.8.8: icmp_req=1 ttl=52 time=7.66 ms |
| 276 | |
| 277 | --- 8.8.8.8 ping statistics --- |
| 278 | 1 packets transmitted, 1 received, 0% packet loss, time 0ms |
| 279 | rtt min/avg/max/mdev = 7.667/7.667/7.667/0.000 ms |
| 280 | }}} |
| 281 | |