热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

宋宝华:用systemd-nspawn运行最轻量级容器

systemd-nspawn可以创建最轻量级的容器(ns的意思就是namespace),本文的实验平台是Ubuntu16.04,x86_64机器。本文的目的是:在Ubuntu中用syst

systemd-nspawn可以创建最轻量级的容器(ns的意思就是namespace),本文的实验平台是Ubuntu 16.04,x86_64机器。本文的目的是:在Ubuntu中用systemd-nspawn启动一个容器,运行Debian操作系统(Jessie),并在主机使用machinectl进行管理。


本文阅读难度:

本文整体质量:

阅读时间: 5分钟

相关阅读: 宋宝华:Docker 最初的2小时(Docker从入门到入门)

关注Linuxer,关注原创技术文章


640?wx_fmt=png


安装

systemd-container包含所有用于启动和管理容器和虚拟机的工具;而debootstrap则是debian/ubuntu下用于构建一套基本的根文件系统的工具。

sudo apt install debootstrap

sudo apt install systemd-container

创建基本文件系统

debootstrap生成的根文件系统符合Linux文件系统标准(FHS),包含了/boot、/etc、/bin、/usr等目录。

sudo debootstrap --arch=amd64 jessie /var/lib/machines/container-deb/

不翻墙超级慢(10几分钟只下载1MB),如果能翻墙是最好的:

proxychains debootstrap --arch=amd64 jessie /var/lib/machines/container-deb/

中途会下载各种构建基本文件系统要的包:

0?wx_fmt=png

0?wx_fmt=png

最后得到的文件系统的size如下:

root@ubuntu:/var/lib/machines/container-deb# du -sh

295M.

运行容器

# systemd-nspawn -D /var/lib/machines/container-deb/ --machine deb_container 

Spawning container deb_container on /var/lib/machines/container-deb.

...

root@deb_container:~# 

这样我们就把容器deb_container运行在了/var/lib/machines/container-deb/这个文件系统上了。

在容器内运行ps,发现容器内只有bash一个进程。

root@deb_container:~# ps aux

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

root         1  0.0  0.1  20264  3316 ?        Ss   05:00   0:00 -bash

root         5  0.0  0.0  17500  2044 ?        R+   05:00   0:00 ps aux

按下ctrl+]]]退出这个容器,再次启动容器,设置passwd:

0?wx_fmt=gif

按下ctrl+]]]退出容器,并用-b参数重新启动之:

# systemd-nspawn -D /var/lib/machines/container-deb/ --machine deb_container -b

Spawning container deb_container on /var/lib/machines/container-deb.

...

Detected virtualization 'systemd-nspawn'.

Detected architecture 'x86-64'.

[  OK  ] Reached target Remote File Systems (Pre).

[  OK  ] Reached target Encrypted Volumes.

[  OK  ] Reached target Paths.

...

[  OK  ] Started Update UTMP about System Runlevel Changes.


Debian GNU/Linux 8 ubuntu console


用root账户和上一步设置的密码login:

ubuntu login: root

Password: 

Linux ubuntu 4.4.0-101-generic #124-Ubuntu SMP Fri Nov 10 18:29:59 UTC 2017 x86_64


...

root@ubuntu:~# 

用-b会导致容器内也启动systemd:

root@ubuntu:/proc# ps aux

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND

root         1  0.0  0.1  27872  4180 ?        Ss   04:57   0:00 /lib/systemd/systemd

root        17  0.0  0.2  32964  6208 ?        Ss   04:57   0:00 /lib/systemd/systemd-journald

root        55  0.0  0.0  25904  2320 ?        Ss   04:57   0:00 /usr/sbin/cron -f

root        57  0.0  0.1 262884  3420 ?        Ssl  04:57   0:00 /usr/sbin/rsyslogd -n

root        60  0.0  0.0  63316  2840 console  Ss   04:57   0:00 /bin/login --     

root        67  0.0  0.1  20232  3256 console  S    04:57   0:00 -bash

root        76  0.0  0.0  17500  2112 console  R+   04:59   0:00 ps aux

管理容器

在主机的另外一个terminal中查看容器的状态:

root@ubuntu:~# machinectl list

MACHINE       CLASS     SERVICE       

deb_container container systemd-nspawn


1 machines listed.


root@ubuntu:~# machinectl status deb_container

deb_container

           Since: Thu 2017-12-07 05:00:10 PST; 2min 7s ago

          Leader: 28524 (bash)

         Service: systemd-nspawn; class container

            Root: /var/lib/machines/container-deb

              OS: Debian GNU/Linux 8 (jessie)

            Unit: machine-deb_container.scope

                  └─28524 -bash


Dec 07 05:00:10 ubuntu systemd[1]: Started Container deb_container.


苹果用户打赏


0?wx_fmt=png

安卓用户打赏



推荐阅读
author-avatar
kyle_G_476
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有