Loading Sitecore 10 Kubernetes specifications from Terraform for turbocharged CI/CD
Since the advent of Sitecore 10, more and more of us are shifting our deployments toward Docker and Kubernetes. Not only can running containers reduce costs, but we can now take advantage of a rich ecosystem of DevOps tools to automate the heavy lifting involved in deployments.
In this blog post, we look at different ways to integrate Sitecore Kubernetes manifests and Terraform
Many organisations have embraced Infrastructure as Code (IAC) - tracking cloud (and less frequently, on-prem) infrastructure assets in version control and putting in place an automated, testable and auditable change workflow.
Terraform is the leading IAC tool which provides a description language for your infrastructure, and a powerful CLI to drive creation and management of your cloud services, networks, security policies, serverless functions and just about everything else. All of the main cloud vendors (Azure, AWS, Google Cloud) are supported, and engineers can create custom providers to ensure their service provider is covered.
#Terraform and Sitecore Kubernetes
Sitecore provides official Kubernetes manifests - that is - descriptions of workloads to run within your cluster. These workloads correspond to the server roles you're familiar with -
Solr, and so on.
Terraform can be used to deploy these workloads to your cluster - which is great if you're already invested in Terraform and have existing deployment pipelines you wish to extend to cover your Sitecore deployments.
Kubernetes manifests are in
.yaml format - but Terraform uses the Hashicorp Configuration Language (HCL) - so, how can we get Terraform to read our Sitecore Kubernetes
#Three ways to read the Sitecore Kubernetes manifests from your Terraform configurations
Let's work with the
cd.yaml manifest (you can download this from Sitecore, along with manifests for other roles)
##1 - tfk8s
tfk8s is a tool which converts Kubernetes
.yaml manifests into HCL, ready for use in Terraform.
- The tool is written in Go, so you will need to install Go first.
https://github.com/jrhouston/tfk8sand run make install
- Now, you can run the tool to get a deployable Terraform configuration:
##2 - The kubectl Terraform provider
As we know from manually deploying Sitecore to Kubernetes, the
kubectl tool is indispensable. Luckily, we can execute
kubectl commands in our Terraform jobs by using the Terraform kubectl provider.
Once imported, you can embed manifest code directly in your Terraform configuration:
##3 - The kubectl Terraform provider with multiple files
The above approach of directly embedding your manifest in Terraform configuration is a bit clumsy and for even an
XM1 topology, we have 6+ separate manifest files.
To ensure our Terraform configuration stays lean and easy to manage, we can leave our manifest code as
.yaml files, and just reference those files from Terraform.
This approach is by far my favourite and feels cleanest. It again uses the excellent Terraform kubectl provider.
XM1 external manifests, we use the following configuration:
kubectl_path_documents.external creates a list of files, looking in the external subdirectory for any
kubectl_manifest.external creates a manifest resource, by loading and merging all of the
.yaml files in the
We can follow the same approach for the other Sitecore manifests:
Have fun and let me know if you're working with Sitecore and Terraform. I'm on Sitecore Chat Slack!