自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Docker 中的 .NET 異常了怎么抓 Dump

開發(fā) 前端
雖然Docker中的 .NET 程序占比較少,但把經(jīng)驗(yàn)總結(jié)出來還是很值得的,以后有人問怎么抓,可以把這篇文章直接丟過去啦!

一:背景

1. 講故事

有很多朋友跟我說,在 Windows 上看過你文章知道了怎么抓 Crash, CPU爆高,內(nèi)存暴漲 等各種Dump,為什么你沒有寫在 Docker 中如何抓的相關(guān)文章呢?瞧不上嗎?

哈哈,在DUMP的分析旅程中,跑在 Docker 中的 .NET 占比真的不多,大概10個dump有 1-2 個是 docker 中的,市場決定了我的研究方向,為了彌補(bǔ)這一塊的空洞,決定寫一篇文章來分享下這三大異常下的捕獲吧。

二:Docker 下的三大異常捕獲

1. crash dump 捕獲

前不久我寫了一篇 Linux 上的 .NET 崩潰了怎么抓 Dump (https://www.cnblogs.com/huangxincheng/p/17440153.html) 的文章,使用了微軟推薦的環(huán)境變量方式,其實(shí)這在 Docker 中是一樣適用的。

為了讓 webapi 崩潰退出,我故意造一個棧溢出異常,參考代碼如下:

public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);
            builder.Services.AddAuthorization();
            var app = builder.Build();
            app.UseAuthorization();

            //1. crash
            Task.Factory.StartNew(() =>
            {
                Test("a");
            });

            app.Run();
        }

        public static string Test(string a)
        {
            return Test("a" + a.Length);
        }
    }

有了代碼之后,接下來寫一個 Dockerfile,主要就是把三個環(huán)境變量塞進(jìn)去。

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS runtime
WORKDIR /app
COPY ./ ./

# 1. 使用中科大鏡像源
RUN sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list

ENV COMPlus_DbgMiniDumpType 4
ENV COMPlus_DbgMiniDumpName /dumps/%p-%e-%h-%t.dmp
ENV COMPlus_DbgEnableMiniDump 1

ENTRYPOINT ["dotnet", "AspNetWebApi.dll"]

這里有一個細(xì)節(jié),為了能夠讓 Docker 中的 webapi 能夠訪問到,將 localhost 設(shè)置為 * ,修改 appsettings.json 如下:

{
  "urls": "http://*:5001",
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}

有了這些基礎(chǔ)最后就是 docker build & docker run 啦。

[root@localhost data]# docker build -t aspnetapp .
[+] Building 0.3s (9/9) FINISHED                                                                         
 => [internal] load build definition from Dockerfile                                                0.0s
 => => transferring dockerfile: 447B                                                                0.0s
 => [internal] load .dockerignore                                                                   0.0s
 => => transferring context: 2B                                                                     0.0s
 => [internal] load metadata for mcr.microsoft.com/dotnet/aspnet:6.0                                0.3s
 => [1/4] FROM mcr.microsoft.com/dotnet/aspnet:6.0@sha256:a2a04325fdb2a871e964c89318921f82f6435b54  0.0s
 => [internal] load build context                                                                   0.0s
 => => transferring context: 860B                                                                   0.0s
 => CACHED [2/4] WORKDIR /app                                                                       0.0s
 => CACHED [3/4] COPY ./ ./                                                                         0.0s
 => CACHED [4/4] RUN sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list          0.0s
 => exporting to image                                                                              0.0s
 => => exporting layers                                                                             0.0s
 => => writing image sha256:be69203995c0e5423b2af913549e618d7ee8306fff3961118ff403b1359ae571        0.0s
 => => naming to docker.io/library/aspnetapp                                                        0.0s

[root@localhost data]# docker run -itd  -p 5001:5001 --privileged -v /data2:/dumps --name aspnetcore_sample aspnetapp
ca34c9274d998096f8562cbef3a43a7cbd9aa5ff2923e0f3e702b159e0b2f447

[root@localhost data]# docker ps -a
CONTAINER ID   IMAGE       COMMAND                  CREATED          STATUS                       PORTS     NAMES
ca34c9274d99   aspnetapp   "dotnet AspNetWebApi…"   20 seconds ago   Exited (139) 9 seconds ago             aspnetcore_sample

[root@localhost data]# docker logs ca34c9274d99
   ...
   at AspNetWebApi.Program.Test(System.String)
   at AspNetWebApi.Program.Test(System.String)
   at AspNetWebApi.Program.Test(System.String)
   at AspNetWebApi.Program.Test(System.String)
   at AspNetWebApi.Program+<>c.<Main>b__0_0()
   at System.Threading.Tasks.Task.InnerInvoke()
   at System.Threading.Tasks.Task+<>c.<.cctor>b__272_0(System.Object)
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(System.Threading.Thread, System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(System.Threading.Tasks.Task ByRef, System.Threading.Thread)
   at System.Threading.Tasks.Task.ExecuteEntryUnsafe(System.Threading.Thread)
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool+WorkerThread.WorkerThreadStart()
   at System.Threading.Thread.StartCallback()

[createdump] Gathering state for process 1 dotnet
[createdump] Crashing thread 0017 signal 6 (0006)
[createdump] Writing full dump to file /dumps/1-dotnet-ca34c9274d99-1687746929.dmp
[createdump] Written 261320704 bytes (63799 pages) to core file
[createdump] Target process is alive
[createdump] Dump successfully written

[root@localhost data2]# cd /data2
[root@localhost data2]# ls -ln
total 255288
-rw-------. 1 0 0 261414912 Jun 26 10:35 1-dotnet-ca34c9274d99-1687746929.dmp

上面的腳本已經(jīng)寫的非常清楚了,這里有幾個注意點(diǎn)提一下:

  • --privileged

一定要加上特殊權(quán)限,否則生成 dump 的時候會提示無權(quán)限。

  • -v /data2:/dumps

防止dump丟失,記得掛載到宿主機(jī)目錄 或者 共享容器 中。

2. 內(nèi)存暴漲 dump 捕獲

要想對 docker 中的 .NET 程序內(nèi)存 進(jìn)行監(jiān)控,我一直都是極力推薦 procdump,目前最新的是版本是 1.5, github官網(wǎng)地址:https://github.com/Sysinternals/ProcDump-for-Linux  鑒于現(xiàn)在訪問 github 太慢,大家可以把 procdump_1.5-16239_amd64.deb 下載到本地,為什么下載它,是因?yàn)槿萜髦惺?debain 系統(tǒng)。

圖片圖片

下載好了之后放到項(xiàng)目中,使用默認(rèn)代碼骨架:

public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);
            builder.Services.AddAuthorization();
            var app = builder.Build();
            app.UseAuthorization();

            app.Run();
        }
    }

接下來就是寫 dockerfile 了,這里有一個細(xì)節(jié),就是如何在 Docker 中開啟多進(jìn)程,這里用 start.sh 腳本的方式開啟,參考代碼如下:

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS runtime
WORKDIR /app
COPY ./ ./

# 1. 使用中科大鏡像源
RUN sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list

# 2. 安裝 gdb & procdump
RUN apt-get update && apt-get install -y gdb
RUN dpkg -i procdump.deb

RUN echo "#!/bin/bash \n\
procdump -m 30 -w dotnet /dumps & \n\
dotnet \$1 \n\
" > ./start.sh

RUN chmod +x ./start.sh

ENTRYPOINT ["./start.sh", "AspNetWebApi.dll"]

有了這些設(shè)置后,接下來就是 publish 代碼用 docker 構(gòu)建啦,為了方便演示,這里就用 前臺模式 開啟了哈。

[root@localhost data]# docker build -t aspnetapp .
[+] Building 11.5s (13/13) FINISHED              

[root@localhost data]# docker rm -f aspnetcore_sample
aspnetcore_sample
[root@localhost data]# docker run -it --rm  -p 5001:5001 --privileged -v /data2:/dumps --name aspnetcore_sample aspnetapp
ProcDump v1.5 - Sysinternals process dump utility
Copyright (C) 2023 Microsoft Corporation. All rights reserved. Licensed under the MIT license.
Mark Russinovich, Mario Hewardt, John Salem, Javid Habibi
Sysinternals - www.sysinternals.com

Monitors one or more processes and writes a core dump file when the processes exceeds the
specified criteria.

[02:57:34 - INFO]: Waiting for processes 'dotnet' to launch

[02:57:34 - INFO]: Press Ctrl-C to end monitoring without terminating the process(es).
Process Name:                           dotnet
CPU Threshold:                          n/a
Commit Threshold:                       >=30 MB
Thread Threshold:                       n/a
File Descriptor Threshold:              n/a
Signal:                                 n/a
Exception monitor                       Off
Polling Interval (ms):                  1000
Threshold (s):                          10
Number of Dumps:                        1
Output directory:                       /dumps
[02:57:34 - INFO]: Starting monitor for process dotnet (9)
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://[::]:5001
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /app/
[02:57:35 - INFO]: Trigger: Commit usage:48MB on process ID: 9
[createdump] Gathering state for process 9 dotnet
[createdump] Writing full dump to file /dumps/dotnet_commit_2023-06-26_02:57:35.9
[createdump] Written 254459904 bytes (62124 pages) to core file
[createdump] Target process is alive
[createdump] Dump successfully written
[02:57:35 - INFO]: Core dump 0 generated: /dumps/dotnet_commit_2023-06-26_02:57:35.9
[02:57:36 - INFO]: Stopping monitors for process: dotnet (9)

[root@localhost data2]# ls -lh
total 243M
-rw-------. 1 root root 243M Jun 26 10:57 dotnet_commit_2023-06-26_02:57:35.9

從腳本信息看,當(dāng)內(nèi)存到了 48MB 的時候觸發(fā)的 dump 生成,也成功的進(jìn)入了 /dumps 目錄中,太棒了。

3. cpu爆高 dump 捕獲

抓 cpu 爆高的dump最好的方式就是多抓幾個,比如說:當(dāng) CPU >20% 連續(xù)超過 5s 抓 2個dump,這種方式抓的dump很容易就能找到真兇,為了方便演示,讓兩個 cpu 直接打滿,參考代碼如下:

public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);
            builder.Services.AddAuthorization();
            var app = builder.Build();
            app.UseAuthorization();

            //3. cpu
            app.MapGet("/cpu", (HttpContext httpContext) =>
            {
                Task.Factory.StartNew(() => { bool b = true; while (true) { b = !b; } });
                Task.Factory.StartNew(() => { bool b = true; while (true) { b = !b; } });

                return new WeatherForecast();
            });

            app.Run();
        }

接下來就是修改 dockerfile,因?yàn)槲业奶摂M機(jī)是 8 核心,如果兩個核心被打滿,那應(yīng)該會占用大概 24% 的 cpu 利用率,所以腳本中就設(shè)置 20% 吧。

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS runtime
WORKDIR /app
COPY ./ ./

# 1. 使用中科大鏡像源
RUN sed -i 's/deb.debian.org/mirrors.ustc.edu.cn/g' /etc/apt/sources.list

# 2. 安裝 wget
RUN apt-get update && apt-get install -y gdb
RUN dpkg -i procdump.deb

RUN echo "#!/bin/bash \n\
procdump -c 20 -n 2 -s 5 -w dotnet /dumps & \n\
dotnet \$1 \n\
" > ./start.sh

RUN chmod +x ./start.sh

ENTRYPOINT ["./start.sh", "AspNetWebApi.dll"]

最后就是 docker 構(gòu)建。

[root@localhost data]# docker build -t aspnetapp .
[+] Building 0.4s (13/13) FINISHED

[root@localhost data]# docker run -it --rm  -p 5001:5001 --privileged -v /data2:/dumps --name aspnetcore_sample aspnetapp

ProcDump v1.5 - Sysinternals process dump utility
Copyright (C) 2023 Microsoft Corporation. All rights reserved. Licensed under the MIT license.
Mark Russinovich, Mario Hewardt, John Salem, Javid Habibi
Sysinternals - www.sysinternals.com

Monitors one or more processes and writes a core dump file when the processes exceeds the
specified criteria.

[03:35:56 - INFO]: Waiting for processes 'dotnet' to launch

[03:35:56 - INFO]: Press Ctrl-C to end monitoring without terminating the process(es).
Process Name:                           dotnet
CPU Threshold:                          >= 20%
Commit Threshold:                       n/a
Thread Threshold:                       n/a
File Descriptor Threshold:              n/a
Signal:                                 n/a
Exception monitor                       Off
Polling Interval (ms):                  1000
Threshold (s):                          5
Number of Dumps:                        2
Output directory:                       /dumps
[03:35:56 - INFO]: Starting monitor for process dotnet (8)
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://[::]:5001
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
      Content root path: /app/

看輸出是正在監(jiān)控,接下來我們訪問下網(wǎng)址:http://192.168.17.129:5001/cpu , 稍等片刻之后就會生成兩個dump 文件。

圖片圖片

三:總結(jié)

雖然Docker中的 .NET 程序占比較少,但把經(jīng)驗(yàn)總結(jié)出來還是很值得的,以后有人問怎么抓,可以把這篇文章直接丟過去啦!

責(zé)任編輯:武曉燕 來源: 一線碼農(nóng)聊技術(shù)
相關(guān)推薦

2009-01-05 09:14:17

.NETcatch性能損失

2014-08-01 10:18:16

.Netdump

2013-08-22 17:10:09

.Net異常處理

2024-06-04 12:32:42

.NET開發(fā)內(nèi)存泄漏

2010-09-06 08:43:13

.NET 4

2021-10-26 10:42:49

NET進(jìn)程托管

2020-09-29 12:15:13

生死鎖MySQL

2010-01-05 09:26:13

.NET 4.0

2024-09-24 17:34:05

2023-08-28 10:54:09

容器Docker

2009-12-22 15:51:18

ADO.NET代碼

2010-04-14 09:20:26

.NET多線程

2009-07-29 09:53:24

ASP.NET異常管理

2009-07-29 17:20:18

應(yīng)用程序異常

2021-06-05 23:41:47

NET異常 HttpClient

2010-01-06 16:45:42

.Net Framew

2009-02-06 14:11:36

ASP.NET.NET全局異常處理

2021-06-06 13:07:06

.NETWindowsLinux

2024-11-21 09:30:38

內(nèi)存泄漏CPU

2024-11-22 09:40:18

Visual內(nèi)存泄漏內(nèi)存
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號