docker资源限制之IO

原理:

通过cgroup限制,相关cgroup项如下:

这里暂且只讨论:

  1. blkio.throttle.read_bps_device
  2. blkio.throttle.read_iops_device
  3. blkio.throttle.write_bps_device
  4. blkio.throttle.write_iops_device

较旧的docker版本不支持设置这些,但是,只要你的内核支持上面这些,就可以直接修改cgroup实现。

我这里使用的是devicemapper,宿主机只有一个设备/dev/sda ,如下:

devicemapper信息如下:

从上面可以看出:

  1. docker 使用的mapper-pool为 /dev/dm-0,设备号: 253,0
  2. 该机器上一个容器,该容器挂载的设备为 /dev/dm-1 , 设备号: 253,1
  3. 物理磁盘 /dev/sda 设备号: 8,0

容器不仅用到了devicemapper(这里是 /dev/dm-1),而且还挂载了外部的磁盘,下面是关于容器限速100mb的写法:

关于读速度,这里先不举例了,还有iops都如法炮制

 

 

docker 文件系统问题

 

dmesg大量如下报错:

[四 9月  8 19:24:24 2016] XFS: possible memory allocation deadlock in kmem_alloc (mode:0x250)

解决办法:

效果不佳:

 

 

 

 

crontab 依赖

crontab不大,但是依赖却不少

 

 

关于php的max_execution_time

缘起:

一个php-fpm的请求执行时间会比较长,总是执行不完就中止了;查看php错误日志,显示执行时长超过了60s。

为什么呢?

参考php官方文档:http://php.net/manual/en/info.configuration.php#ini.max-execution-time

max_execution_time integer

This sets the maximum time in seconds a script is allowed to run before it is terminated by the parser. This helps prevent poorly written scripts from tying up the server. The default setting is 30. When running PHP from the command line the default setting is 0.

The maximum execution time is not affected by system calls, stream operations etc. Please see the set_time_limit() function for more details.

You can not change this setting with ini_set() when running in safe mode. The only workaround is to turn off safe mode or by changing the time limit in the php.ini.

Your web server can have other timeout configurations that may also interrupt PHP execution. Apache has a Timeout directive and IIS has a CGI timeout function. Both default to 300 seconds. See your web server documentation for specific details.

说明:

  1. 命令行执行php没有执行时长限制,尽管显式在php.ini中配置、或者ini_set(“max_execution_time”, n)、或者set_time_limit(n) 都是没用的;
  2. 尽管使用ini_set(…) 、set_time_limit(..) 后,也能通过ini_get(..)发现设置的值确实发生变化了,对于命令行也是没有作用的
  3. 命令行php -i 查看到的 max_execution_time 总是0
  4. 所以: 命令行程序对于PHP执行时长限制的担心都是多余的,cli 模式下max_execution_time被硬编码为0了
  5. max_execution_time 的默认值是30s

 

关于set_time_limit:  http://php.net/manual/en/function.set-time-limit.php

  1. set_time_limit(n) 会重置时间计数器,就是说是从该函数调用的时候计数n的
  2. php-fpm的脚本中执行set_time_limit(n)、或者ini_set(“max_execution_time”, n) 是无效的
    虽然这样更加安全,但是不符合定义: http://php.net/manual/en/info.configuration.php ;这里
    有说明max_execution_time的可修改限制为PHP_INI_ALL,就是说任何条件下都可以随时修改: http://php.net/manual/en/configuration.changes.modes.php

 

结论:

  1. 命令行程序对于PHP执行时长限制的担心都是多余的
  2. php-fpm环境下,试图在脚本中修改max_execution_time是没有用的
  3. 所以,max_execution_time 一般在php.ini 中定义

 

linux 进程按启动时间排序

办法1:

进程在/proc 下面表现为一个目录,也可以使用stat /proc/pid 来查看进程创建时间

测试发现  按照lstart排序结果比较奇怪,不知道排序规则是啥:

 

办法二: k 选项指定排序列

按照启动时间升序排列

按照启动时间降序排列

 

先按照uid升序,再按照启动时间降序

 

参考:

http://serverfault.com/questions/27887/how-to-sort-ps-output-by-process-start-time

如何获取进程启动时间

如何获取进程年龄

git 通过cron自动拉取最新代码

git 可以deploykey的方式来clone代码,但是git没有提供一个参数来指定key的路径,如何是好?

git通过deploykey的方式clone代码其实走的就是ssh,git 2.3.0后会参考一个环境变量GIT_SSH_COMMAND,完全可以通过定制该命令来指定key的路径,如下:

或许在某些情况下-i选项可能会被配置文件中的定义给覆盖掉,那么可以添加 -F /dev/null 选项禁用可能的配置文件,如下:

curl ftp 主动模式与被动模式

curl命令访问ftp默认走被动模式,主动模式可能会不好使,比如说:

  1. curl在防火墙后面,而防火墙又不允许主动进入的连接
  2. curl所在机器有多个IP,控制链路使用的IP和数据链路期望使用不同的IP
    1. 可以通过 -P address 来指定主动模式时数据链路期望使用的IP,如果简单使用数据链路的IP,则可以直接 -P –

svn to gitlab

手动在gitlab上创建git仓库: http://gitlab.phpor.net/phpor/svn2gitlab.git