
一、关于 Docker
Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从 Apache2.0 协议开源。
Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级、可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口(类似 iPhone 的 app),更重要的是容器性能开销极低。
因为对比传统虚拟机有启动快、性能提升、运维成本等等绝对优势,越来越受开发运维童鞋的青睐。
1)bridge 模式,–net=bridge(默认)
2)host 模式,–net=host
3)container 模式 使用–net =container:指定容器名
4)none 模式,–net=none
5)用户自定义模式
实验部分:
环境:VMware 安装 Centos7 虚拟机。
首先在 Centos 虚拟机中安装容器
yum -y install docker-io ####安装 docker

启动容器服务并检查启动状态 systemctl start docker

启动 docker 服务后会发现主机多了一个 docker0 网卡
(PS:目前本机是通过 192.168.32.129 这个地址可以直接上网的)。

我们启动一个 Centos 的容器并进入 bash,由于我们没有本地没有 centos 的镜像文件,默认就直接从 Docker 的官网镜像仓库下载。
[root@cesrc ~]# docker run -itd centos /bin/bash

查看启动的容器
[root@cesrc ~]# docker ps –a

进入容器里面,查看 IP 配置。
[root@cesrc ~]# docker attach b670

默认没有 ifconfig 命令,yum 安装下 net-tools
yum install net-tools

查看容器的 ip 和路由配置,容器分到了与 docker0 同段地址并将默认路由指向 docker0


此容器可以直接上网

iptables -t nat –vnL,查看 docker 生成的 iptables,可以看出相当于 docker0 对容器做了 SNAT,

再启动一个容器
docker run -itd --name centos2 centos /bin/bash

此主机上的容器间可以直接通讯

此模式的网络工作模式如下图所示

容器将不会虚拟出自己的网卡,配置自己的 IP 等,而是直接使用宿主机的 IP 和端口,但是文件系统是和宿主机隔离的
docker run -itd --net=host --name host-rq centos /bin/bash

进入容器

容器的进程如下,是和宿主机独立。

此模式指定新创建的容器和已经存在的一个容器共享一个 Network,和已有指定的容器出了网络方面其他都是独立的。
创建原始的一个容器 s-centos
[root@cesrc ~]# docker run -itd --name s-centos centos /bin/bash
be7425c3fac6845c9d8d150f9b8c710f9268611fa3a6d86d59444ea1e4bfe415
创建新容器 d-centos 并使用原有容器的网络
[root@cesrc ~]# docker run -itd --net=container:s-centos --name d-centos centos /bin/bash
d576e3009391111688004f57c1549572fd534d33d0e7ee7cc1e02a785f6c8c14

分别进入容器内部,查看 IP 配置和文件系统是否独立
S-centos

D-centos

此模式下创建的不会创建网络,容器里面就不会有 ip 容器中只有 lo,用户可以在此基础上,对容器网络做定制,若想使用 pipework 手工配置指定 docker 容器的 ip 地址,必须要在 none 模式下才可以
docker run -itd --net=none --name n-centos centos /bin/bash

None 模式下生成的容器必须经过手工配置后才可以上网。
用户自定义模式主要可选的有三种网络驱动:bridge、overlay、macvlan。bridge 驱动用于创建类似于前面提到的 bridge 网络;overlay 和 macvlan 驱动用于创建跨主机的网络,本例中我们使用 Flannel 和 etcd 使用 overlay 技术实现 docker 跨物理机通信。
Flannel 实现的容器的跨主机通信通过如下过程实现:
在 etcd 中规划配置所有主机的 docker0 子网范围;每个主机上的 flanneld 根据 etcd 中的配置,为本主机的 docker0 分配子网,保证所有主机上的 docker0 网段不重复,并将结果(即本主机上的 docker0 子网信息和本主机 IP 的对应关系)存入 etcd 库中,这样 etcd 库中就保存了所有主机上的 docker 子网信息和本主机 IP 的对应关系;当需要与其他主机上的容器进行通信时,查找 etcd 数据库,找到目的容器的子网所对应的 outip(目的宿主机的 IP),将原始数据包封装在 VXLAN 或 UDP 数据包中,IP 层以 outip 为目的 IP 进行封装;由于目的 IP 是宿主机 IP,因此路由是可达的,VXLAN 或 UDP 数据包到达目的宿主机解封装,解出原始数据包,最终到达目的容器。

图片摘自网络
实验规划


Node1 节点安装 etcd 步骤如下:
1、安装 etcd 程序
yum install -y etcd
2、修改 etcd 配置文件,配置文件在/etc/etcd/etcd.conf,本实验 etcd 单机部署,群集配置部分没有做严格更改,配置如下:
#[Member]
#ETCD_CORS=""
ETCD_DATA_DIR="/var/lib/etcd/host129.etcd" #etcd 数据保存目录
#ETCD_WAL_DIR=""
#ETCD_LISTEN_PEER_URLS=http://192.168.32.129:2380 #集群内部通信使用的 URL
ETCD_LISTEN_CLIENT_URLS=http://127.0.0.1:2379,http://192.168.32.129:2379 #供外部客户端使用的 URL
#ETCD_MAX_SNAPSHOTS=“5”
#ETCD_MAX_WALS=“5”
ETCD_NAME=“host129” #etcd 实例名称
3、设置后期分给 docker 容器的网段
etcdctl mk /network/config ‘{“Network”:“172.18.0.0/16”, “SubnetMin”: “172.18.1.0”, “SubnetMax”: “172.18.254.0”}’
4、设置开机启动 etcd 服务并启动该服务
systemctl enable etcd
systemctl start etcd
Node 2
1、安装 Docker 和 Flannel 服务
yum install -y docker flannel
2、修改 Flannel 配置文件如下:
Flanneld configuration options
# etcd url location. Point this to the server where etcd runs
FLANNEL_ETCD_ENDPOINTS=“http://192.168.32.129:2379” ##设置 etcd 地址和端口信息
# etcd config key. This is the configuration key that flannel queries
# For address range assignment
FLANNEL_ETCD_PREFIX="/network"
# Any additional options that you want to pass
FLANNEL_OPTIONS="-iface=ens33" ##设置 Flannel 与 etcd 通讯网卡
3、设置启动参数
cd /usr/libexec/flannel/
./mk-docker-opts.sh –i
4、启动 Docker 和 Flannel 服务
systemctl enable docker flanneld
systemctl start docker flannel
5、开启宿主机转发功能{一定要打开,很多网上教程都没有提这个,造成无法联通}
iptables -PFORWARD ACCEPT
6、创建容器并进入容器,查看 IP
docker run -itd centos /bin/bash
docker ps –a
docker attach 6c

在 Node2 节点上的操作同样在 Node3 执行一遍,最终看到 Node3 的获取的 IP 如图:

测试容器
Node2 和 node3 上的容器是可以互相访问


宿主机上生成了到各个 node 的路由条目


上述方式可以实现跨主机的 docker 通讯,除此之外还有其他两种方式:
1:在宿主机上添加到其他主机容器的静态路由方式
2:桥接方式
这两种方式都比较好理解,参考网络摘抄的两张图片
原文链接:https://www.cnblogs.com/yy-cxd/p/6553624.html
静态路由方法:

桥接方法:

Docker 镜像
我们都知道,操作系统分为内核和用户空间。对于 Linux 而言,内核启动后,会挂载 root 文件系统为其提供用户空间支持。而 Docker 镜像(Image),就相当于是一个 root 文件系统。比如官方镜像 ubuntu:16.04 就包含了完整的一套 Ubuntu 16.04 最小系统的 root 文件系统。
Docker 容器
镜像(Image)和容器(Container)的关系,就像是面向对象程序设计中的 类 和 实例 一样,镜像是静态的定义,容器是镜像运行时的实体。容器可以被创建、启动、停止、删除、暂停等。
Docker Registry
镜像构建完成后,可以很容易的在当前宿主机上运行,但是,如果需要在其它服务器上使用这个镜像,我们就需要一个集中的存储、分发镜像的服务,Docker Registry 就是这样的服务。
有公有,有私有。
镜像管理命令:
获取镜像
从 Docker 镜像仓库获取镜像的命令是 docker pull。其命令格式为:
docker pull [选项] [Docker Registry 地址[:端口号]/]仓库名[:标签]
命令参考 docker pull ubuntu:16.04
不指定 tag 默认就是下载最新的
docker pull mirrors.aliyun.com:ubuntu 从阿里云仓库
列出镜像
docker image ls
列表包含了 仓库名、标签、镜像 ID、创建时间 以及 所占用的空间。
删除镜像
docker rmi 镜像 Id 删除镜像文件
docker rmi -f 镜像 ID 强制删除镜像
容器管理
docker create 创建不启动
docker start 启动容器
docker stop 容器名称 终止容器
docker restart 容器名称 重启容器
docker rm id 删除容器
docker run -it centos /bin/sh 创建并启动容器 exit 后容器自动退出,若想以守护进程方式继续运行,需要改为 docker run -itd centos /bin/sh
docker ps 查看运行中的容器
docker ps –a 查看所有容器,包含停止的容器。
进入容器
docker attch 名字进入容器
docker exec -it 容器 id command | docker exec -it 7813e41ec005 /bin/sh
导出容器至文件
docker export -o test_for_run_tar 容器 ID
或 docker export 容器 ID >test_for_stop_tar
导入文件变成镜像
docker import test_for_run_tar - test/ubuntu:v1.0
数据管理
a) 数据卷是一个可供容器使用的特殊目录,将操作系统的目录映射到容器
docker run -d -P --name web -v /src/webapp:/opt/webapp centos /bin/sh
挂载主机的/src/webapp 到容器的/opt/webapp 目录,默认可读写
docker run -d -P --name web -v /src/webapp:/opt/webapp:ro centos /bin/sh
只读模式
b) 数据卷容器,可供其他容器共享使用的容器
先建立一个数据卷容器
root@localhost /]# docker run -it -v /cunchu --name chuchurongqi centos
[root@90bd63b06074 /]# ls
bin cunchu dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[root@90bd63b06074 /]# cd cunchu/
[root@90bd63b06074 cunchu]# vi qiang.txt
新建一个其他容器,使用–volumes-from 数据卷容器名字实现挂载容器卷里面的数据卷。
[root@localhost /]# docker run -it --volumes-from chuchurongqi --name db2 centos
本文转载自宜信技术学院网站。
原文链接:http://college.creditease.cn/detail/173
评论