跳到主要内容

2 篇博文 含有标签「shell」

查看所有标签

· 阅读需 2 分钟

记录awx的安装测试过程以及需要注意的点

关于awx

awx(https://github.com/ansible/awx)是ansible tower的开源版本,作为tower的upstream对外开源, 项目从2013年开始维护,2017年由redhat对外开源,目前维护得比较活跃。由于官方的install guide写得有点杂不是很直观, 导致想要安装个简单的测试环境体验一下功能都要折腾半天,这里提供一个简单版本的安装流程方便快速体验。

安装过程

安装软件包

yum -y install epel-release
systemctl disable firewalld
systemctl stop firewalld
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
yum -y install git gettext ansible docker nodejs npm gcc-c++ bzip2 python-docker-py

启动服务

systemctl start docker
systemctl enable docker

clone awx代码

git clone https://github.com/ansible/awx.git
cd awx/installer/
# 注意修改一下postgres_data_dir到其他目录比如/data/pgdocker
vi inventory
ansible-playbook -i inventory install.yml

检查日志

docker logs -f awx_task

以上是安装过程,因为本地环境访问外网要经过代理,这里记录一下配置docker通过代理访问外网的方式,否则pull image会有问题。

mkdir /etc/systemd/system/docker.service.d/
cat > /etc/systemd/system/docker.service.d/http-proxy.conf <<EOF
[Service]
Environment="HTTP_PROXY=proxy.test.dev:8080" "HTTPS_PROXY=proxy.test.dev:8080" "NO_PROXY=localhost,127.0.0.1,172.1.0.2"
EOF

systemctl daemon-reload
systemctl restart docker
systemctl show --property=Environment docker

参考文档:

[1] http://khmel.org/?p=1245

[2] https://docs.docker.com/engine/admin/systemd/#httphttps-proxy

· 阅读需 4 分钟

遇到某服务进程产生的日志始终无法切割,原来原因在这里

问题现象

某业务机器因磁盘容量超过阈值收到告警,分析发现是由于该机器上某个服务进程产生的日志文件量太大,且日志文件未按周期切割,进而导致历史日志信息积累到单个日志文件中。 未避免故障发生采取临时措施手工切割该日志文件,由于该服务进程并未提供内置的日志切割,因此手工模拟类似logrotate的copytruncate模式对日志进行切割。 但在将日志truncate之后,奇怪的一幕发生了,通过ls查看文件大小发现并未减少,直觉判断这可能是文件句柄一直处于打开状态且偏移量未发生改变导致。 在进一步检查了该进程的启动方式之后,发现该进程通过nohup启动,并将标准输出重定向到持续增大的日志文件中。

模拟

我们通过下面几行脚本来模拟此现象:

#!/bin/bash
while true; do
    sleep 1
    head -5000 /dev/urandom
done

脚本启动后会有一个常驻进程每个1秒钟输出一堆字符串以此来模拟日志文件增涨,我们按照以下方式启动:

nohup ./daemon.sh >out.log 2>&1 < /dev/null &

等待一会之后我们观察到日志已经写入了

[root@localhost t]# ll -h out.log ;du -h out.log 
-rw-r--r-- 1 root root 64M Oct 19 17:41 out.log
64M   out.log

接着将日志文件清空,再观察文件大小变化

[root@localhost t]# 
[root@localhost t]# truncate -s0 out.log              
[root@localhost t]# ll -h out.log ;du -h out.log 
-rw-r--r-- 1 root root 93M Oct 19 17:41 out.log
4.0M  out.log

这时可以看到,虽然文件被清空了,但是ls看到的大小依然没有发生变化,也就是说文件中产生了大量空洞。

解决方法

将nohup启动进程后的输出重定向 > 替换为 >>, 即改为append模式来写入日志,这时再truncate就不会出现上面的问题了。

 nohup ./daemon.sh >>out.log 2>&1  </dev/null &
 
 
[root@localhost t]# ll -h out.log ;du -h out.log 
-rw-r--r-- 1 root root 48M Oct 19 19:43 out.log
64M   out.log
[root@localhost t]# ll -h out.log ;du -h out.log 
-rw-r--r-- 1 root root 77M Oct 19 19:43 out.log
128M  out.log
[root@localhost t]# truncate -s0 out.log              
[root@localhost t]# ll -h out.log ;du -h out.log 
-rw-r--r-- 1 root root 1.3M Oct 19 19:43 out.log
2.0M  out.log

这里留一个问题: 为什么使用append模式就不会出现这个问题?

参考文档:

[1] https://www.gnu.org/software/bash/manual/bash.html#Redirections

[2] https://www.gnu.org/software/coreutils/manual/html_node/nohup-invocation.html