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

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

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

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

这里有docker文件

  1. WORKDIR /app
  2. EXPOSE 80
  3. EXPOSE 443
  4. FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
  5. WORKDIR /src
  6. COPY ["IAMHub1.csproj", "IAMHub1/"]
  7. RUN dotnet restore "IAMHub1/IAMHub1.csproj"
  8. WORKDIR "/src/IAMHub1"
  9. COPY . .
  10. RUN dotnet tool install --global dotnet-ef --version 7.0
  11. RUN dotnet build "IAMHub1.csproj" -c Release -o /app/build
  12. FROM build AS publish
  13. RUN dotnet publish "IAMHub1.csproj" -c Release -o /app/publish /p:UseAppHost=false
  14. FROM base AS final
  15. WORKDIR /app
  16. COPY --from=publish /app/publish .
  17. ENTRYPOINT ["dotnet", "IAMHub1.dll"]
  18. e

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

  1. kind: Deployment
  2. metadata:
  3. name: iamhub-deployment
  4. spec:
  5. replicas: 1
  6. selector:
  7. matchLabels:
  8. app: iamhub
  9. template:
  10. metadata:
  11. labels:
  12. app: iamhub
  13. spec:
  14. initContainers:
  15. - name: database-migration
  16. image: mcr.microsoft.com/dotnet/sdk:7.0
  17. command: [ "dotnet", "ef", "database", "update" ]
  18. containers:
  19. - name: iamhub-container
  20. image: my-username/iamhub1:latest
  21. env:
  22. - name: SQL_SERVER_CONNECTION_STRING
  23. valueFrom:
  24. secretKeyRef:
  25. name: xyz-secret
  26. key: connectionstring
  27. ports:
  28. - containerPort: 80

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

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

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

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

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

  1. Events:
  2. Type Reason Age From Message
  3. ---- ------ ---- ---- -------
  4. Normal Scheduled 32s default-scheduler Successfully assigned default/iamhub-deployment-6f8857994f-8mqpw to minikube
  5. Normal Pulled 15s (x3 over 32s) kubelet Container image "mcr.microsoft.com/dotnet/sdk:7.0" already present on machine
  6. Normal Created 15s (x3 over 32s) kubelet Created container database-migration
  7. Normal Started 15s (x3 over 32s) kubelet Started container database-migration
  8. 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检查日志时,将出现以下错误

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

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

t30tvxxf

t30tvxxf1#

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

  1. dotnet new tool-manifest
  2. 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文件:

  1. # Build and publish the application using the SDK
  2. FROM mcr.microsoft.com/dotnet/sdk:7.0 AS build
  3. WORKDIR /src
  4. COPY ["IAMHub1.csproj", "IAMHub1/"]
  5. COPY .config .config
  6. RUN dotnet restore "IAMHub1/IAMHub1.csproj"
  7. # Restore local dotnet tools
  8. RUN dotnet tool restore
  9. COPY . .
  10. # Build the application
  11. RUN dotnet build "IAMHub1.csproj" -c Release -o /app/build
  12. FROM build AS publish
  13. RUN dotnet publish "IAMHub1.csproj" -c Release -o /app/publish /p:UseAppHost=false
  14. # Create the final runtime image
  15. FROM mcr.microsoft.com/dotnet/aspnet:7.0 AS base
  16. WORKDIR /app
  17. EXPOSE 80
  18. EXPOSE 443
  19. COPY --from=publish /app/publish .
  20. ENTRYPOINT ["dotnet", "IAMHub1.dll"]
展开查看全部

相关问题