I'm trying to map an existing azure mssql server to my kubernetes cluster as a service but it seems my application cannot connect to it.
Here's my config file, what am I doing wrong? Maybe it's because I'm doing this on a specific namespace?
---
apiVersion: v1
kind: Service
metadata:
name: my-database
spec:
type: ExternalName
externalName: mydb.database.windows.net
Then, in my application, I just use the following string:
spring.datasource.url: jdbc:sqlserver://my-database:1433;databaseName=DATABASE_DEV
If I use the database host, mydb.database.windows.net, everything works fine so it is not a connection problem but rather something related to my service conf...
For this issue, I think you misunderstand the Kubernetes service. Take a look at the Service. The service just redirects the access traffic to the Kubernetes Pods, not the Azure resource.
For the type ExternalName of service, it shows the working process here:
When looking up the host my-service.prod.svc.cluster.local, the cluster DNS Service returns a CNAME record with the value my.database.example.com. Accessing my-service works in the same way as other Services but with the crucial difference that redirection happens at the DNS level rather than via proxying or forwarding. Should you later decide to move your database into your cluster, you can start its Pods, add appropriate selectors or endpoints, and change the Service’s type.
So if you want to access the Azure database, you can only use the database server name ***.database.windows.net
that the Azure provides you. For you, it's mydb.database.windows.net
.
A solution is to create Service without a selector, then an Endpoints
object for the Service
. Similar to my answer here: Kubernetes pod unable to connect to rabbit mq instance running locally
Just disregard the talk about localhost...