'Access SQL Server database from Kubernetes Pod

My deployed Spring boot application to trying to connect to an external SQL Server database from Kubernetes Pod. But every time it fails with error

Failed to initialize pool: The TCP/IP connection to the host <>, port 1443 has failed.
Error: "Connection timed out: no further information.
Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.

I have tried to exec into the Pod and successfully ping the DB server without any issues

Below are the solutions I have tried:

  1. Created a Service and Endpoint and provided the DB IP in configuration file tried to bring up the application in the Pod

  2. Tried using the Internal IP from Endpoint instead of DB IP in configuration to see Internal IP is resolved to DB IP

But both these cases gave the same result. Below is the yaml I am using the create the Service and Endpoint.

---
apiVersion: v1
kind: Service
metadata:
  name: mssql
  namespace: cattle
spec:
  type: ClusterIP
  ports:
  - port: 1433
---
apiVersion: v1
kind: Endpoints
metadata:
  name: mssql
  namespace: cattle
subsets:
- addresses:
  - ip: <<DB IP>>
  ports:
  - port: 1433

Please let me know if I am wrong or missing in this setup.

Additional information the K8s setup

  • It is clustered master with external etcd cluster topology
  • OS on the nodes is CentOS
  • Able to ping the server from all nodes and the pods that are created


Solution 1:[1]

For this scenario a headless service is very useful. You will redirect traffic to this ip without defining an endpoint.

kind: "Service"
apiVersion: "v1"
metadata:
  namespace: "your-namespace"
  name: "ftp"
spec:
  type: ExternalName
  externalName: your-ip

Solution 2:[2]

The issue was resolved by updating the deployment yaml with IP address. Since all the servers were in same subnet, I did not need the to create a service or endpoint to access the DB. Thank you for all the inputs on the post

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rodrigo Loza
Solution 2 Sujith Shajee