什么是容器?在生活中我们常见的容器有各种瓶瓶罐罐、各种能够容纳其它物料的东西叫容器;容器的特点就是有着很好的隔离作用,使得不同的物料互相隔离;除此之外容器还方便运输、方便储存;这是生活中所说的容器,以及它的特点;在计算机领域中,所谓容器不外乎也有同生活中的容器的特点,隔离,方便“运输”(计算机中的运输我们叫移植,从系统A到系统B),方便“存储”(这里指程序以及运行所依赖的库文件打包,即程序及运行时环境打包);LXC是Linux containers的缩写,意思就是Linux 容器;Linux容器技术其实就是整合内核的功能,让其支持多个容器运行时资源相互隔离;我们知道内核的功能用户是无法直接操作的,必须得有一用户空间的软件,通过系统调用去操作内核功能;所以lxc就是用来操作Linux内核容器化的工具;这种逻辑有点类似iptables,iptables本身不对用户的报文做任何检查,真正生效的是内核netfilter;

  LXC关键技术点:

  1、chroot:根切换,一个改变当前运行进程以及其子进程的根目录的操作。一个运行在这种环境的程序无法访问根目录外的文件和命令。

  2、namespaces:名称空间,LXC在隔离控制方面依赖于Linux内核的namespace特性,具体而言就是在clone时加入相应的flag;

  3、CGroups:控制组,LXC在资源管理方面依赖于Linux内核的cgroups子系统,cgroups子系统是Linux内核提供的一个基于进程组的资源管理的框架,可以为特定的进程组限定可以使用的资源;

  LXC简单使用:

  1、安装LXC工具

[root@lxc ~]# yum install  lxc-* libcgroup* bridge-utils
Loaded plugins: fastestmirror
base | 3.6 kB 00:00:00
epel | 4.7 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/7): base/7/x86_64/group_gz | 153 kB 00:00:00
(2/7): epel/x86_64/group_gz | 95 kB 00:00:00
(3/7): epel/x86_64/updateinfo | 1.0 MB 00:00:00
(4/7): updates/7/x86_64/primary_db | 1.3 MB 00:00:00
(5/7): extras/7/x86_64/primary_db | 190 kB 00:00:00
(6/7): base/7/x86_64/primary_db | 6.1 MB 00:00:01
(7/7): epel/x86_64/primary_db | 6.8 MB 00:00:02
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package bridge-utils.x86_64 0:1.5-9.el7 will be installed
---> Package libcgroup.x86_64 0:0.41-21.el7 will be installed
---> Package libcgroup-devel.x86_64 0:0.41-21.el7 will be installed
---> Package libcgroup-pam.x86_64 0:0.41-21.el7 will be installed
……省略部分信息
Installed:
bridge-utils.x86_64 0:1.5-9.el7 libcgroup.x86_64 0:0.41-21.el7
libcgroup-devel.x86_64 0:0.41-21.el7 libcgroup-pam.x86_64 0:0.41-21.el7
libcgroup-tools.x86_64 0:0.41-21.el7 lxc.x86_64 0:1.0.11-2.el7
lxc-devel.x86_64 0:1.0.11-2.el7 lxc-doc.noarch 0:1.0.11-2.el7
lxc-extra.x86_64 0:1.0.11-2.el7 lxc-libs.x86_64 0:1.0.11-2.el7
lxc-templates.x86_64 0:1.0.11-2.el7 Dependency Installed:
libtirpc.x86_64 0:0.2.4-0.16.el7 lua-alt-getopt.noarch 0:0.7.0-4.el7
lua-filesystem.x86_64 0:1.6.2-2.el7 lua-lxc.x86_64 0:1.0.11-2.el7
python3.x86_64 0:3.6.8-13.el7 python3-libs.x86_64 0:3.6.8-13.el7
python3-pip.noarch 0:9.0.3-7.el7_7 python3-setuptools.noarch 0:39.2.0-10.el7
python36-lxc.x86_64 0:1.0.11-2.el7 Complete!
[root@lxc ~]#

  提示:安装前请配置好epel源,以及base源;

  2、添加网桥

  3、检查当前系统是否满足创建容器的条件

[root@lxc ~]# lxc-checkconfig
Kernel configuration not found at /proc/config.gz; searching...
Kernel configuration found at /boot/config-3.10.0-693.el7.x86_64
--- Namespaces ---
Namespaces: enabled
Utsname namespace: enabled
Ipc namespace: enabled
Pid namespace: enabled
User namespace: enabled
newuidmap is not installed
newgidmap is not installed
Network namespace: enabled
Multiple /dev/pts instances: enabled --- Control groups ---
Cgroup: enabled
Cgroup clone_children flag: enabled
Cgroup device: enabled
Cgroup sched: enabled
Cgroup cpu account: enabled
Cgroup memory controller: enabled
Cgroup cpuset: enabled --- Misc ---
Veth pair device: enabled
Macvlan: enabled
Vlan: enabled
Bridges: enabled
Advanced netfilter: enabled
CONFIG_NF_NAT_IPV4: enabled
CONFIG_NF_NAT_IPV6: enabled
CONFIG_IP_NF_TARGET_MASQUERADE: enabled
CONFIG_IP6_NF_TARGET_MASQUERADE: enabled
CONFIG_NETFILTER_XT_TARGET_CHECKSUM: enabled --- Checkpoint/Restore ---
checkpoint restore: enabled
CONFIG_FHANDLE: enabled
CONFIG_EVENTFD: enabled
CONFIG_EPOLL: enabled
CONFIG_UNIX_DIAG: enabled
CONFIG_INET_DIAG: enabled
CONFIG_PACKET_DIAG: enabled
CONFIG_NETLINK_DIAG: enabled
File capabilities: enabled Note : Before booting a new kernel, you can check its configuration
usage : CONFIG=/path/to/config /usr/bin/lxc-checkconfig [root@lxc ~]#

  提示:如果没有报error之类的就表示当前环境满足创建容器的条件

  4、指定模板创建容器

[root@lxc ~]# lxc-create -n test -t /usr/share/lxc/templates/lxc-centos
Host CPE ID from /etc/os-release: cpe:/o:centos:centos:7
Checking cache download in /var/cache/lxc/centos/x86_64/7/rootfs ...
Downloading CentOS minimal ...
Loaded plugins: fastestmirror
base | 3.6 kB 00:00:00
updates | 2.9 kB 00:00:00
Determining fastest mirrors
* base: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package chkconfig.x86_64 0:1.7.4-1.el7 will be installed
--> Processing Dependency: rtld(GNU_HASH) for package: chkconfig-1.7.4-1.el7.x86_64
--> Processing Dependency: libpopt.so.0(LIBPOPT_0)(64bit) for package: chkconfig-1.7.4-1.el7.x86_64
--> Processing Dependency: libc.so.6(GLIBC_2.14)(64bit) for package: chkconfig-1.7.4-1.el7.x86_64
……省略部分内容 Container rootfs and config have been created.
Edit the config file to check/enable networking setup. The temporary root password is stored in: '/var/lib/lxc/test/tmp_root_pass' The root password is set up as expired and will require it to be changed
at first login, which you should do as soon as possible. If you lose the
root password or wish to change it without starting the container, you
can change it from the host by running the following command (which will
also reset the expired flag): chroot /var/lib/lxc/test/rootfs passwd [root@lxc ~]#

  提示:-t表示指定模板,该模板其实就是一shell脚本,在安装lxc-templates这个包后,默认/usr/share/lxc/templates/下面有几个模板;如果能看到以上提示表示容器已经成功创建,我们可以使用lxc-ls查看当前系统创建的所有容器;

[root@lxc ~]# lxc-ls
test
[root@lxc ~]#

  提示:可以看到当前系统只有名为test的容器

  5、修改当前test容器的root密码

[root@lxc ~]# chroot /var/lib/lxc/test/rootfs passwd
Changing password for user root.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@lxc ~]#

  提示:其实不用修改也行,默认的密码存放在/var/lib/lxc/test/tmp_root_passwd这个文件中;不修改密码登录容器后,它会提示我们修改密码

  6、启动容器,并登录容器

[root@lxc ~]# lxc-start -n test
systemd 219 running in system mode. (+PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN)
Detected virtualization lxc.
Detected architecture x86-64. Welcome to CentOS Linux 7 (Core)! Running in a container, ignoring fstab device entry for /dev/root.
Cannot add dependency job for unit display-manager.service, ignoring: Unit not found.
[ OK ] Reached target Local Encrypted Volumes.
[ OK ] Reached target Swap.
[ OK ] Started Dispatch Password Requests to Console Directory Watch.
[ OK ] Started Forward Password Requests to Wall Directory Watch.
[ OK ] Reached target Paths.
[ OK ] Reached target Remote File Systems.
[ OK ] Created slice Root Slice.
[ OK ] Listening on Journal Socket.
[ OK ] Created slice User and Session Slice.
[ OK ] Listening on /dev/initctl Compatibility Named Pipe.
[ OK ] Listening on Delayed Shutdown Socket.
[ OK ] Created slice System Slice.
Mounting Huge Pages File System...
Starting Read and set NIS domainname from /etc/sysconfig/network...
Starting Journal Service...
Starting Remount Root and Kernel File Systems...
[ OK ] Reached target Slices.
[ OK ] Created slice system-getty.slice.
Mounting POSIX Message Queue File System...
[ OK ] Mounted Huge Pages File System.
[ OK ] Started Journal Service.
[ OK ] Started Read and set NIS domainname from /etc/sysconfig/network.
[ OK ] Mounted POSIX Message Queue File System.
[ OK ] Started Remount Root and Kernel File Systems.
Starting Configure read-only root support...
Starting Flush Journal to Persistent Storage...
Starting Rebuild Hardware Database...
[ OK ] Reached target Local File Systems (Pre).
<46>systemd-journald[16]: Received request to flush runtime journal from PID 1
[ OK ] Started Flush Journal to Persistent Storage.
[ OK ] Started Configure read-only root support.
Starting Load/Save Random Seed...
[ OK ] Reached target Local File Systems.
Starting Rebuild Journal Catalog...
Starting Create Volatile Files and Directories...
Starting Mark the need to relabel after reboot...
[ OK ] Started Load/Save Random Seed.
[ OK ] Started Rebuild Journal Catalog.
[ OK ] Started Create Volatile Files and Directories.
[ OK ] Started Mark the need to relabel after reboot.
Starting Update UTMP about System Boot/Shutdown...
[ OK ] Started Update UTMP about System Boot/Shutdown.
[ OK ] Started Rebuild Hardware Database.
Starting Update is Completed...
[ OK ] Started Update is Completed.
[ OK ] Reached target System Initialization.
[ OK ] Listening on D-Bus System Message Bus Socket.
[ OK ] Reached target Sockets.
[ OK ] Reached target Basic System.
Starting Permit User Sessions...
Starting OpenSSH Server Key Generation...
Starting LSB: Bring up/down networking...
[ OK ] Started D-Bus System Message Bus.
Starting Login Service...
[ OK ] Started Daily Cleanup of Temporary Directories.
[ OK ] Reached target Timers.
[ OK ] Started Permit User Sessions.
Starting Cleanup of Temporary Directories...
[ OK ] Started Console Getty.
[ OK ] Reached target Login Prompts.
[ OK ] Started Command Scheduler.
[ OK ] Started Cleanup of Temporary Directories.
[ OK ] Started Login Service.
[ OK ] Started OpenSSH Server Key Generation. CentOS Linux 7 (Core)
Kernel 3.10.0-693.el7.x86_64 on an x86_64 test login: root
Password:
You are required to change your password immediately (root enforced)
Changing password for root.
(current) UNIX password:
New password:
Retype new password:
[root@test ~]# hostname
test
[root@test ~]#

  提示:以上是启动容器后,用默认密码登录容器,它会提示我们修改密码;如果想要让该容器后台运行(不占据当前shell终端),可以使用-d选项指定即可;

  查看test容器的信息

[root@lxc ~]# lxc-info -n test
Name: test
State: RUNNING
PID: 5896
IP: 192.168.0.216
CPU use: 0.16 seconds
BlkIO use: 0 bytes
Memory use: 1.34 MiB
KMem use: 0 bytes
Link: veth6WQY6V
TX bytes: 1.67 KiB
RX bytes: 17.50 KiB
Total bytes: 19.17 KiB
[root@lxc ~]#

  停止test容器

[root@lxc ~]# lxc-stop -n test
[root@lxc ~]# lxc-info -n test
Name: test
State: STOPPED
[root@lxc ~]#

  销毁容器

[root@lxc ~]# lxc-destroy -n test
[root@lxc ~]# lxc-ls
[root@lxc ~]#

  提示:销毁容器,容器的状态必须是停止状态,否则销毁不了

  以上就lxc容器的简单管理;

容器技术之LXC的更多相关文章

  1. 容器技术之LXC WEB管理工具LXC WEB Panel

    前一篇博文中主要说了下,lxc容器在Linux上的简单管理,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/12901493.html:今天我们来介绍下lxc的图 ...

  2. 容器技术与docker

    名词介绍 IaaS:基础设施即服务,要搭建上层数据应用,先得通过互联网获得基础性设施服务 PaaS:平台即服务,搭建平台,集成应用产品,整合起来提供服务 SaaS:软件即服务,通过网络提供程序应用类服 ...

  3. Docker应用程序容器技术_转

    转自:百度百科 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱机制,相 ...

  4. Linux 容器技术史话:从 chroot 到未来

    Linux 容器是一个在单一 Linux 主机上提供多个隔离的 Linux 环境的操作系统级虚拟技术.不像虚拟机(VM),容器并不需要运行专用的访客(guest)操作系统.容器们共享宿主机的(host ...

  5. Cgroup maintainer丽泽范:解剖Linux核心容器技术

    摘要:Cgroup和namespace等内核特性如何出现,在社区处于如何的开发状况?Docker如火如荼.内核社区是否会因此加紧完好容器技术的隔离性安全性?华为Linux内核高级project师李泽帆 ...

  6. Docker容器技术

    Docker介绍 什么是容器 Linux容器是与系统其他部分隔离开的一系列进程,从另一个系统镜像运行,并由该镜像提供支持进程所需的全部文件. 容器镜像包含了应用的所有依赖项,因而在从开发到测试再到生产 ...

  7. Docker 学习1 容器技术基础入门

    一.虚拟化 1.主机级别虚拟化(两种) a.类型一虚拟化:在硬件上直接安装hyper-ver,然后再安装虚拟机 1).操作系统就两棵树:进程树,文件系统树. 2).一个内核只能有一个root b.类型 ...

  8. Docker 容器技术

    前言: 之前感觉Docker是一种小虚拟机,docker和KVM虚拟机之间有什么区别.联系.可以应用在什么样的生产环境? 一.Docker是什么以及和KVM的区别 1.docker概念? Docker ...

  9. GIS+=地理信息+容器技术(1)——容器技术概述

    -------------------------------------------------------------------------------------- Blog:    http ...

随机推荐

  1. 资料整理:python接口类

    1开发原则(七大原则) 一:开放封闭原则 二:接口隔离原则(将不同接口对象一一展现出来,分别调用各自的接口,完成各自的功能) 三:依赖倒置原则 高级模块不能依赖低级模块,可以依赖抽象 规范的定义: 高 ...

  2. jest enzyme unit test react

    1. 测试类型 单元测试:指的是以原件的单元为单位,对软件进行测试.单元可以是一个函数,也可以是一个模块或一个组件,基本特征就是只要输入不变,必定返回同样的输出.一个软件越容易些单元测试,就表明它的模 ...

  3. JS+Selenium+excel追加写入,使用python成功爬取京东任何商品~

    之前一直是requests库做爬虫,这次尝试下使用selenium做爬虫,效率不高,但是却没有限制,文章是分别结合大牛的selenium爬虫以及excel追加写入操作而成,还有待优化,打算爬取更多信息 ...

  4. c++动态数组的使用

    在c++中,有的时候会遇到变长的数组(不管是一维的还是二维的),这个时候就需要用到动态数组了,并且要用new和delete两个操作符,这俩操作符一般成对使用. 先说一维的动态数组吧,直接上代码 #in ...

  5. java中functional interface的分类和使用

    目录 简介 Functional Interface Function:一个参数一个返回值 BiFunction:接收两个参数,一个返回值 Supplier:无参的Function Consumer: ...

  6. 小白的MyBatis逆向工程

    MyBatis逆向工程   MyBatis逆向工程,简称MBG.是一个专门为MyBatis框架使用者定制的代码生成器. 可以快速的根据数据库表生成对应的映射文件,接口,以及Bean类对象. 在Myba ...

  7. Axure遮罩 or 灯箱

    2019独角兽企业重金招聘Python工程师标准>>> 在做原型设计的时候,常常需要设计弹窗(比如confirm.alert或者弹出面板),加一个全屏的遮罩可以突出要展示的内容,效果 ...

  8. [每日短篇] 1C - Spring Data JPA (0)

    2019独角兽企业重金招聘Python工程师标准>>> 准备把 Spring Data JPA 完整看一遍,顺便把关键要点记录一下.在写这篇文章的今天,再不用 Spring Boot ...

  9. 深入理解Mysql——锁、事务与并发控制

    本文对锁.事务.并发控制做一个总结,看了网上很多文章,描述非常不准确.如有与您观点不一致,欢迎有理有据的拍砖! mysql服务器逻辑架构 每个连接都会在mysql服务端产生一个线程(内部通过线程池管理 ...

  10. java集合的简单用法

    typora-root-url: iamge [TOC] 1.集合接口 1.1将集合的接口与实现分离 ​ 与现代的数据结构类库的常见情况一样,Java集合类库也将接口(interface)与实现(im ...