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
  name: sqlserver
  replicas: 1
        name: sqlserver
      - name: sqlserver1
        - containerPort: 1433
        - name: SA_PASSWORD
          value: "Testing1122"
        - name: ACCEPT_EULA
          value: "Y"
apiVersion: v1
kind: Service
  name: sqlserver-service
  - name: sqlserver
    port: 1433
    targetPort: 1433
    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,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,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!

7 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 -> 1433
        Forwarding from [::1]:60669 -> 1433

        TITLE: Connect to Server

        Cannot connect to,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: Logo

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

Facebook photo

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

Connecting to %s