SQL Server Availability Groups in Containers
Introduction
This guide describes how to create and deploy a DxEnterprise + SQL Server (DxEMSSQL) container cluster and use DxCLI to create an availability group.
This guide covers the deployment of three Availability Group (AG) replicas. When deploying availability groups, please be aware of Quorum Considerations for SQL Server Availability Groups.
Docker and Podman are largely interchangeable for the container commands shown in this guide. Docker commands assume the default docker.io registry. When using Podman, container image names must be fully qualified with the registry name. For example:
podman run docker.io/dh2i/dxe
Prerequisites
-
A supported container runtime installed on three Linux machines (physical or virtual) with at least 8 GB of RAM each.
Supported runtimes: Docker or Podman.
Refer to the official Docker or Podman documentation for installation instructions. -
A DxEMSSQL image available locally or on a repository. To create the image, follow the relevant quick start guide. Alternatively, DH2i provides the
dh2i/dxemssqlimage on Docker Hub for testing and non-production use only. -
A valid DxEnterprise license with availability group management features and tunnels enabled. A fully featured Developer Edition is available free for non-production use. To purchase DxEnterprise software for production workloads, please contact us.
Create the Containers
First you'll create the containers using your container runtime. DH2i's NAT Matchmaking allows containers on disparate networks to connect to each other.
-
Create the first container. Ensure that you replace
dh2i/dxemssqlwith your registry and/or repository if you built and pushed your own image.cautionit is strongly recommended to create volumes for the
/etc/dh2iand/var/opt/mssqlfolders to persist data between container re-deployments.- Docker
- Podman
sudo docker run -d -e ACCEPT_EULA=Y -e SA_PASSWORD=p@ssw0rd \
-v dxe-0:/etc/dh2i \
-v mssql-0:/var/opt/mssql \
--name dxemssql-0 \
-h dxemssql-0 \
dh2i/dxemssqlsudo podman run -d -e ACCEPT_EULA=Y -e SA_PASSWORD=p@ssw0rd \
-v dxe-0:/etc/dh2i \
-v mssql-0:/var/opt/mssql \
--name dxemssql-0 \
-h dxemssql-0 \
docker.io/dh2i/dxemssql -
Repeat the command for the two additional containers on different hosts. Increment the values for
--name,-h, and both volumes (-v):- Docker
- Podman
sudo docker run -d -e ACCEPT_EULA=Y -e SA_PASSWORD=p@ssw0rd \
-v dxe-1:/etc/dh2i \
-v mssql-1:/var/opt/mssql \
--name dxemssql-1 \
-h dxemssql-1 \
dh2i/dxemssql
sudo docker run -d -e ACCEPT_EULA=Y -e SA_PASSWORD=p@ssw0rd \
-v dxe-2:/etc/dh2i \
-v mssql-2:/var/opt/mssql \
--name dxemssql-2 \
-h dxemssql-2 \
dh2i/dxemssqlsudo podman run -d -e ACCEPT_EULA=Y -e SA_PASSWORD=p@ssw0rd \
-v dxe-1:/etc/dh2i \
-v mssql-1:/var/opt/mssql \
--name dxemssql-1 \
-h dxemssql-1 \
docker.io/dh2i/dxemssql
sudo podman run -d -e ACCEPT_EULA=Y -e SA_PASSWORD=p@ssw0rd \
-v dxe-2:/etc/dh2i \
-v mssql-2:/var/opt/mssql \
--name dxemssql-2 \
-h dxemssql-2 \
docker.io/dh2i/dxemssql
Create the Availability Group
This section describes how to start the availability group on the primary, then join the two secondary containers to the DxEnterprise cluster and the availability group.
Configure the First Container
DxCLI commands can be run by using the container runtime's exec command to invoke DxCLI within the container. This method will be used in the examples below.
-
Activate the DxEnterprise license on the first container using the command
dxcli activate-server.infoIf this command fails, retry a few more times. Depending on system resources, it may take 30 or more seconds for DxEnterprise and SQL Server to complete their startup.
- Docker
- Podman
sudo docker exec dxemssql-0 dxcli activate-server AAAA-BBBB-CCCC-DDDD --accept-eulasudo podman exec dxemssql-0 dxcli activate-server AAAA-BBBB-CCCC-DDDD --accept-eula -
Add a Vhost to the cluster using the command
dxcli cluster-add-vhost.- Docker
- Podman
sudo docker exec dxemssql-0 dxcli cluster-add-vhost vhost1 "" dxemssql-0sudo podman exec dxemssql-0 dxcli cluster-add-vhost vhost1 "" dxemssql-0 -
Encrypt the SQL Server sysadmin password for DxEnterprise using the command
dxcli encrypt-text. The encrypted password will be used to create the availability group in the next step.- Docker
- Podman
sudo docker exec dxemssql-0 dxcli encrypt-text p@ssw0rdsudo podman exec dxemssql-0 dxcli encrypt-text p@ssw0rd -
Add an availability group to the Vhost using the command
dxcli add-ags. The SQL Server sysadmin password must be encrypted using the commanddxcli encrypt-textfrom the previous step.- Docker
- Podman
sudo docker exec dxemssql-0 dxcli add-ags vhost1 ags1 "dxemssql-0|mssqlserver|sa|V1DUmkSp6vNmyLDbzT1+xg==|5022|synchronous_commit|0"sudo podman exec dxemssql-0 dxcli add-ags vhost1 ags1 "dxemssql-0|mssqlserver|sa|V1DUmkSp6vNmyLDbzT1+xg==|5022|synchronous_commit|0" -
Set a One-Time PassKey (OTPK) using the command
dxcli set-otpk. The output from this command will be used to join the other containers to the DxEnterprise cluster.- Docker
- Podman
sudo docker exec dxemssql-0 dxcli set-otpksudo podman exec dxemssql-0 dxcli set-otpk -
OPTIONAL: Set a cluster passkey using the command `dxcli cluster-set-secret-ex. A passkey is required to remotely manage your DxEnterprise cluster.
- Docker
- Podman
sudo docker exec dxemssql-0 dxcli cluster-set-secret-ex p@ssw0rdsudo podman exec dxemssql-0 dxcli cluster-set-secret-ex p@ssw0rd
Join the Second Container to the DxEnterprise Cluster
Now that the availability group is created on the primary, you can add a second container to the cluster.
-
Activate the DxEnterprise license for the second container using the command
dxcli activate-server.- Docker
- Podman
sudo docker exec dxemssql-1 dxcli activate-server AAAA-BBBB-CCCC-DDDD --accept-eulasudo podman exec dxemssql-1 dxcli activate-server AAAA-BBBB-CCCC-DDDD --accept-eula -
Join the second container to the DxEnterprise cluster using the command
dxcli join-cluster-ex. Use the default NAT proxy of match.dh2i.com.- Docker
- Podman
sudo docker exec dxemssql-1 dxcli join-cluster-ex match.dh2i.com 331bc8bf-7096-99bc-05e5-0dd097393600 truesudo podman exec dxemssql-1 dxcli join-cluster-ex match.dh2i.com 331bc8bf-7096-99bc-05e5-0dd097393600 true -
Add the second container to the existing availability group using the command
dxcli add-ags-node. The SQL Server sysadmin password must be encrypted using the commanddxcli encrypt-text.- Docker
- Podman
sudo docker exec dxemssql-1 dxcli add-ags-node vhost1 ags1 "dxemssql-1|mssqlserver|sa|V1DUmkSp6vNmyLDbzT1+xg==|5022|synchronous_commit|0"sudo podman exec dxemssql-1 dxcli add-ags-node vhost1 ags1 "dxemssql-1|mssqlserver|sa|V1DUmkSp6vNmyLDbzT1+xg==|5022|synchronous_commit|0"
Join the Third Container to the DxEnterprise Cluster
Finally, join the third container into the cluster.
-
Activate the DxEnterprise license for the third container using the command
dxcli activate-server.- Docker
- Podman
sudo docker exec dxemssql-2 dxcli activate-server AAAA-BBBB-CCCC-DDDD --accept-eulasudo podman exec dxemssql-2 dxcli activate-server AAAA-BBBB-CCCC-DDDD --accept-eula -
Join the third container to the DxEnterprise cluster using the command
dxcli join-cluster-exwith the same parameters from step 2.- Docker
- Podman
sudo docker exec dxemssql-2 dxcli join-cluster-ex match.dh2i.com 331bc8bf-7096-99bc-05e5-0dd097393600 truesudo podman exec dxemssql-2 dxcli join-cluster-ex match.dh2i.com 331bc8bf-7096-99bc-05e5-0dd097393600 true -
Add the third container to the existing availability group using the command
dxcli add-ags-node.- Docker
- Podman
sudo docker exec dxemssql-2 dxcli add-ags-node vhost1 ags1 "dxemssql-2|mssqlserver|sa|V1DUmkSp6vNmyLDbzT1+xg==|5022|synchronous_commit|0"sudo podman exec dxemssql-2 dxcli add-ags-node vhost1 ags1 "dxemssql-2|mssqlserver|sa|V1DUmkSp6vNmyLDbzT1+xg==|5022|synchronous_commit|0"
Add the Availability Group Database(s)
-
If a database is not already available, create it using
sqlcmdon the primary.- Docker
- Podman
sudo docker exec dxemssql-0 /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P p@ssw0rd -Q "create database db1"sudo podman exec dxemssql-0 /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P p@ssw0rd -Q "create database db1" -
Add databases to the availability group from the primary using the command
dxcli add-ags-databases.- Docker
- Podman
sudo docker exec dxemssql-0 dxcli add-ags-databases vhost1 ags1 db1sudo podman exec dxemssql-0 dxcli add-ags-databases vhost1 ags1 db1 -
Verify the details of the availability group using the command
dxcli get-ags-detail.- Docker
- Podman
sudo docker exec dxemssql-0 dxcli get-ags-detail vhost1 ags1sudo podman exec dxemssql-0 dxcli get-ags-detail vhost1 ags1