gpt4 book ai didi

.net - 无法在 Kubernetes 中运行 .NET 应用程序

转载 作者:行者123 更新时间:2023-12-03 08:07:53 27 4
gpt4 key购买 nike

我正在尝试在本地 kubernetes 集群(Kind)中部署一个简单的 .NET 应用程序以进行测试。应用部署时,Pod 启动时不会出现错误。但镜像构建得很好,因为如果在 Docker 中本地启动,容器就可以很好地工作。

NAME                             READY   STATUS    RESTARTS   AGE
orderproducer-68d5ff7944-d2d89 0/1 Error 4 103s
kubectl logs -l app=orderproducer

Could not execute because the application was not found or a compatible .NET SDK is not installed.
Possible reasons for this include:
* You intended to execute a .NET program:
The application 'OrderProducer.dll' does not exist.
* You intended to execute a .NET SDK command:
It was not possible to find any installed .NET SDKs.
Install a .NET SDK from:
https://aka.ms/dotnet-download

这很奇怪,因为如果我在本地(而不是在集群中)从同一个镜像启动一个 docker 容器,它就可以正常工作。此外,我已在该容器上运行 bash 并确保 OrderProducer.dll 确实出现在 /app 文件夹(这是一个工作目录)中。

xxx@xxx:/mnt/c/Users/xxx$ docker run --name test6 orderproducer:latest
Order Producer has started!
Kafka broker: 127.0.0.1:9092

你知道我的错误是什么吗?为什么它在 Docker 中运行,而不是在 K8s 的 pod 中运行?我已经花了大约3个小时试图弄清楚,但仍然没有。非常感谢。

这是我使用的一些工件。

Dockerfile:

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

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["OrderProducer/OrderProducer.csproj", "OrderProducer/"]
COPY ["Common/Common.csproj", "Common/"]
RUN dotnet restore "OrderProducer/OrderProducer.csproj"
COPY . .
WORKDIR "/src/OrderProducer"
RUN dotnet build "OrderProducer.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "OrderProducer.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "OrderProducer.dll"]

然后我构建一个图像并使其可访问:

docker build -f OrderProducer/Dockerfile -t orderproducer:latest .
kind load docker-image orderproducer:latest

然后我应用部署:

kubectl apply -f orderproducer-deployment.yml

订单生产者部署.yml

apiVersion: apps/v1
kind: Deployment
metadata:
name: orderproducer
spec:
replicas: 1
selector:
matchLabels:
app: orderproducer
template:
metadata:
labels:
app: orderproducer
spec:
containers:
- name: orderproducer
image: orderproducer:latest
imagePullPolicy: Never
resources:
limits:
memory: "128Mi"
cpu: "500m"

最佳答案

原因是当卷安装到 /app 时,它会清除容器的所有 /app 内容。

我通过编辑以下部署部分解决了该问题:

volumeMounts:
- name: appsettings-volume
mountPath: /app/appSettings.json
subPath: appSettings.json

关于.net - 无法在 Kubernetes 中运行 .NET 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71625732/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com