Deploying Microsoft SQL Server 2019 Linux to an Kubernetes Cluster using Terraform
After my previous post about deploying a kubernetes cluster on RedHat Enterprise Linux instances already up, I wanted to deploy SQL Server on this existing kubernetes cluster with Terraform.
Prerequisites
Before you get started, you’ll need to have these things:
- Terraform > 0.13.x
- kubectl installed on the compute that hosts terraform
- One kubernetes cluster
- And the cluster configuration file (/etc/kubernetes/admin.conf)
- My Terraform station is a linux under CentOS, it also works very well under macOS
- Microsoft SQL tools :
- sqlcmd or Azure Data Studio or any other tool that can connect to an MS SQL Server instance
Infra
Initial setup
Clone the repository and install the dependencies:
Usage
Create a MS SQL Server instance:
If you use the terraform apply command without parameters the default values will be those defined in the variables.tf file.
This will do the following :
- create a namespace
- create of the secret object for the password sa for the MS SQL Server instance
- the password is base64 encoded, in this example the password is : HPeinvent@
- create a PVC : Persistant Volume Claim
- create a deployment object for MS SQL Server : create a MS SQL Server instance
- create a service object for MS SQL Server instance:
Tear down the whole Terraform plan with :
Resources can be destroyed using the terraform destroy command, which is similar to terraform apply but it behaves as if all of the resources have been removed from the configuration.
Remote control
Check if your SQL Server instance works:
To access the SQL Server Instance you’ll need to find its port map :
In our deployment for the service we used the NodePort directive and port 1433 is mapped externally to port 31195. We can access our instance by specifying the name (or ip address) of one of the cluster nodes and port 31195.
You can connect to the SQL Server instance outside the Kubernetes cluster with command line :
You can access the MS SQL Server instance with your favorite tool like Azure Data Studio :
Conclusion
Terraform makes it easy to manage Kubernetes clusters and Kubernetes resources effectively. It gives organizations the opportunity to work with infrastructure-as-code, management of platforms, and also the opportunity to create modules for self-service infrastructure. Terraform Kubernetes provider gives organizations all the required tools necessary to manage Kubernetes clusters in the environment.
Improving this guide please submit an issue on GitHub at colussim/mssqlk8sdeploy-terraform. Contributions are more than welcome!