LXC学习

来源:互联网 发布:php防cc攻击代码怎么用 编辑:程序博客网 时间:2024/05/22 16:45

文档出自【http://www.techrepublic.com/blog/opensource/introducing-linux-virtual-containers-with-lxc/1289】


Linux Resource Containers (LXC) provide the ability to run containers that contain processes run within them to isolate them from the host operating system. The project is part of the upstream kernel, which means that any Linux distribution using kernel 2.6.29 or later will have the kernel-level bits available, without resorting to a third-party to provide it. For instance, Fedora 12 comes with the appropriate kernel and the user-space tools to use LXC.


To start using LXC, you must install the LXC user-space tools and have an appropriate kernel with LXC support enabled. On Fedora 12, the kernel is provided and the user-space tools can be installed via:
# yum install lxc

The next step is to make sure the kernel properly supports LXC:
$ lxc-checkconfig

It will provide a list of capabilities; if every capability is listed as “enabled,” LXC is ready to be used with the kernel. You must first create and mount the LXC control group filesystem:
# mkdir /cgroup
# mount none -t cgroup /cgroup
# echo "none /cgroup cgroup defaults 0 0" >> /etc/fstab

Next, you need to configure bridge networking. This can be done as root with the brctl command, part of the bridge-utils package (install this package if it is not already installed):
# brctl addbr br0
# brctl setfd br0 0
# ifconfig br0 192.168.250.52 promisc up
# brctl addif br0 eth0
# ifconfig eth0 0.0.0.0 up
# route add -net default gw 192.168.250.1 br0

This creates the bridge interface, br0, and assigns it the existing host IP address (in this case, 192.168.250.52). You will need to do this locally, as once you bring br0 up, the network will go down until the rest of the reconfiguration is complete. The next commands then reset the IP address of eth0 to 0.0.0.0, but since it is bound to the bridge interface, it will respond to the previous IP address anyways. Finally, a route is added for br0, which will be used by containers to connect to the network.

Once this is done, we must create a configuration file for a new container. This is a very basic example, so create the configuration file with the following contents:
lxc.utsname = test
lxc.network.type = veth
lxc.network.flags = up
lxc.network.link = br0
lxc.network.hwaddr = 4a:49:43:49:79:bd
lxc.network.ipv4 = 192.168.250.150
lxc.network.ipv6 = 2003:db8:1:0:214:1234:fe0b:3596

Save it as /etc/lxc/lxc-test.conf or something similar. The next command will start a confined shell process:
# /usr/bin/lxc-execute -n test -f /etc/lxc/lxc-test.conf /bin/bash
[root@test lxc]# ps ax
PID TTY STAT TIME COMMAND
1 pts/1 S 0:00 /usr/libexec/lxc-init -- /bin/bash
2 pts/1 S 0:00 /bin/bash
20 pts/1 R+ 0:00 ps ax

At this point, the confined shell can ping a remote host and can also be pinged by a remote host. It shares the same host filesystem, so /etc in this container is the same as /etc of the host, but as can be seen by the ps output, the process is fully isolated from the host process table. On the host, you can use LXC tools to view the state of the container:
# lxc-info -n test
'test' is RUNNING
# lxc-ps
CONTAINER PID TTY TIME CMD
13095 pts/2 00:00:00 su
13099 pts/2 00:00:00 bash
13134 pts/2 00:00:00 lxc-ps
13135 pts/2 00:00:00 ps

The above is an example of an LXC application container. This example had full separate networking support, however you can also isolate a single application that uses the existing host network (as a result not requiring a configuration file) using:
# lxc-execute -n test /bin/bash

You can also create LXC system containers that are more similar to OpenVZ containers. These mimic an entire operating system with its own file system and network address, fully separate from the host operating system. The simplest way to create these containers is to use OpenVZ templates. Next week, we will create an LXC-based system container.

LXC is powerful, and finally Linux users have something similar to the jail feature that BSD has enjoyed for years. While OpenVZ works great, having something immediately available from your Linux vendor makes maintenance of the system easier as all the bits are already available, and even though LXC is not as mature as OpenVZ, it is quite capable and under active development.


以下文章摘自【http://www.cnblogs.com/zhangzhang/archive/2012/05/08/2489589.html】

lxc能做什么:

可以为容器绑定特定的cpu和memory,分配特定比例的cpu时间、IO时间,限制可以使用的内存大小(包括内存和swap空间),提供device访问控制,提供独立的namespace(网络、pid、ipc、mnt、uts)。

默认的lxc配置是隔离pids(进程) sysv ipc(进程间通信),和挂载点。如果想运行sshd,就必须提供一个新的网络堆栈和hostname,如果想要避免文件冲突,需要重新挂载冲突文件到不同的地方。如果想要避免所有的冲突,你需要重新挂载一个文件系统。

lxc的实现:

Sourceforge上有LXC这个开源项目,但是LXC项目本身只是一个为用户提供一个用户空间的工具集,用来使用和管理LXC容器。LXC真正的实现则是靠Linux内核的相关特性,LXC项目只是对此做了整合。基于容器的虚拟化技术起源于所谓的资源容器和安全容器。

LXC在资源管理方面依赖与Linux内核的cgroups子系统,cgroups子系统是Linux内核提供的一个基于进程组的资源管理的框架,可以为特定的进程组限定可以使用的资源。LXC在隔离控制方面依赖于Linux内核的namespace特性,具体而言就是在clone时加入相应的flag(NEWNS NEWPID等等)。

lxc常见命令使用:

lxc-version 用于显示系统LXC的版本号(可以通过此命令判断系统是否安装了lxc)

用法:lxc-version

例如:lxc-version

lxc-checkconfig 用于判断linux内核是否支持LXC

用法:lxc-checkconfig

例如:lxc-checkconfig

lxc-create用于创建一个容器

用法:lxc-create -n name [-f config_file]

-n 后面跟要创建的容器名字 例如:-n foo

-f 后面跟容器配置文件的路径

注:1.采用lxc-create创建的容器,在停止运行后,不会被销毁,要采用lxc-destroy命令才能销毁

2.容器命令空间是全局的,系统中不允许存在重名的容器,如果-n 后面跟一个已经存在的容器名,创建会失败

例如:lxc-create --n foo --f foo.conf

lxc-execute 用于在一个容器执行应用程序

用法: lxc-execute -n name [-f config_file] [ -s KEY=VAL ]command

-n 后面跟容器名字(容器名字用于管理容器)例如:-n foo

-f 后面跟容器配置文件的路径(如果没有配置文件,可以直接用-s指定配置选项,如果什么都没有,系统采用默认策略)例如:-f foo.conf

-s 后面跟配置键值对 例如:lxc.cgroup.cpu.shares=512

command 为要执行的命令 例如:/bin/bash

这个命令会mount /proc 并且会自动创建/销毁容器。

注:1.如果容器还不存在,lxc-execute会自动创建一个,容器停止运行后会被自动销毁

2.用lxc-execute启动应用程序,配置优先级如下:

如果指定-f选项,那么之前创建容器(如果容器是已存在的)的配置文件不会被使用

如果指定-s选项,则在命令行中的配置键值对会覆盖配置文件(无论之前的还是-f指定的)相同配置

例如:lxc-execute --n foo --s lxc.cgroup.cpu.shares=512 /bin/bash

使用实际例子:

lxc-execute -n test /bin/bash

这个会启动一个lxc并给出类似的一个cmd窗口,网络是与操作系统共用的,这里好像仅仅是创建了一个命名空间

如果没有指定-f,默认的隔离将被使用,这个命令当你需要一个快速在一个隔离的环境中运行程序。在物理机上和container中都会运行lxc-init,在宿主机上面,这个程序用于转发lxc-kill 信号到已经启动的程序中 ,在container中,这个程序的pid为1,它会fork出要执行的命令(pid为2)并执行。



lxc-start 用于在容器中执行给定命令

用法:lxc-start -n name [-f config_file] [-c console_file] [-d] [-s KEY=VAL]
[command]

-d 将容器当做守护进程执行

-f 后面跟配置文件

-c 指定一个文件作为容器console的输出,如果不指定,将输出到终端

-s 指定配置

如果没有指定命令,lxc-start 将要运行 /sbin/init

例如:lxc-start -n foo -f foo.conf -d /bin/bash

注:1.如果容器还不存在,lxc-start会自动创建一个,容器停止运行后会被自动销毁

2.lxc-start配置优先级与lxc-execute相同

3.lxc-start 与lxc.execute的异同:

lxc-start 和 lxc-execute都可以在容器中启动进程,区别在于lxc-start直接创建进程,lxc-execute先创建lxc-init进程,然后在lxc-init中fork一个进程来执行。(关于第4点,lxc-init所占的是一个什么样的地位?)

The orphan process group and daemon are not supported by this command,
use the lxc-execute command instead

If no command is specified, lxc-start will use the default "/sbin/init"
command to run a system container.

4.lxc-start用于在容器启动system,lxc-execute用于在容器执行应用程序

lxc-kill 发送信号给容器中的第一个用户进程(容器内部进程号为2的进程)

用法:lxc-kil -n name SIGNUM

-n 后面跟容器名

SIGNUM 信号 (此参数可选,默认SIGKILL)

例如:lxc-kill -n foo

lxc-stop 用于停止容器中所有的进程

用法:lxc-stop -n name

-n后面跟要停止的容器名

例如:lxc-stop --n foo

lxc-destroy 用于销毁容器

用法:lxc-destroy -n name

-n后面跟要停止的容器名

例如: lxc-destroy --n foo

lxc-cgroup 用于获取或调整与cgroup相关的参数

用法:lxc-cgroup -n name subsystem value

-n 后面跟要调整的容器名

例如: lxc-cgroup -n foo devices.list

lxc-cgroup -n foo cpuset.cpus "0,3"

lxc-info 用户获取一个容器的状态

用法:lxc-info -n name

-n后面跟操作的容器名

例如: lxc-info --n foo

注:容器的状态有:STARTING RUNNING STOPPING STOPPED ABORTING

lxc-monitor 监控一个容器状态的变换,当一个容器的状态变化时,此命令会在屏幕上打印出容器的状态

用法:lxc-monitor -n name

例如:lxc-monitor -n foo

lxc-ls 列出当前系统所有的容器

用法:lxc-ls

例如:lxc-ls

lxc-ps 列出特定容器中运行的进程

用法:lxc-ps

例如:lxc-ps -n foo

更多的命令介绍可以再man page获得

原创粉丝点击