docker_practice从入门到实践总结

docker主要用于云计算,目标是实现操作系统虚拟化,直接利用了系统内核,这样可以复用本机操作系统,提高系统资源的利用率;
传统虚拟化是硬件虚拟化,一个应用对应一个虚拟机,对系统资源的利用率不高。
docker将软件与其“依赖的环境”打包在一起,以镜像的方式交付,让软件运行在“标准的环境”中,这非常符合云计算的要求。
我们交付的东西不再只是代码、配置文件、数据库定义等,而是整个应用程序运行环境:OS+各种中间件、类库+应用程序代码。
以前我们发布的只是代码,现在我们发布的是代码+环境,可以消除线上线下环境不一致的问题。
docker是跨平台的。
docker基本概念:
镜像(Image)
容器(Container)
仓库(Repository)
    |----镜像:
           镜像可以用来创建容器,是一个只读模板,包含完整的操作系统环境(可以理解为创建/启动容器的配置文件)。
    |----容器
           容器是从镜像创建的运行实例,可以看做是一个简易版的Linux环境和运行在其中的应用程序。
           镜像是只读的,容器在启动的时候创建一层可写层作为最上层。
    |----仓库
           仓库是用来存放镜像的地方。仓库注册服务器上往往存放着 多个仓库,每个仓库中又包含多个镜像,每个镜像有不同的标签。
           仓库类似Git,注册服务器类似GitHub这样的托管服务。
镜像是一个模板,用来创建容器;
容器类似简易的Linux环境(和运行其中的应用),可以运行应用程序;
仓库用来存放镜像,类似Git。
镜像和容器的关系可以看做类和对象的关系。
仓库中存放镜像,镜像用来创建容器,容器中运行应用程序。
数据管理:
    |----数据卷
           数据卷是一个特殊的目录,可以挂载到容器中,供多个容器共享和使用。
           数据卷是独立于容器的,默认会一直存在,即使容器被删除。
           可以挂载主机目录或主机文件作为数据卷,一般挂载主机目录作为数据卷。
    |----数据卷容器
           数据卷容器本质上就是一个正常的容器,专门用来提供数据卷供其他容器挂载。
使用网络:
    |----外部访问容器(访问容器中的web应用)
           本机访问容器中的网络应用,可以进行端口映射(本机端口-容器开放的网络端口):
           随机映射本机端口-容器端口;
           映射所有本机端口-容器端口;
           映射指定地址的指定端口-容器端口;
           映射指定地址的任意端口-容器端口。
    |----容器互联
           源容器(子容器)-接收容器(父容器)
           可以链接多个父容器到子容器。
高级网络配置:
    |----docker虚拟网桥docker0
           主要用于容器跟主机交换数据,从而间接实现容器跟容器、容器跟外部主机进行数据交换(当然容器跟容器间也可通过端口直接通信)。
    |----配置DNS
           默认配置:通过/etc/resolv.conf进行更新
           手动配置:
           -h HOSTNAME or --hostname=HOSTNAME
           --link=CONTAINER_NAME:ALIAS
           --dns=IP_ADDRESS
           --dns-search=DOMAIN
    |----容器访问控制
           通过Linux本机上的iptables防火墙进行管理和实现。
               |----容器访问外部网络
                      检查本机Linux系统中net.ipv4.ip_forward是否打开,可以通过--ip-forward=true设置。
               |----容器之间访问
                   |----访问所有端口
                          当启动 Docker 服务时候,默认会添加一条转发策略到 iptables 的 FORWARD 链 上。
                          --icc=true  (缺 省值):策略为通过( ACCEPT  )
                          --icc=false:策略为禁止( DROP  )
                          --iptables=false:不会添 加  iptables  规则
                   |----访问指定端口
                          当-icc=false --iptables=true时通过-- link=CONTAINER_NAME:ALIAS,Docker 会在  iptable  中为两个容器分别添加一条  ACCEPT  规则,允许相 互访问开放的端口(取决于 Dockerfile 中的 EXPOSE 行)。
    |----端口映射实现
           |----映射容器端口到宿主主机的实现
                  默认情况下,容器可以主动访问到外部网络的连接,但是外部网络无法访问到容 器。
                  |----容器访问外部实现
                         容器所有到外部网络的连接,源地址都会被NAT成本地系统的IP地址。
                  |----外部访问容器实现
                         其实也是在本地的  iptable  的 nat 表中添加相应的规则。
    |----配置docker0网桥
           --bip=CIDR  -- IP 地址加掩码格式,例如 192.168.1.5/24:配置docker0  接口 的 IP 地址和子网掩码
           --mtu=BYTES  -- 覆盖默认的 Docker mtu 配置
           每次创建一个新容器的时候,Docker 从可用的地址段中选择一个空闲的 IP 地址分 配给容器的 eth0 端口。
           使用本地主机上  docker0  接口的 IP 作为所有容器的默认 网关。
    |----自定义网桥
           (1)在启动 Docker 服务的时候,使用  -b BRIDGE  或 --bridge=BRIDGE  来指定使用 的网桥。
           (2)如果服务已经运行,那需要先停止服务,并删除旧的网桥。
                   然后创建一个网桥  bridge0  。
                   查看确认网桥创建并启动。
                   配置 Docker 服务,默认桥接到创建的网桥上。
                   启动 Docker 服务。 新建一个容器,可以看到它已经桥接到了  bridge0  上。
    |----工具和示例
    |----编辑网络配置文件
    |----示例:创建一个点到点连接
           解决办法很简单:创建一对  peer  接口,分别放到两个容器中,配置成点到点链 路类型即可。
实战案例:
    |----使用 Supervisor 来管理进程
           方法:
           创建一个Supervisor容器运行ssh和apache服务,然后创建自己的容器使用-p进行端口映射(映射到Supervisor 容器的服务端口),这
样就能在自己的容器中同时访问 ssh 和 apache 服务了。
    |----创建 tomcat/weblogic 集群
           以创建tomcat集群为例:
           (1)将jdk、tomcat等安装软件放到本机home目录下,然后将本机home目录挂载到容器的 /opt/data 目录;
           (2)在supervisor的配置文件中添加tomcat项,用于映射到supervisor容器;
           (3)新建tomcat镜像的dockerfile,并配置好容器和本机的映射端口;
           (4)根据dockerfile创建镜像,根据镜像创建容器,所有的tomcat容器都可访问supervisor容器的tomcat服务,从而创建了tomcat集群。
    |----多台物理主机之间的容器互联
           (1)同一台主机上的容器互联:
           通过虚拟网桥docker0将容器连接起来。
           (2)容器与外部网络互联:
           主机上的容器通过 nat 连接外网,如果要让外网 连接到容器中,就需要做端口映射,即 -p 参数。
           (3)不同物理主机上的容器互联:
           如果在企业内部应用,或者做多个物理主机的集群,可能需要将多个物理主机的容 器组到一个物理网络中来,那么就需要将这个网桥桥接到我们指定的网卡上。
    |----标准化开发测试和生产环境
安全:
    |----内核名字空间
    |----控制组
    |----Docker服务端的防护
    |----内核能力机制
    |----其它安全特性
Dockerfile:
    |----基本结构
           一般分为四部分:镜像信息、维护者信息、镜像指令、容器指令
    |----指令
           FROM、MAINTAINER、RUN、CMD
           EXPOSE、ENV、ADD、COPY、ENTRYPOINT、VOLUME、USER、WORKDIR、ONBUILD
    |----创建镜像
           docker build
底层实现:
    |----基本架构
    |----名字空间
    |----控制组
    |----联合文件系统
    |----容器格式
    |----Docker 网络实现
           docker网络利用虚拟网桥实现:
           docker创建容器的时候会创建一对虚拟接口,分别放到本地主机和容器中;
           主机端接口名为vethXXX,容器端接口名为eth0,如下图所示:
           Linux 通过在内核中进行数据复制来实现虚拟接口之间的数据转发,发送接口 的发送缓存中的数据包被直接复制到接收接口的接收缓存中。
          (虚拟网桥本身就是利用了Linux内核)
CoreOS项目:
CoreOS还是比较好的,可以关注关注

 

【微信扫码关注我的公众号获取更多信息】

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
In September 2013, while browsing Hacker News, I stumbled across an article in Wired about a new technology called “Docker.” 1 As I read it, I became increasingly excited as I realized Docker’s revolutionary potential. The company I’d worked at for over a decade was struggling to deliver software quickly enough. Provisioning environments was a costly, time-consuming, manual, and inelegant affair. Continuous integration was barely existent, and setting up devel- opment environments was an exercise in patience. As my job title included the words “DevOps Manager,” I was peculiarly motivated to solve these problems! I recruited a couple of motivated coworkers (one of them now my coauthor) via a company mailing list, and together our skunkworks team labored to turn a beta tool into a business advantage, reducing the high costs of VM s and enabling new ways of thinking about building and deploying software. We even built and open sourced an automation tool (ShutIt) to suit our organization’s delivery needs. Docker gave us a packaged and maintained tool that solved many problems that would have been effectively insuperable had we taken it upon ourselves to solve them. This was open source at its best, empowering us to take on a challenge using our spare time, overcoming technical debt, and learning lessons daily. Lessons not only about Docker, but about continuous integration, continuous delivery, packaging, automa- tion, and how people respond to speedy and disruptive technological change. 1 http://www.wired.com/2013/09/docker/ PREFACE xviii For us, Docker is a remarkably broad tool. Wherever you run software using Linux, Docker can impact it. This makes writing a book on the subject challenging, because the landscape is as broad as software itself. The task is made more onerous by the extraordinary rate at which the Docker ecosystem is producing solutions to meet the needs that emerge from such a fundamental change in software production. Over time, the shape of problems and solutions became familiar to us, and in this book we’ve endeavored to pass on this experience. This will enable you to figure out solu- tions to your particular technical and business constraints. When giving talks at meetups, we’re struck by how quickly Docker has become effective within organizations willing to embrace it. This book mirrors how we used Docker, going from our desktops, through the DevOps pipeline, and all the way to production. As a consequence, this book is sometimes unorthodox, but as engineers we believe that purity must sometimes give way to practicality, especially when it comes to saving money! Everything in this book is based on real lessons from the field, and we hope you benefit from our hard-won experience.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值