Linux容器虛擬化LXC的使用
Oops:
萬一不能訪問,請內建梯子和火星文翻譯器
官方網站:
https://linuxcontainers.org/
Github:
https://github.com/lxc/lxc
火星文簡介:
What's LXC?
LXC is a userspace interface for the Linux kernel containment features.
Through a powerful API and simple tools, it lets Linux users easily create and manage system or application containers.
Features
Current LXC uses the following kernel features to contain processes:
Kernel namespaces (ipc, uts, mount, pid, network and user)
Apparmor and SELinux profiles
Seccomp policies
Chroots (using pivot_root)
Kernel capabilities
Control groups (cgroups) # 需要用到cgroups子系統
As such, LXC is often considered as something in the middle between a chroot on steroids and a full fledged virtual machine. The goal of LXC is to create an environment as close as possible as a standard Linux installation but without the need for a separate kernel.
Components
LXC is currently made of a few separate components:
The liblxc library
Several language bindings for the API:
python3 (in-tree, long term support in 1.0.x)
lua (in tree, long term support in 1.0.x)
Go
ruby
python2
Haskell
A set of standard tools to control the containers
Container templates
Licensing
LXC is free software, most of the code is released under the terms of the GNU LGPLv2.1+ license, some Android compatibility bits are released under a standard 2-clause BSD license and some binaries and templates are shipped under the GNU GPLv2 license.
Where do I get it?
From upstream
You can fetch the latest upstream tarballs here or grab it directly from git here or with:
git clone git://github.com/lxc/lxc
------------------------------------------------------------------------------------------
實戰部分:
準備工作:
系統內容和yum源
安裝所需軟體包:
1) debootstrap
debootstrap is used to create a Debian base system from scratch, without
requiring the availability of dpkg or apt.
2) libvirt
省去麻煩的配置,直接用這個 ^_^
3) libcgroup
Control groups infrastructure. The tools and library help manipulate, control administrate and monitor control groups and the associated controllers.
4) lxc lxc-templates
主要包以及模板(等下我們稍微改下模板檔案的yum源)
5) 當然還需要 gcc rsync
啟動相關服務:
修改模板檔案:
系統給提供了這麼多模板,下面來修改下centos模板的預設源(修改之前自覺備份一個)
開始正式安裝:
lxc-create creates a container
Options :
-n, --name=NAME NAME for name of the container
-f, --config=file Initial configuration file
-t, --template=t Template to use to setup container
修改密碼:
# chroot /var/lib/lxc/centos_1/rootfs passwd
啟動虛擬機器:
# lxc-start -d -n centos_1
Options :
-n, --name=NAME NAME for name of the container
-d, --daemon daemonize the container
查看虛擬機器狀態:
# lxc-info -n centos_1
連過去看看:
這樣一台就弄好了,如果要做LBC,HAC這類實驗,可以複製多個來實現。
LXC給我們提供了很多管理工具:
lxc-attach
lxc-clone # 複製相關
lxc-destroy # 銷毀
lxc-ls # List containers existing on the system。
lxc-stop # XD
lxc-usernsexec
lxc-autostart
lxc-config
lxc-execute
lxc-monitor
lxc-top # top你懂的
lxc-wait
lxc-cgroup
lxc-console
lxc-freeze
lxc-snapshot # 給一個容器鏡像
lxc-unfreeze
lxc-checkconfig
lxc-create # 建立
lxc-info # 顯示容器狀態資訊
lxc-start # 啟動
lxc-unshare
Python API:
Python 3K
import lxc container = lxc.Container("p1") container.create("ubuntu") container.start() container.get_ips() container.stop()
Python 2.X: