1. 程式人生 > >虛擬容器:LXC安裝使用

虛擬容器:LXC安裝使用

文章目錄

概述

容器是最近比較火的一種東西,不管是學習,還是測試,當需要用到多個主機時,就可以用它來虛擬出來,當然比虛擬機器更加快捷方便。lxc是我學習的一種,分享給大家

LXC介紹

LXC: linux containers的簡稱,是一種基於容器的作業系統層次的虛擬化技術。
lxc可以在作業系統上為程序提供虛擬的操作環境,一個虛擬的執行環境就是一個容器。

安裝LXC

安裝依賴

yum install epel-release
yum install debootstrap perl libvirt
yum install libcap-devel.x86_64

下載安裝包進行安裝

將LXC原始碼https://linuxcontainers.org/downloads/lxc/lxc-2.1.1.tar.gz
下載到/usr/src/解壓,並進入目錄

./configure
make
make install

安裝zfs

安裝zfs的軟體包

yum install http://download.zfsonlinux.org/epel/zfs-release.el7_4.noarch.rpm
yum install kernel-devel zfs

若安裝錯誤,則需要

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum update

如果有報版本錯誤,可先將zfs-release.e17的低版本先解除安裝掉

成功後執行

systemctl enable zfs-import-cache
systemctl enable zfs-mount
systemctl enable zfs-share
systemctl enable zfs-zed
systemctl enable zfs.target

配置LXC

建立檔案/usr/local/etc/lxc/lxc.conf

內容為:

lxc.lxcpath=/lxcroot

配置主機的IP

在宿主機上配置/etc/sysconfig/network-scripts/ifcfg-enp0s3(我的是cnetos7.4)

TYPE=Ethernet
#BOOTPROTO=dhcp
BOOTPROTO=none
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME="enp0s3"
DEVICE=enp0s3
ONBOOT=yes
BRIDGE=br0

重點在於將BRIDGE=br0,BOOTPROTO=none,將系統原先的IP配置先去掉

配置br0

建立/etc/sysconfig/network-scripts/ifcfg-br0

NAME="br0"
DEVICE="br0"
ONBOOT=yes
IPV6INIT=yes
BOOTPROTO=static
TYPE=Bridge
DELAY=0
IPADDR=192.168.0.96
NETMAST=255.255.255.0
GATEWAY=192.168.0.1
DNS1=114.114.114.114

重點在於TYPE=Bridge,還有你配置的IP

配置好後將網路重啟生效

修改LXC建立容器的配置檔案

cat /usr/local/etc/lxc/default.conf

lxc.net.0.type = veth
lxc.net.0.link = lxcbr0
lxc.net.0.flags = up
lxc.net.0.hwaddr = 00:16:3e:xx:xx:xx

將"lxc.net.0.link = lxcbr0”改為“lxc.net.0.link = br0”

再安裝幾個軟體

yum install net-tools sysstat which
yum groupinstall "Development Tools

建立zfs池

最好新增一塊硬碟

新增一塊硬碟在虛擬機器上,我這裡新添了一個10G的sdb盤

[[email protected] lxcroot]# fdisk -l|grep sd
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Disk /dev/sda: 8589 MB, 8589934592 bytes, 16777216 sectors
/dev/sda1   *        2048    11720703     5859328   83  Linux
/dev/sda2        11720704    15626239     1952768   82  Linux swap / Solaris
Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors

用zpool命令將該盤做成zfs檔案系統

使用以下命令

zpool  create  lxcroot   /dev/sdb

會自動將sdb盤做成zfs檔案系統並且在根下建立此目錄,並且將盤掛到該目錄

[[email protected] lxcroot]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1       5.6G  3.5G  2.2G  62% /
devtmpfs        3.9G     0  3.9G   0% /dev
tmpfs           3.9G     0  3.9G   0% /dev/shm
tmpfs           3.9G  8.4M  3.9G   1% /run
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
tmpfs           783M     0  783M   0% /run/user/0
lxcroot         9.3G  885M  8.4G  10% /lxcroot

建立容器

建立指定型別的容器

建立容易命令-t指定容器型別,-n指定名字

[[email protected] ~]# lxc-create -t centos -n test01
Host CPE ID from /etc/os-release: cpe:/o:centos:centos:7
Checking cache download in /usr/local/var/cache/lxc/centos/x86_64/7/rootfs ... 
Cache found. Updating...
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.shu.edu.cn
 * updates: mirrors.shu.edu.cn
No packages marked for update
Loaded plugins: fastestmirror
Cleaning repos: base extras updates
0 package files removed
Update finished
Copy /usr/local/var/cache/lxc/centos/x86_64/7/rootfs to /lxcroot/test01/rootfs ... 
Copying rootfs to /lxcroot/test01/rootfs ...
sed: can't read /lxcroot/test01/rootfs/etc/init/tty.conf: No such file or directory
Storing root password in '/lxcroot/test01/tmp_root_pass'
Expiring password for user root.
passwd: Success
sed: can't read /lxcroot/test01/rootfs/etc/rc.sysinit: No such file or directory
sed: can't read /lxcroot/test01/rootfs/etc/rc.d/rc.sysinit: No such file or directory

Container rootfs and config have been created.
Edit the config file to check/enable networking setup.

The temporary root password is stored in:

        '/lxcroot/test01/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 /lxcroot/test01/rootfs passwd

修改容易登入密碼

生成的容器會有預設密碼,可以修改為自定義密碼

[[email protected] ~]#  chroot /lxcroot/test01/rootfs passwd
Changing password for user root.
New password: 
BAD PASSWORD: The password contains the user name in some form
Retype new password: 
passwd: all authentication tokens updated successfully.

啟動容器

[[email protected] test01]# lxc-start -d -n test01

可以檢視容器的狀態嗎,會分配IP

[[email protected] test01]# lxc-info -n test01
Name:           test01
State:          RUNNING
PID:            25134
IP:             192.168.0.175
CPU use:        1.31 seconds
BlkIO use:      0 bytes
Memory use:     38.75 MiB
KMem use:       0 bytes
Link:           vethMRND23
 TX bytes:      2.00 KiB
 RX bytes:      5.18 KiB
 Total bytes:   7.18 KiB

此時便可以利用工具通過IP連線到剛剛建立的主機上進行操作了

本次我已經建立的主機資訊

[[email protected] test01]# lxc-ls -f
NAME           STATE   AUTOSTART GROUPS IPV4          IPV6 
centos         STOPPED 0         -      -             -    
centostemplate STOPPED 0         -      -             -    
test           RUNNING 0         -      192.168.0.174 -    
test01         RUNNING 0         -      192.168.0.175 -    

LXC的常用命令

lxc-ls -f 檢視LXC已經建立的主機資訊

lxc-start -n name 開啟一個容器

lxc-destroy -n name 徹底刪除一個容器,與之相關的一切資料都不可用

lxc-stop -n name 關閉一個容器。與lxc-start類似,要stop的容器必須是已經lxc-create並且lxc-start的

lxc-freeze -n name 凍結該容器所有的程序。程序會被鎖定,直到lxc-unfreeze進行解鎖

lxc-unfreeze -n name 用於解除被lxc-freeze凍結的容器程序。