forked from jenkins-infra/azure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
privatek8s.tf
227 lines (200 loc) · 9.22 KB
/
privatek8s.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
resource "azurerm_resource_group" "privatek8s" {
name = "prod-privatek8s"
location = var.location
tags = local.default_tags
}
resource "random_pet" "suffix_privatek8s" {
# You want to taint this resource in order to get a new pet
}
data "azurerm_subnet" "privatek8s_tier" {
name = "privatek8s-tier"
resource_group_name = data.azurerm_resource_group.private.name
virtual_network_name = data.azurerm_virtual_network.private.name
}
data "azurerm_subnet" "privatek8s_release_tier" {
name = "privatek8s-release-tier"
resource_group_name = data.azurerm_resource_group.private.name
virtual_network_name = data.azurerm_virtual_network.private.name
}
#tfsec:ignore:azure-container-logging #tfsec:ignore:azure-container-limit-authorized-ips
resource "azurerm_kubernetes_cluster" "privatek8s" {
name = "privatek8s-${random_pet.suffix_privatek8s.id}"
location = azurerm_resource_group.privatek8s.location
resource_group_name = azurerm_resource_group.privatek8s.name
kubernetes_version = "1.24.9"
dns_prefix = "privatek8s-${random_pet.suffix_privatek8s.id}"
role_based_access_control_enabled = true # default value, added to please tfsec
api_server_access_profile {
authorized_ip_ranges = setunion(
formatlist("%s/32", values(local.admin_allowed_ips)),
data.azurerm_subnet.private_vnet_data_tier.address_prefixes,
)
}
network_profile {
network_plugin = "azure"
network_policy = "azure"
}
default_node_pool {
name = "syspool"
vm_size = "Standard_D2as_v4"
os_sku = "Ubuntu"
os_disk_type = "Ephemeral"
os_disk_size_gb = 50 # Ref. Cache storage size at https://learn.microsoft.com/en-us/azure/virtual-machines/dav4-dasv4-series#dasv4-series (depends on the instance size)
kubelet_disk_type = "OS"
enable_auto_scaling = true
min_count = 1
max_count = 3
vnet_subnet_id = data.azurerm_subnet.privatek8s_tier.id
tags = local.default_tags
zones = [3]
}
identity {
type = "SystemAssigned"
}
tags = local.default_tags
}
resource "azurerm_kubernetes_cluster_node_pool" "linuxpool" {
name = "linuxpool"
vm_size = "Standard_D4s_v3"
os_disk_type = "Ephemeral"
os_disk_size_gb = 100 # Ref. Cache storage size at https://learn.microsoft.com/en-us/azure/virtual-machines/dv3-dsv3-series#dsv3-series (depends on the instance size)
kubernetes_cluster_id = azurerm_kubernetes_cluster.privatek8s.id
enable_auto_scaling = true
min_count = 0
max_count = 5
zones = [3]
vnet_subnet_id = data.azurerm_subnet.privatek8s_tier.id
tags = local.default_tags
}
resource "azurerm_kubernetes_cluster_node_pool" "infracipool" {
name = "infracipool"
vm_size = "Standard_D8s_v3"
os_disk_type = "Ephemeral"
os_disk_size_gb = 200 # Ref. Cache storage size at https://learn.microsoft.com/en-us/azure/virtual-machines/dv3-dsv3-series#dsv3-series (depends on the instance size)
kubernetes_cluster_id = azurerm_kubernetes_cluster.privatek8s.id
enable_auto_scaling = true
min_count = 0
max_count = 20
zones = [3]
vnet_subnet_id = data.azurerm_subnet.privatek8s_tier.id
# Spot instances
priority = "Spot"
eviction_policy = "Delete"
spot_max_price = "-1" # in $, -1 = On demand pricing
# Note: label and taint added automatically when in "Spot" priority, putting it here to explicit them
node_labels = {
"kubernetes.azure.com/scalesetpriority" = "spot"
}
node_taints = [
"jenkins=infra.ci.jenkins.io:NoSchedule",
"kubernetes.azure.com/scalesetpriority=spot:NoSchedule",
]
tags = local.default_tags
}
resource "azurerm_kubernetes_cluster_node_pool" "releasepool" {
name = "releasepool"
vm_size = "Standard_D8s_v3" # 8 vCPU 32 GiB RAM
os_disk_type = "Ephemeral"
os_disk_size_gb = 200 # Ref. Cache storage size at https://learn.microsoft.com/en-us/azure/virtual-machines/dv3-dsv3-series#dsv3-series (depends on the instance size)
kubernetes_cluster_id = azurerm_kubernetes_cluster.privatek8s.id
enable_auto_scaling = true
min_count = 0
max_count = 3
zones = [3]
vnet_subnet_id = data.azurerm_subnet.privatek8s_release_tier.id
node_taints = [
"jenkins=release.ci.jenkins.io:NoSchedule",
]
tags = local.default_tags
}
resource "azurerm_kubernetes_cluster_node_pool" "windows2019pool" {
name = "w2019"
vm_size = "Standard_D4s_v3" # 4 vCPU 16 GiB RAM
os_disk_type = "Ephemeral"
os_disk_size_gb = 100 # Ref. Cache storage size at https://learn.microsoft.com/en-us/azure/virtual-machines/dv3-dsv3-series#dsv3-series (depends on the instance size)
os_type = "Windows"
os_sku = "Windows2019"
kubernetes_cluster_id = azurerm_kubernetes_cluster.privatek8s.id
enable_auto_scaling = true
min_count = 0
max_count = 3
zones = [3]
vnet_subnet_id = data.azurerm_subnet.privatek8s_release_tier.id
node_taints = [
"os=windows:NoSchedule",
"jenkins=release.ci.jenkins.io:NoSchedule",
]
tags = local.default_tags
}
# Allow cluster to manage LBs in the privatek8s-tier subnet (Public LB)
resource "azurerm_role_assignment" "privatek8s_networkcontributor" {
scope = "${data.azurerm_subscription.jenkins.id}/resourceGroups/${data.azurerm_resource_group.private.name}/providers/Microsoft.Network/virtualNetworks/${data.azurerm_virtual_network.private.name}/subnets/${data.azurerm_subnet.privatek8s_tier.name}"
role_definition_name = "Network Contributor"
principal_id = azurerm_kubernetes_cluster.privatek8s.identity[0].principal_id
skip_service_principal_aad_check = true
}
# Allow cluster to manage LBs in the data-tier subnet (internal LBs)
resource "azurerm_role_assignment" "datatier_networkcontributor" {
scope = "${data.azurerm_subscription.jenkins.id}/resourceGroups/${data.azurerm_resource_group.private.name}/providers/Microsoft.Network/virtualNetworks/${data.azurerm_virtual_network.private.name}/subnets/${data.azurerm_subnet.private_vnet_data_tier.name}"
role_definition_name = "Network Contributor"
principal_id = azurerm_kubernetes_cluster.privatek8s.identity[0].principal_id
skip_service_principal_aad_check = true
}
resource "kubernetes_storage_class" "managed_csi_premium_retain" {
metadata {
name = "managed-csi-premium-retain"
}
storage_provisioner = "disk.csi.azure.com"
reclaim_policy = "Retain"
parameters = {
skuname = "Premium_LRS"
}
provider = kubernetes.privatek8s
}
resource "kubernetes_storage_class" "azurefile_csi_premium_retain" {
metadata {
name = "azurefile-csi-premium-retain"
}
storage_provisioner = "file.csi.azure.com"
reclaim_policy = "Retain"
parameters = {
skuname = "Premium_LRS"
}
mount_options = ["dir_mode=0777", "file_mode=0777", "uid=1000", "gid=1000", "mfsymlinks", "nobrl"]
provider = kubernetes.privatek8s
}
# Used later by the load balancer deployed on the cluster, see https://github.com/jenkins-infra/kubernetes-management/config/privatek8s.yaml
resource "azurerm_public_ip" "public_privatek8s" {
name = "public-privatek8s"
resource_group_name = azurerm_kubernetes_cluster.privatek8s.node_resource_group
location = var.location
allocation_method = "Static"
sku = "Standard" # Needed to fix the error "PublicIPAndLBSkuDoNotMatch"
tags = local.default_tags
}
resource "azurerm_dns_a_record" "public_privatek8s" {
name = "public.privatek8s"
zone_name = data.azurerm_dns_zone.jenkinsio.name
resource_group_name = data.azurerm_resource_group.proddns_jenkinsio.name
ttl = 300
records = [azurerm_public_ip.public_privatek8s.ip_address]
tags = local.default_tags
}
resource "azurerm_dns_a_record" "private_privatek8s" {
name = "private.privatek8s"
zone_name = data.azurerm_dns_zone.jenkinsio.name
resource_group_name = data.azurerm_resource_group.proddns_jenkinsio.name
ttl = 300
records = ["10.248.1.5"] # External IP of the private-nginx ingress LoadBalancer, created by https://github.com/jenkins-infra/kubernetes-management/blob/54a0d4aa72b15f4236abcfbde00a080905bbb890/clusters/privatek8s.yaml#L112-L118
tags = local.default_tags
}
output "privatek8s_kube_config" {
value = azurerm_kubernetes_cluster.privatek8s.kube_config_raw
sensitive = true
}
output "privatek8s_public_ip_address" {
value = azurerm_public_ip.public_privatek8s.ip_address
}
output "privatek8s_kube_config_command" {
value = "az aks get-credentials --name ${azurerm_kubernetes_cluster.privatek8s.name} --resource-group ${azurerm_kubernetes_cluster.privatek8s.resource_group_name}"
}