Creating Google Kubernetes Engine (GKE) with Azure DevOps and Terraform

Introduction

Combining Google Kubernetes Engine (GKE) with Azure DevOps and Terraform provides a powerful and efficient way to manage your Kubernetes clusters and CI/CD pipelines. Terraform allows for infrastructure as code, making it easier to provision and manage resources, while Azure DevOps automates the deployment process. This blog post will guide you through setting up GKE with all necessary components using Azure DevOps and Terraform, utilizing main.tf, providers.tf, variables.tf, output.tf, deployment.yaml, and services.yaml files.

Prerequisites

  • Google Cloud Platform (GCP) Account: Ensure you have an active GCP account.
  • Azure DevOps Account: Set up an Azure DevOps organization and project.
  • Terraform: Install Terraform for infrastructure provisioning.
  • Google Cloud SDK: Install Google Cloud SDK for command-line operations.
  • Kubectl: Install kubectl to interact with your Kubernetes cluster.

Step-by-Step Guide

1. Define Providers in providers.tf

Create a providers.tf file in your repository with the following content:

provider "google" {
  project = var.project
  region  = var.region
}

2. Define Variables in variables.tf

Create a variables.tf file to manage your configuration variables:

variable "project" {
  description = "The GCP project ID"
  default     = "my-gcp-project"
}

variable "region" {
  description = "The GCP region to deploy resources"
  default     = "us-central1"
}

variable "cluster_name" {
  description = "The name of the GKE cluster"
  default     = "gke-cluster"
}

variable "node_count" {
  description = "The number of nodes in the GKE cluster"
  default     = 1
}

variable "node_size" {
  description = "The size of the nodes in the GKE cluster"
  default     = "e2-medium"
}

3. Define Infrastructure in main.tf

Create a main.tf file to define the GKE cluster and its components:

resource "google_container_cluster" "primary" {
  name     = var.cluster_name
  location = var.region

  node_config {
    machine_type = var.node_size
  }

  initial_node_count = var.node_count
}

4. Define Outputs in output.tf

Create an output.tf file to capture the outputs of your Terraform configuration:

output "cluster_name" {
  value = google_container_cluster.primary.name
}

output "region" {
  value = google_container_cluster.primary.location
}

5. Create Kubernetes Deployment in deployment.yaml

Create a deployment.yaml file to define your Kubernetes deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: my-app-image:latest
        ports:
        - containerPort: 80

6. Create Kubernetes Service in services.yaml

Create a services.yaml file to define your Kubernetes service:

apiVersion: v1
kind: Service
metadata:
  name: my-app-service
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer

7. Set Up Azure DevOps Pipeline

  • In your Azure DevOps project, navigate to Pipelines > Create Pipeline.
  • Select your repository source (e.g., GitHub, Azure Repos).
  • Choose the pipeline configuration (YAML or classic editor).

8. Configure Service Connection

  • Go to Project Settings > Service connections.
  • Create a new service connection for Google Cloud.
  • Authenticate and authorize Azure DevOps to access your GCP account.

9. Define Pipeline YAML 

Create a azure-pipelines.yml file in your repository with the following content:

trigger:
  branches:
    include:
      - main

pool:
  vmImage: 'ubuntu-latest'

steps:
- task: TerraformInstaller@0
  inputs:
    terraformVersion: 'latest'

- script: |
    terraform init
    terraform plan -out=tfplan
    terraform apply -auto-approve tfplan
  displayName: 'Terraform Apply'

- task: gcloud@0
  inputs:
    gcpServiceConnection: '<Your Service Connection>'
    scriptType: 'bash'
    scriptLocation: 'inlineScript'
    inlineScript: |
      gcloud container clusters get-credentials $(terraform output -raw cluster_name) --region $(terraform output -raw region)
      kubectl apply -f k8s/deployment.yaml
      kubectl apply -f k8s/services.yaml
  displayName: 'Deploy to GKE'

10. Deploy Infrastructure and Application

  • Commit and push the providers.tf, variables.tf, main.tf, output.tf, deployment.yaml, services.yaml, and azure-pipelines.yml files to your repository.
  • The pipeline will trigger automatically, provisioning the GKE cluster with Terraform and deploying your application to the cluster.

Conclusion

By following these steps, you can efficiently set up a Google Kubernetes Engine with all necessary components using Azure DevOps and Terraform. This integration ensures a seamless CI/CD process, enabling continuous deployment and management of your applications in a scalable and reliable manner.

Feel free to reach out if you have any questions or need further assistance! Happy deploying! 

I hope this helps! Let me know if you need any more details or have other questions.

Comments

Popular posts from this blog

How to update build number in Azure DevOps pipeline?

How to get latest build ID from Azure DevOps pipeline?

How to install AWS System Manager (SSM) Agent on windows using PowerShell?