简体   繁体   English

.NET 和 SQL 带有 EF Core 和 K8s 的服务器容器

[英].NET and SQL Server container with EF Core and K8s

My goal is to deploy a blazor web application using ASP.NET as the frontend and a SQL Server (on Linux) as the backend. My goal is to deploy a blazor web application using ASP.NET as the frontend and a SQL Server (on Linux) as the backend. The problem is utilizing the EF Core code because of my embedded connection string:由于我的嵌入式连接字符串,问题在于使用 EF Core 代码:

var connectionString = "Server=localhost,1433;Database=HelmDevDb;uid=sa;pwd=27u6DG;MultipleActiveResultSets=true;";

The problem is that within the Blazor application it either has to have EF Core commented out (working) or I have to leave uncommented and guess the connection string and问题是在 Blazor 应用程序中,要么必须将 EF Core 注释掉(工作),要么我必须不加注释并猜测连接字符串和

docker build -t schneeple/myapp:v3 -f Dockerfile ..

command.命令。 and then update the helm script with:然后使用以下命令更新 helm 脚本:

helm uninstall myapp
helm install myapp myapp
kubectl get pods

and then see the status which usually tends to make the myapp-deployment app break.然后查看通常会使 myapp-deployment 应用程序崩溃的状态。

➜ kubectl get pods --watch
NAME                                 READY   STATUS      RESTARTS   AGE
myapp-deployment-5cdcd7d4bc-nrl72    0/1     Completed   1          36s
myapp-mssql-linux-784b846c6c-666s5   1/1     Running     0          36s

When I view the k8s logs of the myapp-deployment-5cdcd7d4bc-nrl72 pod it gives the following error:当我查看myapp-deployment-5cdcd7d4bc-nrl72 pod 的 k8s 日志时,会出现以下错误:

➜ kubectl logs myapp-deployment-5cdcd7d4bc-nrl72
[23:30:36 INF] Starting up
[23:30:36 WRN] Storing keys in a directory '/root/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable when container is destroyed.
[23:30:36 INF] User profile is available. Using '/root/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.
[23:30:36 INF] Creating key {bc4aa022-61fe-4fe4-b215-572c8337869d} with creation date 2020-06-30 23:30:36Z, activation date 2020-06-30 23:30:36Z, and expiration date 2020-09-28 23:30:36Z.
[23:30:36 WRN] No XML encryptor configured. Key {bc4aa022-61fe-4fe4-b215-572c8337869d} may be persisted to storage in unencrypted form.
[23:30:36 INF] Writing data to file '/root/.aspnet/DataProtection-Keys/key-bc4aa022-61fe-4fe4-b215-572c8337869d.xml'.
[23:30:37 INF] Entity Framework Core 3.1.4 initialized 'AppDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
[23:30:52 FTL] Application startup exception
Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken, DbConnectionPool pool, SqlAuthenticationProviderManager sqlAuthProviderManager)
   at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at Microsoft.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
   at Microsoft.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at Microsoft.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at Microsoft.Data.SqlClient.SqlConnection.Open()
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.<>c__DisplayClass18_0.<Exists>b__0(DateTime giveUp)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.<>c__DisplayClass12_0`2.<Execute>b__0(DbContext c, TState s)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func`2 operation, Func`2 verifySucceeded)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func`2 operation)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Exists(Boolean retryOnNotExists)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.Migrate(DatabaseFacade databaseFacade)
   at AuSight.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env, AppDbContext data) in /src/AuSight/Startup.cs:line 105
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
   at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>c__DisplayClass4_0.<Build>b__0(IApplicationBuilder builder)
   at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass13_0.<UseStartup>b__2(IApplicationBuilder app)
   at Microsoft.ApplicationInsights.AspNetCore.ApplicationInsightsStartupFilter.<>c__DisplayClass2_0.<Configure>b__0(IApplicationBuilder app)
   at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.<Configure>g__MiddlewareFilterBuilder|0(IApplicationBuilder builder)
   at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
   at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
ClientConnectionId:00000000-0000-0000-0000-000000000000
[23:30:52 FTL] Application start-up failed
Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 40 - Could not open a connection to SQL Server)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling, String accessToken, DbConnectionPool pool, SqlAuthenticationProviderManager sqlAuthProviderManager)
   at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at Microsoft.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
   at Microsoft.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at Microsoft.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at Microsoft.Data.SqlClient.SqlConnection.Open()
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.<>c__DisplayClass18_0.<Exists>b__0(DateTime giveUp)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.<>c__DisplayClass12_0`2.<Execute>b__0(DbContext c, TState s)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func`2 operation, Func`2 verifySucceeded)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func`2 operation)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Exists(Boolean retryOnNotExists)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.Migrate(DatabaseFacade databaseFacade)
   at AuSight.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env, AppDbContext data) in /src/AuSight/Startup.cs:line 105
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
   at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>c__DisplayClass4_0.<Build>b__0(IApplicationBuilder builder)
   at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass13_0.<UseStartup>b__2(IApplicationBuilder app)
   at Microsoft.ApplicationInsights.AspNetCore.ApplicationInsightsStartupFilter.<>c__DisplayClass2_0.<Configure>b__0(IApplicationBuilder app)
   at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.<Configure>g__MiddlewareFilterBuilder|0(IApplicationBuilder builder)
   at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
   at Microsoft.AspNetCore.Hosting.GenericWebHostService.StartAsync(CancellationToken cancellationToken)
   at Microsoft.Extensions.Hosting.Internal.Host.StartAsync(CancellationToken cancellationToken)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.RunAsync(IHost host, CancellationToken token)
   at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)
   at AuSight.Program.Main(String[] args) in /src/AuSight/Program.cs:line 29
ClientConnectionId:00000000-0000-0000-0000-000000000000

I believe I have my mssql-linux helm dependency setup correctly and has a targetPort=1433;port=1433 for the backend service which have the appropriate selectors. I believe I have my mssql-linux helm dependency setup correctly and has a targetPort=1433;port=1433 for the backend service which have the appropriate selectors.

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: RELEASE-NAME-mssql-linux
  labels:
    app: mssql-linux
    chart: mssql-linux-0.11.2
    release: RELEASE-NAME
    heritage: Helm
    tier: backend
spec:
  type: ClusterIP
  ports:
  - name: mssql
    port: 1433
    targetPort: mssql
    protocol: TCP
  selector:
    app: mssql-linux
    release: RELEASE-NAME

With part of the deployment.yaml部分deployment.yaml

# Source: vault/charts/mssql-linux/templates/deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: RELEASE-NAME-mssql-linux
  labels:
    app: mssql-linux
    chart: mssql-linux-0.11.2
    release: RELEASE-NAME
    heritage: Helm
    tier: backend
spec:
  replicas: 
  selector:
    matchLabels:
      app: mssql-linux
      release: RELEASE-NAME
  template:
    metadata:
      labels:
        app: mssql-linux
        release: RELEASE-NAME
    spec:
      containers:
        - name: mssql-linux
          image: "microsoft/mssql-server-linux:2017-CU5"
          imagePullPolicy: IfNotPresent
          env:
            - name: ACCEPT_EULA
              value: "Y"
            - name: MSSQL_PID
              value: "Enterprise"
            - name: SA_PASSWORD
              valueFrom:
               secretKeyRef:
                 name: RELEASE-NAME-mssql-linux-secret
                 key: sapassword
            - name: MSSQL_DATA_DIR
              value: /mssql-data/data
            - name: MSSQL_LOG_DIR
              value: /mssql-translog/translog
            - name: MSSQL_TCP_PORT
              value: "1433"
            - name: MSSQL_BACKUP_DIR
              value: "/mssql-backup/backup"
            - name: MSSQL_COLLATION
              value: "SQL_Latin1_General_CP1_CI_AS"
            - name: MSSQL_LCID
              value: "1033"

Is there something I am missing to have myapp-deployment pod be able to find the correct port/IP inside the mssql-linux pod?我是否缺少让 myapp-deployment pod 能够在 mssql-linux pod 中找到正确的端口/IP 的东西?

I guess what here is missing is the Readiness Probe for the DB.我猜这里缺少的是数据库的就绪探针。 Your App does not wait for the DB to be running and fails...您的应用程序不会等待数据库运行并失败...

Your connection string is wrong ( Server=localhost,1433 ) it should be:您的连接字符串错误( Server=localhost,1433 )它应该是:

Server=mssql-linux;Database=HelmDevDb;uid=sa;pwd=27u6DG;MultipleActiveResultSets=true;

You should use the pod name to access to your SqlServer instance.您应该使用 pod 名称来访问您的 SqlServer 实例。 More, if you want to specifie a port use : not ,更多,如果你想指定一个端口使用: not ,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 k8s如何执行distroless容器 - k8s how to exec to distroless container 如何在k8s容器中禁止网络? - how to ban network in k8s container? 在 cronjob k8s 中运行容器 - Run container in cronjob k8s 如何使用containerd限制k8s集群上容器中的核心文件大小? - How to restrict core file size in container on k8s cluster with containerd? io.k8s.api.core.v1.PodSecurityContext 中的未知字段“功能”(在容器/k8s pod 中运行 tshark) - unknown field "capabilities" in io.k8s.api.core.v1.PodSecurityContext (running tshark in a container/k8s pod) 将来自 k8s 容器的所有网络流量路由到同一个 pod 中的另一个容器 - Routing all net traffic from a k8s container through another in the same pod CloudSQL docker容器暂时无法连接到SQL实例然后在k8s中连接 - CloudSQL docker container fails to connect to sql instance for a while then connects in k8s 'realpath(): Permission denied' 在 k8s 上的 dotnet/sdk:3.1-bullseye 容器中以非 root 身份运行 dotnet ef 命令时 - 'realpath(): Permission denied' when running dotnet ef command as non-root in dotnet/sdk:3.1-bullseye container on k8s k8s监视服务器容器 - k8s watch server containers 我们可以控制k8s节点运行容器成员吗? - Can we control the k8s node run container members?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM