[DevOps for Developers] 读书笔记1

dev & ops 分别对change的态度, 决定了...
devOps 是随着agile实践一步步发展而来, agile 先是在开发team, 后延续到ops team. 它强调的是合作, 沟通.

dev: change code, and want to delivery quickly to production;
ops: no change for production, keep it stable;

devops 概念的萌芽发展历程

  1. Patrick Debois coined the term DevOps in 2009 while organizing the
    DevOpsDays conference in Belgium.
  2. Patrick Debois ran a session called “Agile Operations and
    Infrastructure: How Infra-gile Are You?”4 at the Agile 2008
    conference in Toronto and published a paper with a similar name.
  3. Marcel Wegermann published a e-mail list called “Agile System
    Administration.”
  4. John Allspaw gave a presentation called “10+ Deploys per Day: Dev
    and Ops Cooperation”7 at the Velocity 2009 conference in San Jose.
  5. Steven Blank published a book called Four Steps to the Epiphany.
  6. Eric Ries published The Lean Startup9 and others have written on the
    “lean startup” scene.
  7. The 451 Group published the first analyst report on DevOps (titled
    “The Rise of DevOps”10) in September 2010.

DevOps can be examined from the following overlapping perspectives:

  1. Metrics and measurement view: This aspect addresses quality and
    testing and stresses shared incentives.
  2. Process view: This aspect covers congruence and flow to gain fast
    feedback and set up a holistic process.
  3. Technical view: This aspect discusses fast feedback through
    automation, particularly automatic releasing, specification by
    example, and infrastructure as code.

Docker Daemon 监听tcp端口, 远程API 调用

默认情况下, Docker Daemon 监听在本地的 unix:///var/run/docker.sock 上, 只允许本地 root 用户 docker client 连接. 如果要想远程连接, 必须监听 tcp 端口.

Docker Daemon 可以选择在启动的时候, 设置监听在 tcp 端口, IPC socket 监听, 或者2个都监听.

  • 首先看一下Docker Daemon 是不是在监听 (ps -aux | grep docker);
  • 如果已经起来了, 先shutdown (service docker stop);
  • 重新启动, 设置tcp 和 sock 同时监听 (sudo docker -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock -d &);

官方关于设置Docker Daemon 的文档: http://docs.docker.com/articles/basics/
如下图:
Screen Shot 2015-07-12 at 12.12.17 PM.png

重新查看进程状态, 可以看到 Docker Deamon 已经起来了. 那么就可以远程http连接了
http://docker.tianxiaohui.com:2375/containers/json?all=1 (查看所有container)

使用 RESTful client, 启动一个container, 这里返回204, 代表没有任何错误
Screen Shot 2015-07-12 at 2.30.33 PM.png

官方关于docker remote API 的文档 http://docs.docker.com/reference/api/docker_remote_api/

Microservices architectures pros and cons 微服务架构的优缺点

这里全是引用或者从这篇文章引发的: Microservices - Not A Free Lunch!

pros:



    每个单独的服务都是以某个业务模块为中心, 不用关心其他服务, 开发简单;
    sale out, 伸缩性, HA
    rollback only involve single app
    每个服务都可以根据自己业务特性, 采用不同的内部架构, 工具, 开发语言;
    每个服务可以根据load 情况, 增加减少cluster内机器的数量;
    系统中的服务都是松耦合的, 便于开发, 维护;
    每个服务的开发团队都不至于太大, 团队之间相互独立;
    现在很多的开源的工具支持这种开发模式, 便于上手;

cons:



    原来你测试部署一个app就好了, 那么现在按照模块, 根据微服务架构拆分成了多个app, 相互提供服务, 那么你逻辑上就要有多个team, 多个app, 原来测试, 部署一个app就好了, 现在要部署测试多个app, 要有更多的开发, 测试, 生产环境;
    原来一个app 内部之间直接调用, 现在要通过网络, 要有服务注册, 发现机制; 网络之间的调用, 就有了failover;
    多个prodution cluster 在线上, 那么就需要等多的mnoitor, 报警机制, 以及分布式的log 机制;
    Substantial DevOps Skills Required
    定义接口, 单机应用的接口可以随时改变, 然后修改, 微服务的分布式架构就要求协调多个team, 按计划发布; 并且接口最好在一段时间内不要修改;
    Once we have distributed a system, we have to consider a whole host of concerns that we didn't before. Network latency, fault tolerance, message serialisation, unreliable networks, asynchronicity, versioning, varying loads within our application tiers etc.
    Asynchronicity Is Difficult!
    Testability Challenges

Using Docker [读书笔记] 3 docker 命令

docker run

--restart 参数 可以是 no | always | on-failure:10, 所以可以设置是否自动启动

          --rm 自动删除 container 当container 退出的时候;
          -e, --env 设置环境变量
          --env-file  设置环境变量名字
          -h, --hostname
          --name NAME
          -v, --volume
          --volumes-from
          --expose
          --link
          -p, --publish  发布一个端口, 如果不给定, 就随机设置一个未用的
          -P, --publish-all
          --add-host  Adds the given IP and hostname mapping to /etc/hosts in the container.
          --dns  定制 dns server
          --mac-address  设置 MAC 地址
          --net  设置网络模式  bridge | none | container | host
          -c, --cpu-shares  CPU 使用量
          -cap-add   / --cap-drop    增加 或 去除 linux 某个功能
          --cpuset  可以使用那个CPU
          --device  设置 container 可以访问那个那个硬件设备 如 磁盘, 打印机, 声卡
          -m, --memory  内存使用量 设置
          --entrypoint  override ENTRYPOINT 
          -u, --user  override USER
          -w, --workdir

管理 Container



    docker attach [OPTIONS] CONTAINER   
    docker create  从image 创建一个container, 但是不run, 后续可以使用 docker start 去run
    docker cp  从container copy 文件或路径到 host
    docker exec  run 一个在container 的命令
    docker kill  杀死 container 的main process (PID : 1)
    docker pause  / docker unpause  冻结/解冻 container 的所有进程
    docker rename  重命名 一个 container
    docker restart  = docker stop; docker start
    docker rm   remove one or more containers. -f 选项强制remove 正在运行的, -v 去除相关volume

Container 相关命令



    docker diff  对比从image 到现在container 为止 container文件系统做的变化
    docker events   打印实时事件信息
    docker inspect   查看 image 或 container 的相关信息
    docker logs    查看 container 的STDOUT, STDERR 相关信息
    docker port    查看 container 对应的端口 mapping 信息
    docker ps  查看在run 或者 停止的 container 的 high-level 相关信息, -q 只返回container id, 一般用着其他后续命令
    docker top 类似 top, 不过只和这个container 相关的, 也就是container 内部的进程

Image 相关命令



    docker build  从Dockerfile 创建 image 
    docker commit  从 container 创建 image. 从一个正在运行的container 创建 image, 可能导致它暂停, 有参数 --pause=false 可以设置
    docker history  Outputs information on each of the layers in an image.
    docker images  查看本地 image 信息
    docker rmi   删除本地 某 image
    docker tag   给image 打tag

Registry 相关命令 .dockercfg in your home directory



    docker login 
    docker logout
    docker push
    docker search

Using Docker [读书笔记] 2

Dockerfile 中的 EXEC VS SHELL 形式
Several instructions (RUN, CMD and ENTRYPOINT), take both a shell format and an exec format. The exec form takes a JSON array (e.g. ["executable", "param1", "param2"]) which assumes the first item is the name of an executable which is then executed with the remaining items as parameters. The shell format is a freeform string which will be interpreted by passing to /bin/sh -c. Use the exec form to avoid the shell munging strings or in cases where the image doesn’t have /bin/sh.

FROM base image
ADD 从远程或本地Context copy到image, 如果是压缩文件, 会自动解压. 最好不要用ADD, 用COPY 或 RUN wget/curl;
CMD 当Container启动完, 执行的命令. 后边的CMD 会覆盖前面的CMD;
COPY 有EXEC 和 SHELL 格式, EXEC 的json 格式支持路径中有空格, 支持Wildcard, 但不支持到Context的上层目录;
ENV 设置环境变量
RUN run 给定的脚本, 并commit 其结果
USER 设置用户, 用以给后续的CMD, RUN, COPY, ENTRYPOINT 等用; host 和Container 之间的userId是一样的, user name 可能不一样.
EXPOSE 标明要监听的端口, 用以告诉docker server.
ENTRYPOINT Container 启动后默认执行的脚本, 可能被CMD 或 docker run的命令覆盖.
MAINTAINER 维护者信息
ONBUILD 如果本image 被用作base image, 那么后续build 时候要执行的脚本
VOLUME 设置要挂载的文件
WORKDIR 设置工作路径, 用来给RUN, CMD, ENTRYPOINT, ADD or COPY 等命令用

Docker links are the simplest way to allow containers on the same host to talk to each other. Using Docker links will also add the alias and the link container ID to /etc/hosts on the master container, allowing the link container to be addressed by name from the master container.By default, containers will be able to talk to each other whether not they have been explicitly linked. Perhaps most significantly they are static — links aren’t updated if the link container goes down or is replaced. Also, the link container must be started before the master container, meaning you can’t have bidirectional links.

Volumes are directories[18] that are not part of the container’s UFS, they are just normal directories on the host that are bind mounted into the container.

通过Dockerfile的 VOLUME命令, 不能指定host的文件路径, 所以都是动态指定的, 所以可以通过 docker inspect -f {{.Volumes}} ${containerId} 查看
通过docker run -v hostDir:containerDir 指定路径.

通过 --volumes-from CONTAINER shared volume 即使原container 没有在run, 照样可以使用这个volume.