Loading Sitecore 10 Kubernetes specifications from Terraform for turbocharged CI/CD
Feb 2021
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.
info
In this blog post, we look at different ways to integrate Sitecore Kubernetes manifests and Terraform
#
Introducing TerraformMany 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 KubernetesSitecore 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 - Content Delivery
, Content Management
, 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 .yaml
files?
#
Three ways to read the Sitecore Kubernetes manifests from your Terraform configurationsLet's work with the cd.yaml
manifest (you can download this from Sitecore, along with manifests for other roles)
#
#1 - tfk8stfk8s 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.
- Clone
https://github.com/jrhouston/tfk8s
and run make install - Now, you can run the tool to get a deployable Terraform configuration:
#
#2 - The kubectl Terraform providerAs 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 filesThe 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.
For the XM1
external manifests, we use the following configuration:
Here, kubectl_path_documents.external
creates a list of files, looking in the external subdirectory for any .yaml
files.
kubectl_manifest.external
creates a manifest resource, by loading and merging all of the .yaml
files in the external/
subdirectory.
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!