Continuous declarative IaC using an Environment repository
As we know by now, we must create a GKE cluster to host our microservices. So far, we’ve been using gcloud commands to do this; however, because gcloud commands are not declarative, using them is not ideal when implementing GitOps. Instead, we’ll use Terraform to create the GKE cluster for us. This will ensure we can deploy and manage the cluster declaratively using a Git Environment repository. So, let’s go ahead and create one.
Creating and setting up our Environment repository
Navigate to https://github.com and create a repository using a name of your choice. For this exercise, we will use mdo-environments. Once you have done that, navigate to Google Cloud Shell, generate a ssh-key pair using the ssh-keygen command, copy the public key to GitHub (refer to Chapter 2, Source Code Management with Git and GitOps, for step-by-step instructions), and clone the repository using the following commands:
$ cd ~
$ git clone https://github.com/PacktPublishing/Modern-DevOps-Practices-2e.git \ modern-devops
$ git clone [email protected]:<your_account>/mdo-environments.git $ cd mdo-environments
Let’s copy a .gitignore file for Terraform to ensure that we do not unexpectedly check in Terraform state, backend, or .tfvars files by using the following command:
$ cp -r ~/modern-devops/ch12/.gitignore .
Now, let’s push this code to GitHub using the following commands:
$ git add –all
$ git commit -m ‘Added gitignore’
$ git push
Now that we’ve pushed our first file and initialized our repository, let’s structure our repository according to our environments. We will have two branches within the Environment repository – dev and prod. All configurations in the dev branch will apply to the development environment, and those on prod will apply to the production environment. The following diagram illustrates this approach in detail:
Figure 12.7 – CD process
The existing repository has a single branch called master . However, since we will be managing multiple environments in this repository, it would be good to rename the master branch to prod.
Go to https://github.com/<your_user>/mdo-environments/branches and click the pencil icon beside master. Type in prod and click on Rename Branch.
Now that we’ve renamed the branch, let’s remove the existing local repository and clone the repository again using the following commands:
$ cd ~ && rm -rf mdo-environments
$ git clone [email protected]:<your_account>/mdo-environments.git $ cd mdo-environments
We want to start with the dev environment, so it will be good to create a branch called dev from the prod branch. Run the following command to do so:
$ git branch dev && git checkout dev
Now, we can start writing the Terraform configuration within this directory. The configuration is available in ~/modern-devops/ch12/mdo-environments/environments. Copy everything from that directory to the current directory using the following commands:
$ cp -r ~/modern-devops/ch12/environments/terraform .
$ cp -r ~/modern-devops/ch12/environments/.github .
Within the terraform directory, there are several Terraform files.
The cluster.tf file contains the configuration to create the Kubernetes cluster. It looks like this:
resource “google_service_account” “main” {
account_id = “gke-${var.cluster_name}-${var.branch}-sa”
display_name = “GKE Cluster ${var.cluster_name}-${var.branch} Service Account”
}
resource “google_container_cluster” “main” {
name = “${var.cluster_name}-${var.branch}”
location = var.location
initial_node_count = 3
node_config {
service_account = google_service_account.main.email
oauth_scopes = [
“https://www.googleapis.com/auth/cloud-platform”
]
}
timeouts {
create = “30m”
update = “40m”
}
}