容器技術之LXC

来源:https://www.cnblogs.com/qiuhom-1874/archive/2020/05/16/12901493.html
-Advertisement-
Play Games

什麼是容器?在生活中我們常見的容器有各種瓶瓶罐罐、各種能夠容納其它物料的東西叫容器;容器的特點就是有著很好的隔離作用,使得不同的物料互相隔離;除此之外容器還方便運輸、方便儲存;這是生活中所說的容器,以及它的特點;在電腦領域中,所謂容器不外乎也有同生活中的容器的特點,隔離,方便“運輸”(電腦中的... ...


  什麼是容器?在生活中我們常見的容器有各種瓶瓶罐罐、各種能夠容納其它物料的東西叫容器;容器的特點就是有著很好的隔離作用,使得不同的物料互相隔離;除此之外容器還方便運輸、方便儲存;這是生活中所說的容器,以及它的特點;在電腦領域中,所謂容器不外乎也有同生活中的容器的特點,隔離,方便“運輸”(電腦中的運輸我們叫移植,從系統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容器的簡單管理;


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 最近寫了幾個有關RESTful的API相關內容,也談談對常見問題的自己的理解。 1.什麼是RESTful 詳情可以看 "http://www.ruanyifeng.com/blog/2011/09/restful.html" 。 簡單可以這麼理解,使用 去代表資源,使用HTTP VERB(GET P ...
  • 項目介紹 此個人博客項目底層基於 "ABP Framework" (不完全依賴)搭建項目 和免費開源跨平臺的 .NET Core 3.1 開發,可作為 .NET Core 入門項目進行學習,支持各種主流資料庫(SqlServer、MySQL、PostgreSql、Sqlite)接入,介面遵循 RES ...
  • 最近在寫一個導入工具時用到了一個新的東西Quartz 計時器 ,timer有的Quartz 基本都有,所以用Quartz 來寫了下代碼,並記錄。 1、首先需要引用GuGet程式包,搜索' quartz ' ,下載安裝到程式。 2、直接上代碼,創建job,trigger等方法 job傳參: job.J ...
  • 一、概念名稱 Windows服務(即以前的 NT 服務),使您能夠創建在它們自己的Windows會話中可長時間運行的可執行應用程式。這些服務可以在電腦啟動時自動啟動,可以暫停和重新啟動而且不顯示任何用戶界面。這種服務非常適合在伺服器上使用,或任何時候,為了不影響在同一臺電腦上工作的其他用戶,需要 ...
  • 需求提出: GridControl表格控制項的使用過程中,如果表格能根據單元格數據的範圍,擁有不同的Appearance,包括前景色,背景色,圖標甚至進度條等,表格的表現力將大大增加。此時可在表格的設計視窗添加對應的Format Rules,設置相應範圍及外觀即可。 此處針對的數據類型基本屬於數值類型 ...
  • 用戶管理 1 選擇 1.1 增加一個新用戶admin,許可權是root?(A) ​ A.useradd u 0 o admin B.useradd g 0 o admin C.useradd g 1 o admin D.useradd u 1 o admin 1.2 普通用戶的ID一般是(BC) ​ ...
  • 磁碟管理 1 選擇 1 若一臺電腦的記憶體為8GB,則交換分區的大小通常是(c) A.64GB B.128GB C.16GB D.32GB 2 若一臺電腦的記憶體為128M,則交換分區的大小通常為(C) A.64M B.128M C.256M D.512M 3在安裝Linux操作系統時,必須創建的兩 ...
  • 如何從安裝了Windows的工作電腦連遠程接到Linux伺服器?其實有很多軟體,比如 PuTTY、XShell、CRT、MobaXterm等等。不過還是 PuTTY最簡單易用、無需安裝、並且開源免費。PuTTY其實是一個軟體套裝,裡邊除了最常用的putty之外,還包含了像 pscp、psftp等可以 ...
一周排行
    -Advertisement-
    Play Games
  • GoF之工廠模式 @目錄GoF之工廠模式每博一文案1. 簡單說明“23種設計模式”1.2 介紹工廠模式的三種形態1.3 簡單工廠模式(靜態工廠模式)1.3.1 簡單工廠模式的優缺點:1.4 工廠方法模式1.4.1 工廠方法模式的優缺點:1.5 抽象工廠模式1.6 抽象工廠模式的優缺點:2. 總結:3 ...
  • 新改進提供的Taurus Rpc 功能,可以簡化微服務間的調用,同時可以不用再手動輸出模塊名稱,或調用路徑,包括負載均衡,這一切,由框架實現並提供了。新的Taurus Rpc 功能,將使得服務間的調用,更加輕鬆、簡約、高效。 ...
  • 本章將和大家分享ES的數據同步方案和ES集群相關知識。廢話不多說,下麵我們直接進入主題。 一、ES數據同步 1、數據同步問題 Elasticsearch中的酒店數據來自於mysql資料庫,因此mysql數據發生改變時,Elasticsearch也必須跟著改變,這個就是Elasticsearch與my ...
  • 引言 在我們之前的文章中介紹過使用Bogus生成模擬測試數據,今天來講解一下功能更加強大自動生成測試數據的工具的庫"AutoFixture"。 什麼是AutoFixture? AutoFixture 是一個針對 .NET 的開源庫,旨在最大程度地減少單元測試中的“安排(Arrange)”階段,以提高 ...
  • 經過前面幾個部分學習,相信學過的同學已經能夠掌握 .NET Emit 這種中間語言,並能使得它來編寫一些應用,以提高程式的性能。隨著 IL 指令篇的結束,本系列也已經接近尾聲,在這接近結束的最後,會提供幾個可供直接使用的示例,以供大伙分析或使用在項目中。 ...
  • 當從不同來源導入Excel數據時,可能存在重覆的記錄。為了確保數據的準確性,通常需要刪除這些重覆的行。手動查找並刪除可能會非常耗費時間,而通過編程腳本則可以實現在短時間內處理大量數據。本文將提供一個使用C# 快速查找並刪除Excel重覆項的免費解決方案。 以下是實現步驟: 1. 首先安裝免費.NET ...
  • C++ 異常處理 C++ 異常處理機制允許程式在運行時處理錯誤或意外情況。它提供了捕獲和處理錯誤的一種結構化方式,使程式更加健壯和可靠。 異常處理的基本概念: 異常: 程式在運行時發生的錯誤或意外情況。 拋出異常: 使用 throw 關鍵字將異常傳遞給調用堆棧。 捕獲異常: 使用 try-catch ...
  • 優秀且經驗豐富的Java開發人員的特征之一是對API的廣泛瞭解,包括JDK和第三方庫。 我花了很多時間來學習API,尤其是在閱讀了Effective Java 3rd Edition之後 ,Joshua Bloch建議在Java 3rd Edition中使用現有的API進行開發,而不是為常見的東西編 ...
  • 框架 · 使用laravel框架,原因:tp的框架路由和orm沒有laravel好用 · 使用強制路由,方便介面多時,分多版本,分文件夾等操作 介面 · 介面開發註意欄位類型,欄位是int,查詢成功失敗都要返回int(對接java等強類型語言方便) · 查詢介面用GET、其他用POST 代碼 · 所 ...
  • 正文 下午找企業的人去鎮上做貸後。 車上聽同事跟那個司機對罵,火星子都快出來了。司機跟那同事更熟一些,連我在內一共就三個人,同事那一手指桑罵槐給我都聽愣了。司機也是老社會人了,馬上聽出來了,為那個無辜的企業經辦人辯護,實際上是為自己辯護。 “這個事情你不能怪企業。”“但他們總不能讓銀行的人全權負責, ...