.NET Core pod can't connect to SQL Server pod in Kubernetes

6/27/2021

I have a .NET Core pod that needs to access to SQL Server pod in Kubernetes(docker-desktop). Using port forwarding I can connect to that SQL Server from SQL Server Management Studio. But when I trying to connect from .NET Core pod then it says

The server was not found or was not accessible

Here is the error from log

[04:28:38 Error] Microsoft.EntityFrameworkCore.Database.Connection
An error occurred using the connection to database 'MyTestDatabase' on server 'tcp:sqlserver-service,1433'.

[04:28:38 Error] Microsoft.EntityFrameworkCore.Query
An exception occurred while iterating over the results of a query for context type 'Web.Data.ApplicationDbContext'.
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)

My Connection String in Container

Server=tcp:sqlserver-service,1433;User ID=sa;Password=myPass12;Initial Catalog=MyTestDatabase;MultipleActiveResultSets=true;Connection Timeout=30;

SQL Server deployment yml file

apiVersion: v1
kind: PersistentVolume
metadata:
  name: sqldata
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/sqldata"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: dbclaim
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 2Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: sqlserver
spec:
  replicas: 1
  selector:
    matchLabels:
      app: sqlserver
  template:
    metadata:
      labels:
        app: sqlserver
    spec:
      volumes:
        - name: sqldata-storage
          persistentVolumeClaim:
            claimName: dbclaim
      terminationGracePeriodSeconds: 10
      initContainers:
       - name: volume-permissions
         image: busybox
         command: ["sh", "-c", "chown -R 10001:0 /var/opt/mssql"]
         volumeMounts:
         - mountPath: "/var/opt/mssql"
           name: sqldata-storage
      containers:
        - name: sqlserver1
          image: mcr.microsoft.com/mssql/server
          ports:
            - containerPort: 1433
          env:
            - name: MSSQL_PID
              value: "Developer"
            - name: SA_PASSWORD
              value: "myPass12"
            - name: ACCEPT_EULA
              value: "Y"
          volumeMounts:
          - mountPath: "/var/opt/mssql/data"
            name: sqldata-storage
---
apiVersion: v1
kind: Service
metadata:
  name: sqlserver-service
spec:
  ports:
  - name: sqlserver
    port: 1433
    targetPort: 1433
    protocol: TCP
  selector:
    name: sqlserver
  type: LoadBalancer
  

Connect from SQL Server Management Studio

enter image description here

Surely I am missed something.

Thanks in advance

-- Hasanuzzaman
.net-core
c#
docker
kubernetes
sql-server

1 Answer

6/27/2021

It`s my bad. Actually selector of service was wrong.

 selector:
    name: sqlserver

It should be

 selector:
    app: sqlserver

Thank you all

-- Hasanuzzaman
Source: StackOverflow