阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

libvirt 中体验容器

120次阅读
没有评论

共计 2083 个字符,预计需要花费 6 分钟才能阅读完成。

libvirt 是一个通用的虚拟化框架,支持 xen,kvm,lxc 多种虚拟化技术,本文作为一个笔记。

设置 LXC 为默认(默认为 qemu)

export LIBVIRT_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.xm

virsh net-define wan.xml

开启网卡

virsh net-start lan

virsh net-autostart lan

virsh net-start wan

virsh net-autostart wan

应用容器

应用容器共享宿主机的各种资源,包括内核,各种软件包,各种库,它仅有一个独立的运行空间,像是一个沙箱环境。

创建一个名为 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/centos7

wget  xxx

xz -d rootfs.tar.xz

tar 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 centos7

virsh autostart centos7

进入容器

virsh console centos7

停止开机启动

virsh autostart –disable centos7

退出 lxc console

ctrl+]

使用 libvirt 创建和管理 KVM 虚拟机 http://www.linuxidc.com/Linux/2012-06/62934.htm

应用 Libvirt 连接 KVM 虚拟化平台 http://www.linuxidc.com/Linux/2013-02/79939.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计2083字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中