从安装源安装依赖包

从阿里云中安装epel源:

  1. wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
  2. yum install epel-release
  1. yum install debootstrap perl libvirt

从安装源安装lxc

这种方法安装的lxc版本比较低,不好用,建议用后面的编译安装

  1. yum install lxc lxc-templates
  1. ls -l /usr/local/share/lxc/templates/
  2. ls -l /usr/share/lxc/templates/

编译安装

  1. yum install libcap-devel.x86_64

否则会报:

  1. LXC start error : setup_caps: 2337 unknown capability mac_admin
  1. Use lxc-attach or chroot directly into the rootfs to set a root password
  2. or create user accounts

把lxc源代码: https://linuxcontainers.org/downloads/lxc/lxc-2.1.1.tar.gz

解压到/usr/src目录下编译:

  1. ./configure
  2. make
  3. make install

创建一个容器:

  1. lxc-create --template download --name centos

这里会从网上下载,下载时会让你选择使用哪个模板:

  1. lxc-create: centos: lxccontainer.c: lxc_container_new: 4509 Error: centos creation was not completed
  2. Setting up the GPG keyring
  3. Downloading the image index
  4. ---
  5. DIST RELEASE ARCH VARIANT BUILD
  6. ---
  7. alpine 3.4 amd64 default 20171203_17:50
  8. ...
  9. ...
  10. centos 7 amd64 default 20171204_02:16
  11. ...
  12. Distribution:
  13. centos
  14. Release:
  15. 7
  16. Architecture:
  17. amd64

也可以使用这样的命令:

  1. lxc-create -n centos01 -t download -B zfs --zfsroot=zpool/lxc -- --dist centos --release 7 --arch amd64

安装zfs

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

如果安装出错:

  1. Error: Package: zfs-dkms-0.7.5-1.el7_4.noarch (zfs)
  2. Requires: dkms >= 2.2.0.3
  3. Error: Package: spl-dkms-0.7.5-1.el7_4.noarch (zfs)
  4. Requires: dkms >= 2.2.0.2

需要安装epel:

  1. wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
  2. yum update
  1. systemctl enable zfs-import-cache
  2. systemctl enable zfs-mount
  3. systemctl enable zfs-share
  4. systemctl enable zfs-zed
  5. systemctl enable zfs.target

配置编译安装的lxc

创建文件:/usr/local/etc/lxc/lxc.conf

  1. lxc.lxcpath=/lxcroot

在主机上,把网卡配置成桥接,即建br0:

在/etc/sysconfig/network-scripts/ifcfg-eth0

  1. NAME="eth0"
  2. HWADDR="08:00:27:66:0c:0d"
  3. ONBOOT=yes
  4. NETBOOT=yes
  5. UUID="2f815033-d5fa-4dea-ab0a-49bf830b19e8"
  6. IPV6INIT=yes
  7. BOOTPROTO=none
  8. TYPE=Ethernet
  9. USERCTL=no
  10. BRIDGE=br0

在/etc/sysconfig/network-scripts/ifcfg-br0

  1. NAME="br0"
  2. DEVICE="br0"
  3. ONBOOT=yes
  4. IPV6INIT=yes
  5. BOOTPROTO=static
  6. TYPE=Bridge
  7. DELAY=0
  8. IPADDR=192.168.56.21
  9. NETMAST=255.255.255.0
  10. GATEWAY=192.168.56.1
  11. DNS1=114.114.114.114

修改/usr/local/etc/lxc/default.conf的内容,默认让其创建的容器是桥接网卡:

  1. lxc.net.0.type = veth
  2. lxc.net.0.link = br0
  3. lxc.net.0.flags = up
  4. 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上:

  1. zpool create -o atime=off recordsize=8k dedup=on lxcroot /dev/sdb

把zfs的块去重打开,这样可以节省空间:

  1. zfs set dedup=on lxcroot
  1. lxc-create -n centostemplate -t centos -B zfs --zfsroot=lxcroot

如果用我们自己的仓库:

  1. 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中

  1. yum install net-tools sysstat which strace perf
  1. yum groupinstall "Development Tools"
  1. yum install man-pages.noarch

如果使用我们自己的python3,安装python3的依赖:

  1. yum -y install openssl-devel openssl-devel tkinter tcl-devel tk-devel

如果使用pg:

  1. yum install postgresql96-server.x86_64 postgresql96-contrib.x86_64

后续可以把这个centostemplate容器做为模板来复制出新的容器了:

  1. lxc-copy -s -n centostemplate -N pg01

注意:

如果在virtualbox中创建的虚报机,虚拟机的网络使用的是Host-only的网络,然后在此虚拟机中创建出的容器,想让容器能与外部通信,则需要把网卡的模式设置为“混杂”,否则此容器与外部或此网络上的其它虚拟机无法连接。

2 评论  
Stephev · 2L · 2018-10-19 19:51:07

测试

osdba · 1L · 2018-10-19 19:25:02

测试一下评论

添加一条新评论