diff --git a/README.md b/README.md index ffbece6..971713e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Docker — 从入门到实践 -0.8.2 +0.8.3 [Docker](http://www.docker.com) 是个划时代的开源项目,它彻底释放了计算虚拟化的威力,极大提高了应用的运行效率,降低了云计算资源供应的成本! 使用 Docker,可以让应用的部署、测试和分发都变得前所未有的高效和轻松! diff --git a/SUMMARY.md b/SUMMARY.md index 20a18d3..d725988 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -65,11 +65,6 @@ * [工具和示例](advanced_network/example.md) * [编辑网络配置文件](advanced_network/config_file.md) * [实例:创建一个点到点连接](advanced_network/ptp.md) -* [实战案例](cases/README.md) - * [使用 Supervisor 来管理进程](cases/supervisor.md) - * [创建 tomcat\/weblogic 集群](cases/tomcat.md) - * [多台物理主机之间的容器互联](cases/container_connect.md) - * [标准化开发测试和生产环境](cases/environment.md) * [安全](security/README.md) * [内核命名空间](security/kernel_ns.md) * [控制组](security/control_group.md) @@ -133,6 +128,13 @@ * [腾讯云](cloud/qcloud.md) * [阿里云](cloud/alicloud.md) * [小结](cloud/summary.md) +* [实战案例-操作系统](cases/os/README.md) + * [Busybox](cases/os/busybox.md) + * [Alpine](cases/os/alpine.md) + * [Debian\/Ubuntu](cases/os/debian.md) + * [CentOS\/Fedora](cases/os/centos.md) + * [CoreOS](cases/os/coreos.md) + * [本章小结](cases/os/summary.md) * [附录](appendix/README.md) * [附录一:常见问题总结](appendix/faq/README.md) * [附录二:热门镜像介绍](appendix/repo/README.md) diff --git a/cases/README.md b/cases/README.md deleted file mode 100644 index 4f03d4b..0000000 --- a/cases/README.md +++ /dev/null @@ -1,2 +0,0 @@ -#实战案例 -介绍一些典型的应用场景和案例。 diff --git a/cases/container_connect.md b/cases/container_connect.md deleted file mode 100644 index fc72123..0000000 --- a/cases/container_connect.md +++ /dev/null @@ -1,78 +0,0 @@ -## 多台物理主机之间的容器互联(暴露容器到真实网络中) -Docker 默认的桥接网卡是 docker0。它只会在本机桥接所有的容器网卡,举例来说容器的虚拟网卡在主机上看一般叫做 veth*** 而 Docker 只是把所有这些网卡桥接在一起,如下: -``` -[root@opnvz ~]# brctl show -bridge name bridge id STP enabled interfaces -docker0 8000.56847afe9799 no veth0889 - veth3c7b - veth4061 -``` -在容器中看到的地址一般是像下面这样的地址: -``` -root@ac6474aeb31d:~# ip a -1: lo: mtu 1500 qdisc noqueue state UNKNOWN group default - link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 - inet 127.0.0.1/8 scope host lo - valid_lft forever preferred_lft forever - inet6 ::1/128 scope host - valid_lft forever preferred_lft forever -11: eth0: mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 - link/ether 4a:7d:68:da:09:cf brd ff:ff:ff:ff:ff:ff - inet 172.17.0.3/16 scope global eth0 - valid_lft forever preferred_lft forever - inet6 fe80::487d:68ff:feda:9cf/64 scope link - valid_lft forever preferred_lft forever -``` -这样就可以把这个网络看成是一个私有的网络,通过 nat 连接外网,如果要让外网连接到容器中,就需要做端口映射,即 -p 参数。 - -如果在企业内部应用,或者做多个物理主机的集群,可能需要将多个物理主机的容器组到一个物理网络中来,那么就需要将这个网桥桥接到我们指定的网卡上。 - -### 拓扑图 -主机 A 和主机 B 的网卡一都连着物理交换机的同一个 vlan 101,这样网桥一和网桥三就相当于在同一个物理网络中了,而容器一、容器三、容器四也在同一物理网络中了,他们之间可以相互通信,而且可以跟同一 vlan 中的其他物理机器互联。 -![物理拓扑图](../_images/container_connect_topology.png) - -### ubuntu 示例 -下面以 ubuntu 为例创建多个主机的容器联网: -创建自己的网桥,编辑 /etc/network/interface 文件 -``` -auto br0 -iface br0 inet static -address 192.168.7.31 -netmask 255.255.240.0 -gateway 192.168.7.254 -bridge_ports em1 -bridge_stp off -dns-nameservers 8.8.8.8 192.168.6.1 -``` -将 Docker 的默认网桥绑定到这个新建的 br0 上面,这样就将这台机器上容器绑定到 em1 这个网卡所对应的物理网络上了。 - -ubuntu 修改 /etc/default/docker 文件,添加最后一行内容 - -``` -# Docker Upstart and SysVinit configuration file -# Customize location of Docker binary (especially for development testing). -#DOCKER="/usr/local/bin/docker" -# Use DOCKER_OPTS to modify the daemon startup options. -#DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4" - -# If you need Docker to use an HTTP proxy, it can also be specified here. -#export http_proxy="http://127.0.0.1:3128/" - -# This is also a handy place to tweak where Docker's temporary files go. -#export TMPDIR="/mnt/bigdrive/docker-tmp" - -DOCKER_OPTS="-b=br0" -``` - -在启动 Docker 的时候 使用 -b 参数 将容器绑定到物理网络上。重启 Docker 服务后,再进入容器可以看到它已经绑定到你的物理网络上了。 - -``` -root@ubuntudocker:~# docker ps -CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES -58b043aa05eb desk_hz:v1 "/startup.sh" 5 days ago Up 2 seconds 5900/tcp, 6080/tcp, 22/tcp yanlx -root@ubuntudocker:~# brctl show -bridge name bridge id STP enabled interfaces -br0 8000.7e6e617c8d53 no em1 - vethe6e5 -``` -这样就直接把容器暴露到物理网络上了,多台物理主机的容器也可以相互联网了。需要注意的是,这样就需要自己来保证容器的网络安全了。 diff --git a/cases/environment.md b/cases/environment.md deleted file mode 100644 index 30d1ad9..0000000 --- a/cases/environment.md +++ /dev/null @@ -1,14 +0,0 @@ -## 标准化开发测试和生产环境 -对于大部分企业来说,搭建 PaaS 既没有那个精力,也没那个必要,用 Docker 做个人的 sandbox 用处又小了点。 - -可以用 Docker 来标准化开发、测试、生产环境。 - - -![企业应用结构](../_images/enterprise_usage.png) - - -Docker 占用资源小,在一台 E5 128 G 内存的服务器上部署 100 个容器都绰绰有余,可以单独抽一个容器或者直接在宿主物理主机上部署 samba,利用 samba 的 home 分享方案将每个用户的 home 目录映射到开发中心和测试部门的 Windows 机器上。 - -针对某个项目组,由架构师搭建好一个标准的容器环境供项目组和测试部门使用,每个开发工程师可以拥有自己单独的容器,通过 `docker run -v` 将用户的 home 目录映射到容器中。需要提交测试时,只需要将代码移交给测试部门,然后分配一个容器使用 `-v` 加载测试部门的 home 目录启动即可。这样,在公司内部的开发、测试基本就统一了,不会出现开发部门提交的代码,测试部门部署不了的问题。 - -测试部门发布测试通过的报告后,架构师再一次检测容器环境,就可以直接交由部署工程师将代码和容器分别部署到生产环境中了。这种方式的部署横向性能的扩展性也极好。 diff --git a/cases/os/README.md b/cases/os/README.md new file mode 100644 index 0000000..f3c4e95 --- /dev/null +++ b/cases/os/README.md @@ -0,0 +1,9 @@ +# 操作系统 + +目前常用的 Linux 发行版主要包括 Debian/Ubuntu 系列和 CentOS/Fedora 系列。 + +前者以自带软件包版本较新而出名;后者则宣称运行更稳定一些。选择哪个操作系统取决于读者的具体需求。同时,社区还推出了完全基于 Docker 的 Linux 发行版 CoreOS。 + +使用 Docker,读者只需要一个命令就能快速获取一个 linux 发行版镜像,这是以往包括各种虚拟化技术都难以实现的。这些镜像一般都很精简,但是可以支持完整 linux 系统的大部分功能。 + +本章将介绍如何使用 Docker 安装和使用 Busybox、Alphine、Debian/Ubuntu、CentOS/Fedora、CoreOS 等操作系统。 diff --git a/cases/os/_images/alpinelinux-logo.png b/cases/os/_images/alpinelinux-logo.png new file mode 100644 index 0000000..acfe2b1 Binary files /dev/null and b/cases/os/_images/alpinelinux-logo.png differ diff --git a/cases/os/_images/busybox-logo.png b/cases/os/_images/busybox-logo.png new file mode 100644 index 0000000..4d3126a Binary files /dev/null and b/cases/os/_images/busybox-logo.png differ diff --git a/cases/os/_images/centos-logo.png b/cases/os/_images/centos-logo.png new file mode 100644 index 0000000..fec4449 Binary files /dev/null and b/cases/os/_images/centos-logo.png differ diff --git a/cases/os/_images/coreos-login.png b/cases/os/_images/coreos-login.png new file mode 100644 index 0000000..02142e7 Binary files /dev/null and b/cases/os/_images/coreos-login.png differ diff --git a/cases/os/_images/coreos-logo.jpg b/cases/os/_images/coreos-logo.jpg new file mode 100644 index 0000000..dee6e9e Binary files /dev/null and b/cases/os/_images/coreos-logo.jpg differ diff --git a/cases/os/_images/coreos_crt.png b/cases/os/_images/coreos_crt.png new file mode 100644 index 0000000..0260b26 Binary files /dev/null and b/cases/os/_images/coreos_crt.png differ diff --git a/cases/os/_images/coreos_list.png b/cases/os/_images/coreos_list.png new file mode 100644 index 0000000..9896b0a Binary files /dev/null and b/cases/os/_images/coreos_list.png differ diff --git a/cases/os/_images/coreos_run_ip.png b/cases/os/_images/coreos_run_ip.png new file mode 100644 index 0000000..ab121f4 Binary files /dev/null and b/cases/os/_images/coreos_run_ip.png differ diff --git a/cases/os/_images/debian-logo.png b/cases/os/_images/debian-logo.png new file mode 100644 index 0000000..1cfcc65 Binary files /dev/null and b/cases/os/_images/debian-logo.png differ diff --git a/cases/os/_images/docker_version.png b/cases/os/_images/docker_version.png new file mode 100644 index 0000000..479a903 Binary files /dev/null and b/cases/os/_images/docker_version.png differ diff --git a/cases/os/_images/fedora-logo.png b/cases/os/_images/fedora-logo.png new file mode 100644 index 0000000..7dd1589 Binary files /dev/null and b/cases/os/_images/fedora-logo.png differ diff --git a/cases/os/_images/php_pulling.png b/cases/os/_images/php_pulling.png new file mode 100644 index 0000000..189e058 Binary files /dev/null and b/cases/os/_images/php_pulling.png differ diff --git a/cases/os/_images/ubuntu-logo.jpg b/cases/os/_images/ubuntu-logo.jpg new file mode 100644 index 0000000..4138fab Binary files /dev/null and b/cases/os/_images/ubuntu-logo.jpg differ diff --git a/cases/os/_images/vmware_coreos.png b/cases/os/_images/vmware_coreos.png new file mode 100644 index 0000000..1003e4c Binary files /dev/null and b/cases/os/_images/vmware_coreos.png differ diff --git a/cases/os/alpine.md b/cases/os/alpine.md new file mode 100644 index 0000000..c878f19 --- /dev/null +++ b/cases/os/alpine.md @@ -0,0 +1,73 @@ +## Alpine + +### 简介 + +![Apline Linux 操作系统](_images/alpinelinux-logo.png) + +`Alpine` 操作系统是一个面向安全的轻型 `Linux` 发行版。它不同于通常 `Linux` 发行版,`Alpine` 采用了 `musl libc` 和 `busybox` 以减小系统的体积和运行时资源消耗,但功能上比 `busybox` 又完善的多,因此得到开源社区越来越多的青睐。在保持瘦身的同时,`Alpine` 还提供了自己的包管理工具 `apk`,可以通过 `https://pkgs.alpinelinux.org/packages` 网站上查询包信息,也可以直接通过 `apk` 命令直接查询和安装各种软件。 + +`Alpine` 由非商业组织维护的,支持广泛场景的 `Linux`发行版,它特别为资深/重度`Linux`用户而优化,关注安全,性能和资源效能。`Alpine` 镜像可以适用于更多常用场景,并且是一个优秀的可以适用于生产的基础系统/环境。 + +`Alpine` Docker 镜像也继承了 Alpine Linux 发行版的这些优势。相比于其他 `Docker` 镜像,它的容量非常小,仅仅只有 5 MB 左右(对比 Ubuntu 系列镜像接近 200 MB),且拥有非常友好的包管理机制。官方镜像来自 `docker-alpine` 项目。 + +目前 Docker 官方已开始推荐使用 `Alpine` 替代之前的 `Ubuntu` 做为基础镜像环境。这样会带来多个好处。包括镜像下载速度加快,镜像安全性提高,主机之间的切换更方便,占用更少磁盘空间等。 + +下表是官方镜像的大小比较: + +```sh +REPOSITORY TAG IMAGE ID VIRTUAL SIZE +alpine latest 4e38e38c8ce0 4.799 MB +debian latest 4d6ce913b130 84.98 MB +ubuntu latest b39b81afc8ca 188.3 MB +centos latest 8efe422e6104 210 MB +``` + +### 获取并使用官方镜像 + +由于镜像很小,下载时间往往很短,读者可以直接使用 `docker run` 指令直接运行一个 `Alpine` 容器,并指定运行的 Linux 指令,例如: + +```sh +$ docker run alpine echo '123' +123 +``` + +笔者使用 time 工具来测试下在本地没有提前 pull 镜像情况下,执行 echo 命令的时间,仅需要 3 秒左右。 + +```sh +$ time docker run alpine echo '123'Unable to find image 'alpine:latest' locallylatest: Pulling from library/alpine + +e110a4a17941: Pull completeDigest: sha256:3dcdb92d7432d56604d4545cbd324b14e647b313626d99b889d0626de158f73aStatus: Downloaded newer image for alpine:latest123 + +real 0m3.367s user 0m0.040s sys 0m0.007s +``` + +### 迁移至 `Alpine` 基础镜像 + +目前,大部分 Docker 官方镜像都已经支持 Alpine 作为基础镜像,可以很容易进行迁移。 + +例如: + +* ubuntu/debian -> alpine +* python:2.7 -> python:2.7-alpine +* ruby:2.3 -> ruby:2.3-alpine + +另外,如果使用 `Alpine` 镜像替换 `Ubuntu` 基础镜像,安装软件包时需要用 apk 包管理器替换 apt 工具,如 + +```sh +$ apk add --no-cache +``` + +`Alpine` 中软件安装包的名字可能会与其他发行版有所不同,可以在 `https://pkgs.alpinelinux.org/packages` 网站搜索并确定安装包名称。如果需要的安装包不在主索引内,但是在测试或社区索引中。那么可以按照以下方法使用这些安装包。 + +```sh +$ echo "http://dl-4.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories +$ apk --update add --no-cache +``` + +### 相关资源 + +* `Apline` 官网:`http://alpinelinux.org/` +* `Apline` 官方仓库:`https://github.com/alpinelinux` +* `Apline` 官方镜像:`https://hub.docker.com/_/alpine/` +* `Apline` 官方镜像仓库:`https://github.com/gliderlabs/docker-alpine` + diff --git a/cases/os/busybox.md b/cases/os/busybox.md new file mode 100644 index 0000000..1b931d9 --- /dev/null +++ b/cases/os/busybox.md @@ -0,0 +1,114 @@ +## Busybox + +### 简介 + +![Busybox - Linux 瑞士军刀](_images/busybox-logo.png) + +BusyBox 是一个集成了一百多个最常用 Linux 命令和工具(如 cat、echo、grep、mount、telnet 等)的精简工具箱,它只需要几 MB 的大小,很方便进行各种快速验证,被誉为“Linux 系统的瑞士军刀”。 + +BusyBox 可运行于多款 POSIX 环境的操作系统中,如 Linux(包括 Android)、Hurd、FreeBSD 等。 + +### 获取官方镜像 + +在 Docker Hub 中搜索 busybox 相关的镜像。 + +```sh +$ docker search busybox +NAME DESCRIPTION STARS OFFICIAL AUTOMATED +busybox Busybox base image. 755 [OK] +progrium/busybox 63 [OK] +radial/busyboxplus Full-chain, Internet enabled, busybox made... 11 [OK] +odise/busybox-python 3 [OK] +multiarch/busybox multiarch ports of ubuntu-debootstrap 2 [OK] +azukiapp/busybox This image is meant to be used as the base... 2 [OK] +... +``` + +读者可以看到最受欢迎的镜像同时带有 OFFICIAL 标记,说明它是官方镜像。用户使用 docker pull 指令下载镜像 `busybox:latest`: + +```sh +$ docker pull busybox:latest +busybox:latest: The image you are pulling has been verified +e433a6c5b276: Pull complete +e72ac664f4f0: Pull complete +511136ea3c5a: Pull complete +df7546f9f060: Pull complete +Status: Downloaded newer image for busybox:latest +``` + +下载后,可以看到 busybox 镜像只有2.433 MB: + +```sh +$ docker images +REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE +busybox latest e72ac664f4f0 6 weeks ago 2.433 MB +``` + +### 运行 busybox + +启动一个 busybox 容器,并在容器中执行 grep 命令。 + +```sh +$ docker run -it busybox +/ # grep +BusyBox v1.22.1 (2014-05-22 23:22:11 UTC) multi-call binary. + +Usage: grep [-HhnlLoqvsriwFE] [-m N] [-A/B/C N] PATTERN/-e PATTERN.../-f FILE [FILE]... + +Search for PATTERN in FILEs (or stdin) + + -H Add 'filename:' prefix + -h Do not add 'filename:' prefix + -n Add 'line_no:' prefix + -l Show only names of files that match + -L Show only names of files that don't match + -c Show only count of matching lines + -o Show only the matching part of line + -q Quiet. Return 0 if PATTERN is found, 1 otherwise + -v Select non-matching lines + -s Suppress open and read errors + -r Recurse + -i Ignore case + -w Match whole words only + -x Match whole lines only + -F PATTERN is a literal (not regexp) + -E PATTERN is an extended regexp + -m N Match up to N times per file + -A N Print N lines of trailing context + -B N Print N lines of leading context + -C N Same as '-A N -B N' + -e PTRN Pattern to match + -f FILE Read pattern from file +``` + +查看容器内的挂载信息。 + +```sh +/ # mount +rootfs on / type rootfs (rw) +none on / type aufs (rw,relatime,si=b455817946f8505c) +proc on /proc type proc (rw,nosuid,nodev,noexec,relatime) +tmpfs on /dev type tmpfs (rw,nosuid,mode=755) +shm on /dev/shm type tmpfs (rw,nosuid,nodev,noexec,relatime,size=65536k) +devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=666) +sysfs on /sys type sysfs (ro,nosuid,nodev,noexec,relatime) +/dev/disk/by-uuid/b1f2dba7-d91b-4165-a377-bf1a8bed3f61 on /etc/resolv.conf type ext4 (rw,relatime,errors=remount-ro,data=ordered) +/dev/disk/by-uuid/b1f2dba7-d91b-4165-a377-bf1a8bed3f61 on /etc/hostname type ext4 (rw,relatime,errors=remount-ro,data=ordered) +/dev/disk/by-uuid/b1f2dba7-d91b-4165-a377-bf1a8bed3f61 on /etc/hosts type ext4 (rw,relatime,errors=remount-ro,data=ordered) +devpts on /dev/console type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=000) +proc on /proc/sys type proc (ro,nosuid,nodev,noexec,relatime) +proc on /proc/sysrq-trigger type proc (ro,nosuid,nodev,noexec,relatime) +proc on /proc/irq type proc (ro,nosuid,nodev,noexec,relatime) +proc on /proc/bus type proc (ro,nosuid,nodev,noexec,relatime) +tmpfs on /proc/kcore type tmpfs (rw,nosuid,mode=755) +``` + +busybox 镜像虽然小巧,但包括了大量常见的 Linux 命令,读者可以用它快速熟悉 Linux 命令。 + +### 相关资源 + +* `Busybox` 官网:`https://busybox.net/` +* `Busybox` 官方仓库:`https://git.busybox.net/busybox/` +* `Busybox` 官方镜像:`https://hub.docker.com/_/busybox/` +* `Busybox` 官方仓库:`https://github.com/docker-library/busybox` + diff --git a/cases/os/centos.md b/cases/os/centos.md new file mode 100644 index 0000000..c8c1199 --- /dev/null +++ b/cases/os/centos.md @@ -0,0 +1,76 @@ +## CentOS/Fedora + +### CentOS 系统简介 + +CentOS 和 Fedora 都是基于 Redhat 的常见 Linux 分支。CentOS 是目前企业级服务器的常用操作系统;Fedora 则主要面向个人桌面用户。 + +![CentOS 操作系统](_images/centos-logo.png) + +CentOS(Community Enterprise Operating System,中文意思是:社区企业操作系统),它是基于 Red Hat Enterprise Linux 源代码编译而成。由于 CentOS 与 Redhat Linux 源于相同的代码基础,所以很多成本敏感且需要高稳定性的公司就使用 CentOS 来替代商业版 Red Hat Enterprise Linux。CentOS 自身不包含闭源软件。 + +#### 使用 CentOS 官方镜像 + +首先使用 `docker search` 命令来搜索标星至少为 25 的 CentOS 相关镜像。 + +```sh +$ docker search -f stars=25 centos +NAME DESCRIPTION STARS OFFICIAL AUTOMATED +centos The official... 2543 [OK] +jdeathe/centos-ssh 27 [OK] +``` + +使用 docker run 直接运行最新的 CentOS 镜像,并登录 bash。 + +```sh +$ docker run -it centos bash +Unable to find image 'centos:latest' locally +latest: Pulling from library/centos +3d8673bd162a: Pull complete +Digest: sha256:a66ffcb73930584413de83311ca11a4cb4938c9b2521d331026dad970c19adf4 +Status: Downloaded newer image for centos:latest +[root@43eb3b194d48 /]# cat /etc/redhat-release +CentOS Linux release 7.2.1511 (Core) +``` + +### Fedora 系统简介 + +![Fedora 操作系统](_images/fedora-logo.png) + +Fedora 由 Fedora Project 社区开发,红帽公司赞助的 Linux 发行版。它的目标是创建一套新颖、多功能并且自由和开源的操作系统。Fedora 的功能对于用户而言,它是一套功能完备的,可以更新的免费操作系统,而对赞助商 Red Hat 而言,它是许多新技术的测试平台。被认为可用的技术最终会加入到 Red Hat Enterprise Linux中。 + +#### 使用 Fedora 官方镜像 + +首先使用 `docker search` 命令来搜索标星至少为 2 的 Fedora 相关镜像,结果如下。 + +```sh +$ docker search -f stars=2 fedora +NAME DESCRIPTION STARS OFFICIAL AUTOMATED +fedora Official Docker builds of Fedora 433 [OK] +dockingbay/fedora-rust Trusted build of Rust programming language... 3 [OK] +gluster/gluster-fedora Official GlusterFS image [ Fedora 21 + Glu... 3 [OK] +startx/fedora Simple container used for all startx based... 2 [OK] +``` + +使用 docker run 命令直接运行 Fedora 官方镜像,并登录 bash。 + +```sh +$ docker run -it fedora bash +Unable to find image 'fedora:latest' locally +latest: Pulling from library/fedora +2bf01635e2a0: Pull complete +Digest: sha256:64a02df6aac27d1200c2572fe4b9949f1970d05f74d367ce4af994ba5dc3669e +Status: Downloaded newer image for fedora:latest +[root@196ca341419b /]# cat /etc/redhat-release +Fedora release 24 (Twenty Four) +``` + +### 相关资源 + +* `Fedora` 官网:`https://getfedora.org/` +* `Fedora` 官方仓库:`https://github.com/fedora-infra` +* `Fedora` 官方镜像:`https://hub.docker.com/_/fedora/` +* `Fedora` 官方镜像仓库:`https://github.com/fedora-cloud/docker-brew-fedora` +* `CentOS` 官网:`https://getfedora.org/` +* `CentOS` 官方仓库:`https://github.com/CentOS` +* `CentOS` 官方镜像:`https://hub.docker.com/_/centos/` +* `CentOS` 官方镜像仓库:`https://github.com/CentOS/CentOS-Dockerfiles` diff --git a/cases/os/coreos.md b/cases/os/coreos.md new file mode 100644 index 0000000..d88bc31 --- /dev/null +++ b/cases/os/coreos.md @@ -0,0 +1,116 @@ +## CoreOS + +### 简介 + +![CoreOS - 容器化操作系统](_images/coreos-logo.jpg) + +CoreOS 是一个轻量级容器化 Linux 发行版,专为数据中心场景而设计,旨在通过轻量的系统架构和灵活的应用程序部署能力简化数据中心的维护成本和复杂度。CoreOS 作为 Docker 生态圈中的重要一员,日益得到各大云服务商的重视。CoreOS 于 2014 年 7 月首次发布了稳定版本。 + +与其他历史悠久、使用广泛的 Linux 操作系统相比,CoreOS 拥有下面几个优点。 + +* CoreOS 通过容器化(Containerized)的运算环境向应用程序提供运算资源。 + +传统类 Unix 系统往往提供包管理工具。随着系统安装更多的程序,而程序的依赖要求各不相同,这就容易出现“依赖地狱”(Dependency Hell),系统的更新和安装会非常痛苦。 + +CoreOS 的应用程序通过 Docker 运行在容器中,彼此之间共享系统内核和资源,同时互不可见。这种方式使得操作系统、应用程序及运行环境之间的耦合度大大降低。相对于传统的部署方式而言,运维可以更加灵活便捷的在 CoreOS 集群中部署应用程序,同时各运行环境之间的干扰更少,操作系统自身的维护也更加容易。 + +* CoreOS 采用双系统分区(Dual Root Partition)设计。 + +CoreOs 的两个分区在系统运行期间各司其职,它们分别被设置成主动模式和被动模式。主动分区负责系统运行,被动分区负责系统升级。一旦 CoreOS 发布了新版系统,运维只需下载一个完整的系统安装文件至被动分区,然后设置系统下一次重启时从新版本分区启动,将主、被动分区的职能进行调换即可。在 CoreOS 系统运行期间,系统分区被设置成只读状态,这样也确保了 CoreOS 的安全性。CoreOS 的升级过程在默认条件下将自动完成,并且通过 cgroup 对升级过程中使用到的网络和磁盘资源进行限制,这将系统升级所带来的影响降至最低。 + +* CoreOS 使用 Systemd 取代 SysV 作为系统和服务的管理工具。 + +与 SysV 相比,Systemd 不仅可以更好的追踪系统进程,还具备优秀的并行化处理能力。Systemd 将自己的按需启动的特性与 Docker 的快速启动能力相结合,在 CoreOS 集群中大规模部署 Docker Containers 的业务中优势明显。Systemd 引入了“target”的概念,每个 target 应用于一个特定的服务,并且可以通过继承一个已有的 target 来扩展额外的功能,即操作系统对系统上运行的服务拥有更好的控制力。 + +CoreOS 团队还推出了很多有益的工具,包括 etcd, fleet, flannel 等。 + +安装 CoreOS 有几种方法,笔者推荐初学者使用 VMware Workstation 虚拟机方式来运行 CoreOS。VMware Workstation 工具则可以前往其官网`http://www.vmware.com/products/workstation/` 下载获取。 + +### 使用官方镜像 + +#### 获取虚拟机镜像 +从官方网站下载 CoreOS 镜像,地址为 [https:\/\/coreos.com\/releases\/](http://alpha.release.core-os.net/amd64-usr/current/coreos_production_vmware_insecure.zip)。 + +如果读者已经安装 VMware Workstation,则解压镜像包后双击`vmx` 文件: +![虚拟机镜像文件](_images/coreos_list.png)! + +双击`vmx` 文件后,即可启动 CoreOS 虚拟机,如下图所示: + +![启动 CoreOS 虚拟机](_images/vmware_coreos.png) + +*注意:使用免费版 VMware Player 运行 CoreOS 官方镜像时,可能出现无法通过 DHCP 自动获取 IP 地址的问题,读者可配置静态地址或使用 VMware Workstation 来运行 `vmx` 文件。* + +#### 获取地址信息 + +此时CoreOS 系统已经在 VMware Station 中启动,显示登录提示: + +![登录 CoreOS](_images/coreos-login.png) + +直接按回车键,获取当前系统的 IP 地址,如下图所示: + +![获取 IP 地址](_images/coreos_run_ip.png) + +如上图所示,查看此时 CoreOS 的 IP地址是:`192.168.66.128`。 + +#### 使用 SSH 客户端访问镜像 + +笔者以 Windows 环境为例,使用 SecureCRT 工具进行连接。此处读者需要确定: + +* CoreOS 虚拟机的 IP地址 +* CoreOS 虚拟机的文件目录下含有 `insecure_ssh_key` 公钥文件 + +打开 SecureCRT,建立新的 SSH 连接,如下图示: + +![使用 SecureCRT 访问 CoreOS](_images/coreos_crt.png) + +点击 ` 属性` 按钮添加 `insecure_ssh_key` 公钥文件后,即可点击 `连接`。 + +如果连接成功,则读者可以看到命令行页面,读者在命令行中查看 Docker 的版本信息: + +```sh +$ docker version +``` + +结果如下图示: + +![查看 Docker 版本信息](_images/docker_version.png) + +此时,CoreOS 虚拟机已经成功运行,并且读者可以使用 SSH 客户端方便的操作 CoreOS 虚拟机。 Docker 已经内置于 CoreOS 中,读者可以进行各种 Docker 操作,如下图示: + +![使用 Docker 命令](_images/php_pulling.png) + +如果读者的本机环境是 Linux 系统,读者可以使用 SSH 公钥(在解压后的根目录下),直接使用 `ssh` 命令连接 CoreOS 虚拟机,并使用 `ip a` 命令查看 IP 地址信息。 + +如下所示: + +```sh +$ ssh -i ~/insecure_ssh_key core@192.168.6.153 +CoreOS (alpha) +core@localhost ~ $ ls +core@localhost ~ $ docker ps +CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES +core@localhost ~ $ ip a +1: lo: mtu 65536 qdisc noqueue state UNKNOWN + link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 + inet 127.0.0.1/8 scope host lo + valid_lft forever preferred_lft forever + inet6 ::1/128 scope host + valid_lft forever preferred_lft forever +2: enp0s17: mtu 1500 qdisc pfifo_fast state UNKNOWN qlen 1000 + link/ether 00:0c:29:ff:73:46 brd ff:ff:ff:ff:ff:ff + inet 192.168.6.153/20 brd 192.168.15.255 scope global dynamic enp0s17 + valid_lft 604500sec preferred_lft 604500sec + inet6 fe80::20c:29ff:feff:7346/64 scope link + valid_lft forever preferred_lft forever +3: docker0: mtu 1500 qdisc noqueue state DOWN + link/ether 56:84:7a:fe:97:99 brd ff:ff:ff:ff:ff:ff + inet 172.17.42.1/16 scope global docker0 + valid_lft forever preferred_lft forever +core@localhost ~ $ +``` + +### 相关资源 + +* `CoreOS` 官网:`https://coreos.com/` +* `CoreOS` 官方仓库:`https://github.com/coreos/` + diff --git a/cases/os/debian.md b/cases/os/debian.md new file mode 100644 index 0000000..41e986c --- /dev/null +++ b/cases/os/debian.md @@ -0,0 +1,170 @@ +## Debian/Ubuntu +Debian 和 Ubuntu 都是目前较为流行的 Debian 系的服务器操作系统,十分适合研发场景。Docker Hub 上提供了官方镜像,国内各大容器云服务也基本都提供了相应的支持。 + +### Debian 系统简介 + +![Debian 操作系统](_images/debian-logo.png) + +Debian 是由 GPL 和其他自由软件许可协议授权的自由软件组成的操作系统,由Debian 计划(Debian Project)组织维护。Debian 计划是一个独立的、分散的组织,由 3000 人志愿者组成,接受世界多个非盈利组织的资金支持,Software in the Public Interest 提供支持并持有商标作为保护机构。Debian 以其坚守 Unix 和自由软件的精神,以及其给予用户的众多选择而闻名。现时 Debian 包括了超过 25,000 个软件包并支持 12 个计算机系统结构。 + +Debian 作为一个大的系统组织框架,其下有多种不同操作系统核心的分支计划,主要为采用 Linux 核心的 Debian GNU/Linux 系统,其他还有采用 GNU Hurd 核心的 Debian GNU/Hurd 系统、采用 FreeBSD 核心的 Debian GNU/kFreeBSD 系统,以及采用 NetBSD 核心的 Debian GNU/NetBSD 系统。甚至还有利用 Debian 的系统架构和工具,采用 OpenSolaris 核心构建而成的 Nexenta OS 系统。在这些 Debian 系统中,以采用 Linux 核心的 Debian GNU/Linux 最为著名。 + +众多的 Linux 发行版,例如 Ubuntu、Knoppix 和 Linspire 及 Xandros 等,都基于 Debian GNU/Linux。 + +#### 使用 Debian 官方镜像 + +读者可以使用 docker search 搜索 Docker Hub,查找 Debian 镜像: + +```sh +$ docker search debian +NAME DESCRIPTION STARS OFFICIAL AUTOMATED +debian Debian is... 1565 [OK] +neurodebian NeuroDebian... 26 [OK] +armbuild/debian port of debian 8 [OK] +... +``` + +官方提供了大家熟知的 debian 镜像以及面向科研领域的 neurodebian 镜像。 + +可以使用 docker run 直接运行 Debian 镜像。 + +```sh +$ docker run -it debian bash +root@668e178d8d69:/# cat /etc/issue +Debian GNU/Linux 8 +``` + +Debian 镜像很适合作为基础镜像,构建自定义镜像。 + +### Ubuntu 系统简介 + +![Ubuntu 操作系统](_images/ubuntu-logo.jpg) + +Ubuntu 是一个以桌面应用为主的GNU/Linux操作系统,其名称来自非洲南部祖鲁语或豪萨语的“ubuntu”一词(官方译名“友帮拓”,另有“吾帮托”、“乌班图”、“有奔头”或“乌斑兔”等译名)。Ubuntu 意思是“人性”以及“我的存在是因为大家的存在”,是非洲传统的一种价值观,类似华人社会的“仁爱”思想。 Ubuntu 基于 Debian 发行版和 GNOME/Unity 桌面环境,与 Debian 的不同在于它每 6 个月会发布一个新版本,每 2 年推出一个长期支持(Long Term Support,LTS)版本,一般支持 3 年时间。 + +#### 使用 Ubuntu 官方镜像 + +Ubuntu 相关的镜像有很多,这里使用 `-s 10` 参数,只搜索那些被收藏 10 次以上的镜像。 + +```sh +$ docker search -s 10 ubuntu + +NAME DESCRIPTION STARS OFFICIAL AUTOMATED +ubuntu Official Ubuntu base image 840 [OK] +dockerfile/ubuntu Trusted automated Ubuntu (http://www.ubunt... 30 [OK] +crashsystems/gitlab-docker A trusted, regularly updated build of GitL... 20 [OK] +sylvainlasnier/memcached This is a Memcached 1.4.14 docker images b... 16 [OK] +ubuntu-upstart Upstart is an event-based replacement for ... 16 [OK] +mbentley/ubuntu-django-uwsgi-nginx 16 [OK] +ansible/ubuntu14.04-ansible Ubuntu 14.04 LTS with ansible 15 [OK] +clue/ttrss The Tiny Tiny RSS feed reader allows you t... 14 [OK] +dockerfile/ubuntu-desktop Trusted automated Ubuntu Desktop (LXDE) (h... 14 [OK] +tutum/ubuntu Ubuntu image with SSH access. For the root... 12 [OK] +``` +*注意,Docker 1.12 版本中已经不支持 --stars 参数,则可以使用 -f stars=N 参数。* + +根据搜索出来的结果,读者可以自行选择下载镜像并使用。 + +下面以 ubuntu 14.04 为例,演示如何使用该镜像安装一些常用软件。 + +首先使用 `-ti` 参数启动容器,登录 bash,查看 ubuntu 的发行版本号。 + +```sh +$ docker run -ti ubuntu:14.04 /bin/bash +root@7d93de07bf76:/# lsb_release -a +No LSB modules are available. +Distributor ID: Ubuntu +Description: Ubuntu 14.04.1 LTS +Release: 14.04 +Codename: trusty +``` + +当试图直接使用 `apt-get` 安装一个软件的时候,会提示 `E: Unable to locate package`。 + +```sh +root@7d93de07bf76:/# apt-get install curl +Reading package lists... Done +Building dependency tree +Reading state information... Done +E: Unable to locate package curl +``` + +这并非系统不支持 `apt-get` 命令。Docker 镜像在制作时为了精简清除了 apt 仓库信息,因此需要先执行 `apt-get update` 命令来更新仓库信息。更新信息后即可成功通过 apt-get 命令来安装软件。 + +```sh +root@7d93de07bf76:/# apt-get update +Ign http://archive.ubuntu.com trusty InRelease +Ign http://archive.ubuntu.com trusty-updates InRelease +Ign http://archive.ubuntu.com trusty-security InRelease +Ign http://archive.ubuntu.com trusty-proposed InRelease +Get:1 http://archive.ubuntu.com trusty Release.gpg [933 B] +... +``` + +首先,安装 curl 工具。 + +```sh +root@7d93de07bf76:/# apt-get install curl +Reading package lists... Done +Building dependency tree +Reading state information... Done +The following extra packages will be installed: + ca-certificates krb5-locales libasn1-8-heimdal libcurl3 libgssapi-krb5-2 + libgssapi3-heimdal libhcrypto4-heimdal libheimbase1-heimdal + libheimntlm0-heimdal libhx509-5-heimdal libidn11 libk5crypto3 libkeyutils1 + libkrb5-26-heimdal libkrb5-3 libkrb5support0 libldap-2.4-2 + libroken18-heimdal librtmp0 libsasl2-2 libsasl2-modules libsasl2-modules-db + libwind0-heimdal openssl +... +root@7d93de07bf76:/# curl +curl: try 'curl --help' or 'curl --manual' for more information +``` + +接下来,再安装 apache 服务。 + +```sh +root@7d93de07bf76:/# apt-get install -y apache2 +Reading package lists... Done +Building dependency tree +Reading state information... Done +The following extra packages will be installed: + apache2-bin apache2-data libapr1 libaprutil1 libaprutil1-dbd-sqlite3 + libaprutil1-ldap libxml2 sgml-base ssl-cert xml-core +... +``` + +启动这个 apache 服务,然后使用 curl 来测试本地访问。 + +``` +root@7d93de07bf76:/# service apache2 start + * Starting web server apache2 AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message + * +root@7d93de07bf76:/# curl 127.0.0.1 + + + + + + + Apache2 Ubuntu Default Page: It works +