在libvirt 中體驗容器

來源:互聯網
上載者:User

標籤: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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.