[zz]Troubleshooting Memory Usage

本文介绍了一个用于监控服务器内存使用的Shell脚本。该脚本能够记录当前日期、内存使用情况及运行中的进程,并定期记录这些信息到日志文件中。此外,还提供了设置cron任务以定时运行此脚本的方法。
[url]http://rimuhosting.com/howto/memory.jsp[/url]

[code]
# create a memmon.sh script that tracks the current date, memory usage and running processes
cat << EOF > /root/memmon.sh
#!/bin/bash
date;
uptime
free -m
vmstat 1 5
ps auxf --width=200
if which iptables 2>&1 > /dev/null; then
iptables -L | diff iptables_default - | awk '{print "IPTABLES: " $0}'
iptables -L > iptables_default
else
echo "IPTABLES MISSING"
fi
dmesg | diff -u dmesg_default - | grep '^+' | awk '{print "DMESG:" $0}'
dmesg > dmesg_default
EOF

chmod +x /root/memmon.sh

# create a cronjob that runs every few minutes to log the memory usage
echo '0-59/10 * * * * root /root/memmon.sh >> /root/memmon.txt' > /etc/cron.d/memmon
/etc/init.d/cron* restart

# create a logrotate entry so the log file does not get too large
echo '/root/memmon.txt {}' > /etc/logrotate.d/memmon

[/code]
### 什么是 Troubleshooting 镜像? Troubleshooting 镜像是一种专门用于系统故障排查的容器镜像,通常基于一个轻量级操作系统(如 CentOS、Alpine 等),并预装了多种诊断和调试工具。该镜像被设计为在出现问题的环境中快速启动,帮助运维人员进行网络诊断、日志查看、进程追踪、权限检查等操作。 此类镜像常见于 Kubernetes 或 Docker 环境中,用于排查容器无法启动、服务异常、网络不通等问题。通过运行 Troubleshooting 容器,可以访问宿主机的命名空间或挂载相关卷,从而获取更全面的系统信息[^1]。 ### 如何使用 Troubleshooting 镜像? #### 1. 构建 Troubleshooting 镜像 首先,创建一个包含必要工具的 Dockerfile,例如: ```Dockerfile FROM centos:7 LABEL maintainer="admin@example.com" RUN yum update -y && \ yum install -y net-tools iproute tcpdump curl wget vim strace ltrace gdb && \ yum clean all WORKDIR /root/ COPY entrypoint.sh /entrypoint.sh RUN chmod +x /entrypoint.sh ENTRYPOINT ["/entrypoint.sh"] ``` 接着编写 `entrypoint.sh` 脚本以设置初始环境: ```bash #!/bin/bash echo "Starting troubleshooting container..." exec "$@" ``` 然后执行构建命令: ```bash docker build -t troubleshoot-centos7 . ``` #### 2. 运行 Troubleshooting 容器 启动容器时可挂载宿主机的文件系统、网络命名空间等资源,以便进行深度排查: ```bash docker run -it --rm \ --privileged \ --net=host \ --pid=host \ -v /:/host \ troubleshoot-centos7 bash ``` 其中: - `--privileged` 提供容器对宿主机硬件的完全访问权限; - `--net=host` 共享宿主机网络命名空间; - `-v /:/host` 挂载宿主机根目录到 `/host`; - `--pid=host` 允许访问宿主机的进程信息。 #### 3. 使用内置工具进行排查 进入容器后,可以使用如下常用工具进行故障分析: - **网络诊断**:`tcpdump`, `ping`, `traceroute`, `nslookup` - **系统监控**:`top`, `htop`, `iostat`, `vmstat` - **日志查看**:`journalctl`, `dmesg`, `cat /var/log/messages` - **进程跟踪**:`strace`, `ltrace`, `gdb` - **文件与权限检查**:`ls`, `find`, `chmod`, `chown` #### 4. 常见用途场景 - **Pod ImagePullBackOff 问题排查**:可使用 `docker pull` 测试镜像拉取是否正常,并检查网络配置、代理设置、私有仓库认证等。 - **容器运行异常**:通过 `strace` 跟踪容器内进程调用栈,定位卡顿或崩溃原因。 - **节点资源不足**:使用 `top`、`df`、`free` 等命令分析 CPU、内存、磁盘使用情况。 - **Kubernetes 网络问题**:利用 `tcpdump` 抓包分析 Pod 间通信问题。 #### 5. 注意事项 - 确保 Troubleshooting 镜像体积适中,避免因安装过多工具导致部署效率下降。 - 若需从外部仓库拉取基础镜像,应确保网络连通性及代理配置正确。 - 在生产环境中使用时,建议限制容器权限,防止安全风险。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值