Skip to content

Commit c9f2435

Browse files
committed
Init aks azure
1 parent 68671a7 commit c9f2435

File tree

7 files changed

+335
-0
lines changed

7 files changed

+335
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# azure-aks-k8s
2+
3+
This example creates the following:
4+
5+
- a Virtual Network with appropriate subnets using the [Azure RM Module for Network](https://registry.terraform.io/modules/Azure/network/azurerm/latest)
6+
from the Terraform Registry
7+
- an Azure Kubernetes Service (AKS) cluster with default node pool
8+
- a system-assigned managed identity for the AKS cluster
9+
- Azure CNI networking for better network performance
10+
- Optional Log Analytics workspace for monitoring
11+
12+
## Prerequisites
13+
14+
- [Azure CLI](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli) installed
15+
- [Terraform](https://www.terraform.io/downloads.html) installed (version >= 1.0)
16+
- Azure subscription and appropriate permissions
17+
18+
## To use
19+
20+
Follow the documentation to configure the Azure provider:
21+
22+
- [Azure](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs)
23+
24+
### Deploy
25+
26+
```shell
27+
terraform init
28+
terraform apply
29+
```
30+
31+
## To destroy
32+
33+
```shell
34+
terraform destroy
35+
```
36+
37+
## Configuration
38+
39+
The example uses variables with default values that can be overridden. You can create a `terraform.tfvars` file to customize the deployment:
40+
41+
```hcl
42+
resource_group_name = "my-aks-rg"
43+
location = "westeurope"
44+
cluster_name = "my-production-cluster"
45+
node_count = 3
46+
vm_size = "Standard_D4s_v3"
47+
```
48+
49+
## Outputs
50+
51+
After applying the configuration, Terraform will output:
52+
- `kube_config`: The Kubernetes config file (sensitive)
53+
- `cluster_endpoint`: The AKS cluster endpoint
54+
- `cluster_ca_certificate`: The cluster CA certificate (sensitive)
55+
- `cluster_name`: The name of the AKS cluster
56+
- `resource_group_name`: The name of the resource group
57+
58+
## Features
59+
60+
- Azure CNI networking
61+
- System-assigned managed identity
62+
- Auto-scaling enabled by default
63+
- Customizable node pool configuration
64+
- Network security through VNet integration
65+
- Resource tagging support
66+
67+
## Notes
68+
69+
- The default configuration uses `Standard_D2_v2` VMs which are suitable for development/testing
70+
- For production workloads, consider using larger VM sizes and enabling additional security features
71+
- The network configuration uses Azure CNI for better network performance and security
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# AKS Resources
2+
3+
resource "azurerm_resource_group" "aks" {
4+
name = "${local.name}-rg"
5+
location = var.location
6+
tags = local.tags
7+
}
8+
9+
module "vpc" {
10+
source = "../internal-modules/azure-network"
11+
12+
name = local.name
13+
tags = local.tags
14+
15+
location = var.location
16+
resource_group_name = azurerm_resource_group.aks.name
17+
18+
cidrs = var.vnet_address_space
19+
subnet_cidrs = var.subnet_address_prefixes
20+
subnet_name_public = "aks-nodes"
21+
subnet_name_private = "aks-private"
22+
subnet_name_private_dns_resolver = "dns-resolver"
23+
}
24+
25+
# AKS Cluster
26+
resource "azurerm_kubernetes_cluster" "aks" {
27+
name = local.name
28+
location = azurerm_resource_group.aks.location
29+
resource_group_name = azurerm_resource_group.aks.name
30+
dns_prefix = local.name
31+
kubernetes_version = var.kubernetes_version
32+
33+
# Add node resource group name
34+
node_resource_group = "${local.name}-node-rg"
35+
36+
default_node_pool {
37+
name = "default"
38+
vm_size = var.vm_size
39+
vnet_subnet_id = module.vpc.public_subnet_id
40+
enable_auto_scaling = var.enable_auto_scaling
41+
min_count = var.min_count
42+
max_count = var.max_count
43+
os_disk_size_gb = 50
44+
zones = [1, 2, 3]
45+
}
46+
47+
identity {
48+
type = "SystemAssigned"
49+
}
50+
51+
network_profile {
52+
network_plugin = "azure"
53+
service_cidr = var.service_cidr
54+
dns_service_ip = var.dns_service_ip
55+
load_balancer_sku = "standard"
56+
}
57+
58+
# Use oms_agent addon directly instead of addon_profile
59+
dynamic "oms_agent" {
60+
for_each = var.enable_log_analytics_workspace ? [1] : []
61+
content {
62+
log_analytics_workspace_id = azurerm_log_analytics_workspace.aks[0].id
63+
}
64+
}
65+
66+
tags = local.tags
67+
}
68+
69+
# Conditionally create Log Analytics workspace if monitoring is enabled
70+
resource "azurerm_log_analytics_workspace" "aks" {
71+
count = var.enable_log_analytics_workspace ? 1 : 0
72+
name = "${local.name}-logs"
73+
location = azurerm_resource_group.aks.location
74+
resource_group_name = azurerm_resource_group.aks.name
75+
sku = "PerGB2018"
76+
retention_in_days = var.log_retention_in_days
77+
tags = local.tags
78+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
}
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
provider "azurerm" {
2+
skip_provider_registration = true
3+
features {
4+
resource_group {
5+
prevent_deletion_if_contains_resources = false
6+
}
7+
}
8+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
}
11+
12+
variable "environment" {
13+
description = "Environment for the resources (e.g., dev, test, prod)"
14+
type = string
15+
default = "dev"
16+
}
17+
18+
variable "tags" {
19+
description = "A map of tags to add to all resources"
20+
type = map(string)
21+
default = {}
22+
}
23+
24+
variable "kubernetes_version" {
25+
description = "Kubernetes version to use for the AKS cluster"
26+
type = string
27+
default = "1.31.6"
28+
}
29+
30+
variable "vm_size" {
31+
description = "VM size for the AKS node pool"
32+
type = string
33+
default = "Standard_DS2_v2"
34+
}
35+
36+
variable "enable_auto_scaling" {
37+
description = "Enable auto scaling for the AKS node pool"
38+
type = bool
39+
default = true
40+
}
41+
42+
variable "min_count" {
43+
description = "Minimum number of nodes in the AKS node pool"
44+
type = number
45+
default = 1
46+
}
47+
48+
variable "max_count" {
49+
description = "Maximum number of nodes in the AKS node pool"
50+
type = number
51+
default = 3
52+
}
53+
54+
variable "vnet_address_space" {
55+
description = "Address space for the virtual network"
56+
type = list(string)
57+
default = ["10.0.0.0/16"]
58+
}
59+
60+
variable "subnet_address_prefixes" {
61+
description = "Address prefixes for the subnets (requires 3 subnets for nodes, private, and DNS resolver)"
62+
type = list(string)
63+
default = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
64+
}
65+
66+
variable "service_cidr" {
67+
description = "CIDR range for Kubernetes services"
68+
type = string
69+
default = "172.16.0.0/16"
70+
}
71+
72+
variable "dns_service_ip" {
73+
description = "IP address for Kubernetes DNS service (must be within service_cidr)"
74+
type = string
75+
default = "172.16.0.10"
76+
}
77+
78+
variable "docker_bridge_cidr" {
79+
description = "CIDR notation IP for Docker bridge"
80+
type = string
81+
default = "172.17.0.1/16"
82+
}
83+
84+
variable "availability_zones" {
85+
description = "List of availability zones to use for the node pool"
86+
type = list(number)
87+
default = [1, 2, 3]
88+
}
89+
90+
variable "os_disk_size_gb" {
91+
description = "Disk size for nodes in GB"
92+
type = number
93+
default = 50
94+
}
95+
96+
variable "os_disk_type" {
97+
description = "Disk type for nodes"
98+
type = string
99+
default = "Managed"
100+
}
101+
102+
variable "node_labels" {
103+
description = "Labels to apply to nodes in the default node pool"
104+
type = map(string)
105+
default = {}
106+
}
107+
108+
variable "enable_log_analytics_workspace" {
109+
description = "Enable the creation of a Log Analytics workspace for the AKS cluster"
110+
type = bool
111+
default = false
112+
}
113+
114+
variable "log_retention_in_days" {
115+
description = "Number of days to retain logs in Log Analytics"
116+
type = number
117+
default = 30
118+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
terraform {
2+
required_providers {
3+
azurerm = {
4+
source = "hashicorp/azurerm"
5+
version = "~> 3.0"
6+
}
7+
}
8+
}

0 commit comments

Comments
 (0)