docker容器简介
docker容器简介
这个技术互联网用的越来越多了 早些用虚拟化技术多 慢慢超越KVM
一、什么是容器?
容器就是在隔离的环境运行的一个进程,如果进程停止,容器就会销毁。隔离的环境拥有自己的系统文件,ip地址,主机名等
kvm虚拟机,linux,系统文件
程序:代码,命令
进程:正在运行的程序
二、容器和虚拟化的区别
linux容器技术,容器虚拟化和kvm虚拟化的区别
linux开机启动流程:
bios开机硬件自检
根据bios设置的优先启动项boot 网卡 硬盘 u盘 光驱
读取mbr引导 2T UEFI(gpt分区) mbr硬盘分区信息,内核加载路径,
加载内核
启动第一个进程/sbin/init systemd
系统初始化完成
运行服务(nginx,httpd,mysql)
。。。
容器启动流程:
共用宿主机内核:
第一个进程直接启动服务(nginx,httpd,mysql)
#容器:共用宿主机内核,轻量级,损耗少,启动快,性能高,只能运行在linux系统上
#虚拟机:需要硬件的支持,需要模拟硬件,需要走开机启动流程,可以运行不同的操作系统
三、容器技术的发展过程:
1.chroot技术,新建一个子系统(拥有自己完整的系统文件)
参考资料:https://www.ibm.com/developerworks/cn/linux/l-cn-chroot/
chang root
作业1:使用chroot监狱限制SSH用户访问指定目录和使用指定命令(cp,ls)
https://linux.cn/article-8313-1.html
ls
2.linux容器(lxc) linux container(namespaces 命名空间 隔离环境 及cgroups 资源限制)
cgroups 限制一个进程能够使用的资源。cpu,内存,硬盘io
kvm虚拟机:资源限制(1c 1G 20G)
# 1.安装epel源
yum install epel-release -y
# 2.编译epel源配置文件
vi /etc/yum.repos.d/epel.repo
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7/$basearch
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-7&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
[epel-debuginfo]
name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7/$basearch/debug
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-7&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=1
[epel-source]
name=Extra Packages for Enterprise Linux 7 - $basearch - Source
baseurl=https://mirrors.tuna.tsinghua.edu.cn/epel/7/SRPMS
#mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-source-7&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=1
# 3.安装lxc
yum install lxc-* -y
yum install libcgroup* -y
yum install bridge-utils.x86_64 -y
# 4.桥接网卡
[root@controller ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
echo 'TYPE=Ethernet
BOOTPROTO=none
NAME=eth0
DEVICE=eth0
ONBOOT=yes
BRIDGE=virbr0' >/etc/sysconfig/network-scripts/ifcfg-eth0
[root@controller ~]# cat /etc/sysconfig/network-scripts/ifcfg-virbr0
echo 'TYPE=Bridge
BOOTPROTO=static
NAME=virbr0
DEVICE=virbr0
ONBOOT=yes
IPADDR=10.0.0.11
NETMASK=255.255.255.0
GATEWAY=10.0.0.254
DNS1=180.76.76.76' >/etc/sysconfig/network-scripts/ifcfg-virbr0
# 5.启动cgroup
systemctl start cgconfig.service
# 6.启动lxc
systemctl start lxc.service
# 7.创建lxc容器
方法1:
lxc-create -t download -n centos6 -- --server mirrors.tuna.tsinghua.edu.cn/lxc-images -d centos -r 6 -a amd64
方法2:
lxc-create -t centos -n test
# 8.为lxc容器设置root密码:
[root@controller ~]# chroot /var/lib/lxc/test/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.
# 9.为容器指定ip和网关
vi /var/lib/lxc/centos7/config
lxc.network.name = eth0
lxc.network.ipv4 = 10.0.0.111/24
lxc.network.ipv4.gateway = 10.0.0.254
# 10.启动容器
lxc-start -n centos7
3.docker容器
centos7.6 2G 10.0.0.11 docker01 host解析
centos7.6 2G 10.0.0.12 docker02 host解析
Docker是通过进程虚拟化技术(namespaces及cgroups cpu、内存、磁盘io等)来提供容器的资源隔离与安全保障等。
由于Docker通过操作系统层的虚拟化实现隔离,所以Docker容器在运行时,不需要类似虚拟机(VM)额外的操作系统开销,提高资源利用率。
【cgroups 进程的资源限制 】限制进程使用多少内存 cpu 硬盘io什么的
【namespace 资源隔离】
docker初期就是把lxc二级开发了 后来觉得的引擎破 就换成了自己的libcontainer
引擎