Skip to content

Commit 6e8d5ce

Browse files
committed
Init aks
1 parent 68671a7 commit 6e8d5ce

File tree

5 files changed

+341
-0
lines changed

5 files changed

+341
-0
lines changed

terraform/azure/aks-k8s/README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Azure Kubernetes Service (AKS) Terraform Example
2+
3+
This example demonstrates how to deploy an Azure Kubernetes Service (AKS) cluster using Terraform. The configuration includes:
4+
5+
- Resource Group
6+
- Virtual Network and Subnet
7+
- AKS Cluster with default node pool
8+
- System-assigned managed identity
9+
- Azure CNI networking
10+
11+
## Prerequisites
12+
13+
1. [Azure CLI](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli) installed
14+
2. [Terraform](https://www.terraform.io/downloads.html) installed (version >= 1.0)
15+
3. Azure subscription and appropriate permissions
16+
17+
## Authentication
18+
19+
Before running Terraform, you need to authenticate with Azure. You can do this by running:
20+
21+
```bash
22+
az login
23+
```
24+
25+
## Usage
26+
27+
1. Initialize Terraform:
28+
```bash
29+
terraform init
30+
```
31+
32+
2. Review the planned changes:
33+
```bash
34+
terraform plan
35+
```
36+
37+
3. Apply the configuration:
38+
```bash
39+
terraform apply
40+
```
41+
42+
4. To destroy the infrastructure:
43+
```bash
44+
terraform destroy
45+
```
46+
47+
## Configuration
48+
49+
The example uses variables with default values that can be overridden. You can create a `terraform.tfvars` file to customize the deployment:
50+
51+
```hcl
52+
resource_group_name = "my-aks-rg"
53+
location = "westeurope"
54+
cluster_name = "my-production-cluster"
55+
node_count = 3
56+
vm_size = "Standard_D4s_v3"
57+
```
58+
59+
## Outputs
60+
61+
After applying the configuration, Terraform will output:
62+
- `kube_config`: The Kubernetes config file (sensitive)
63+
- `cluster_endpoint`: The AKS cluster endpoint
64+
- `cluster_ca_certificate`: The cluster CA certificate (sensitive)
65+
- `cluster_name`: The name of the AKS cluster
66+
- `resource_group_name`: The name of the resource group
67+
68+
## Features
69+
70+
- Azure CNI networking
71+
- System-assigned managed identity
72+
- Auto-scaling enabled by default
73+
- Customizable node pool configuration
74+
- Network security through VNet integration
75+
- Resource tagging support
76+
77+
## Notes
78+
79+
- The default configuration uses `Standard_D2_v2` VMs which are suitable for development/testing
80+
- For production workloads, consider using larger VM sizes and enabling additional security features
81+
- The network configuration uses Azure CNI for better network performance and security

terraform/azure/aks-k8s/cluster.tf

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
locals {
2+
name = var.cluster_name != "" ? var.cluster_name : "example-${basename(path.cwd)}"
3+
4+
tags = merge({
5+
Name = local.name
6+
Environment = var.environment
7+
ManagedBy = "Terraform"
8+
}, var.tags)
9+
}
10+
11+
resource "azurerm_resource_group" "aks" {
12+
name = "${local.name}-rg"
13+
location = var.location
14+
tags = local.tags
15+
}
16+
17+
module "vpc" {
18+
source = "../internal-modules/azure-network"
19+
20+
name = local.name
21+
tags = local.tags
22+
23+
location = var.location
24+
resource_group_name = azurerm_resource_group.aks.name
25+
26+
cidrs = var.vnet_address_space
27+
subnet_cidrs = var.subnet_address_prefixes
28+
subnet_name_public = "aks-nodes"
29+
subnet_name_private = "aks-private"
30+
subnet_name_private_dns_resolver = "dns-resolver"
31+
}
32+
33+
# AKS Cluster
34+
resource "azurerm_kubernetes_cluster" "aks" {
35+
name = local.name
36+
location = azurerm_resource_group.aks.location
37+
resource_group_name = azurerm_resource_group.aks.name
38+
dns_prefix = local.name
39+
kubernetes_version = var.kubernetes_version
40+
41+
# Add node resource group name
42+
node_resource_group = "${local.name}-node-rg"
43+
44+
default_node_pool {
45+
name = "default"
46+
vm_size = var.vm_size
47+
vnet_subnet_id = module.vpc.public_subnet_id
48+
enable_auto_scaling = var.enable_auto_scaling
49+
min_count = var.min_count
50+
max_count = var.max_count
51+
os_disk_size_gb = 50
52+
zones = [1, 2, 3]
53+
}
54+
55+
identity {
56+
type = "SystemAssigned"
57+
}
58+
59+
network_profile {
60+
network_plugin = "azure"
61+
service_cidr = var.service_cidr
62+
dns_service_ip = var.dns_service_ip
63+
load_balancer_sku = "standard"
64+
}
65+
66+
# Use oms_agent addon directly instead of addon_profile
67+
dynamic "oms_agent" {
68+
for_each = var.enable_log_analytics_workspace ? [1] : []
69+
content {
70+
log_analytics_workspace_id = azurerm_log_analytics_workspace.aks[0].id
71+
}
72+
}
73+
74+
tags = local.tags
75+
}
76+
77+
# Conditionally create Log Analytics workspace if monitoring is enabled
78+
resource "azurerm_log_analytics_workspace" "aks" {
79+
count = var.enable_log_analytics_workspace ? 1 : 0
80+
name = "${local.name}-logs"
81+
location = azurerm_resource_group.aks.location
82+
resource_group_name = azurerm_resource_group.aks.name
83+
sku = "PerGB2018"
84+
retention_in_days = var.log_retention_in_days
85+
tags = local.tags
86+
}

terraform/azure/aks-k8s/main.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
terraform {
2+
required_providers {
3+
azurerm = {
4+
source = "hashicorp/azurerm"
5+
version = "~> 3.0"
6+
}
7+
}
8+
}
9+
10+
provider "azurerm" {
11+
features {}
12+
}

terraform/azure/aks-k8s/outputs.tf

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Output the cluster's credentials
2+
output "kube_config" {
3+
description = "Raw kubeconfig content for the AKS cluster"
4+
value = azurerm_kubernetes_cluster.aks.kube_config_raw
5+
sensitive = true
6+
}
7+
8+
output "cluster_endpoint" {
9+
description = "Kubernetes API server endpoint"
10+
value = azurerm_kubernetes_cluster.aks.kube_config.0.host
11+
sensitive = true
12+
}
13+
14+
output "cluster_ca_certificate" {
15+
description = "Base64 encoded certificate authority of the Kubernetes cluster"
16+
value = azurerm_kubernetes_cluster.aks.kube_config.0.cluster_ca_certificate
17+
sensitive = true
18+
}
19+
20+
output "cluster_name" {
21+
description = "Name of the AKS cluster"
22+
value = azurerm_kubernetes_cluster.aks.name
23+
}
24+
25+
output "resource_group_name" {
26+
description = "Name of the resource group containing the AKS cluster"
27+
value = azurerm_resource_group.aks.name
28+
}
29+
30+
output "vnet_id" {
31+
description = "ID of the virtual network"
32+
value = module.vpc.vnet_id
33+
}
34+
35+
output "principal_id" {
36+
description = "Principal ID of the AKS cluster identity"
37+
value = azurerm_kubernetes_cluster.aks.identity[0].principal_id
38+
}
39+
40+
output "node_resource_group" {
41+
description = "Auto-generated resource group for the AKS cluster nodes"
42+
value = azurerm_kubernetes_cluster.aks.node_resource_group
43+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
variable "location" {
2+
description = "Azure region where resources will be created"
3+
type = string
4+
default = "eastus"
5+
}
6+
7+
variable "cluster_name" {
8+
description = "Name of the AKS cluster (will generate one if empty)"
9+
type = string
10+
default = "raj"
11+
}
12+
13+
variable "environment" {
14+
description = "Environment for the resources (e.g., dev, test, prod)"
15+
type = string
16+
default = "dev"
17+
}
18+
19+
variable "tags" {
20+
description = "A map of tags to add to all resources"
21+
type = map(string)
22+
default = {}
23+
}
24+
25+
variable "kubernetes_version" {
26+
description = "Kubernetes version to use for the AKS cluster"
27+
type = string
28+
default = "1.31.6"
29+
}
30+
31+
variable "vm_size" {
32+
description = "VM size for the AKS node pool"
33+
type = string
34+
default = "Standard_DS2_v2"
35+
}
36+
37+
variable "enable_auto_scaling" {
38+
description = "Enable auto scaling for the AKS node pool"
39+
type = bool
40+
default = true
41+
}
42+
43+
variable "min_count" {
44+
description = "Minimum number of nodes in the AKS node pool"
45+
type = number
46+
default = 1
47+
}
48+
49+
variable "max_count" {
50+
description = "Maximum number of nodes in the AKS node pool"
51+
type = number
52+
default = 3
53+
}
54+
55+
variable "vnet_address_space" {
56+
description = "Address space for the virtual network"
57+
type = list(string)
58+
default = ["10.0.0.0/16"]
59+
}
60+
61+
variable "subnet_address_prefixes" {
62+
description = "Address prefixes for the subnets (requires 3 subnets for nodes, private, and DNS resolver)"
63+
type = list(string)
64+
default = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
65+
}
66+
67+
variable "service_cidr" {
68+
description = "CIDR range for Kubernetes services"
69+
type = string
70+
default = "172.16.0.0/16"
71+
}
72+
73+
variable "dns_service_ip" {
74+
description = "IP address for Kubernetes DNS service (must be within service_cidr)"
75+
type = string
76+
default = "172.16.0.10"
77+
}
78+
79+
variable "docker_bridge_cidr" {
80+
description = "CIDR notation IP for Docker bridge"
81+
type = string
82+
default = "172.17.0.1/16"
83+
}
84+
85+
variable "availability_zones" {
86+
description = "List of availability zones to use for the node pool"
87+
type = list(number)
88+
default = [1, 2, 3]
89+
}
90+
91+
variable "os_disk_size_gb" {
92+
description = "Disk size for nodes in GB"
93+
type = number
94+
default = 50
95+
}
96+
97+
variable "os_disk_type" {
98+
description = "Disk type for nodes"
99+
type = string
100+
default = "Managed"
101+
}
102+
103+
variable "node_labels" {
104+
description = "Labels to apply to nodes in the default node pool"
105+
type = map(string)
106+
default = {}
107+
}
108+
109+
variable "enable_log_analytics_workspace" {
110+
description = "Enable the creation of a Log Analytics workspace for the AKS cluster"
111+
type = bool
112+
default = false
113+
}
114+
115+
variable "log_retention_in_days" {
116+
description = "Number of days to retain logs in Log Analytics"
117+
type = number
118+
default = 30
119+
}

0 commit comments

Comments
 (0)