1. 安装容器环境 – 以Ubuntu为例

apt install lxc lxc-templates   //必须安装templates, 后面创建容器需要在此查找相关的template

查看templates

ls -al /usr/share/lxc/templates

检查网络接口是否已安装好.

root@li29-8:~# ifconfig
lxcbr0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.3.1  netmask 255.255.255.0  broadcast 0.0.0.0
        inet6 fe80::216:3eff:fe00:0  prefixlen 64  scopeid 0x20
        ether 00:16:3e:00:00:00  txqueuelen 1000  (Ethernet)
        RX packets 17  bytes 1806 (1.8 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 20  bytes 2176 (2.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

LXC默认网桥接口(lxcbr0),可在/etc/lxc/default.conf中配置

root@li29-8:~# cat /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

查看Linux Kernel 对LXC 运行环境支持情况

root@li29-8:~# lxc-checkconfig
Kernel configuration found at /boot/config-4.15.0-32-generic
--- Namespaces ---
Namespaces: enabled
Utsname namespace: enabled
Ipc namespace: enabled
Pid namespace: enabled
User namespace: enabled
Network namespace: enabled

--- Control groups ---
Cgroups: enabled
......

2. 创建并运行容器
创建一个名为test的Ubuntu容器, 容器被安装在/var/lib/lxc/test,根文件系统位于/var/lib/lxc/test/rootfs

lxc-create -n test -t ubuntu

查看容器列表

root@li29-8:~# lxc-ls -f
NAME STATE   AUTOSTART GROUPS IPV4 IPV6 UNPRIVILEGED
test STOPPED 0         -      -    -    false

运行test容器

root@li29-8:~# lxc-start -n test
root@li29-8:~# lxc-ls -f
NAME STATE   AUTOSTART GROUPS IPV4       IPV6 UNPRIVILEGED
test RUNNING 0         -      10.0.3.143 -    false

若需要容器随着Host启动时启动

$ ln -s /var/lib/lxc/test/config /etc/lxc/auto/test.conf

Attach到容器中

root@li29-8:~# lxc-attach -n test
root@test:~# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.3.143  netmask 255.255.255.0  broadcast 10.0.3.255
        inet6 fe80::216:3eff:fe71:28e7  prefixlen 64  scopeid 0x20
        ether 00:16:3e:71:28:e7  txqueuelen 1000  (Ethernet)
        RX packets 85  bytes 201105 (201.1 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 85  bytes 6524 (6.5 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 6  bytes 582 (582.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6  bytes 582 (582.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

root@test:~# ps
  PID TTY          TIME CMD
   85 pts/2    00:00:00 agetty
   90 pts/2    00:00:00 bash
  136 pts/2    00:00:00 ps

3. 停止,销毁容器

lxc-stop -n test       //停止test容器
lxc-destroy -n test    //销毁test容器
分类: LXC/LXD

发表评论

电子邮件地址不会被公开。 必填项已用*标注