Connect a dotnet core web api to an external SQL Server

11/29/2021

I spent menu hours to try to understand why I can't connect an external (even internal SQL Server instance) to Web API running in kubernetes pod.

Here is my deployment yaml file :

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mssql-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mssql
  template:
    metadata:
      labels:
        app: mssql
    spec:
      terminationGracePeriodSeconds: 1
      containers:
      - name: mssql
        image: mcr.microsoft.com/mssql/server:2017-latest
        resources:
          limits: 
            cpu: "100"
            memory: 2Gi
          requests:
            cpu: 100m
        ports:
        - containerPort: 1433
        env:
        - name: ACCEPT_EULA
          value: "Y"
        - name: SA_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mssql
              key: password
        - name: MSSQL_PID
          value: Developer
        volumeMounts:
        - name: sql-pv
          mountPath: /var/opt/mssql
      volumes:
      - name: sql-pv
        persistentVolumeClaim:
          claimName: sql-pvc
---
apiVersion: v1
kind: Service
metadata:
  name: mssql-deployment
spec:
  selector:
    app: mssql
  ports:
  - protocol: TCP
    port: 1433
    targetPort: 1433   
  type: LoadBalancer

Here is the connection string in appsettings.json:

"ConnectionsStrings": {
    "Customers": "Server=x.x.x.xx,1433;Database=dbname;User ID=sa;Password=*************;"
}

Please tell me if I'm missing something.

Thank you for your help

-- Aziz Gaamouz
.net-core
kubernetes
sql-server

0 Answers