aws ubuntu ec2, nginx, uwsgi, django 11: Resource temporarily unavailable 에러

     

ec2위에 nginx + uwsgi + django를 docker로 구성해놨는데, 앱 사용자가 늘어나면서 어느 순간 죽기 시작했다. nginx로그를 떠보면 아래와 같다. 

 

2021/06/30 09:10:39 [error] 31#31: *279264 connect() to unix:/srv/ec2-docker-compose/apps.sock failed (11: Resource temporarily unavailable) while connecting to upstream, client: 172.31.21.91, server: localhost, request: "POST /graphql/ HTTP/1.1", upstream: "uwsgi://unix:/srv/ec2-docker-compose/apps.sock:", host: "xxxxx.co"

 

검색해보면 동시접속 세션이 넘쳐서 에러가 난다는것 같은데, 그만큼 사용자가 많은가? 생각하다가 하나씩 검증해보기로 한다. 일단 OS에서 최대 접속 가능한 소켓수를 확인한다.

 

cat /proc/sys/net/core/somaxconn

 

요롷게 하면 아마 기본 ubuntu EC2는 128을 반환할 것이다. 디폴트로 128개의 소켓만 받도록 되어있나 보다. 이걸 4096으로 늘려준다.

 

sudo /sbin/sysctl -w net.core.somaxconn=4096​

 

근데 이게 ec2가 재 실행되면 초기화가 되기 때문에, 재부팅해도 유지되도록 config를 바꿔줘야한다. /etc/sysctl.conf에 다음 줄을 추가하면 재부팅되어도 설정이 유지된다.

 

net.core.somaxconn = 4096

 

 

그다음 uwsgi.ini파일을 좀 봐줘야 하는데 listen이라는 옵션을 설정해 줄 필요가 있다.

https://uwsgi-docs.readthedocs.io/en/latest/articles/TheArtOfGracefulReloading.html?highlight=listen#the-listen-queue 

 

The Art of Graceful Reloading — uWSGI 2.0 documentation

We all make mistakes, sysadmins must improve their skill of fast disaster recovery. Focusing on avoiding them is a waste of time. Unfortunately, we are all humans. Rolling back deployments could be your life-safer. We have seen how zerg mode allows us to h

uwsgi-docs.readthedocs.io

 

위에 공식 문서를 보면 알겠지만 uwsgi의 listen queue 사이즈는 운영체제의 somaxconn 수보다 작거나 같아야 한다. 크면 에러 나는 듯? ㅎㅎ

 

그래서 혹시 모르니까 크기를 4000으로 만들었다.

 

[uwsgi]
...
listen = 4000

 

마지막으로, 추후 nginx에서 Too many open files라는 에러가 발생할 수 있다는 것을 파악하고 ( 아마 소켓을 동시에 많이 열면 발생하는 듯) 시스템 설정으로 open file 값을 바꿔주기로 결정했다.

 

/etc/security/limits.conf 

 

위 경로의 파일을 열어서 아래 내용을 입력해주면 끝~!

 

# open files
     ubuntu hard nofile 512000
     ubuntu soft no file 512000 

# process
     ubuntu hard nproc 512000
     ubuntu soft nproc 512000

 

 

 

 

 

 

 

반응형

댓글

Designed by JB FACTORY