线上问题解决-socket: too many open files(打开的文件过多)

     ▲ 点击上方"DevOps和k8s全栈技术"关注公众号


错误信息:

Failed to create pod sandbox: rpc error: code = Unknown desc =kubelet 133.0.218142failed pulling image"133.0.6.129:8021 /library/pause:3.1". Errorresponse from daemon: Get http;413306129:8021/v2/: dial tcp133.0.6.129:802t: socket: too many open files

从字面意思上看就是说程序打开的文件数过多,不过这里的files不单是文件的意思,也包括打开的通讯链接(比如socket),正在监听的端口等等,所以有时候也可以叫做句柄(handle),这个错误通常也可以叫做句柄数超出系统限制。

分析:

引起的原因就是进程在某个时刻打开了超过系统限制的文件数量以及通讯链接数,通过命令ulimit -a可以查看当前系统设置的最大句柄数是多少:

ef06d9152918dedc64f53e161ec3fb62.png

查看当前系统打开的文件数量:

代码如下:

lsof | wc -l 

watch "lsof | wc -l"

查看某一进程的打开文件数量:

代码如下:

lsof -p pid | wc -l 

lsof -p 1234 | wc -l 

解决方案

增大允许打开的文件数——命令方式:

1.临时(重启后失效): 

ulimit -n 1024000(非root用户限制到4096)    

2.永久生效(需要重启)

vim /etc/security/limits.conf  

#在最后加入  

* soft nofile 1024000 

* hard nofile 1024000

2.检查程序问题

  如果你对你的程序有一定的解的话,应该对程序打开文件数(链接数)上限有一定的估算,如果感觉数字异常,请使用第一步的lsof -p 进程id > openfiles.log命令,获得当前占用句柄的全部详情进行分析

1)打开的这些文件是不是都是必要的?
2)定位到打开这些文件的代码
3)是否程序操作了文件写入,但是没有进行正常关闭
4)是否程序进行了通讯,但是没有正常关闭(也就是没有超时结束的机制)–(eg:post.releaseConnection()关闭连接)

 代码中只用post.releaseConnection()关闭连接,经过再网上查找资料,说这种关闭并没有真正关闭连接,而是将该连接提交给 MultiThreadedHttpConnectionManager,等待复用。

Close_wate需要延迟几秒钟才能关闭连接,而每个Socket连接都需要等待几秒钟,压力过大时,开启的Socket连接超过了系统所能承受的最大连接数(ulimit -u 1024000),所以抛出Too many open files异常。

精彩文章推荐

年底了,总结下这一年,收获满满

看看kubernetes在2022年的这11个数据

Kuberentes 上 GitOps 最佳实践

Kubernetes常见的日常故障处理指南|干货分享|适合各类基础人员学习

K8S大规模集群优化方案系列文章-第一篇

微信公众号

   点亮收藏,服务器10年不宕机1d5ba466aea2af72351de1be701b6188.gifd4ee05dda3344fc31767ca70fc6cb8bb.gif

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
The error "java socket: too many open files" typically occurs when your Java program reaches the maximum limit of concurrently open files that can be handled by the operating system. This limit is usually determined by the ulimit value set on your system. To resolve this issue, you can take the following steps: 1. Check the current ulimit value by running the command: `ulimit -n`. This will display the maximum number of open files allowed per process. 2. If the current value is too low, you can temporarily increase it by running: `ulimit -n <new_value>`, where `<new_value>` is the desired higher limit. Please note that this change is temporary and will be valid for the current session only. 3. Alternatively, you can permanently increase the ulimit value by modifying the system configuration file. The location of this file varies depending on the Linux distribution you are using. For example, in Ubuntu, you can edit the `/etc/security/limits.conf` file and add a line like this: ``` * hard nofile <new_value> ``` 4. If you're creating multiple socket connections, make sure you close each socket after you're done using it. Failing to close sockets can result in resource leaks and eventually lead to reaching the maximum open file limit. 5. Consider optimizing your code to reuse existing socket connections instead of creating new ones for each request. This can help reduce the number of open files and improve performance. By following these steps, you should be able to resolve the "java socket: too many open files" error.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值