从安装源安装依赖包
从阿里云中安装epel源:
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum install epel-release
yum install debootstrap perl libvirt
从安装源安装lxc
这种方法安装的lxc版本比较低,不好用,建议用后面的编译安装
yum install lxc lxc-templates
ls -l /usr/local/share/lxc/templates/
ls -l /usr/share/lxc/templates/
编译安装
yum install libcap-devel.x86_64
否则会报:
LXC start error : setup_caps: 2337 unknown capability mac_admin
Use lxc-attach or chroot directly into the rootfs to set a root password
or create user accounts
把lxc源代码: https://linuxcontainers.org/downloads/lxc/lxc-2.1.1.tar.gz
解压到/usr/src目录下编译:
./configure
make
make install
创建一个容器:
lxc-create --template download --name centos
这里会从网上下载,下载时会让你选择使用哪个模板:
lxc-create: centos: lxccontainer.c: lxc_container_new: 4509 Error: centos creation was not completed
Setting up the GPG keyring
Downloading the image index
---
DIST RELEASE ARCH VARIANT BUILD
---
alpine 3.4 amd64 default 20171203_17:50
...
...
centos 7 amd64 default 20171204_02:16
...
Distribution:
centos
Release:
7
Architecture:
amd64
也可以使用这样的命令:
lxc-create -n centos01 -t download -B zfs --zfsroot=zpool/lxc -- --dist centos --release 7 --arch amd64
安装zfs
yum install http://download.zfsonlinux.org/epel/zfs-release.el7_4.noarch.rpm
yum install kernel-devel zfs
如果安装出错:
Error: Package: zfs-dkms-0.7.5-1.el7_4.noarch (zfs)
Requires: dkms >= 2.2.0.3
Error: Package: spl-dkms-0.7.5-1.el7_4.noarch (zfs)
Requires: dkms >= 2.2.0.2
需要安装epel:
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum update
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
在主机上,把网卡配置成桥接,即建br0:
在/etc/sysconfig/network-scripts/ifcfg-eth0
NAME="eth0"
HWADDR="08:00:27:66:0c:0d"
ONBOOT=yes
NETBOOT=yes
UUID="2f815033-d5fa-4dea-ab0a-49bf830b19e8"
IPV6INIT=yes
BOOTPROTO=none
TYPE=Ethernet
USERCTL=no
BRIDGE=br0
在/etc/sysconfig/network-scripts/ifcfg-br0
NAME="br0"
DEVICE="br0"
ONBOOT=yes
IPV6INIT=yes
BOOTPROTO=static
TYPE=Bridge
DELAY=0
IPADDR=192.168.56.21
NETMAST=255.255.255.0
GATEWAY=192.168.56.1
DNS1=114.114.114.114
修改/usr/local/etc/lxc/default.conf的内容,默认让其创建的容器是桥接网卡:
lxc.net.0.type = veth
lxc.net.0.link = br0
lxc.net.0.flags = up
lxc.net.0.hwaddr = 00:16:3e:xx:xx:xx
实际上是把上面的“lxc.net.0.link = virtbr0”改成“lxc.net.0.link = br0”
在zfs上创建容器
创建一个zpool,名称为lxcroot,前面把lxc的根目录指向的是lxcroot,这样lxc就会把容器创建到这个ZFS上:
zpool create -o atime=off recordsize=8k dedup=on lxcroot /dev/sdb
把zfs的块去重打开,这样可以节省空间:
zfs set dedup=on lxcroot
lxc-create -n centostemplate -t centos -B zfs --zfsroot=lxcroot
如果用我们自己的仓库:
lxc-create -n centostemplate -B zfs --zfsroot=lxcroot -t centos -- -R 7.4 --repo=http://192.168.0.4/cobbler/repo_mirror/Centos-7.4-distro-x86_64/
配置容器
进入容器模板centostemplate中
yum install net-tools sysstat which strace perf
yum groupinstall "Development Tools"
yum install man-pages.noarch
如果使用我们自己的python3,安装python3的依赖:
yum -y install openssl-devel openssl-devel tkinter tcl-devel tk-devel
如果使用pg:
yum install postgresql96-server.x86_64 postgresql96-contrib.x86_64
后续可以把这个centostemplate容器做为模板来复制出新的容器了:
lxc-copy -s -n centostemplate -N pg01
注意:
如果在virtualbox中创建的虚报机,虚拟机的网络使用的是Host-only的网络,然后在此虚拟机中创建出的容器,想让容器能与外部通信,则需要把网卡的模式设置为“混杂”,否则此容器与外部或此网络上的其它虚拟机无法连接。
测试
测试一下评论