一、什么是容器
容器是在隔离的环境里面运行的一个进程,这个隔离的环境有自己的系统目录文件,有自己的ip地址,主机名等。容器是一种轻量级虚拟化的技术。
二、容器的优势
相对于kvm虚拟机,容器的优势如下:
1:容器能提供接近宿主机的性能,而kvm虚拟机会损害一部分宿主机的性能
2:同样硬件配置的宿主机最多能启动10虚拟机,那么它可以启动100+容器
3:启动一台kvm虚拟机,可以能需要20秒,启动一个容器只需要1秒
4:kvm需要硬件cpu的支持,容器不需要
linux开机启动流程:
1. bios开机硬件自检
2. 根据bios设置的优先启动项
3. 读取mbr引导
4. 加载内核
5. 启动第一个进程/sbin/init
6. 执行系统初始化脚本/etc/rc.d/rc.sysinit完成系统初始化
7. 运行想要的服务sshd
kvm虚拟机运行一个sshd服务需要完整的开机启动流程,容器是直接启动sshd服务,中间的流程全部精简
三、容器的发展史
1. chroot技术
chroot,即change root directory(更改 root 目录),在 linux 系统中,系统默认的目录结构都是以`/`,即是以根 (root) 开始的,而在使用chroot之后,系统的目录结构将以指定的位置作为`/`位置
2. lxc容器
全称:linux container,通过namespaces 命名空间实现的隔离环境,通过cgroups实现的资源限制,提供类似虚拟机一样的体验。
3. docker容器
早期的docker容器底层就是调用的lxc,后期才换成了自己的libcontainer
四、lxc容器的安装和使用
1. 安装lxc容器
[root@kvm01 ~]# yum install lxc-* -y
[root@web01 ~]# yum install libcgroup* -y
2. 创建网桥
[root@web01 ~]# yum install bridge-utils -y
[root@web01 ~]# echo ‘TYPE=Ethernet
> BOOTPROTO=none
> NAME=eth0
> DEVICE=eth0
> ONBOOT=yes
> BRIDGE=virbr0‘ >/etc/sysconfig/network-scripts/ifcfg-eth0
[root@web01 ~]# echo ‘TYPE=Bridge
> BOOTPROTO=static
> NAME=virbr0
> DEVICE=virbr0
> ONBOOT=yes
> IPADDR=10.0.0.7
> NETMASK=255.255.255.0
> GATEWAY=10.0.0.254
> DNS1=223.5.5.5‘ >/etc/sysconfig/network-scripts/ifcfg-virbr0
3. 重启网络服务
[root@web01 ~]# systemctl restart NetworkManager
[root@web01 ~]# ip a
[root@web01 ~]# cat /etc/lxc/default.conf
lxc.network.type = veth
lxc.network.link = virbr0
lxc.network.flags = up
4. 创建lxc容器
[root@web01 ~]# lxc-create -t download -n centos6 -- --server mirrors.tuna.tsinghua.edu.cn/lxc-images
.....
Distribution: centos
Release: 6
Architecture: amd64
Downloading the image index
Downloading the rootfs
Downloading the metadata
The image cache is now ready
Unpacking the rootfs
---
You just created a Centos 6 x86_64 (20200825_07:08) container.
下载的文件所在目录
[root@web01 ~]# ll /var/cache/lxc/download/centos/6/amd64/default/
total 68060
-rw-r--r-- 1 root root 15 Sep 3 21:30 build_id
-rw-r--r-- 1 root root 64 Aug 25 15:18 config
-rw-r--r-- 1 root root 71 Aug 25 15:18 config.1
-rw-r--r-- 1 root root 71 Aug 25 15:18 config.2
-rw-r--r-- 1 root root 71 Aug 25 15:18 config.3
-rw-r--r-- 1 root root 71 Aug 25 15:18 config.4
-rw-r--r-- 1 root root 110 Aug 25 15:18 config-user
-rw-r--r-- 1 root root 124 Aug 25 15:18 config-user.1
-rw-r--r-- 1 root root 124 Aug 25 15:18 config-user.2
-rw-r--r-- 1 root root 124 Aug 25 15:18 config-user.3
-rw-r--r-- 1 root root 124 Aug 25 15:18 config-user.4
-rw-r--r-- 1 root root 63 Aug 25 15:18 create-message
-rw-r--r-- 1 root root 1 Aug 25 15:18 excludes-user
-rw-r--r-- 1 root root 11 Aug 25 15:18 expiry
-rw-r--r-- 1 root root 69631488 Aug 25 15:30 rootfs.tar.xz
-rw-r--r-- 1 root root 76 Aug 25 15:18 templates
5. 设置容器的root用户密码
[root@web01 ~]# chroot /var/lib/lxc/centos6/rootfs passwd
Changing password for user root.
New password:
BAD PASSWORD: it is too simplistic/systematic
BAD PASSWORD: is too simple
Retype new password:
passwd: all authentication tokens updated successfully.
6. 启动容器:
lxc-start -n centos6
7. 查看容器
[root@web01 ~]# lxc-ls
centos6
[root@web01 ~]# lxc-info -n centos6
8. lxc容器的进程和系统目录文件
a. lxc容器的进程
容器中运行的命令,在宿主机中都可以看到
b. lxc容器的系统目录文件
lxc容器的系统目录文件在宿主机的某一个目录下/var/lib/lxc/容器名/rootfs
原文:https://www.cnblogs.com/jiawei2527/p/13610845.html