LXC 为 Linux Container 的简写。可以提供轻量级的虚拟化,以便隔离进程和资源,而且不需要提供指令解释机 制以及全虚拟化的其他复杂性。容器有效地将由单个操作系统管理的资源划分到孤立的组中,以更好地在孤立的组 之间平衡有冲突的资源使用需求。下图是 KVM 虚拟机和容器之间的区别:
KVM Container
APP
Guest OS
Hypervisor APP
Host OS Host OS
可以看出来,容器中的的程序是直接运行在 Host OS 上,省去了 Guest OS 和 hypervisor 层,所以效 率大大提高。与传统虚拟化技术相比,它的优势在于: 1)与宿主机使用同一个内核,性能损耗小; 2)不需要指令级模拟; 3)不需要即时(Just-in-time)编译; 4)容器可以在 CPU 核心的本地运行指令,不需要任何专门的解释机制; 5)避免了准虚拟化和系统调用替换中的复杂性; 6)轻量级隔离,在隔离的同时还提供共享机制,以实现容器与宿主机的资源共享。
3 LXC 容器的搭建
3.1 内核编译选项
内核版本 v4.19.1,为了使系统支持 LXC,内核必须选上如下选项。 General setup ---> <*> Kernel .config support [*] Enable access to .config through /proc/config.gz Kernel Performance Events And Counters ─> [*] Kernel performance events and counters [*] Control Group support ---> [*] Memory controller
2
[*] IO controller [*] CPU controller ---> [*] PIDs controller [*] RDMA controller [*] Freezer controller [*] Cpuset controller [*] Include legacy /proc//cpuset file [*] Device controller [*] Simple CPU accounting controller [*] Perf controller [*] Namespaces support ---> [*] UTS namespace [*] IPC namespace [*] User namespace [*] PID Namespaces [*] Network namespace [*] Checkpoint/restore support [*] Networking support ---> --- Networking support Networking options ---> Packet socket Packet: sockets monitoring interface Unix domain sockets UNIX: socket monitoring interface Transformation user configuration interface PF_KEY sockets [*] TCP/IP networking [*] IP: kernel level autoconfiguration [*] IP: DHCP support [*] IP: BOOTP support [*] IP: RARP support -*- IP: TCP syncookie support Virtual (secure) IP: tunneling IP: IPsec transport mode IP: IPsec tunnel mode IP: IPsec BEET mode INET: socket monitoring interface UDP: socket monitoring interface [*] TCP: advanced congestion control ---> Binary Increase Congestion (BIC) control <*> CUBIC TCP TCP Westwood+ H-TCP The IPv6 protocol --->
3
IPv6: IPsec transport mode IPv6: IPsec tunnel mode IPv6: IPsec BEET mode IPv6: IPv6-in-IPv4 tunnel (SIT driver) [*] Network packet filtering framework (Netfilter) ---> [*] Advanced netfilter configuration Bridged IP/ARP packets filtering Core Netfilter Configuration ---> [*] Netfilter ingress support {M} Netfilter NFACCT over NFNETLINK interface {M} Netfilter NFQUEUE over NFNETLINK interface {M} Netfilter LOG over NFNETLINK interface {M} Netfilter OSF over NFNETLINK interface Netfilter connection tracking support -*- Connection mark tracking support [*] Connection tracking zones [*] Supply CT list in procfs (OBSOLETE) [*] Connection tracking events [*] Connection tracking timeout [*] Connection tracking timestamping -*- Connection tracking labels [*] DCCP protocol connection tracking support [*] SCTP protocol connection tracking support [*] UDP-Lite protocol connection tracking support Amanda backup protocol support FTP protocol support H.323 protocol support IRC protocol support NetBIOS name service protocol support SNMP service protocol support PPtP protocol support SANE protocol support SIP protocol support TFTP protocol support Connection tracking netlink interface Connection tracking timeout tuning via Netlink Connection tracking helpers in user-space via Netlink [*] NFQUEUE and NFLOG integration with Connection Tracking Netfilter nf_tables support Netfilter nf_tables set infrastructure [*] Netfilter nf_tables mixed IPv4/IPv6 tables support [*] Netfilter nf_tables netdev tables support Netfilter nf_tables number generator module Netfilter nf_tables conntrack module
4
Netfilter nf_tables counter module Netfilter nf_tables connlimit module Netfilter nf_tables log module Netfilter nf_tables limit module Netfilter nf_tables masquerade support Netfilter nf_tables redirect support Netfilter nf_tables nat module Netfilter nf_tables tunnel module Netfilter nf_tables stateful object reference module Netfilter nf_tables queue module Netfilter nf_tables quota module Netfilter nf_tables reject support Netfilter nf_tables hash module Netfilter nf_tables socket match support Netfilter nf_tables passive OS fingerprint support Netfilter nf_tables tproxy support -M- Netfilter packet duplication support Netfilter nf_tables netdev packet duplication support Netfilter flow table mixed IPv4/IPv6 module Netfilter flow table module {M} Netfilter Xtables support (required for ip_tables) *** Xtables combined modules *** -M- nfmark target and match support -M- ctmark target and match support set target and match support *** Xtables targets *** AUDIT target support CHECKSUM target support "CLASSIFY" target support "CONNMARK" target support "DSCP" and "TOS" target support -M- "HL" hoplimit target support "HMARK" target support IDLETIMER target support LOG target support "MARK" target support -M- "SNAT and DNAT" targets support -M- "NETMAP" target support "NFLOG" target support "NFQUEUE" target Support -M- "RATEEST" target support -M- REDIRECT target support "TEE" - packet cloning to alternate destination "TPROXY" target transparent proxying support
5
"TCPMSS" target support "TCPOPTSTRIP" target support *** Xtables matches *** "addrtype" address type match support "bpf" match support "control group" match support "cluster" match support "comment" match support "connbytes" per-connection counter match support "connlabel" match support "connlimit" match support "connmark" connection mark match support "conntrack" connection tracking match support "cpu" match support "dccp" protocol match support "devgroup" match support "dscp" and "tos" match support -M- "ecn" match support "esp" match support "hashlimit" match support "helper" match support -M- "hl" hoplimit/TTL match support "ipcomp" match support "iprange" address range match support "ipvs" match support "l2tp" match support "length" match support "limit" match support "mac" address match support "mark" match support "multiport" Multiple port match support "nfacct" match support "osf" Passive OS fingerprint match "owner" match support IPsec "policy" match support "physdev" match support "pkttype" packet type match support "quota" match support "rateest" match support "realm" match support "recent" match support "sctp" protocol match support "socket" match support "state" match support
6
"statistic" match support "string" match support "tcpmss" match support "time" match support "u32" match support IP set support --->全选 IP virtual server support --->全选 IP: Netfilter Configuration --->全选 IPv6: Netfilter Configuration --->全选 [*] Ethernet Bridge nf_tables support ---> Netfilter nf_tables bridge reject support Ethernet Bridge tables (ebtables) support --->全选 802.1d Ethernet Bridging 802.1Q/802.1ad VLAN Support NETLINK: socket monitoring interface [*] Network priority cgroup -*- Network classid cgroup Device Drivers ---> [*] Network device support ---> MAC-VLAN support Virtual ethernet pair device File systems ---> FUSE (Filesystem in Userspace) support