kubernetes 在minikube集群中部署容器化的.Net7 WebAPI应用程序失败,同时initContainers进行“dotnet ef database update”

yftpprvb  于 2023-10-17  发布在  Kubernetes
关注(0)|答案(1)|浏览(140)

我已经创建了新的.Net 7.0 webapi项目,该项目具有Entity framework core 7,并且必须部署在本地minikube集群中。以下是项目参考资料

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.10" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.9" />
        <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.9">

这里有docker文件

WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["IAMHub1.csproj", "IAMHub1/"]
RUN dotnet restore "IAMHub1/IAMHub1.csproj"
WORKDIR "/src/IAMHub1"
COPY . .

RUN dotnet tool install --global dotnet-ef --version 7.0

RUN dotnet build "IAMHub1.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "IAMHub1.csproj" -c Release -o /app/publish /p:UseAppHost=false

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

下面是部署yaml for webApi应用程序

kind: Deployment
metadata:
  name: iamhub-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: iamhub
  template:
    metadata:
      labels:
        app: iamhub
    spec:
      initContainers:
        - name: database-migration
          image: mcr.microsoft.com/dotnet/sdk:7.0
          command: [ "dotnet", "ef", "database", "update" ]
      containers:
        - name: iamhub-container
          image: my-username/iamhub1:latest
          env:
          - name: SQL_SERVER_CONNECTION_STRING
            valueFrom:
             secretKeyRef:
               name: xyz-secret
               key: connectionstring
          ports:
            - containerPort: 80

对于minikube集群中的sql server,我已经使用以下命令安装了simcube/mssqlserver-2022

.\helm install my-mssqlserver-2022 simcube/mssqlserver-2022 --version 1.2.3

my-mssqlserver-2022已在minikube集群中安装并成功运行

我的目标是在部署应用程序时更新数据库

问题:-在应用部署yaml后,它创建了部署,但容器没有启动,在调查后,我得到了以下错误
1.当我描述(kubectl describe pod pod-id)时,它会显示以下事件

Events:
  Type     Reason     Age                From               Message
  ----     ------     ----               ----               -------
  Normal   Scheduled  32s                default-scheduler  Successfully assigned default/iamhub-deployment-6f8857994f-8mqpw to minikube
  Normal   Pulled     15s (x3 over 32s)  kubelet            Container image "mcr.microsoft.com/dotnet/sdk:7.0" already present on machine
  Normal   Created    15s (x3 over 32s)  kubelet            Created container database-migration
  Normal   Started    15s (x3 over 32s)  kubelet            Started container database-migration
  Warning  BackOff    2s (x4 over 29s)   kubelet            Back-off restarting failed container database-migration in pod iamhub-deployment-6f8857994f-8mqpw_default

1.当使用kubectl logs iamhub-deployment-6f8857994f-8mqpw -c database-migration检查日志时,将出现以下错误

Could not execute because the specified command or file was not found.
Possible reasons for this include:
  * You misspelled a built-in dotnet command.
  * You intended to execute a .NET program, but dotnet-ef does not exist.
  * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.

如果我能知道这里出了什么问题并找到可能的解决办法,那就太好了。提前感谢!

t30tvxxf

t30tvxxf1#

在项目目录(本地开发计算机)中,运行以下命令:

dotnet new tool-manifest
dotnet tool install dotnet-ef --version 7.0

这将在项目中生成一个.config目录,并在其中创建一个dotnet-tools.json文件。我们应该确保.config/dotnet-tools.json文件提交给源代码控制。然后我们可以修复Could not execute because the specified command or file was not found.错误。
docker文件:

# Build and publish the application using the SDK
FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
WORKDIR /src
COPY ["IAMHub1.csproj", "IAMHub1/"]
COPY .config .config
RUN dotnet restore "IAMHub1/IAMHub1.csproj"

# Restore local dotnet tools
RUN dotnet tool restore

COPY . .

# Build the application
RUN dotnet build "IAMHub1.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "IAMHub1.csproj" -c Release -o /app/publish /p:UseAppHost=false

# Create the final runtime image
FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "IAMHub1.dll"]

相关问题