標籤:libvirt lxc
libvirt是一個通用的虛擬化架構,支援xen,kvm,lxc多種虛擬化技術,本文作為一個筆記。
設定LXC為預設(預設為qemu)
exportLIBVIRT_DEFAULT_URI=lxc:///
virsh-c lxc:/// 可省略
網路設定
NAT
NAT是libvirt預設內建,叫做default,通過下面的命令查看
virsh net-list
假設宿主機有兩個網卡,eth0為內網,eth1為外網,我們可以為容器橋接出兩個介面:
#cat lan.xml
<network> <name>lan</name> <forward mode="bridge" /> <interface dev="eth0" /> </network>
#cat wan.xml
<network> <name>wan</name> <forward mode="bridge" /> <interface dev="eth1" /> </network>
匯入配置virsh net-define lan.xmvirsh net-define wan.xml
開啟網卡
virsh net-start lanvirsh net-autostart lanvirsh net-start wanvirsh net-autostart wan
應用程式容器
應用程式容器共用宿主機的各種資源,包括核心,各種軟體包,各種庫,它僅有一個獨立的Runspace,像是一個沙箱環境。
建立一個名為web的應用程式容器
cat web.xml
<domain type="lxc"> <name>web</name> <memory>102400</memory> <os> <type>exec</type> <init>/bin/sh</init> </os> <device> <console type="pty" /> </device> </domain>
匯入virsh
virsh define web.xml
開機啟動
virsh autostart web
進入容器
virsh console web
編輯容器配置
virsh edit web
刪除容器
virsh undefine web
作業系統容器
作業系統容器除了Linux核心,自己包含一整套運行環境,像是一個chroot環境。
作業系統容器建立稍稍複雜一點
1、建立根目錄
mkdir -p /lxc/centos7
2、下載並解壓鏡像 http://images.linuxcontainers.org/images/centos/7/amd64/default/20140807_02:37/rootfs.tar.xz
cd /lxc/centos7wget xxxxz -d rootfs.tar.xztar xvf rootfs.tar
3、建立xml模板
cat centos7.xml
<domain type="lxc"> <name>centos7</name> <memory unit="KiB">1048576</memory> <currentMemory unit="KiB">1048576</currentMemory> <resource> <partition>/machine</partition> </resource> <os> <clock offset=‘utc‘ /> <on_poweroff>destory</on_poweroff> <on_reboot>restart</on_reboot> <on_crash>restart<on_crash></devices> <emulator>/usr/lib/libvirt/libvirt_lxc</emulator> <filesystem type=‘mount‘ accessmode=‘passthrough‘> <source dir=‘/lxc/centos7‘/> <target dir=‘/‘/> </filesystem> <interface type=‘network‘> <mac address=‘00:16:3e:52:87:f1‘/> <source network=‘lan‘/> </interface> <console type=‘pty‘> <target type=‘lxc‘ port=‘0‘/> </console> </devices> </domain>
匯入容器
virsh define centos7.xml
啟動容器
virsh start centos7virsh autostart centos7
進入容器
virsh console centos7
停止開機啟動
virsh autostart --disable centos7
退出lxc console
ctrl+]
本文出自 “專註Linux 營運” 部落格,請務必保留此出處http://purplegrape.blog.51cto.com/1330104/1537221