Difference between revisions of "Aks terraform ha python"
Jump to navigation
Jump to search
(Created page with "``` import os import json from jinja2 import Template # Define variables for your AKS cluster resource_group_name = "my-aks-rg" location = "eastus" cluster_name = "my-aks-clu...") |
|||
Line 37: | Line 37: | ||
os.system("terraform plan") | os.system("terraform plan") | ||
os.system("terraform apply") | os.system("terraform apply") | ||
+ | ``` | ||
+ | |||
+ | |||
+ | # Another | ||
+ | |||
+ | ``` | ||
+ | import os | ||
+ | import subprocess | ||
+ | |||
+ | # Configuration: specify regions and AKS cluster parameters | ||
+ | regions = ["eastus", "westus", "northeurope"] # Modify as needed | ||
+ | aks_params = { | ||
+ | "cluster_name": "my-aks-cluster", | ||
+ | "resource_group": "my-aks-resource-group", | ||
+ | "node_count": 3, | ||
+ | "kubernetes_version": "1.26.3" | ||
+ | } | ||
+ | |||
+ | # Terraform template directory | ||
+ | terraform_dir = "./terraform" # Path to your Terraform configuration files | ||
+ | |||
+ | # Ensure Terraform is installed | ||
+ | def check_terraform_installed(): | ||
+ | try: | ||
+ | subprocess.run(["terraform", "--version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | ||
+ | print("Terraform is installed.") | ||
+ | except FileNotFoundError: | ||
+ | print("Terraform is not installed. Please install Terraform before running this script.") | ||
+ | exit(1) | ||
+ | |||
+ | # Initialize and apply Terraform for each region | ||
+ | def deploy_aks_clusters(): | ||
+ | for region in regions: | ||
+ | print(f"Deploying AKS cluster in {region}...") | ||
+ | # Set environment variables for Terraform | ||
+ | env = os.environ.copy() | ||
+ | env.update({ | ||
+ | "TF_VAR_region": region, | ||
+ | "TF_VAR_cluster_name": f"{aks_params['cluster_name']}-{region}", | ||
+ | "TF_VAR_resource_group": f"{aks_params['resource_group']}-{region}", | ||
+ | "TF_VAR_node_count": str(aks_params['node_count']), | ||
+ | "TF_VAR_kubernetes_version": aks_params["kubernetes_version"] | ||
+ | }) | ||
+ | |||
+ | # Terraform commands | ||
+ | try: | ||
+ | # Initialize Terraform | ||
+ | subprocess.run(["terraform", "init"], cwd=terraform_dir, check=True, env=env) | ||
+ | |||
+ | # Apply Terraform configuration | ||
+ | subprocess.run(["terraform", "apply", "-auto-approve"], cwd=terraform_dir, check=True, env=env) | ||
+ | |||
+ | print(f"Successfully deployed AKS cluster in {region}.") | ||
+ | except subprocess.CalledProcessError as e: | ||
+ | print(f"Error deploying AKS cluster in {region}: {e}") | ||
+ | continue | ||
+ | |||
+ | if __name__ == "__main__": | ||
+ | check_terraform_installed() | ||
+ | deploy_aks_clusters() | ||
+ | ``` | ||
+ | |||
+ | maint.tf | ||
+ | |||
+ | ``` | ||
+ | provider "azurerm" { | ||
+ | features {} | ||
+ | } | ||
+ | |||
+ | variable "region" {} | ||
+ | variable "cluster_name" {} | ||
+ | variable "resource_group" {} | ||
+ | variable "node_count" {} | ||
+ | variable "kubernetes_version" {} | ||
+ | |||
+ | resource "azurerm_resource_group" "aks" { | ||
+ | name = var.resource_group | ||
+ | location = var.region | ||
+ | } | ||
+ | |||
+ | resource "azurerm_kubernetes_cluster" "aks" { | ||
+ | name = var.cluster_name | ||
+ | location = var.region | ||
+ | resource_group_name = azurerm_resource_group.aks.name | ||
+ | kubernetes_version = var.kubernetes_version | ||
+ | |||
+ | default_node_pool { | ||
+ | name = "default" | ||
+ | node_count = var.node_count | ||
+ | vm_size = "Standard_DS2_v2" | ||
+ | } | ||
+ | |||
+ | identity { | ||
+ | type = "SystemAssigned" | ||
+ | } | ||
+ | } | ||
``` | ``` |
Latest revision as of 21:22, 14 December 2024
import os import json from jinja2 import Template # Define variables for your AKS cluster resource_group_name = "my-aks-rg" location = "eastus" cluster_name = "my-aks-cluster" node_pool_name = "default-nodepool" vm_size = "Standard_D2_v4" kubernetes_version = "1.26" dns_prefix = "myakscluster" # Load Terraform template with open("aks_ha_template.tf", "r") as f: template_string = f.read() template = Template(template_string) # Generate Terraform configuration terraform_config = template.render( resource_group_name=resource_group_name, location=location, cluster_name=cluster_name, node_pool_name=node_pool_name, vm_size=vm_size, kubernetes_version=kubernetes_version, dns_prefix=dns_prefix ) # Write generated Terraform code to a file with open("main.tf", "w") as f: f.write(terraform_config) # Execute Terraform commands (replace with your actual execution method) os.system("terraform init") os.system("terraform plan") os.system("terraform apply")
Another
import os import subprocess # Configuration: specify regions and AKS cluster parameters regions = ["eastus", "westus", "northeurope"] # Modify as needed aks_params = { "cluster_name": "my-aks-cluster", "resource_group": "my-aks-resource-group", "node_count": 3, "kubernetes_version": "1.26.3" } # Terraform template directory terraform_dir = "./terraform" # Path to your Terraform configuration files # Ensure Terraform is installed def check_terraform_installed(): try: subprocess.run(["terraform", "--version"], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) print("Terraform is installed.") except FileNotFoundError: print("Terraform is not installed. Please install Terraform before running this script.") exit(1) # Initialize and apply Terraform for each region def deploy_aks_clusters(): for region in regions: print(f"Deploying AKS cluster in {region}...") # Set environment variables for Terraform env = os.environ.copy() env.update({ "TF_VAR_region": region, "TF_VAR_cluster_name": f"{aks_params['cluster_name']}-{region}", "TF_VAR_resource_group": f"{aks_params['resource_group']}-{region}", "TF_VAR_node_count": str(aks_params['node_count']), "TF_VAR_kubernetes_version": aks_params["kubernetes_version"] }) # Terraform commands try: # Initialize Terraform subprocess.run(["terraform", "init"], cwd=terraform_dir, check=True, env=env) # Apply Terraform configuration subprocess.run(["terraform", "apply", "-auto-approve"], cwd=terraform_dir, check=True, env=env) print(f"Successfully deployed AKS cluster in {region}.") except subprocess.CalledProcessError as e: print(f"Error deploying AKS cluster in {region}: {e}") continue if __name__ == "__main__": check_terraform_installed() deploy_aks_clusters()
maint.tf
provider "azurerm" { features {} } variable "region" {} variable "cluster_name" {} variable "resource_group" {} variable "node_count" {} variable "kubernetes_version" {} resource "azurerm_resource_group" "aks" { name = var.resource_group location = var.region } resource "azurerm_kubernetes_cluster" "aks" { name = var.cluster_name location = var.region resource_group_name = azurerm_resource_group.aks.name kubernetes_version = var.kubernetes_version default_node_pool { name = "default" node_count = var.node_count vm_size = "Standard_DS2_v2" } identity { type = "SystemAssigned" } }