Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 

Python - uwsgi 응용 프로그램이 k8s 환경에서 OOM 발생하는 문제

직장 동료가 재미있는 오류를 하나 해결했습니다. ^^ uwsgi로 유독 k8s에 호스팅하는 경우에만 다음과 같은 예외가 발생하는 것인데요,

# cat uwsgi.ini
[uwsgi]

limit-as = 256 # limit the project to 256 MB

# uwsgi --http :8001 --chdir /app/my_tennis_club --ini ./uwsgi.ini
[uWSGI] getting INI configuration from ./uwsgi.ini
*** Starting uWSGI 2.0.24 (64bit) on [Thu Mar 21 05:37:03 2024] ***
compiled with version: 8.3.0 on 14 March 2024 07:42:21
os: Linux-5.15.150-1-MANJARO #1 SMP PREEMPT Fri Mar 1 16:56:12 UTC 2024
nodename: python-demo-deployment-f699c6585-n44h8
machine: x86_64
clock source: unix
detected number of CPU cores: 16
current working directory: /app
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
chdir() to /app/my_tennis_club
*** WARNING: you are running uWSGI without its master process manager ***
limiting address space of processes...
your process address space limit is 268435456 bytes (256 MB)
your memory page size is 4096 bytes
detected max file descriptor number: 1073741816
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uWSGI http bound on :8001 fd 4
spawned uWSGI http 1 (pid: 18)
uwsgi socket 0 bound to TCP address 127.0.0.1:39361 (port auto-assigned) fd 3
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
Python version: 3.11.4 (main, Jun 13 2023, 15:34:37) [GCC 8.3.0]
malloc(): Cannot allocate memory [core/utils.c line 1801]
!!! tried memory allocation of 8589934528 bytes !!!
*** backtrace of 18 ***
uwsgi(uwsgi_backtrace+0x2a) [0x55dde7709c5a]
uwsgi(uwsgi_malloc+0x5d) [0x55dde76c1eed]
uwsgi(uwsgi_corerouter_loop+0x36) [0x55dde772d556]
uwsgi(gateway_respawn+0x20a) [0x55dde770571a]
uwsgi(register_gateway+0x1c2) [0x55dde7705a12]
uwsgi(uwsgi_corerouter_init+0x9e) [0x55dde772ccbe]
uwsgi(http_init+0x33) [0x55dde772f933]
uwsgi(uwsgi_start+0x527) [0x55dde770b1a7]
uwsgi(uwsgi_setup+0x23a7) [0x55dde770e5c7]
uwsgi(main+0x9) [0x55dde76be7e9]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xeb) [0x7f3b321da09b]
uwsgi(_start+0x2a) [0x55dde76be81a]
*** end of backtrace ***
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x7f3b328ff8f8
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 72904 bytes (71 KB) for 1 cores
*** Operational MODE: single process ***
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 17, cores: 1)

원인을 다음의 글에서 찾아냈습니다.

uWSGI OOMKilled on Kubernetes #9562
; https://github.com/DefectDojo/django-DefectDojo/issues/9562

정리해 보면, uwsgi 실행 시 --max-fd 옵션을 이용해 file descriptor의 최댓값을 제한하는 것으로 해결했다는 것입니다.




사실, 저건 k8s의 문제만은 아닙니다. 다음의 글에 이와 관련한 문제가 또 나오는데요,

High memory usage if fs.nr_open is very high and no ulimit set on Linux systems #2299
; https://github.com/unbit/uwsgi/issues/2299

즉, uwsgi가 시스템에 설정된 file descriptor의 최댓값을 그대로 적용해 내부적으로 메모리를 할당하는 문제가 있는 것입니다. 실제로 위에서 예를 든 k8s의 문제가 되었던 uwsgi 로그 출력 내용에,

...[생략]...
detected max file descriptor number: 1073741816
...[생략]...

라는 문구가 나오는데요, 결국 시스템에 저 값이 설정돼 있기 때문에 uwsgi는 그에 따라 반응한 것입니다. 따라서, 이 문제는 단순히 uwsgi의 --max-fd 옵션으로 해결할 것이 아니라, 어차피 해당 옵션으로 제한할 수 있다면 애당초 uwsgi 스스로 내부에서 file descriptor를 위한 기본값을 1048576 정도로 낮게 설정해 두는 것이 더 나을 수 있었을 것입니다.

그나저나, Windows의 Ubutun 20.04 WSL 환경에서는 이 값이,

$ sysctl fs.nr_open
fs.nr_open = 1048576

1048576로 나오는데요, uwsgi의 출력 화면에서는 여전히 "detected max file descriptor number: 1024"로 잡힙니다.

별도로 설치한 Ubuntu 18.04의 경우에도 fs.nr_open은 1048576이었는데, 문제가 되었던 1073741816으로 대폭 상향시켜도,

$ sudo sysctl -w fs.nr_open=1073741816

$ django-admin startproject testdj

$ uwsgi --http :8000 --chdir ./testdj/  --wsgi-file ./testdj/wsgi.py 
...[생략]...
detected max file descriptor number: 1048576
...[생략]...

여전히 uwsgi는 1048576 값을 출력합니다. 뭔가, 이쪽도 알 수 없는 설정 절차가 있는 듯합니다. ^^;




참고로, Ubuntu 18.04 테스트 머신에서 "sysctl -w fs.nr_open=1073741816" 설정 후 재부팅하고 나면 다시 1048576으로 돌아옵니다. 관련 sysctl.conf 파일의 내용을 보면 이렇고,

$ cat /etc/sysctl.conf
#
# /etc/sysctl.conf - Configuration file for setting system variables
# See /etc/sysctl.d/ for additional system variables.
# See sysctl.conf (5) for information.
#

#kernel.domainname = example.com

# Uncomment the following to stop low-level messages on console
#kernel.printk = 3 4 1 3

##############################################################3
# Functions previously found in netbase
#

# Uncomment the next two lines to enable Spoof protection (reverse-path filter)
# Turn on Source Address Verification in all interfaces to
# prevent some spoofing attacks
#net.ipv4.conf.default.rp_filter=1
#net.ipv4.conf.all.rp_filter=1

# Uncomment the next line to enable TCP/IP SYN cookies
# See http://lwn.net/Articles/277146/
# Note: This may impact IPv6 TCP sessions too
#net.ipv4.tcp_syncookies=1

# Uncomment the next line to enable packet forwarding for IPv4
#net.ipv4.ip_forward=1

# Uncomment the next line to enable packet forwarding for IPv6
#  Enabling this option disables Stateless Address Autoconfiguration
#  based on Router Advertisements for this host
#net.ipv6.conf.all.forwarding=1


###################################################################
# Additional settings - these settings can improve the network
# security of the host and prevent against some network attacks
# including spoofing attacks and man in the middle attacks through
# redirection. Some network environments, however, require that these
# settings are disabled so review and enable them as needed.
#
# Do not accept ICMP redirects (prevent MITM attacks)
#net.ipv4.conf.all.accept_redirects = 0
#net.ipv6.conf.all.accept_redirects = 0
# _or_
# Accept ICMP redirects only for gateways listed in our default
# gateway list (enabled by default)
# net.ipv4.conf.all.secure_redirects = 1
#
# Do not send ICMP redirects (we are not a router)
#net.ipv4.conf.all.send_redirects = 0
#
# Do not accept IP source route packets (we are not a router)
#net.ipv4.conf.all.accept_source_route = 0
#net.ipv6.conf.all.accept_source_route = 0
#
# Log Martian Packets
#net.ipv4.conf.all.log_martians = 1
#

###################################################################
# Magic system request Key
# 0=disable, 1=enable all
# Debian kernels have this set to 0 (disable the key)
# See https://www.kernel.org/doc/Documentation/sysrq.txt
# for what other values do
#kernel.sysrq=1

###################################################################
# Protected links
#
# Protects against creating or following links under certain conditions
# Debian kernels have both set to 1 (restricted) 
# See https://www.kernel.org/doc/Documentation/sysctl/fs.txt
#fs.protected_hardlinks=0
#fs.protected_symlinks=0

/etc/sysctl.d 디렉터리에는 이런 파일들이 있는데,

$ ls /etc/sysctl.d
10-console-messages.conf  10-kernel-hardening.conf   10-magic-sysrq.conf       10-ptrace.conf    99-sysctl.conf
10-ipv6-privacy.conf      10-link-restrictions.conf  10-network-security.conf  10-zeropage.conf  README

그중에 99-sysctl.conf 파일이 /etc/sysctl.conf와 링크가 된 파일입니다. 그래서, 일단 /etc/sysctl.conf 파일의 마지막에,

$ cat /etc/sysctl.conf
...[생략]...
fs.nr_open=1073741816

값을 직접 기록하고 재부팅을 했더니 저 값이 유지됩니다. (찾아보니까 "sysctl -p" 옵션으로 재부팅 없이 반영할 수 있다고 합니다.)

그렇다 해도 여전히 uwsgi는 1048576으로 설정이 잡히는데요, 휴~~~ 일부러 1073741816 값으로 인한 오동작을 재현하려고 해도 쉽지가 않군요. ^^;

마지막으로, fs.nr_open을 확인하는 명령어에서 단순히 fs만 주면 모든 값을 볼 수 있습니다.

$ sudo sysctl fs
[sudo] password for kevin: 
fs.aio-max-nr = 65536
fs.aio-nr = 0
fs.binfmt_misc.status = enabled
fs.dentry-state = 89220 72200   45      0       47968   0
fs.dir-notify-enable = 1
fs.epoll.max_user_watches = 822005
fs.file-max = 400247
fs.file-nr = 2432       0       400247
fs.inode-nr = 42041     905
fs.inode-state = 42041  905     0       0       0       0       0
fs.inotify.max_queued_events = 16384
fs.inotify.max_user_instances = 128
fs.inotify.max_user_watches = 8192
fs.lease-break-time = 45
fs.leases-enable = 1
fs.mount-max = 100000
fs.mqueue.msg_default = 10
fs.mqueue.msg_max = 10
fs.mqueue.msgsize_default = 8192
fs.mqueue.msgsize_max = 8192
fs.mqueue.queues_max = 256
fs.nr_open = 1048576
fs.overflowgid = 65534
fs.overflowuid = 65534
fs.pipe-max-size = 1048576
fs.pipe-user-pages-hard = 0
fs.pipe-user-pages-soft = 16384
fs.protected_fifos = 0
fs.protected_hardlinks = 1
fs.protected_regular = 0
fs.protected_symlinks = 1
fs.quota.allocated_dquots = 0
fs.quota.cache_hits = 0
fs.quota.drops = 0
fs.quota.free_dquots = 0
fs.quota.lookups = 0
fs.quota.reads = 0
fs.quota.syncs = 0
fs.quota.warnings = 1
fs.quota.writes = 0
fs.suid_dumpable = 0
fs.xfs.error_level = 3
fs.xfs.filestream_centisecs = 3000
fs.xfs.inherit_noatime = 1
fs.xfs.inherit_nodefrag = 1
fs.xfs.inherit_nodump = 1
fs.xfs.inherit_nosymlinks = 0
fs.xfs.inherit_sync = 1
fs.xfs.irix_sgid_inherit = 0
fs.xfs.irix_symlink_mode = 0
fs.xfs.panic_mask = 0
fs.xfs.rotorstep = 1
fs.xfs.speculative_cow_prealloc_lifetime = 1800
fs.xfs.speculative_prealloc_lifetime = 300
fs.xfs.stats_clear = 0
fs.xfs.xfssyncd_centisecs = 3000




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 3/31/2024]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




1  2  3  [4]  5  6  7  8  9  10  11  12  13  14  15  ...
NoWriterDateCnt.TitleFile(s)
13552정성태2/13/20242050닷넷: 2214. windbg - Monitor.Enter의 thin lock과 fat lock
13551정성태2/12/20242369닷넷: 2213. ASP.NET/Core 웹 응용 프로그램 - 2차 스레드의 예외로 인한 비정상 종료
13550정성태2/11/20242545Windows: 256. C# - Server socket이 닫히면 Accept 시켰던 자식 소켓이 닫힐까요?
13549정성태2/3/20242947개발 환경 구성: 706. C# - 컨테이너에서 실행하기 위한 (소켓) 콘솔 프로젝트 구성
13548정성태2/1/20242712개발 환경 구성: 705. "Docker Desktop for Windows" - ASP.NET Core 응용 프로그램의 소켓 주소 바인딩(IPv4/IPv6 loopback, Any)
13547정성태1/31/20242466개발 환경 구성: 704. Visual Studio - .NET 8 프로젝트부터 dockerfile에 추가된 "USER app" 설정
13546정성태1/30/20242289Windows: 255. (디버거의 영향 등으로) 대상 프로세스가 멈추면 Socket KeepAlive로 연결이 끊길까요?
13545정성태1/30/20242186닷넷: 2212. ASP.NET Core - 우선순위에 따른 HTTP/HTTPS 호스트:포트 바인딩 방법
13544정성태1/30/20242215오류 유형: 894. Microsoft.Data.SqlClient - Could not load file or assembly 'System.Security.Permissions, ...'
13543정성태1/30/20242259Windows: 254. Windows - 기본 사용 중인 5357 포트 비활성화는 방법
13542정성태1/30/20242258오류 유형: 893. Visual Studio - Web Application을 실행하지 못하는 IISExpress - 두 번째 이야기
13541정성태1/29/20242373VS.NET IDE: 188. launchSettings.json의 useSSL 옵션
13540정성태1/29/20242504Linux: 69. 리눅스 - "Docker Desktop for Windows" Container 환경에서 IPv6 Loopback Address 바인딩 오류
13539정성태1/26/20242475개발 환경 구성: 703. Visual Studio - launchSettings.json을 이용한 HTTP/HTTPS 포트 바인딩
13538정성태1/25/20242660닷넷: 2211. C# - NonGC(FOH) 영역에 .NET 개체를 생성파일 다운로드1
13537정성태1/24/20242719닷넷: 2210. C# - Native 메모리에 .NET 개체를 생성파일 다운로드1
13536정성태1/23/20242744닷넷: 2209. .NET 8 - NonGC Heap / FOH (Frozen Object Heap) [1]
13535정성태1/22/20242683닷넷: 2208. C# - GCHandle 구조체의 메모리 분석
13534정성태1/21/20242432닷넷: 2207. C# - SQL Server DB를 bacpac으로 Export/Import파일 다운로드1
13533정성태1/18/20242617닷넷: 2206. C# - TCP KeepAlive의 서버 측 구현파일 다운로드1
13532정성태1/17/20242531닷넷: 2205. C# - SuperSimpleTcp 사용 시 주의할 점파일 다운로드1
13531정성태1/16/20242568닷넷: 2204. C# - TCP KeepAlive에 새로 추가된 Retry 옵션파일 다운로드1
13530정성태1/15/20242328닷넷: 2203. C# - Python과의 AES 암호화 연동파일 다운로드1
13529정성태1/15/20242325닷넷: 2202. C# - PublishAot의 glibc에 대한 정적 링킹하는 방법
13528정성태1/14/20242473Linux: 68. busybox 컨테이너에서 실행 가능한 C++, Go 프로그램 빌드
13527정성태1/14/20242329오류 유형: 892. Visual Studio - Failed to launch debug adapter. Additional information may be available in the output window.
1  2  3  [4]  5  6  7  8  9  10  11  12  13  14  15  ...