Coder Social home page Coder Social logo

terraform-gke's Introduction

Test Devops Assignment

This project uses Terraform to create a GKE cluster, and deploy a sample application to it. It also uses modular Terraform structure and is powered by Terratest for IaC tests.

Requirements

  • Google Cloud Platform account
  • Terraform
  • Terratest
  • Google Cloud SDK
  • kubectl

Usage

  1. Clone this repository and navigate to the project environments/develop directory.

  2. Set up a service account for Terraform to use, and download the JSON key file. You can do this by following the instructions in the Google Cloud Platform documentation.

NOTE: Ensure your service account has these roles or for test you can bind owner role to your service account(NOT recommended in production!)

  • compute.networks.create
  • container.
  1. Export the path to the JSON key file as an environment variable:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/keyfile.json

Also, you can use credentials key of google provider to address your json file from your local system. It is commented in providers.tf for you.

  1. Initialize the Terraform modules:
terraform init
  1. Customize the environments/your-env/terraform.tfvars file to your needs. You can change the region, zone, cluster name, and other configuration options here.

  2. Provision the GKE cluster and apply resources using Terraform:

terraform plan
terraform apply
  1. (Optional) This project will create a kubeconfig file in your environment directory. You can connect to your kubernetes cluster api-server via kubectl by exporting this variable:
export KUBECONFIG=/path-to-your-kubeconfig-file
# exp: get k8s nodes 
kubectl get nodes

Also, you can get your cluster kubeconfig by gcloud command:

gcloud container clusters get-credentials <cluster_name> --region <google_region>

Run Tests

It will run your terraform on specific GCP project and checks some part of your Iac to realize they are working correctly. The tests are written using Gruntwork's Terratest library containing:

  • Bringing your terraform code up using terraform apply
  • Verify desired kubernetes nodes are up and ready.
  • Checks your application service type is LoadBalancer
  • Expected 200 http response code and content on your application service.
  • Destroy all the project using terraform destroy

To Run the tests follow these steps:

  1. Navigate to the test directory
  2. Initialize your GO module using these commands:
export GO111MODULE=auto
go mod init test
go mod tidy
  1. Run your tests by:
go test -v -timeout 40m

Wait to tests run. Next you should see PASS as a result of your tests.

--- PASS: TestTerraformGcpHelloWorldExample (1870.07s)
PASS
ok      test    1870.081s

Contributing

Pull requests are welcome! For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

terraform-gke's People

Contributors

jnaghiloo avatar

Watchers

Poom avatar Jai Govindani avatar Javad N. avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.