forked from mspnp/aks-baseline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster-stamp.bicep
2341 lines (2199 loc) · 77.1 KB
/
cluster-stamp.bicep
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
targetScope = 'resourceGroup'
/*** PARAMETERS ***/
@description('The regional network spoke VNet Resource ID that the cluster will be joined to')
@minLength(79)
param targetVnetResourceId string
@description('Azure AD Group in the identified tenant that will be granted the highly privileged cluster-admin role. If Azure RBAC is used, then this group will get a role assignment to Azure RBAC, else it will be assigned directly to the cluster\'s admin group.')
param clusterAdminAadGroupObjectId string
@description('Azure AD Group in the identified tenant that will be granted the read only privileges in the a0008 namespace that exists in the cluster. This is only used when Azure RBAC is used for Kubernetes RBAC.')
param a0008NamespaceReaderAadGroupObjectId string
@description('Your AKS control plane Cluster API authentication tenant')
param k8sControlPlaneAuthorizationTenantId string
@description('The certificate data for app gateway TLS termination. It is base64')
param appGatewayListenerCertificate string
@description('The Base64 encoded AKS Ingress Controller public certificate (as .crt or .cer) to be stored in Azure Key Vault as secret and referenced by Azure Application Gateway as a trusted root certificate.')
param aksIngressControllerCertificate string
@description('IP ranges authorized to contact the Kubernetes API server. Passing an empty array will result in no IP restrictions. If any are provided, remember to also provide the public IP of the egress Azure Firewall otherwise your nodes will not be able to talk to the API server (e.g. Flux).')
param clusterAuthorizedIPRanges array = []
@description('AKS Service, Node Pool, and supporting services (KeyVault, App Gateway, etc) region. This needs to be the same region as the vnet provided in these parameters.')
@allowed([
'australiaeast'
'canadacentral'
'centralus'
'eastus'
'eastus2'
'westus2'
'francecentral'
'germanywestcentral'
'northeurope'
'southafricanorth'
'southcentralus'
'uksouth'
'westeurope'
'japaneast'
'southeastasia'
])
param location string = 'eastus2'
param kubernetesVersion string = '1.26.0'
@description('Domain name to use for App Gateway and AKS ingress.')
param domainName string = 'contoso.com'
@description('Your cluster will be bootstrapped from this git repo.')
@minLength(9)
param gitOpsBootstrappingRepoHttpsUrl string = 'https://github.com/mspnp/aks-baseline'
@description('You cluster will be bootstrapped from this branch in the identified git repo.')
@minLength(1)
param gitOpsBootstrappingRepoBranch string = 'main'
/*** VARIABLES ***/
var subRgUniqueString = uniqueString('aks', subscription().subscriptionId, resourceGroup().id)
var clusterName = 'aks-${subRgUniqueString}'
var agwName = 'apw-${clusterName}'
var aksIngressDomainName = 'aks-ingress.${domainName}'
var aksBackendDomainName = 'bu0001a0008-00.${aksIngressDomainName}'
var isUsingAzureRBACasKubernetesRBAC = (subscription().tenantId == k8sControlPlaneAuthorizationTenantId)
/*** EXISTING TENANT RESOURCES ***/
// Built-in 'Kubernetes cluster pod security restricted standards for Linux-based workloads' Azure Policy for Kubernetes initiative definition
var psdAKSLinuxRestrictiveId = tenantResourceId('Microsoft.Authorization/policySetDefinitions', '42b8ef37-b724-4e24-bbc8-7a7708edfe00')
// Built-in 'Kubernetes clusters should be accessible only over HTTPS' Azure Policy for Kubernetes policy definition
var pdEnforceHttpsIngressId = tenantResourceId('Microsoft.Authorization/policyDefinitions', '1a5b4dca-0b6f-4cf5-907c-56316bc1bf3d')
// Built-in 'Kubernetes clusters should use internal load balancers' Azure Policy for Kubernetes policy definition
var pdEnforceInternalLoadBalancersId = tenantResourceId('Microsoft.Authorization/policyDefinitions', '3fc4dc25-5baf-40d8-9b05-7fe74c1bc64e')
// Built-in 'Kubernetes cluster containers should run with a read only root file system' Azure Policy for Kubernetes policy definition
var pdRoRootFilesystemId = tenantResourceId('Microsoft.Authorization/policyDefinitions', 'df49d893-a74c-421d-bc95-c663042e5b80')
// Built-in 'AKS container CPU and memory resource limits should not exceed the specified limits' Azure Policy for Kubernetes policy definition
var pdEnforceResourceLimitsId = tenantResourceId('Microsoft.Authorization/policyDefinitions', 'e345eecc-fa47-480f-9e88-67dcc122b164')
// Built-in 'AKS containers should only use allowed images' Azure Policy for Kubernetes policy definition
var pdEnforceImageSourceId = tenantResourceId('Microsoft.Authorization/policyDefinitions', 'febd0533-8e55-448f-b837-bd0e06f16469')
// Built-in 'Kubernetes cluster pod hostPath volumes should only use allowed host paths' Azure Policy for Kubernetes policy definition
var pdAllowedHostPathsId = tenantResourceId('Microsoft.Authorization/policyDefinitions', '098fc59e-46c7-4d99-9b16-64990e543d75')
// Built-in 'Kubernetes cluster services should only use allowed external IPs' Azure Policy for Kubernetes policy definition
var pdAllowedExternalIPsId = tenantResourceId('Microsoft.Authorization/policyDefinitions', 'd46c275d-1680-448d-b2ec-e495a3b6cc89')
// Built-in 'Kubernetes clusters should not allow endpoint edit permissions of ClusterRole/system:aggregate-to-edit' Azure Policy for Kubernetes policy definition
var pdDisallowEndpointEditPermissionsId = tenantResourceId('Microsoft.Authorization/policyDefinitions', '1ddac26b-ed48-4c30-8cc5-3a68c79b8001')
// Built-in 'Kubernetes clusters should not use the default namespace' Azure Policy for Kubernetes policy definition
var pdDisallowNamespaceUsageId = tenantResourceId('Microsoft.Authorization/policyDefinitions', '9f061a12-e40d-4183-a00e-171812443373')
// Built-in 'Azure Kubernetes Service clusters should have Defender profile enabled' Azure Policy policy definition
var pdDefenderInClusterEnabledId = tenantResourceId('Microsoft.Authorization/policyDefinitions', 'a1840de2-8088-4ea8-b153-b4c723e9cb01')
// Built-in 'Azure Kubernetes Service Clusters should enable Azure Active Directory integration' Azure Policy policy definition
var pdAadIntegrationEnabledId = tenantResourceId('Microsoft.Authorization/policyDefinitions', '450d2877-ebea-41e8-b00c-e286317d21bf')
// Built-in 'Azure Kubernetes Service Clusters should have local authentication methods disabled' Azure Policy policy definition
var pdLocalAuthDisabledId = tenantResourceId('Microsoft.Authorization/policyDefinitions', '993c2fcd-2b29-49d2-9eb0-df2c3a730c32')
// Built-in 'Azure Policy Add-on for Kubernetes service (AKS) should be installed and enabled on your clusters' Azure Policy policy definition
var pdAzurePolicyEnabledId = tenantResourceId('Microsoft.Authorization/policyDefinitions', '0a15ec92-a229-4763-bb14-0ea34a568f8d')
// Built-in 'Authorized IP ranges should be defined on Kubernetes Services' Azure Policy policy definition
var pdAuthorizedIpRangesDefinedId = tenantResourceId('Microsoft.Authorization/policyDefinitions', '0e246bcf-5f6f-4f87-bc6f-775d4712c7ea')
// Built-in 'Kubernetes Services should be upgraded to a non-vulnerable Kubernetes version' Azure Policy policy definition
var pdOldKuberentesDisabledId = tenantResourceId('Microsoft.Authorization/policyDefinitions', 'fb893a29-21bb-418c-a157-e99480ec364c')
// Built-in 'Role-Based Access Control (RBAC) should be used on Kubernetes Services' Azure Policy policy definition
var pdRbacEnabledId = tenantResourceId('Microsoft.Authorization/policyDefinitions', 'ac4a19c2-fa67-49b4-8ae5-0b2e78c49457')
// Built-in 'Azure Kubernetes Service Clusters should use managed identities' Azure Policy policy definition
var pdManagedIdentitiesEnabledId = tenantResourceId('Microsoft.Authorization/policyDefinitions', 'da6e2401-19da-4532-9141-fb8fbde08431')
/*** EXISTING SUBSCRIPTION RESOURCES ***/
resource nodeResourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' existing = {
name: 'rg-${clusterName}-nodepools'
scope: subscription()
}
// Built-in Azure RBAC role that is applied to a cluster to indicate they can be considered a user/group of the cluster, subject to additional RBAC permissions
resource serviceClusterUserRole 'Microsoft.Authorization/roleDefinitions@2018-01-01-preview' existing = {
name: '4abbcc35-e782-43d8-92c5-2d3f1bd2253f'
scope: subscription()
}
// Built-in Azure RBAC role that can be applied to a cluster or a namespace to grant read and write privileges to that scope for a user or group
resource clusterAdminRole 'Microsoft.Authorization/roleDefinitions@2018-01-01-preview' existing = {
name: 'b1ff04bb-8a4e-4dc4-8eb5-8693973ce19b'
scope: subscription()
}
// Built-in Azure RBAC role that can be applied to a cluster or a namespace to grant read privileges to that scope for a user or group
resource clusterReaderRole 'Microsoft.Authorization/roleDefinitions@2018-01-01-preview' existing = {
name: '7f6c6a51-bcf8-42ba-9220-52d62157d7db'
scope: subscription()
}
// Built-in Azure RBAC role that is applied to a cluster to grant its monitoring agent's identity with publishing metrics and push alerts permissions.
resource monitoringMetricsPublisherRole 'Microsoft.Authorization/roleDefinitions@2018-01-01-preview' existing = {
name: '3913510d-42f4-4e42-8a64-420c390055eb'
scope: subscription()
}
// Built-in Azure RBAC role that can be applied to an Azure Container Registry to grant the authority pull container images. Granted to the AKS cluster's kubelet identity.
resource acrPullRole 'Microsoft.Authorization/roleDefinitions@2018-01-01-preview' existing = {
name: '7f951dda-4ed3-4680-a7ca-43fe172d538d'
scope: subscription()
}
// Built-in Azure RBAC role that is applied a Key Vault to grant with metadata, certificates, keys and secrets read privileges. Granted to App Gateway's managed identity.
resource keyVaultReaderRole 'Microsoft.Authorization/roleDefinitions@2018-01-01-preview' existing = {
name: '21090545-7ca7-4776-b22c-e363652d74d2'
scope: subscription()
}
// Built-in Azure RBAC role that is applied to a Key Vault to grant with secrets content read privileges. Granted to both Key Vault and our workload's identity.
resource keyVaultSecretsUserRole 'Microsoft.Authorization/roleDefinitions@2018-01-01-preview' existing = {
name: '4633458b-17de-408a-b874-0445c86b69e6'
scope: subscription()
}
/*** EXISTING RESOURCE GROUP RESOURCES ***/
// Useful to think of these as resources that are not tied to the lifecycle of any individual
// cluster. Logging sinks, container registries, backup destinations, etc are typical
// resources that would exist before & after any individual cluster is deployed or is removed
// from the solution.
// Azure Container Registry
resource acr 'Microsoft.ContainerRegistry/registries@2021-12-01-preview' existing = {
scope: resourceGroup()
name: 'acraks${subRgUniqueString}'
}
// Log Analytics Workspace
resource la 'Microsoft.OperationalInsights/workspaces@2021-12-01-preview' existing = {
scope: resourceGroup()
name: 'la-${clusterName}'
}
// Kubernetes namespace: a0008 -- this doesn't technically exist prior to deployment, but is required as a resource reference later in the template
// to support Azure RBAC-managed API Server access, scoped to the namespace level.
#disable-next-line BCP081 // this namespaces child type doesn't have a defined bicep type yet.
resource nsA0008 'Microsoft.ContainerService/managedClusters/namespaces@2022-01-02-preview' existing = {
parent: mc
name: 'a0008'
}
/*** EXISTING SPOKE RESOURCES ***/
// Spoke resource group
resource targetResourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' existing = {
scope: subscription()
name: split(targetVnetResourceId, '/')[4]
}
// Spoke virtual network
resource targetVirtualNetwork 'Microsoft.Network/virtualNetworks@2022-05-01' existing = {
scope: targetResourceGroup
name: last(split(targetVnetResourceId, '/'))
// Spoke virutual network's subnet for the cluster nodes
resource snetClusterNodes 'subnets' existing = {
name: 'snet-clusternodes'
}
// Spoke virutual network's subnet for all private endpoints
resource snetPrivatelinkendpoints 'subnets' existing = {
name: 'snet-privatelinkendpoints'
}
// Spoke virutual network's subnet for application gateway
resource snetApplicationGateway 'subnets' existing = {
name: 'snet-applicationgateway'
}
}
/*** RESOURCES ***/
resource alaRgRecommendations 'Microsoft.Insights/activityLogAlerts@2020-10-01' = {
name: 'AllAzureAdvisorAlert'
location: 'Global'
properties: {
scopes: [
resourceGroup().id
]
condition: {
allOf: [
{
field: 'category'
equals: 'Recommendation'
}
{
field: 'operationName'
equals: 'Microsoft.Advisor/recommendations/available/action'
}
]
}
actions: {
actionGroups: []
}
enabled: true
description: 'All azure advisor alerts'
}
}
// A query pack to hold any custom quries you may want to write to monitor your cluster or workloads
resource qpBaselineQueryPack 'Microsoft.OperationalInsights/queryPacks@2019-09-01' = {
location: location
name: 'AKS baseline bundled queries'
properties: {}
}
// Example query that shows all scraped Prometheus metrics
resource qPrometheusAll 'Microsoft.OperationalInsights/queryPacks/queries@2019-09-01' = {
parent: qpBaselineQueryPack
name: guid(resourceGroup().id, 'PrometheusAll', clusterName)
properties: {
displayName: 'All collected Prometheus information'
description: 'This is all collected Prometheus metrics'
body: 'InsightsMetrics | where Namespace == "prometheus"'
related: {
categories: [
'container'
]
}
}
}
// Example query that shows the usage of a specific Prometheus metric emitted by Kured
resource qNodeReboots 'Microsoft.OperationalInsights/queryPacks/queries@2019-09-01' = {
parent: qpBaselineQueryPack
name: guid(resourceGroup().id, 'KuredNodeReboot', clusterName)
properties: {
displayName: 'Kubenertes node reboot requested'
description: 'Which Kubernetes nodes are flagged for reboot (based on Prometheus metrics).'
body: 'InsightsMetrics | where Namespace == "prometheus" and Name == "kured_reboot_required" | where Val > 0'
related: {
categories: [
'container'
'management'
]
}
}
}
resource sci 'Microsoft.OperationsManagement/solutions@2015-11-01-preview' = {
name: 'ContainerInsights(${la.name})'
location: location
properties: {
containedResources: []
referencedResources: []
workspaceResourceId: la.id
}
plan: {
name: 'ContainerInsights(${la.name})'
product: 'OMSGallery/ContainerInsights'
promotionCode: ''
publisher: 'Microsoft'
}
}
resource maHighNodeCPUUtilization 'Microsoft.Insights/metricAlerts@2018-03-01' = {
name: 'Node CPU utilization high for ${clusterName} CI-1'
location: 'global'
properties: {
autoMitigate: true
scopes: [
mc.id
]
actions: []
criteria: {
allOf: [
{
criterionType: 'StaticThresholdCriterion'
dimensions: [
{
name: 'host'
operator: 'Include'
values: [
'*'
]
}
]
metricName: 'cpuUsagePercentage'
metricNamespace: 'Insights.Container/nodes'
name: 'Metric1'
operator: 'GreaterThan'
threshold: 80
timeAggregation: 'Average'
skipMetricValidation: true
}
]
'odata.type': 'Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria'
}
description: 'Node CPU utilization across the cluster.'
enabled: true
evaluationFrequency: 'PT1M'
severity: 3
targetResourceType: 'microsoft.containerservice/managedclusters'
windowSize: 'PT5M'
}
dependsOn: [
sci
]
}
resource maHighNodeWorkingSetMemoryUtilization 'Microsoft.Insights/metricAlerts@2018-03-01' = {
name: 'Node working set memory utilization high for ${clusterName} CI-2'
location: 'global'
properties: {
autoMitigate: true
actions: []
criteria: {
allOf: [
{
criterionType: 'StaticThresholdCriterion'
dimensions: [
{
name: 'host'
operator: 'Include'
values: [
'*'
]
}
]
metricName: 'memoryWorkingSetPercentage'
metricNamespace: 'Insights.Container/nodes'
name: 'Metric1'
operator: 'GreaterThan'
threshold: 80
timeAggregation: 'Average'
skipMetricValidation: true
}
]
'odata.type': 'Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria'
}
description: 'Node working set memory utilization across the cluster.'
enabled: true
evaluationFrequency: 'PT1M'
scopes: [
mc.id
]
severity: 3
targetResourceType: 'microsoft.containerservice/managedclusters'
windowSize: 'PT5M'
}
dependsOn: [
sci
]
}
resource maJobsCompletedMoreThan6HoursAgo 'Microsoft.Insights/metricAlerts@2018-03-01' = {
name: 'Jobs completed more than 6 hours ago for ${clusterName} CI-11'
location: 'global'
properties: {
autoMitigate: true
actions: []
criteria: {
allOf: [
{
criterionType: 'StaticThresholdCriterion'
dimensions: [
{
name: 'controllerName'
operator: 'Include'
values: [
'*'
]
}
{
name: 'kubernetes namespace'
operator: 'Include'
values: [
'*'
]
}
]
metricName: 'completedJobsCount'
metricNamespace: 'Insights.Container/pods'
name: 'Metric1'
operator: 'GreaterThan'
threshold: 0
timeAggregation: 'Average'
skipMetricValidation: true
}
]
'odata.type': 'Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria'
}
description: 'This alert monitors completed jobs (more than 6 hours ago).'
enabled: true
evaluationFrequency: 'PT1M'
scopes: [
mc.id
]
severity: 3
targetResourceType: 'microsoft.containerservice/managedclusters'
windowSize: 'PT1M'
}
dependsOn: [
sci
]
}
resource maHighContainerCPUUsage 'Microsoft.Insights/metricAlerts@2018-03-01' = {
name: 'Container CPU usage violates the configured threshold for ${clusterName} CI-19'
location: 'global'
properties: {
autoMitigate: true
actions: []
criteria: {
allOf: [
{
criterionType: 'StaticThresholdCriterion'
dimensions: [
{
name: 'controllerName'
operator: 'Include'
values: [
'*'
]
}
{
name: 'kubernetes namespace'
operator: 'Include'
values: [
'*'
]
}
]
metricName: 'cpuThresholdViolated'
metricNamespace: 'Insights.Container/containers'
name: 'Metric1'
operator: 'GreaterThan'
threshold: 0 // This threshold is defined in the container-azm-ms-agentconfig.yaml file.
timeAggregation: 'Average'
skipMetricValidation: true
}
]
'odata.type': 'Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria'
}
description: 'This alert monitors container CPU usage. It uses the threshold defined in the config map.'
enabled: true
evaluationFrequency: 'PT1M'
scopes: [
mc.id
]
severity: 3
targetResourceType: 'microsoft.containerservice/managedclusters'
windowSize: 'PT5M'
}
dependsOn: [
sci
]
}
resource maHighContainerWorkingSetMemoryUsage 'Microsoft.Insights/metricAlerts@2018-03-01' = {
name: 'Container working set memory usage violates the configured threshold for ${clusterName} CI-20'
location: 'global'
properties: {
autoMitigate: true
actions: []
criteria: {
allOf: [
{
criterionType: 'StaticThresholdCriterion'
dimensions: [
{
name: 'controllerName'
operator: 'Include'
values: [
'*'
]
}
{
name: 'kubernetes namespace'
operator: 'Include'
values: [
'*'
]
}
]
metricName: 'memoryWorkingSetThresholdViolated'
metricNamespace: 'Insights.Container/containers'
name: 'Metric1'
operator: 'GreaterThan'
threshold: 0 // This threshold is defined in the container-azm-ms-agentconfig.yaml file.
timeAggregation: 'Average'
skipMetricValidation: true
}
]
'odata.type': 'Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria'
}
description: 'This alert monitors container working set memory usage. It uses the threshold defined in the config map.'
enabled: true
evaluationFrequency: 'PT1M'
scopes: [
mc.id
]
severity: 3
targetResourceType: 'microsoft.containerservice/managedclusters'
windowSize: 'PT5M'
}
dependsOn: [
sci
]
}
resource maPodsInFailedState 'Microsoft.Insights/metricAlerts@2018-03-01' = {
name: 'Pods in failed state for ${clusterName} CI-4'
location: 'global'
properties: {
autoMitigate: true
actions: []
criteria: {
allOf: [
{
criterionType: 'StaticThresholdCriterion'
dimensions: [
{
name: 'phase'
operator: 'Include'
values: [
'Failed'
]
}
]
metricName: 'podCount'
metricNamespace: 'Insights.Container/pods'
name: 'Metric1'
operator: 'GreaterThan'
threshold: 0
timeAggregation: 'Average'
skipMetricValidation: true
}
]
'odata.type': 'Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria'
}
description: 'Pod status monitoring.'
enabled: true
evaluationFrequency: 'PT1M'
scopes: [
mc.id
]
severity: 3
targetResourceType: 'microsoft.containerservice/managedclusters'
windowSize: 'PT5M'
}
dependsOn: [
sci
]
}
resource maHighDiskUsage 'Microsoft.Insights/metricAlerts@2018-03-01' = {
name: 'Disk usage high for ${clusterName} CI-5'
location: 'global'
properties: {
autoMitigate: true
actions: []
criteria: {
allOf: [
{
criterionType: 'StaticThresholdCriterion'
dimensions: [
{
name: 'host'
operator: 'Include'
values: [
'*'
]
}
{
name: 'device'
operator: 'Include'
values: [
'*'
]
}
]
metricName: 'DiskUsedPercentage'
metricNamespace: 'Insights.Container/nodes'
name: 'Metric1'
operator: 'GreaterThan'
threshold: 80
timeAggregation: 'Average'
skipMetricValidation: true
}
]
'odata.type': 'Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria'
}
description: 'This alert monitors disk usage for all nodes and storage devices.'
enabled: true
evaluationFrequency: 'PT1M'
scopes: [
mc.id
]
severity: 3
targetResourceType: 'microsoft.containerservice/managedclusters'
windowSize: 'PT5M'
}
dependsOn: [
sci
]
}
resource maNodesInNotReadyStatus 'Microsoft.Insights/metricAlerts@2018-03-01' = {
name: 'Nodes in not ready status for ${clusterName} CI-3'
location: 'global'
properties: {
autoMitigate: true
actions: []
criteria: {
allOf: [
{
criterionType: 'StaticThresholdCriterion'
dimensions: [
{
name: 'status'
operator: 'Include'
values: [
'NotReady'
]
}
]
metricName: 'nodesCount'
metricNamespace: 'Insights.Container/nodes'
name: 'Metric1'
operator: 'GreaterThan'
threshold: 0
timeAggregation: 'Average'
skipMetricValidation: true
}
]
'odata.type': 'Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria'
}
description: 'Node status monitoring.'
enabled: true
evaluationFrequency: 'PT1M'
scopes: [
mc.id
]
severity: 3
targetResourceType: 'microsoft.containerservice/managedclusters'
windowSize: 'PT5M'
}
dependsOn: [
sci
]
}
resource maContainersGettingKilledOOM 'Microsoft.Insights/metricAlerts@2018-03-01' = {
name: 'Containers getting OOM killed for ${clusterName} CI-6'
location: 'global'
properties: {
autoMitigate: true
actions: []
criteria: {
allOf: [
{
criterionType: 'StaticThresholdCriterion'
dimensions: [
{
name: 'kubernetes namespace'
operator: 'Include'
values: [
'*'
]
}
{
name: 'controllerName'
operator: 'Include'
values: [
'*'
]
}
]
metricName: 'oomKilledContainerCount'
metricNamespace: 'Insights.Container/pods'
name: 'Metric1'
operator: 'GreaterThan'
threshold: 0
timeAggregation: 'Average'
skipMetricValidation: true
}
]
'odata.type': 'Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria'
}
description: 'This alert monitors number of containers killed due to out of memory (OOM) error.'
enabled: true
evaluationFrequency: 'PT1M'
scopes: [
mc.id
]
severity: 3
targetResourceType: 'microsoft.containerservice/managedclusters'
windowSize: 'PT1M'
}
dependsOn: [
sci
]
}
resource maHighPersistentVolumeUsage 'Microsoft.Insights/metricAlerts@2018-03-01' = {
name: 'Persistent volume usage high for ${clusterName} CI-18'
location: 'global'
properties: {
autoMitigate: true
actions: []
criteria: {
allOf: [
{
criterionType: 'StaticThresholdCriterion'
dimensions: [
{
name: 'podName'
operator: 'Include'
values: [
'*'
]
}
{
name: 'kubernetesNamespace'
operator: 'Include'
values: [
'*'
]
}
]
metricName: 'pvUsageExceededPercentage'
metricNamespace: 'Insights.Container/persistentvolumes'
name: 'Metric1'
operator: 'GreaterThan'
threshold: 80
timeAggregation: 'Average'
skipMetricValidation: true
}
]
'odata.type': 'Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria'
}
description: 'This alert monitors persistent volume utilization.'
enabled: false
evaluationFrequency: 'PT1M'
scopes: [
mc.id
]
severity: 3
targetResourceType: 'microsoft.containerservice/managedclusters'
windowSize: 'PT5M'
}
dependsOn: [
sci
]
}
resource maPodsNotInReadyState 'Microsoft.Insights/metricAlerts@2018-03-01' = {
name: 'Pods not in ready state for ${clusterName} CI-8'
location: 'global'
properties: {
autoMitigate: true
actions: []
criteria: {
allOf: [
{
criterionType: 'StaticThresholdCriterion'
dimensions: [
{
name: 'controllerName'
operator: 'Include'
values: [
'*'
]
}
{
name: 'kubernetes namespace'
operator: 'Include'
values: [
'*'
]
}
]
metricName: 'PodReadyPercentage'
metricNamespace: 'Insights.Container/pods'
name: 'Metric1'
operator: 'LessThan'
threshold: 80
timeAggregation: 'Average'
skipMetricValidation: true
}
]
'odata.type': 'Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria'
}
description: 'This alert monitors for excessive pods not in the ready state.'
enabled: true
evaluationFrequency: 'PT1M'
scopes: [
mc.id
]
severity: 3
targetResourceType: 'microsoft.containerservice/managedclusters'
windowSize: 'PT5M'
}
dependsOn: [
sci
]
}
resource maRestartingContainerCount 'Microsoft.Insights/metricAlerts@2018-03-01' = {
name: 'Restarting container count for ${clusterName} CI-7'
location: 'global'
properties: {
autoMitigate: true
actions: []
criteria: {
allOf: [
{
criterionType: 'StaticThresholdCriterion'
dimensions: [
{
name: 'kubernetes namespace'
operator: 'Include'
values: [
'*'
]
}
{
name: 'controllerName'
operator: 'Include'
values: [
'*'
]
}
]
metricName: 'restartingContainerCount'
metricNamespace: 'Insights.Container/pods'
name: 'Metric1'
operator: 'GreaterThan'
threshold: 0
timeAggregation: 'Average'
skipMetricValidation: true
}
]
'odata.type': 'Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria'
}
description: 'This alert monitors number of containers restarting across the cluster.'
enabled: true
evaluationFrequency: 'PT1M'
scopes: [
mc.id
]
severity: 3
targetResourceType: 'Microsoft.ContainerService/managedClusters'
windowSize: 'PT1M'
}
dependsOn: [
sci
]
}
resource skva 'Microsoft.OperationsManagement/solutions@2015-11-01-preview' = {
name: 'KeyVaultAnalytics(${la.name})'
location: location
properties: {
containedResources: []
referencedResources: []
workspaceResourceId: la.id
}
plan: {
name: 'KeyVaultAnalytics(${la.name})'
product: 'OMSGallery/KeyVaultAnalytics'
promotionCode: ''
publisher: 'Microsoft'
}
}
resource sqrPodFailed 'Microsoft.Insights/scheduledQueryRules@2018-04-16' = {
name: 'PodFailedScheduledQuery'
location: location
properties: {
autoMitigate: true
displayName: '[${clusterName}] Scheduled Query for Pod Failed Alert'
description: 'Alert on pod Failed phase.'
enabled: 'true'
source: {
query: '//https://learn.microsoft.com/azure/azure-monitor/insights/container-insights-alerts \r\n let endDateTime = now(); let startDateTime = ago(1h); let trendBinSize = 1m; let clusterName = "${clusterName}"; KubePodInventory | where TimeGenerated < endDateTime | where TimeGenerated >= startDateTime | where ClusterName == clusterName | distinct ClusterName, TimeGenerated | summarize ClusterSnapshotCount = count() by bin(TimeGenerated, trendBinSize), ClusterName | join hint.strategy=broadcast ( KubePodInventory | where TimeGenerated < endDateTime | where TimeGenerated >= startDateTime | distinct ClusterName, Computer, PodUid, TimeGenerated, PodStatus | summarize TotalCount = count(), PendingCount = sumif(1, PodStatus =~ "Pending"), RunningCount = sumif(1, PodStatus =~ "Running"), SucceededCount = sumif(1, PodStatus =~ "Succeeded"), FailedCount = sumif(1, PodStatus =~ "Failed") by ClusterName, bin(TimeGenerated, trendBinSize) ) on ClusterName, TimeGenerated | extend UnknownCount = TotalCount - PendingCount - RunningCount - SucceededCount - FailedCount | project TimeGenerated, TotalCount = todouble(TotalCount) / ClusterSnapshotCount, PendingCount = todouble(PendingCount) / ClusterSnapshotCount, RunningCount = todouble(RunningCount) / ClusterSnapshotCount, SucceededCount = todouble(SucceededCount) / ClusterSnapshotCount, FailedCount = todouble(FailedCount) / ClusterSnapshotCount, UnknownCount = todouble(UnknownCount) / ClusterSnapshotCount| summarize AggregatedValue = avg(FailedCount) by bin(TimeGenerated, trendBinSize)'
dataSourceId: la.id
queryType: 'ResultCount'
}
schedule: {
frequencyInMinutes: 5
timeWindowInMinutes: 10
}
action: {
'odata.type': 'Microsoft.WindowsAzure.Management.Monitoring.Alerts.Models.Microsoft.AppInsights.Nexus.DataContracts.Resources.ScheduledQueryRules.AlertingAction'
severity: '3'
trigger: {
thresholdOperator: 'GreaterThan'
threshold: 3
metricTrigger: {
thresholdOperator: 'GreaterThan'
threshold: 2
metricTriggerType: 'Consecutive'
}
}
}
}
}
// Resource Group Azure Policy Assignments - Azure Policy for Kubernetes Policies
// Applying the built-in 'Kubernetes cluster pod security restricted standards for Linux-based workloads' initiative at the resource group level.
// Constraint Names: K8sAzureAllowedSeccomp, K8sAzureAllowedCapabilities, K8sAzureContainerNoPrivilege, K8sAzureHostNetworkingPorts, K8sAzureVolumeTypes, K8sAzureBlockHostNamespaceV2, K8sAzureAllowedUsersGroups, K8sAzureContainerNoPrivilegeEscalation
resource paAKSLinuxRestrictive 'Microsoft.Authorization/policyAssignments@2021-06-01' = {
name: guid(psdAKSLinuxRestrictiveId, resourceGroup().id, clusterName)
location: 'global'
scope: resourceGroup()
properties: {
displayName: take('[${clusterName}] ${reference(psdAKSLinuxRestrictiveId, '2021-06-01').displayName}', 120)
description: reference(psdAKSLinuxRestrictiveId, '2021-06-01').description
policyDefinitionId: psdAKSLinuxRestrictiveId
parameters: {
excludedNamespaces: {
value: [
'kube-system'
'gatekeeper-system'
'azure-arc'
'flux-system'
// Known violations
// K8sAzureAllowedSeccomp
// - Kured, no profile defined
// K8sAzureContainerNoPrivilege
// - Kured, requires privileged to perform reboot
// K8sAzureBlockHostNamespaceV2
// - Kured, shared host namespace
// K8sAzureAllowedUsersGroups
// - Kured, no runAsNonRoot, no runAsGroup, no supplementalGroups, no fsGroup
'cluster-baseline-settings'
// Known violations
// K8sAzureAllowedSeccomp
// - Traefik, no profile defined
// - aspnetapp-deployment, no profile defined
// K8sAzureVolumeTypes
// - Traefik, uses csi
// K8sAzureAllowedUsersGroups
// - Traefik, no supplementalGroups, no fsGroup
// = aspnetapp-deployment, no supplementalGroups, no fsGroup
'a0008'