如何在centos6.5上使用LXC虚拟机

来源:互联网 发布:java高并发面试题实例 编辑:程序博客网 时间:2024/06/03 17:10

http://www.07net01.com/2014/09/59307.html

http://www.thegeekstuff.com/2016/01/install-lxc-linux-containers/

https://segmentfault.com/q/1010000002451481

如何在centos6.5上使用LXC虚拟机

   一:什么是LXC

    LXC,其名称来自linux软件容器(Linux Containers)的缩写,一种操作系统层虚拟化(Operating system–level virtualization)技术,为Linux内核容器功能的一个用户空间接口。它将应用软件系统打包成一个软件容器(Container),内含应用软件本身的代码,以及所需要的操作系统核心和库。通过统一的命名空间和共用API来分配不同软件容器的可用硬件资源,创造出应用程序的独立沙箱运行环境,使得Linux用户可以容易的创建和管理系统或应用容器。

   二:LXC内网卡信息。

wKioL1QGgn-iTTKWAACqjd_GcYQ489.jpg

   把主机的物理网卡做成网桥设备(交换机设备),把物理网卡做成网桥设备后主机就没有自己的网卡了,所以主机要虚拟出自己的虚拟网卡,此时网桥设备就要为虚拟机的网卡,主机的的网卡接收报文,他便工作在混杂模式下。

   三:LXC的配置过程

 1)把eth0做成网桥,前提是要确保NetworkManager关掉,确保network启动起来。

 

 service NetworkManager stop
 service NetworkManager status #查看虚拟机NetworkManager
 cd /etc/sysconfig/network-scriprs/
 cp ifcfg-eth0 ifcfg-br0  #创建网桥设备
 vim ifcfg-bro #改配置文件
 
DEVICE=”br0″                          #改为br0
BOOTPROTO=”none”
HWADDR=”00:0C:29:D3:F8:8F” 
NM_CONTROLLED=”no”                    #不启动NM
ONBOOT=”yes”                          #类型改为Bridge
TYPE=”Bridge”
UUID=”5808601c-b6ce-496f-b9d0-c44a5ae9bd84″
IPADDR=”172.16.16.5″
NETMASK=”255.255.0.0″
GATEWAY=”172.16.0.1″

2)修改ifcfg-eth0的IP配置信息

DEVICE=”eth0″
BOOTPROTO=”none”
HWADDR=”00:0C:29:D3:F8:8F”
NM_CONTROLLED=”no”
ONBOOT=”yes”
TYPE=”Ethernet”
UUID=”5808601c-b6ce-496f-b9d0-c44a5ae9bd84″
BRIDGE=br0   #添加BRIDGE 注意要为大写 其他的IP地址,网关,DNS,都删除。

3)service network restart
    ifconfig


 4)安装虚拟机

  下载虚拟机rpm包,安装此包组。安装依靠的包组

   

yum install libcgroup -y  #安装包组
service cgconfig start  #启动cgconfig
lxc-checkconfig

vim /etc/lxc/default.conf
lxc.network.type = veth
lxc.network.link = br0        #修改为我们命名的br0
lxc.network.flags = up

vim /usr/share/lxc/templates/lxc-centos #修改里面的yum源改为本地的yum源地址。
[base]
   baseurl= #本地的yum源地址。

 5)创建虚拟机

 

lxc-create -n centos6 -t /usr/share/lxc/templates/lxc-centos  #创建虚拟机

6)启动虚拟机

 

 chroot /var/lib/lxc/centos6-1/rootfs/   #切换到根目录下 修改虚拟机的启动密码
 passwd   #执行命令后输入新的密码
 exit  #修改完后退出虚拟机 这一步很重要
 lxc-start -n centos6  #启动虚拟机  加上-d为后台启动
 shutdown -h now #关闭虚拟机操作

 


OK 在Linux主机上安装虚拟主机的过程已经完成。


How to Install and Setup LXC Linux Containers on CentOS / RHEL / Ubuntu

Tweet

LXC Linux ContainersLXC stands for Linux Containers.

Linux containers are light-weight “virtualization” methods that runs multiple virtual units simultaneously on a single control host on a single Linux kernel.

The Linux kernel contains cgroups for resource isolation (CPU, memory, block I/O, network, etc, ), which does not require starting any virtual machines.

Cgroups also provides namespace isolation to completely isolate application view of the operating environment, including process trees, networks, userids and mounted filesystems.

LXC is open source software and licensed under GNU LGPLv2.1+ license.

Containers actually make use of kernel features called namespaces, cgroups, and chroots, to carve off a contained area.

So, the end result of LXC looks much like a virtual machine, but without any hypervisor.

1. Pre-req for LXC Install

Before you install LXC, make sure your system is up-to-date using yum update as shown below.

# yum update

LXC is dependent on two libraries; libpcap and libcgroup. This also requires busybox and bridge utilities.

Install the following required packages for our LXC install.

# yum install libcap-devel libcgroup busybox wget bridge-utils

Let us make sure we got all the compilers and related development tools that are required to compile and install any software from source code.

# yum groupinstall "Development tools"

For more information about yum groups, refer to our yum commands article.

2. Download LXC Linux Containers

Download the latest version of the LXC from Linux Containers Project.

Use wget to download the tar ball of the latest stable version of LXC to your machine as shown below.

# cd /# wget http://linuxcontainers.org/downloads/lxc-1.1.5.tar.gz

If you get a certificate related error message while using the above wget, then use the no-check-certificate option as shown below.

# wget --no-check-certificate https://linuxcontainers.org/downloads/lxc-1.1.5.tar.gz

Alternatively, if you prefer, you can also use git clone to download the latest development version or stable version as shown below.

git clone git://github.com/lxc/lxc -b {branch}

In the above command, the {branch} can either be “master” for the current development branch, or “stable-1.0” for the stable update branch of the 1.0 version.

3. Configure Bridged Adapter

Next, create a bridged adapter and configure the static IP on the bridged adapter as shown in the following ifcfg-eth0 example. Ensure your physical adapter points to this bridged adapter in ifcfg-eth0.

# cd /etc/sysconfig/network-scripts# vi ifcfg-br0DEVICE="br0"BOOTPROTO="static"IPADDR="xxx.xxx.xxx.xxx"NETMASK="255.255.255.xxx"ONBOOT="yes"TYPE="Bridge"NM_CONTROLLED="no"

Replace all the “xxx” in the above file with the values that matches your ip-address and networkmask. For example, change the IPADDR value in the above file to the ip-address of your machine.

4. Install LXC Linux Container

Next, untar the LXC tar ball, that we downloaded earlier, execute the ./configure, and do make and make install, to install the LXC on your system as shown below.

By default, this will install all the lxc binaries under /usr/local/bin directory.

# cd /# tar xvfz lxc-1.1.5.tar.gz# cd lxc-1.1.5/# ./configure# make && make install

The following are few of the last few lines from the output for the above ./configure command.

# ./configure......config.status: creating src/python-lxc/setup.pyconfig.status: creating src/lua-lxc/Makefileconfig.status: executing depfiles commandsconfig.status: executing default commands----------------------------Environment: - compiler: gcc - distribution: centos - init script type(s): sysvinit - rpath: no - GnuTLS: no - Bash integration: yesSecurity features: - Apparmor: no - Linux capabilities: yes - seccomp: no - SELinux: no - cgmanager: noBindings: - lua: no - python3: noDocumentation: - examples: yes - API documentation: yes - user documentation: noDebugging: - tests: no - mutex debugging: noPaths: - Logs in configpath: no

The following are the last few lines from the output for the above make command.

# make...Building full member lists recursively...Adding members to member groups....Generating style sheet...Generating index page...Generating page index...Generating example documentation...Generating file sources...Generating code for file /usr/save/lxc-1.1.5/src/lxc/attach_options.h...Generating code for file /usr/save/lxc-1.1.5/src/lxc/lxccontainer.h...Generating code for file /usr/save/lxc-1.1.5/src/lxc/lxclock.h...Generating file documentation...Generating docs for file /usr/save/lxc-1.1.5/src/lxc/attach_options.h......Generating file member index...make[2]: Leaving directory `/usr/save/lxc-1.1.5/doc/api'make[2]: Entering directory `/usr/save/lxc-1.1.5/doc'...Making all in hooksmake[1]: Entering directory `/usr/save/lxc-1.1.5/hooks'make[1]: Nothing to be done for `all'....

The following are the last few lines from the output for the above make install command.

# make install...Making install in hooksmake[1]: Entering directory `/usr/save/lxc-1.1.5/hooks' /bin/mkdir -p '/usr/local/share/lxc/hooks' /usr/bin/install -c clonehostname mountecryptfsroot ubuntu-cloud-prep squid-deb-proxy-client '/usr/local/share/lxc/hooks'.../bin/mkdir -p /usr/local/var/lib/lxc/bin/mkdir -p /usr/local/var/cache/lxc /bin/mkdir -p '/usr/local/lib/pkgconfig' /usr/bin/install -c -m 644 lxc.pc '/usr/local/lib/pkgconfig'...

5. Create Soft Link for LXC Shared Libraries

Execute lxc-info to verify that the LXC is installed successfully. If everything is installed properly, we’ll see a message saying that the give container name (for example, test) doesn’t exist. We have not created any container at this stage. We just want to make sure this command is not throwing any shared library related errors.

# lxc-info --name testtest doesn't exist

Depending on how things on configured on your system, you might also get the following liblxc.so.1 library related error message.

# lxc-info --name testlxc-info: error while loading shared libraries: liblxc.so.1: cannot open shared object file: No such file or directory

In this case, to fix it, create a link called libxc.so.1 as shown below, which will point to the correct version of the liblxc.so.x.x.x file.

# ln -s /usr/local/lib/liblxc.so.1.1.5 /lib64/liblxc.so.1# ls -l /lib64/liblxc.so.1lrwxrwxrwx. 1 root root 30 Jan 20 09:17 /lib64/liblxc.so.1 -> /usr/local/lib/liblxc.so.1.1.5

6. Verify LXC Setup and Configuration

Next, you can also execute the lxc-checkconfig to verify that your lxc environment is setup correctly.

The following will have output in four different sections: 1) Namespaces, 2) Control Groups 3) Misc and 4) Checkpoint/Restore. Partial output is shown below.

# lxc-checkconfigKernel configuration not found at /proc/config.gz; searching...Kernel configuration found at /boot/config-2.6.32-431.el6.x86_64--- Namespaces ---Namespaces: enabledUtsname namespace: enabledIpc namespace: enabledPid namespace: enabledUser namespace: enabledNetwork namespace: enabledMultiple /dev/pts instances: enabled--- Control groups ---Cgroup: enabledCgroup namespace: enabledCgroup device: enabledCgroup sched: enabledCgroup cpu account: enabled..--- Misc ---Veth pair device: enabledMacvlan: enabledVlan: enabledBridges: enabledAdvanced netfilter: enabled..--- Checkpoint/Restore ---CONFIG_EVENTFD: enabledCONFIG_EPOLL: enabled..Note : Before booting a new kernel, you can check its configurationusage : CONFIG=/path/to/config /usr/local/bin/lxc-checkconfig

If everything is configured properly on your system, in the above output, all of the options in the above groups should say “enabled”.

Tomorrow, in the next part of the LXC series of articles, we’ll explain how to create, start, and use lxc linux containers using various lxc commands.

Tweet

原创粉丝点击