Loading... Brandone Rhdoes写的《Python网络编程》这本书的代码库中包含了一块拓展内容.它提供了一个由12台机器组成的网络实验环境,每台机器通过一个Docker容器实现.程序库中也包含了一个安装脚本来构建、启动、连接各容器的镜像.本程序的代码库:https://github.com/brandon-rhodes/fopnp/tree/m/playground. (你可以直接clone这个项目) 下图展示了这12台机器以及他们的连接架构.该网络的目的是模拟一个小型的互联网.  如何搭建这个Docker镜像群呢?在这个项目中提供了VirtualBox虚拟机和在你的ubuntu本地搭建Docker这两种方式,但是在这里我只用第二种方式,即在我的ubuntu16.04LTS环境下构建这个Docker集群. 在开始之前你的ubuntu本地环境需要有docker和git. 使用APT安装Docker 由于 `apt` 源使用 HTTPS 以确保软件下载过程中不被篡改。因此,我们首先需要添加使用 HTTPS 传输的软件包以及 CA 证书。 ```shell $ sudo apt-get install \ apt-transport-https \ ca-certificates \ curl \ gnupg-agent \ software-properties-common ``` 为了确认所下载软件包的合法性,需要添加软件源的 `GPG` 密钥。 ```shell # 清华源 $ curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add - # 官方源 # $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - ``` 然后,我们需要向 `sources.list` 中添加 Docker 软件源 ```shell $ sudo add-apt-repository \ "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu \ $(lsb_release -cs) \ stable" # 官方源 # $ sudo add-apt-repository \ # "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ # $(lsb_release -cs) \ # stable" ``` 安装Docker,更新 apt 软件包缓存,并安装 `docker-ce`: ```shell $ sudo apt-get update $ sudo apt-get install docker-ce docker-ce-cli containerd.io ``` 启动Docker ```shell $ sudo systemctl enable docker $ sudo systemctl start docker ``` 建立 `docker` 组 ```shell $ sudo groupadd docker ``` 将当前用户加入 `docker` 组: ```shell $ sudo usermod -aG docker $USER ``` 退出当前终端并重新登录.测试 Docker 是否安装正确 ```shell $ docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world d1725b59e92d: Pull complete Digest: sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788 Status: Downloaded newer image for hello-world:latest Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/ ``` 若能正常输出以上信息,则说明安装成功。 ```shell $ git clone https://github.com/brandon-rhodes/fopnp.git $ cd fopnp/playground ``` 构建五个Docker映像。需要`sudo`使用`root`权限来执行以下步骤,因为它们涉及创建和配置网络接口。 这里需要修改`build.sh`和`launch.sh`内容,注释其中的 ```shell #if ! echo "$(groups)" | grep -q docker #then # sudo adduser nc docker # exec newgrp docker < ./build.sh #fi ``` 然后运行(较长时间) ```shell $ ./build.sh ``` 启动映像. ```shell $ ./launch.sh ``` 完成之后,运行 ```shell ssh -F ssh-config h1 # password is abc123 ``` Last modification:January 18th, 2021 at 01:39 pm © 允许规范转载