AKS环境下如何通过服务发现访问SQL Server 2019 Linux数据库?
Great question! The good news is you absolutely can use Kubernetes service discovery to access your SQL Server 2019 AG without hardcoding a LoadBalancer IP—this is exactly how internal cluster services are designed to work. Let’s walk through the steps and options to make this happen:
1. Use the Operator’s Default Internal Services
When you deploy a SQL Server AG via the official operator, it automatically creates ClusterIP services for internal cluster access (alongside any LoadBalancer services you might have configured for external access).
First, list all services in your namespace to find these internal services:
kubectl get svc -n <your-namespace>
You’ll see entries like:
<your-ag-name>-svc: A ClusterIP service that automatically routes traffic to the AG’s primary node (it follows AG failovers automatically)<your-ag-name>-readonly-svc: A ClusterIP service for connecting to read-only replicas
To connect from your Web API or frontend (deployed within the same AKS cluster), use the fully qualified domain name (FQDN) of this primary service in your connection string. The FQDN follows this format:
<service-name>.<namespace>.svc.cluster.local
Example connection string for a .NET app:
Server=sql-ag-svc.your-db-namespace.svc.cluster.local,1433;Database=YourAppDB;User Id=sa;Password=YourStrongPassword;TrustServerCertificate=True;
2. Convert LoadBalancer Service to ClusterIP (If External Access Isn’t Needed)
If you only need access from within the AKS cluster (no external clients), you can convert your existing AG primary LoadBalancer service to a ClusterIP service to avoid exposing it publicly:
kubectl patch svc <your-ag-primary-loadbalancer-svc> -n <your-namespace> -p '{"spec":{"type":"ClusterIP"}}'
Once updated, you can use the same service name + FQDN format from step 1 to connect.
3. Create a Custom Internal Service (For Advanced Control)
If you want more control over the service configuration, you can define a custom ClusterIP service that targets only the AG’s primary node. This service will automatically update when failovers occur thanks to the operator’s node labeling:
Create a file sql-ag-primary-internal.yaml with this content:
apiVersion: v1 kind: Service metadata: name: sql-ag-primary-internal namespace: <your-namespace> spec: type: ClusterIP ports: - port: 1433 targetPort: 1433 selector: sqlserver.microsoft.com/ag: <your-ag-name> sqlserver.microsoft.com/role: primary
Apply it with:
kubectl apply -f sql-ag-primary-internal.yaml
Now use sql-ag-primary-internal.<your-namespace>.svc.cluster.local in your connection strings—Kubernetes will handle routing to the current primary node automatically.
Key Notes
- This only works for applications deployed within the same AKS cluster (or connected via a peered network that can resolve Kubernetes DNS). If your frontend is external, you’ll still need a LoadBalancer or Ingress, but internal apps can rely fully on service discovery.
- The operator maintains the
sqlserver.microsoft.com/role: primarylabel on the current primary pod, so your service will always point to the right instance after failovers.
内容的提问来源于stack exchange,提问作者Nilesh Gule

