How do I connect a kubernetes cluster to an external SQL Server database using docker desktop?

6/13/2019

I need to know how to connect my Kubernetes cluster to an external SQL Server database running in a docker image outside of the Kubernetes cluster.

I currently have two pods in my cluster that are running, each has a different image in it created from asp.net core applications. There is a completely separate (outside of Kubernetes but running locally on my machine localhost,1433) docker image that hosts a SQL Server database. I need the applications in my Kubernetes pods to be able to reach and manipulate that database. I have tried creating a YAML file and configuring different ports but I do not know how to get this working, or how to test that it actually is working after setting it up. I need the exact steps/commands to create a service capable of routing a connection from the images in my cluster to the DB and back.

  • Docker SQL Server creation (elevated powershell/docker desktop):

    docker pull mcr.microsoft.com/mssql/server:2017-latest
    
    docker run -d -p 1433:1433 --name sql -v "c:/Temp/DockerShared:/host_mount" -e SA_PASSWORD="aPasswordPassword" -e ACCEPT_EULA=Y mcr.microsoft.com/mssql/server:2017-latest
  • definitions.yaml

    #Pods in the cluster
    apiVersion: v1
    kind: Pod
    metadata:
      name: pod-1
      labels:
        app: podnet
        type: module
    spec:
      containers:
       - name: container1
         image: username/image1
    
    ---
    
    apiVersion: v1
    kind: Pod
    metadata:
      name: pod-2
      labels:
        app: podnet
        type: module
    spec:
      containers:
       - name: container2
         image: username/image2
    
    ---
    #Service created in an attempt to contact external SQL Server DB
    apiVersion: v1
    kind: Service
    metadata:
     name: ext-sql-service
    spec:
     ports:
     - port: 1433
       targetPort: 1433
    type: ClusterIP
    ---
    apiVersion: v1
    kind: Endpoints
    metadata:
     name: ext-sql-service
    subsets:
     - addresses:
         - ip: (Docker IP for DB Instance)
       ports:
         - port: 1433

Ideally I would like applications in my kubernetes cluster to be able to manipulate the SQL Server I already have set up (running outside of the cluster but locally on my machine).

-- C1pher6710
docker
kubernetes
powershell
sql-server

2 Answers

7/2/2019

Problem may be in kind of service you put. ClusterIP enable you juest to connect among pods inside cluster. To connect to external service you should just change definition of service kind as NodePort.

Try to change service definition:

#Service created in an attempt to contact external SQL Server DB
apiVersion: v1
kind: Service
metadata:
 name: ext-sql-service
spec:
 type: NodePort
 ports:
 - port: 1433
   targetPort: 1433

and execute command:

$ kubectl apply -f your_service_definition_file_name.yaml 

Remember to run this command in proper namespace, where your deployment is configured.

Bad practice is to overlay an environment variable onto the container. And with "docker run" pass that environment variable VALUE to the container.

Of course in context of executing docker command

$ docker run -d -p 1433:1433 --name sql -v "c:/Temp/DockerShared:/host_mount" -e SA_PASSWORD="aPasswordPassword" -e ACCEPT_EULA=Y mcr.microsoft.com/mssql/server:2017-latest

Putting the db-password visible is insecure. Use Kubernetes secrets.

More information you can find here: kubernetes-secret.

-- MaggieO
Source: StackOverflow

6/13/2019

When running from local docker, you connection string is NOT your local machine. It is the local docker "world", that happens to be running on your machine.

host.docker.internal:1433

The above is docker container talking to your local machine. Obviously, the port could be different based on how you exposed it.

......

If you're trying to get your running container to talk to sql-server which is ALSO running inside of the docker world, that connection string looks like:

ServerName:

my-mssql-service-deployment-name.$_CUSTOMNAMESPACENAME.svc.cluster.local

Where $_CUSTOMNAMESPACENAME is probably "default", but you may be running a different namespace.

my-mssql-service-deployment-name is the name of YOUR deployment (I have it stubbed here)

Note there is no port number here.

This is documented here:

https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#services

-- granadaCoder
Source: StackOverflow