docker一:容器介紹與lxc容器


1.什么是容器:容器就是在隔離的環境運行的一個進程,如果進程停止,容器就會銷毀。

隔離的環境擁有自己的系統文件,IP地址,主機名等等。

2.容器和虛擬化的區別:

kvm虛擬化:需要硬件的支持,需要模擬硬件,可以運行不同的操作系統,啟動時間分鍾級(開機開啟流程)。

linux開機啟動流程:

  • bios開機硬件自檢
  • 根據bios設置的優先啟動項
  • 讀取mbr引導:UEFI(GPT分區),包括mbr硬盤分區信息,內核的加載路徑
  • 加載內核
  • 啟動第一個進程init, systemed................

容器虛擬化:不需要硬件的支持。不需要模擬硬件,共用宿主機的內核,啟動時間秒級(沒有開機啟動流程)

容器的啟動流程:

  • 共享宿主機的內核
  • 啟動第一個進程init, systemed................

 

KVM與docker的對比,參考:https://www.qstack.com.cn/archives/148.html

3.chroot,新建一個子系統

linux容器下載地址:https://mirrors.tuna.tsinghua.edu.cn/lxc-images/images/ubuntu/trusty/amd64/default/20190801_07%3A42/,下載rootfs.tar.xz,解壓到/opt/ubuntu/

切換系統到ununtu: chroot /opt/ubuntu/

查看環境變量:echo $PATH

缺少兩個目錄:/bin和/sbin

添加以上兩個目錄到PATH:export PATH=$PATH:/bin:/sbin

4.linux容器(lxc):linux container(命名空間,隔離環境,cgroups資源限制)

cgroups可以限制一個進程能夠使用的資源,如cpu,內存,硬盤io

 需要使用epel源

1).安裝epel源:yum install epel-release.noarch -y

 

以下內容:將aliyun的域名,劫持到本地的服務器192.168.14.200

查看yum源:ls /etc/yum.repos.d/

刪除yum源: mv  /etc/yum.repos.d/  /etc/yum.repos.d.bak/

echo '192.168.14.200 mirrors.aliyun.com'
echo '192.168.14.200 mirrors.aliyun.com' >>/etc/hosts
cat /etc/hosts

下載repo文件:curl -o /etc/yum.repos.d/Centos-Base.repo http://192.168.14.200/repo/Centos-7.repo

 以下內容:將epel源,劫持到本地的服務器192.168.14.200

culr -o /etc/yum.repos.d/epel.repo http://192.168.14.200/repo/epel-7.repo

2).安裝lxc

yum install -y lxc-*
yum install -y libcgroup*
yum install -y bridge-utils.x86_64

 橋接網卡:

vim /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=none
NAME=eth0
DEVICE=eth0
ONBOOT=yes
BRIDGE=virbr0
vim /etc/sysconfig/network-scripts/ifcfg-virbr0
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=233.5.5.5

 啟動cgroup

systemctl start cgconfig.service

啟動lxc

systemctl start lxc.service

創建lxc容器

方法一:

# lxc-create -t download -n my-container -- --server mirrors.tuna.tsinghua.edu.cn/lxc-images
lxc-create -t download -n centos6 -- --server mirrors.tuna.tsinghua.edu.cn/lxc-images -d centos -r 6 -a amd64

方法二:

lxc-create -t centos -n test

 修改容器的密碼:

chroot /var/lib/lxc/test/rootfs passwd

創建lxc容器:

lxc-create -t centos -n test

 

 

啟動lxc容器:

lxc-start -n test

 

如果要后台啟動,使用參數-d

lxc-start -d -n test

 

 

登出lxc容器:

logout

克隆lxc容器:

lxc-clone -o test1 -n test2

 

關閉容器:

lxc-stop -n test

 

使用lxc-attache連接主機

lxc-attach -n test

 

刪除一個lxc容器

[admin@localhost lxc]$ sudo lxc-destroy -n test

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM