| title | Connect to SQL Server Always On Availability Group on Kubernetes cluster |
|---|---|
| description | This article explains how to connect to an Always On Availability Group |
| author | MikeRayMSFT |
| ms.author | mikeray |
| manager | craigg |
| ms.date | 08/09/2018 |
| ms.topic | article |
| ms.prod | sql |
| ms.custom | sql-linux |
| ms.technology | linux |
| monikerRange | >=sql-server-ver15||>=sql-server-linux-ver15||=sqlallproducts-allversions |
To connect to SQL Server instances in containers on a Kubernetes cluster, create a load balancer service. The load balancer is an endpoint. It holds an IP address and forwards requests for the IP address to the pod running the SQL Server instance.
To connect to an availability group replica, create a service for different replica types. You can see examples of services for different types of replicas in sql-server-samples/ag-services.yaml.
ag1-primarypoints to the primary replica.ag1-secondarypoints to any secondary replica.
If more than one secondary replica, Kubernetes routes your connection to the different replicas in a round-robin fashion.
To create load balancer services for the primary and replicas, copy ag1-services.yaml from sql-server-samples and update it for your availability group.
The following command applies the configuration from the .yaml file to cluster:
kubectl apply -f ag1-services.yaml --namespace ag1
To get the load balancer IP address for your load balancer service, run
kubectl get services
Identify the IP address of the service you want to connect to.
To connect to the primary replica with SQL authentication, use the sa account, the value for sapassword from the secret you created, and this IP address.
For example:
sqlcmd -S <0.0.0.0> -U sa -P "<MyC0m9l&xP@ssw0rd>"