Write GKE Cluster in Terraform IAC code. Develop a Python application. Set up CI/CD with Cloud Build for GKE infrastructure provisioning and application deployments to automate deployment simultaneously.
3 min readJun 15, 2024
- Terraform Configuration: The
main.tf
file defines the GKE cluster. Terraform will provision the cluster in Google Cloud. - Python Application: The Python app uses Flask and is containerized with Docker.
- Kubernetes Manifest: The
k8s-deployment.yaml
file contains the Kubernetes deployment and service definitions. - Cloud Build Pipeline:
- Step 1: Builds the Docker image of the Python application.
- Step 2: Pushes the Docker image to Google Container Registry.
- Step 3: Applies the Terraform configuration to provision the GKE cluster.
- Step 4: Configures
kubectl
with the credentials of the newly created GKE cluster. - Step 5: Deploys the Python application to the GKE cluster using
kubectl
.
This setup ensures that the GKE cluster is provisioned, and the application is deployed in a single Cloud Build run. Adjust paths, project IDs, and other configurations as needed.
Prepare your directory structure:
.
├── app.py
├── cloudbuild.yaml
├── Dockerfile
├── k8s-deployment.yaml
├── requirements.txt
└── terraform
├── main.tf
Step 1: Create GKE Cluster with Terraform IaC
- Install Terraform and Google Cloud SDK:
- Follow the steps to install Terraform and Google Cloud SDK from the official documentation.
- Authenticate with Google Cloud:
gcloud init
gcloud auth application-default login
Create a Terraform configuration file for GKE (main.tf
)
provider "google" {
project = "<YOUR_PROJECT_ID>"
region = "us-central1"
zone = "us-central1-a"
}
resource "google_container_cluster" "primary" {
name = "example-gke-cluster"
location = "us-central1"
initial_node_count = 1
node_config {
machine_type = "e2-medium"
}
}
output "kubernetes_cluster_name" {
value = google_container_cluster.primary.name
}
output "kubernetes_cluster_endpoint" {
value = google_container_cluster.primary.endpoint
}
output "kubernetes_cluster_ca_certificate" {
value = base64decode(google_container_cluster.primary.master_auth[0].cluster_ca_certificate)
}
output "kubernetes_cluster_client_certificate" {
value = base64decode(google_container_cluster.primary.master_auth[0].client_certificate)
}
output "kubernetes_cluster_client_key" {
value = base64decode(google_container_cluster.primary.master_auth[0].client_key)
}
Initialize and apply the Terraform configuration
terraform init
terraform plan
Step 2: Develop Python Application
- Create the Python application:
- Create a directory for your application.
- Inside the directory, create
app.py
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
Create a requirements.txt
file
Flask==2.0.1
Create a Dockerfile
# Use the official Python image from the Docker Hub
FROM python:3.9-slim
# Set the working directory
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Make port 8080 available to the world outside this container
EXPOSE 8080
# Run app.py when the container launches
CMD ["python", "app.py"]
Create a Kubernetes Deployment and Service manifest (k8s-deployment.yaml
)
apiVersion: apps/v1
kind: Deployment
metadata:
name: python-app-deployment
spec:
replicas: 2
selector:
matchLabels:
app: python-app
template:
metadata:
labels:
app: python-app
spec:
containers:
- name: python-app
image: gcr.io/<YOUR_PROJECT_ID>/python-app:latest
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: python-app-service
spec:
selector:
app: python-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: LoadBalancer
Step 3: Setup CI/CD with Cloud Build
- Create a Cloud Build configuration file (
cloudbuild.yaml
)
steps:
# Step 1: Build Docker image
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/$PROJECT_ID/python-app', '.']
# Step 2: Push Docker image to Container Registry
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/$PROJECT_ID/python-app']
# Step 3: Apply Terraform to provision GKE cluster
- name: 'hashicorp/terraform:light'
entrypoint: 'sh'
args:
- '-c'
- |
cd terraform
terraform init
terraform apply -auto-approve
env:
- 'GOOGLE_CREDENTIALS=${_GOOGLE_CREDENTIALS}'
volumes:
- name: 'terraform-config'
path: '/workspace/terraform'
# Step 4: Configure kubectl with GKE cluster credentials
- name: 'gcr.io/cloud-builders/gcloud'
entrypoint: 'bash'
args:
- '-c'
- |
gcloud container clusters get-credentials $(terraform output -raw kubernetes_cluster_name) --region us-central1
# Step 5: Deploy application to GKE cluster
- name: 'gcr.io/cloud-builders/kubectl'
args:
- 'apply'
- '-f'
- 'k8s-deployment.yaml'
images:
- 'gcr.io/$PROJECT_ID/python-app'
timeout: '900s'
options:
volumes:
- name: 'terraform-config'
path: '/workspace/terraform'
Submit the build to Cloud Build
gcloud builds submit --config cloudbuild.yaml .