Use port forwarding to access SQL Server running in Kubernetes

A really handy feature in Kubernetes is port forwarding. This can be used to narrow down an issue when connections are failing to SQL Server running in a cluster.

Say we have deployed the following to a Kubernetes cluster: –

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: sqlserver
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: sqlserver
    spec:
      containers:
      - name: sqlserver1
        image: mcr.microsoft.com/mssql/server:2019-RC1-ubuntu
        ports:
        - containerPort: 1433
        env:
        - name: SA_PASSWORD
          value: "Testing1122"
        - name: ACCEPT_EULA
          value: "Y"
---
apiVersion: v1
kind: Service
metadata:
  name: sqlserver-service
spec:
  ports:
  - name: sqlserver
    port: 1433
    targetPort: 1433
  selector:
    name: sqlserver
  type: LoadBalancer

This will create the following in the Kubernetes cluster: –

The load balanced service’s IP can be usually be used to connect into the SQL instance running in the pod, but what if we’re unable to connect? Does the issue lie with the service or the pod?

In order to narrow this down, port forwarding can be used to directly connect to the pod: –

kubectl port-forward pod/sqlserver-889b56d7b-nb2b4 15789:1433

This will allow us to use 127.0.0.1,15789 (localhost won’t work) and connect from our local machine to the pod running in the Kubernetes cluster (in a separate window): –

mssql-cli -S 127.0.0.1,15789 -U sa

We can use the same port to connect via ADS and SSMS as well: –

If a connection can be established to the pod via the forwarded port then we know that the issue doesn’t lie with the pod but with the service or the connection from the service to the pod.

Thanks for reading!

6 thoughts on “Use port forwarding to access SQL Server running in Kubernetes

  1. Thanks for the post. Could you please explain how did you find out the port number 15789 for localhost. I’m trying to connect from SSMS on my local machine(laptop) to a pod in kubernetes and im not able to do it..

    • I picked port 15789 at random and set it in
      kubectl port-forward pod/sqlserver-889b56d7b-nb2b4 15789:1433

      That maps port 15789 to port 1433 in the pod, so that I can connect to SQL Server running within it.

      I picked 15789 at random as I just need a port locally that’s not in use

      • Thanks for the quick response..
        I tried the port forward and tried to connect to pod from my SSMS on my laptop, it errors out with following error message..however im able to connect with Loadbalancer IP from SSMS. I tried netstat -aon and choose a free port which is not being used by any process..Could you please check…

        Commands :
        kubectl port-forward pod/mssql-deployment-74dfcbb496-7x5qx 60669:1433
        Forwarding from 127.0.0.1:60669 -> 1433
        Forwarding from [::1]:60669 -> 1433

        Error:
        TITLE: Connect to Server
        ——————————

        Cannot connect to 127.0.0.1,60669.

        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: 0 – The wait operation timed out.) (Microsoft SQL Server, Error: 258)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s