Hi! My name is Angelina, and I’m a DevOps Engineer at Valor Software. With the help of this article, we’ll set up your GCP environment to work in tandem with Terraform. I’ll guide you through the setup of a basic cluster. For this, I picked the services that I use daily within my DevOps routine, since they are optimal for your typical project setup. Let me explain myself here! By typical I mean common projects that are natural (habitual) for your company or industry. In this case, I bet you’d appreciate a chance to build the environment once and clone it for future projects. That’s exactly the way you can go with the services I offer below. So, once you learn the basics for the project setup in this stack, you’ll be able to run new ones in the cloud in no time.
When we need to set up a cloud infrastructure for the project, we want to make it quickly. That's when automation tools come to help. Terraform is one of the instruments that serve for automated setup, and it is compatible with more than 70 providers, which is beyond handy. Besides, Terraform is pretty straightforward to get along with thanks to its declarative language. You don’t need to get used to a completely different CLI, like in cases when you switch to another cloud provider.
Why I suggest picking Terraform from the great variety of services is because it’s perfect for creating reusable infrastructures. You can also clone the existing infrastructure and apply it to your next project, with small alterations if needed. This benefit of reusability, together with the security that Terraform provides, makes it a first-choice technology for solving my daily tasks. BTW, it’s also the right place for keeping electronic keys and hidden or encrypted variables.
Another good point is that Terraform allows for managing several cloud infrastructures from different providers in parallel with minimal time and effort spent.
In this article, I’ll lead you through setting up infrastructure on Google Cloud using Terraform, so buckle up!
Being one of the native Google products, Kubernetes is integrated into GCP quite as is, with no alterations. So you can conveniently set it up and manage it through the GCP CLI or with the help of external instruments like Terraform.
Create a new directory for the project and create a main.tf file for the Terraform config, and populate it with the following content:
Before we jump to the next step of creating a configuration file, we should remember to keep data (e.g. keys, project names) in separate files. This way we ensure project security and give ourselves a chance to reuse the configuration in the future. So, locate the project name, region, and credentials file in a separate file with variables.
First, create a variables.tf file and declare variables for it:
Add the variables’ values to secrets.tfvars:
Then format your main.tf file like so:
Now, when your variables are safe, you’ll be able to pick the needed files with variables instead of repeatedly typing in new data in the main file.
With the `terraform init` command, you’re pulling up modules that you will need to proceed with further steps.
If you see this message, it’s a success! Hurray!
For the next step, you add variables to the variables.tf file:
Before creating a cluster, you should first add variables describing this particular cluster to the secrets.tfvars file:
Now add the cluster configuration to your main.tf file:
In the configuration, you define the cluster’s name, its location, and the number of nodes and their labels. The default namespace is the place where you create a cluster.
To operate with the cluster and its components conveniently, you need to create an output.tf file, where you’ll add variables that will be displayed after running the `terraform apply` command. You can use them later by calling them during configuration.
Now add a new resource which is deployment:
In the deployment configuration, you specify the number of replicas, tags, labels, an image that we’re going to use, as well as internal resources. Finally, you have it all ready for the first launch and go with this command:
In this way, you check if the configuration is created properly and if you’re satisfied with the range of resources.
When you’re quite sure that everything is correct, it’s time for the `terraform apply` command:
And don’t forget to specify the file with variables! At this stage, you’ll have to confirm your actions by running a `yes` command. Terraform will build your new GKE cluster on GCP. After the cluster is created, you’ll see the output list.
For your convenience, in the future, you can split the config file into a few separate files. Place the provider block in the providers.tf file, and a "google_container_cluster" block — in the cluster.tf file.
You’re almost done with your journey, the cluster is up and running, and you can be (ugh) proud of yourself!
For your future projects, you’ll be able to add more metrics and parameters when creating a resource. This will help you adjust your configuration for solving every particular task.
Let's discuss how Valor Software can help with your project's development needs!