linux下的sar很强大,但是是命令行的,尤其是一大堆数据看趋势的时候,非常不直观,其实有人已经都想过来,看这里: http://sargraph.com/
docker资源限制之IO
原理:
通过cgroup限制,相关cgroup项如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
-r--r--r-- 1 root root 0 9月 8 19:03 blkio.io_merged -r--r--r-- 1 root root 0 9月 8 19:03 blkio.io_merged_recursive -r--r--r-- 1 root root 0 9月 8 19:03 blkio.io_queued -r--r--r-- 1 root root 0 9月 8 19:03 blkio.io_queued_recursive -r--r--r-- 1 root root 0 9月 8 19:03 blkio.io_service_bytes -r--r--r-- 1 root root 0 9月 8 19:03 blkio.io_service_bytes_recursive -r--r--r-- 1 root root 0 9月 8 19:03 blkio.io_serviced -r--r--r-- 1 root root 0 9月 8 19:03 blkio.io_serviced_recursive -r--r--r-- 1 root root 0 9月 8 19:03 blkio.io_service_time -r--r--r-- 1 root root 0 9月 8 19:03 blkio.io_service_time_recursive -r--r--r-- 1 root root 0 9月 8 19:03 blkio.io_wait_time -r--r--r-- 1 root root 0 9月 8 19:03 blkio.io_wait_time_recursive -rw-r--r-- 1 root root 0 9月 8 19:03 blkio.leaf_weight -rw-r--r-- 1 root root 0 9月 8 19:03 blkio.leaf_weight_device --w------- 1 root root 0 9月 8 19:03 blkio.reset_stats -r--r--r-- 1 root root 0 9月 8 19:03 blkio.sectors -r--r--r-- 1 root root 0 9月 8 19:03 blkio.sectors_recursive -r--r--r-- 1 root root 0 9月 8 19:03 blkio.throttle.io_service_bytes -r--r--r-- 1 root root 0 9月 8 19:03 blkio.throttle.io_serviced -rw-r--r-- 1 root root 0 9月 8 19:03 blkio.throttle.read_bps_device -rw-r--r-- 1 root root 0 9月 8 19:03 blkio.throttle.read_iops_device -rw-r--r-- 1 root root 0 9月 8 19:28 blkio.throttle.write_bps_device -rw-r--r-- 1 root root 0 9月 8 19:03 blkio.throttle.write_iops_device -r--r--r-- 1 root root 0 9月 8 19:03 blkio.time -r--r--r-- 1 root root 0 9月 8 19:03 blkio.time_recursive -rw-r--r-- 1 root root 0 9月 8 19:03 blkio.weight -rw-r--r-- 1 root root 0 9月 8 19:03 blkio.weight_device -rw-r--r-- 1 root root 0 9月 8 19:03 cgroup.clone_children --w--w--w- 1 root root 0 9月 8 19:03 cgroup.event_control -rw-r--r-- 1 root root 0 9月 8 19:28 cgroup.procs -rw-r--r-- 1 root root 0 9月 8 19:03 notify_on_release -rw-r--r-- 1 root root 0 9月 8 19:03 tasks |
这里暂且只讨论:
- blkio.throttle.read_bps_device
- blkio.throttle.read_iops_device
- blkio.throttle.write_bps_device
- blkio.throttle.write_iops_device
较旧的docker版本不支持设置这些,但是,只要你的内核支持上面这些,就可以直接修改cgroup实现。
我这里使用的是devicemapper,宿主机只有一个设备/dev/sda ,如下:
1 2 3 4 5 6 7 |
# ll /dev/sda* brw-rw---- 1 root disk 8, 0 7月 11 13:43 /dev/sda brw-rw---- 1 root disk 8, 1 7月 11 13:43 /dev/sda1 brw-rw---- 1 root disk 8, 2 7月 11 13:43 /dev/sda2 brw-rw---- 1 root disk 8, 3 7月 11 13:43 /dev/sda3 brw-rw---- 1 root disk 8, 4 7月 11 13:43 /dev/sda4 brw-rw---- 1 root disk 8, 5 7月 11 13:43 /dev/sda5 |
devicemapper信息如下:
1 2 3 4 5 |
# ll /dev/mapper/ 总用量 0 crw------- 1 root root 10, 236 7月 11 13:43 control lrwxrwxrwx 1 root root 7 9月 8 19:03 docker-8:3-1075755737-eb519f8a4930573e982fe8df6c62e1732fb295fca0fe64494157dccef4b52653 -> ../dm-1 lrwxrwxrwx 1 root root 7 8月 24 12:24 docker-8:3-1075755737-pool -> ../dm-0 |
1 2 3 |
# ll /dev/dm-* brw-rw---- 1 root disk 253, 0 8月 24 12:24 /dev/dm-0 brw-rw---- 1 root disk 253, 1 9月 8 19:03 /dev/dm-1 |
从上面可以看出:
- docker 使用的mapper-pool为 /dev/dm-0,设备号: 253,0
- 该机器上一个容器,该容器挂载的设备为 /dev/dm-1 , 设备号: 253,1
- 物理磁盘 /dev/sda 设备号: 8,0
容器不仅用到了devicemapper(这里是 /dev/dm-1),而且还挂载了外部的磁盘,下面是关于容器限速100mb的写法:
1 2 3 4 5 6 |
#cd /sys/fs/cgroup/blkio/system.slice/docker-eb519f8a4930573e982fe8df6c62e1732fb295fca0fe64494157dccef4b52653.scope #echo "8:0 102400000" >blkio.throttle.write_bps_device #echo "253:1 102400000" >blkio.throttle.write_bps_device # cat blkio.throttle.write_bps_device 8:0 102400000 253:1 10240000 |
关于读速度,这里先不举例了,还有iops都如法炮制
docker 文件系统问题
1 2 3 4 5 6 7 8 9 10 11 |
[root@bs2 ~]# top top - 18:08:43 up 149 days, 6:41, 4 users, load average: 396.86, 356.44, 250.94 Tasks: 2776 total, 2 running, 2764 sleeping, 1 stopped, 9 zombie %Cpu(s): 0.8 us, 3.8 sy, 0.0 ni, 6.3 id, 89.1 wa, 0.0 hi, 0.1 si, 0.0 st KiB Mem : 66059044 total, 629988 free, 53493632 used, 11935424 buff/cache KiB Swap: 8191996 total, 0 free, 8191996 used. 7737832 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 45674 root 20 0 0 0 0 R 99.7 0.0 5:18.03 kworker/u289:6 18863 root 20 0 3459280 256908 5816 S 22.7 0.4 6902:42 cadvisor |
1 2 3 4 5 6 |
top - 18:06:10 up 149 days, 6:39, 3 users, load average: 322.56, 331.35, 225.97 Tasks: 2788 total, 2 running, 2776 sleeping, 1 stopped, 9 zombie %Cpu(s): 0.7 us, 4.4 sy, 0.0 ni, 6.3 id, 88.5 wa, 0.0 hi, 0.1 si, 0.0 st KiB Mem : 66059044 total, 635152 free, 53491180 used, 11932712 buff/cache KiB Swap: 8191996 total, 0 free, 8191996 used. 7739852 avail Mem |
dmesg大量如下报错:
[四 9月 8 19:24:24 2016] XFS: possible memory allocation deadlock in kmem_alloc (mode:0x250)
解决办法:
1 |
echo 1 > /proc/sys/vm/drop_caches |
效果不佳:
1 2 3 4 5 6 7 8 9 10 |
top - 18:12:59 up 149 days, 6:45, 4 users, load average: 211.63, 320.98, 266.11 Tasks: 2748 total, 2 running, 2741 sleeping, 1 stopped, 4 zombie %Cpu(s): 6.8 us, 15.2 sy, 0.0 ni, 9.2 id, 68.8 wa, 0.0 hi, 0.1 si, 0.0 st KiB Mem : 66059044 total, 2339032 free, 53697124 used, 10022888 buff/cache KiB Swap: 8191996 total, 0 free, 8191996 used. 7518744 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 6778 root 20 0 231840 11548 6740 R 66.9 0.0 63:20.26 php 103487 root 20 0 197984 18040 5444 S 27.2 0.0 1:08.14 python |
1 2 3 4 5 6 |
#iostat 5 -m sda avg-cpu: %user %nice %system %iowait %steal %idle 6.29 0.01 15.31 59.91 0.00 18.49 Device: tps MB_read/s MB_wrtn/s MB_read MB_wrtn sda 396.20 1.38 3.11 6 15 |
crontab 依赖
crontab不大,但是依赖却不少
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
================================================================================================================== Package Arch Version Repository Size ================================================================================================================== Installing: crontabs noarch 1.10-33.el6 base 10 k Installing for dependencies: cronie x86_64 1.4.4-16.el6_8.2 updates 75 k cronie-anacron x86_64 1.4.4-16.el6_8.2 updates 31 k dbus-glib x86_64 0.86-6.el6 base 170 k hwdata noarch 0.233-16.1.el6 base 1.3 M initscripts x86_64 9.03.53-1.el6.centos.1 updates 947 k iproute x86_64 2.6.32-54.el6 base 389 k iptables x86_64 1.4.7-16.el6 base 254 k iputils x86_64 20071127-21.el6 base 120 k libdrm x86_64 2.4.65-2.el6 base 136 k libpciaccess x86_64 0.13.4-1.el6 base 24 k logrotate x86_64 3.7.8-26.el6_7 base 58 k plymouth x86_64 0.8.3-27.el6.centos.1 base 89 k policycoreutils x86_64 2.0.83-29.el6 base 663 k redhat-logos noarch 60.0.14-12.el6.centos base 15 M rsyslog x86_64 5.8.10-10.el6_6 base 650 k ssmtp x86_64 2.61-22.el6 epel 53 k sysvinit-tools x86_64 2.87-6.dsf.el6 base 60 k udev x86_64 147-2.73.el6_8.1 updates 358 k util-linux-ng x86_64 2.17.2-12.24.el6 base 1.6 M Transaction Summary ================================================================================================================== Install 20 Package(s) Total download size: 21 M Installed size: 42 M |
关于php的max_execution_time
缘起:
一个php-fpm的请求执行时间会比较长,总是执行不完就中止了;查看php错误日志,显示执行时长超过了60s。
1 2 |
$ php -i |grep max_execution_time $ max_execution_time => 0 => 0 |
为什么呢?
参考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.
说明:
- 命令行执行php没有执行时长限制,尽管显式在php.ini中配置、或者ini_set(“max_execution_time”, n)、或者set_time_limit(n) 都是没用的;
- 尽管使用ini_set(…) 、set_time_limit(..) 后,也能通过ini_get(..)发现设置的值确实发生变化了,对于命令行也是没有作用的
- 命令行php -i 查看到的 max_execution_time 总是0
- 所以: 命令行程序对于PHP执行时长限制的担心都是多余的,cli 模式下max_execution_time被硬编码为0了
- max_execution_time 的默认值是30s
关于set_time_limit: http://php.net/manual/en/function.set-time-limit.php
- set_time_limit(n) 会重置时间计数器,就是说是从该函数调用的时候计数n的
- 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
结论:
- 命令行程序对于PHP执行时长限制的担心都是多余的
- php-fpm环境下,试图在脚本中修改max_execution_time是没有用的
- 所以,max_execution_time 一般在php.ini 中定义
linux 进程按启动时间排序
办法1:
1 |
ps aux --sort=lstart |
1 |
ps aux --sort=start_time |
1 |
( cd /proc; ls -td --full-time --time-style=+%s [0123456789]*; ) |
进程在/proc 下面表现为一个目录,也可以使用stat /proc/pid 来查看进程创建时间
1 2 3 4 5 6 7 8 |
stat /proc/42 File: "/proc/42" Size: 0 Blocks: 0 IO Block: 1024 目录 Device: 3h/3d Inode: 4012968359 Links: 8 Access: (0555/dr-xr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root) Access: 2016-08-16 19:25:00.354194514 +0800 Modify: 2016-08-16 19:25:00.354194514 +0800 Change: 2016-08-16 19:25:00.354194514 +0800 |
测试发现 按照lstart排序结果比较奇怪,不知道排序规则是啥:
办法二: k 选项指定排序列
按照启动时间升序排列
1 |
ps kstart_time -ef |
按照启动时间降序排列
1 |
ps k-start_time -ef |
先按照uid升序,再按照启动时间降序
1 |
ps kuid,-start_time -ef |
参考:
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的路径,如下:
1 |
GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_example" git clone git@gitlab.phpor.net:test/test.git |
或许在某些情况下-i选项可能会被配置文件中的定义给覆盖掉,那么可以添加 -F /dev/null 选项禁用可能的配置文件,如下:
1 |
GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_example -F /dev/null" git clone git@gitlab.phpor.net:test/test.git |
curl ftp 主动模式与被动模式
curl命令访问ftp默认走被动模式,主动模式可能会不好使,比如说:
- curl在防火墙后面,而防火墙又不允许主动进入的连接
- curl所在机器有多个IP,控制链路使用的IP和数据链路期望使用不同的IP
- 可以通过 -P address 来指定主动模式时数据链路期望使用的IP,如果简单使用数据链路的IP,则可以直接 -P –
svn to gitlab
手动在gitlab上创建git仓库: http://gitlab.phpor.net/phpor/svn2gitlab.git
1 2 3 4 |
yum install git-svn.noarch git svn clone http://svn.phpor.net/svn2gitlab/ svn2gitlab git remote add origin http://gitlab.phpor.net/phpor/svn2gitlab.git git push --all |
https + http2.0
给自己的blog添加了https访问,同时支持了http2.0,访问速度明显快了不少(假的,4G网络下没看出来差别)