129 | | |
130 | | = Ubuntu 12.04 = |
131 | | * http://manpages.ubuntu.com/manpages/precise/man5/lxc.conf.5.html |
132 | | |
133 | | * mitty@precise:~$ sudo aptitude install lxc |
134 | | {{{ |
135 | | The following NEW packages will be installed: |
136 | | bridge-utils{a} cgroup-lite{a} cloud-utils{a} debootstrap{a} |
137 | | dnsmasq-base{a} euca2ools{a} libapparmor1{a} libcap2-bin{a} libgmp10{a} |
138 | | libnetfilter-conntrack3{a} libpam-cap{a} libyaml-0-2{a} lxc |
139 | | python-boto{a} python-crypto{a} python-m2crypto{a} python-paramiko{a} |
140 | | python-yaml{a} |
141 | | 0 packages upgraded, 18 newly installed, 0 to remove and 0 not upgraded. |
142 | | Need to get 2,873 kB of archives. After unpacking 16.1 MB will be used. |
143 | | }}} |
144 | | |
145 | | == lxcbr0 == |
146 | | * mitty@precise:~$ ifconfig lxcbr0 |
147 | | {{{ |
148 | | lxcbr0 Link encap:Ethernet HWaddr 12:5e:23:12:4a:0f |
149 | | inet addr:10.0.3.1 Bcast:10.0.3.255 Mask:255.255.255.0 |
150 | | inet6 addr: fe80::105e:23ff:fe12:4a0f/64 Scope:Link |
151 | | UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 |
152 | | RX packets:0 errors:0 dropped:0 overruns:0 frame:0 |
153 | | TX packets:7 errors:0 dropped:0 overruns:0 carrier:0 |
154 | | collisions:0 txqueuelen:0 |
155 | | RX bytes:0 (0.0 B) TX bytes:554 (554.0 B) |
156 | | }}} |
157 | | * /etc/default/lxc |
158 | | {{{ |
159 | | |
160 | | USE_LXC_BRIDGE="true" |
161 | | |
162 | | |
163 | | LXC_BRIDGE="lxcbr0" |
164 | | LXC_ADDR="10.0.3.1" |
165 | | LXC_NETMASK="255.255.255.0" |
166 | | LXC_NETWORK="10.0.3.0/24" |
167 | | LXC_DHCP_RANGE="10.0.3.2,10.0.3.254" |
168 | | LXC_DHCP_MAX="253" |
169 | | }}} |
170 | | * /etc/lxc/lxc.conf |
171 | | {{{ |
172 | | lxc.network.type=veth |
173 | | lxc.network.link=lxcbr0 |
174 | | lxc.network.flags=up |
175 | | }}} |
176 | | * /etc/dnsmasq.d/lxc |
177 | | {{{ |
178 | | bind-interfaces |
179 | | except-interface=lxcbr0 |
180 | | }}} |
181 | | |
182 | | * /etc/init/lxc-net.conf も参考になる |
183 | | |
184 | | = USE_LXC_BRIDGE="false" = |
185 | | * 手動でLXCコンテナのNAT設定を行う |
186 | | * eth0 -> br0にブリッジされていて、KVMで使用 |
187 | | * LXCとKVMの共存のテスト |
188 | | |
189 | | == ip forwarding == |
190 | | * mitty@precise:~$ cat /etc/sysctl.d/60-ip_forward.conf |
191 | | {{{ |
192 | | net.ipv4.ip_forward=1 |
193 | | net.ipv6.conf.all.forwarding=1 |
194 | | }}} |
195 | | |
196 | | * mitty@precise:~$ cat /etc/network/interfaces |
197 | | {{{ |
198 | | # This file describes the network interfaces available on your system |
199 | | # and how to activate them. For more information, see interfaces(5). |
200 | | |
201 | | # The loopback network interface |
202 | | auto lo |
203 | | iface lo inet loopback |
204 | | |
205 | | # The primary network interface |
206 | | auto eth0 |
207 | | iface eth0 inet manual |
208 | | |
209 | | auto br0 |
210 | | iface br0 inet dhcp |
211 | | bridge_ports eth0 eth0 |
212 | | bridge_maxwait 0 |
213 | | |
214 | | auto lxcbr0 |
215 | | iface lxcbr0 inet static |
216 | | bridge_ports none |
217 | | bridge_maxwait 0 |
218 | | address 10.0.0.254 |
219 | | netmask 255.255.255.0 |
220 | | post-up iptables -A POSTROUTING -s 10.0.0.0/24 -t nat -j MASQUERADE |
221 | | pre-down iptables -D POSTROUTING -s 10.0.0.0/24 -t nat -j MASQUERADE |
222 | | }}} |
223 | | |
224 | | == change apt repository mirror and disable auto start lxcbr0 == |
225 | | * デフォルトのミラーが遅いので、ftp.tsukubaに変更 |
226 | | * lxcの起動スクリプトによるlxcbr0の作成を抑制 |
227 | | |
228 | | * mitty@precise:~$ cat /etc/default/lxc |
229 | | {{{ |
230 | | MIRROR="http://ftp.tsukuba.wide.ad.jp/Linux/ubuntu" |
231 | | |
232 | | USE_LXC_BRIDGE="false" |
233 | | }}} |
234 | | |
235 | | == host settings == |
236 | | |
237 | | * mitty@precise:~$ ifconfig -a |
238 | | {{{ |
239 | | br0 Link encap:Ethernet HWaddr 52:54:00:bc:53:bc |
240 | | inet addr:192.168.10.172 Bcast:192.168.10.255 Mask:255.255.255.0 |
241 | | inet6 addr: fe80::5054:ff:febc:53bc/64 Scope:Link |
242 | | UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 |
243 | | RX packets:1340 errors:0 dropped:0 overruns:0 frame:0 |
244 | | TX packets:948 errors:0 dropped:0 overruns:0 carrier:0 |
245 | | collisions:0 txqueuelen:0 |
246 | | RX bytes:116870 (116.8 KB) TX bytes:111171 (111.1 KB) |
247 | | |
248 | | eth0 Link encap:Ethernet HWaddr 52:54:00:bc:53:bc |
249 | | UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 |
250 | | RX packets:1400 errors:0 dropped:0 overruns:0 frame:0 |
251 | | TX packets:945 errors:0 dropped:0 overruns:0 carrier:0 |
252 | | collisions:0 txqueuelen:1000 |
253 | | RX bytes:150975 (150.9 KB) TX bytes:110725 (110.7 KB) |
254 | | |
255 | | lo Link encap:Local Loopback |
256 | | inet addr:127.0.0.1 Mask:255.0.0.0 |
257 | | inet6 addr: ::1/128 Scope:Host |
258 | | UP LOOPBACK RUNNING MTU:16436 Metric:1 |
259 | | RX packets:0 errors:0 dropped:0 overruns:0 frame:0 |
260 | | TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 |
261 | | collisions:0 txqueuelen:0 |
262 | | RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) |
263 | | |
264 | | lxcbr0 Link encap:Ethernet HWaddr 4a:5a:12:a4:0a:ac |
265 | | inet addr:10.0.0.254 Bcast:10.0.0.255 Mask:255.255.255.0 |
266 | | inet6 addr: fe80::485a:12ff:fea4:aac/64 Scope:Link |
267 | | UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 |
268 | | RX packets:0 errors:0 dropped:0 overruns:0 frame:0 |
269 | | TX packets:4 errors:0 dropped:0 overruns:0 carrier:0 |
270 | | collisions:0 txqueuelen:0 |
271 | | RX bytes:0 (0.0 B) TX bytes:408 (408.0 B) |
272 | | }}} |
273 | | |
274 | | * mitty@precise:~$ ip route |
275 | | {{{ |
276 | | default via 192.168.10.254 dev br0 metric 100 |
277 | | 10.0.0.0/24 dev lxcbr0 proto kernel scope link src 10.0.0.254 |
278 | | 192.168.10.0/24 dev br0 proto kernel scope link src 192.168.10.172 |
279 | | }}} |
280 | | |
281 | | * mitty@precise:~$ brctl show |
282 | | {{{ |
283 | | bridge name bridge id STP enabled interfaces |
284 | | br0 8000.525400bc53bc no eth0 |
285 | | lxcbr0 8000.000000000000 no |
286 | | }}} |
287 | | |
288 | | === iptables on host === |
289 | | * mitty@precise:~$ sudo iptables -L -t nat -vx |
290 | | {{{ |
291 | | Chain PREROUTING (policy ACCEPT 30 packets, 10827 bytes) |
292 | | pkts bytes target prot opt in out source destination |
293 | | |
294 | | Chain INPUT (policy ACCEPT 4 packets, 323 bytes) |
295 | | pkts bytes target prot opt in out source destination |
296 | | |
297 | | Chain OUTPUT (policy ACCEPT 83 packets, 5999 bytes) |
298 | | pkts bytes target prot opt in out source destination |
299 | | |
300 | | Chain POSTROUTING (policy ACCEPT 83 packets, 5999 bytes) |
301 | | pkts bytes target prot opt in out source destination |
302 | | 0 0 MASQUERADE all -- any any 10.0.0.0/24 anywhere |
303 | | }}} |
304 | | |
305 | | * MASQUERADEが正しく設定されている |
306 | | |
307 | | == make LXC container with ubuntu template == |
308 | | * mitty@precise:~$ sudo lxc-create -t ubuntu -n lxc-test |
309 | | {{{ |
310 | | |
311 | | No config file specified, using the default config |
312 | | debootstrap is /usr/sbin/debootstrap |
313 | | Checking cache download in /var/cache/lxc/precise/rootfs-amd64 ... |
314 | | installing packages: vim,ssh |
315 | | Downloading ubuntu precise minimal ... |
316 | | I: Retrieving Release |
317 | | I: Retrieving Release.gpg |
318 | | I: Checking Release signature |
319 | | |
320 | | .... |
321 | | |
322 | | I: Checking component main on http://ftp.tsukuba.wide.ad.jp/Linux/ubuntu... |
323 | | |
324 | | .... |
325 | | |
326 | | Processing triggers for initramfs-tools ... |
327 | | Download complete |
328 | | Copy /var/cache/lxc/precise/rootfs-amd64 to /var/lib/lxc/lxc-test/rootfs ... |
329 | | Copying rootfs to /var/lib/lxc/lxc-test/rootfs ... |
330 | | |
331 | | ## |
332 | | # The default user is 'ubuntu' with password 'ubuntu'! |
333 | | # Use the 'sudo' command to run tasks as root in the container. |
334 | | ## |
335 | | |
336 | | 'ubuntu' template installed |
337 | | 'lxc-test' created |
338 | | }}} |
339 | | |
340 | | === set container IP with LXC/config === |
341 | | * LXCコンテナのconfigファイルからIPアドレスを指定する |
342 | | * 結論としては、デフォルトゲートウェイなどを設定できないので、不便 |
343 | | |
344 | | * mitty@precise:~$ sudo vim /var/lib/lxc/lxc-test/config |
345 | | {{{ |
346 | | lxc.network.ipv4 = 10.0.0.10/24 |
347 | | }}} |
348 | | |
349 | | * mitty@precise:~$ sudo lxc-start -n lxc-test -d |
350 | | * mitty@precise:~$ ping 10.0.0.10 -c 1 |
351 | | {{{ |
352 | | PING 10.0.0.10 (10.0.0.10) 56(84) bytes of data. |
353 | | 64 bytes from 10.0.0.10: icmp_req=1 ttl=64 time=0.060 ms |
354 | | |
355 | | --- 10.0.0.10 ping statistics --- |
356 | | 1 packets transmitted, 1 received, 0% packet loss, time 0ms |
357 | | rtt min/avg/max/mdev = 0.060/0.060/0.060/0.000 ms |
358 | | }}} |
359 | | |
360 | | * mitty@precise:~$ ssh 10.0.0.10 -l ubuntu |
361 | | * ubuntu@lxc-test:~$ ifconfig |
362 | | {{{ |
363 | | eth0 Link encap:Ethernet HWaddr 00:16:3e:ba:3e:ef |
364 | | inet addr:10.0.0.10 Bcast:10.0.0.255 Mask:255.255.255.0 |
365 | | }}} |
366 | | * ubuntu@lxc-test:~$ ip route |
367 | | {{{ |
368 | | 10.0.0.0/24 dev eth0 proto kernel scope link src 10.0.0.10 |
369 | | }}} |
370 | | * ubuntu@lxc-test:~$ ping 8.8.8.8 |
371 | | {{{ |
372 | | connect: Network is unreachable |
373 | | }}} |
374 | | |
375 | | === set container IP with interfaces === |
376 | | * ホストOS上から、あらかじめコンテナのinterfacesファイルを設定して、起動する |
377 | | * 間違いが無く、かつ楽そう |
378 | | |
379 | | * mitty@precise:~$ sudo vim /var/lib/lxc/lxc-test/rootfs/etc/network/interfaces |
380 | | {{{ |
381 | | auto eth0 |
382 | | iface eth0 inet static |
383 | | address 10.0.0.1 |
384 | | netmask 255.255.255.0 |
385 | | gateway 10.0.0.254 |
386 | | }}} |
387 | | |
388 | | * mitty@precise:~$ ssh 10.0.0.1 -l ubuntu |
389 | | * ubuntu@lxc-test:~$ ifconfig |
390 | | {{{ |
391 | | eth0 Link encap:Ethernet HWaddr 00:16:3e:ba:3e:ef |
392 | | inet addr:10.0.0.1 Bcast:10.0.0.255 Mask:255.255.255.0 |
393 | | }}} |
394 | | * ubuntu@lxc-test:~$ ip route |
395 | | {{{ |
396 | | default via 10.0.0.254 dev eth0 metric 100 |
397 | | 10.0.0.0/24 dev eth0 proto kernel scope link src 10.0.0.1 |
398 | | }}} |
399 | | * ubuntu@lxc-test:~$ ping 8.8.8.8 -c 1 |
400 | | {{{ |
401 | | PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data. |
402 | | 64 bytes from 8.8.8.8: icmp_req=1 ttl=52 time=7.66 ms |
403 | | |
404 | | --- 8.8.8.8 ping statistics --- |
405 | | 1 packets transmitted, 1 received, 0% packet loss, time 0ms |
406 | | rtt min/avg/max/mdev = 7.667/7.667/7.667/0.000 ms |
407 | | }}} |