Setting the file. One moment.
AKS Baseline · Azure Diagnostics · microsoft/azure-skills · Skills Docs
ContentsBack to the top of the page ·
scripts/aks-baseline.ps1
scripts/ aks-baseline.ps1
PowerShell · 164 lines · 7 KB
14 8. Namespace pod overview (optional) (kubectl get pods -n <namespace>)
15
16 All steps are READ-ONLY. Each step is guarded so a single failure (for example,
17 kubectl not authenticated) prints a note and the sweep continues.
18 . PARAMETER ResourceGroup
19 Resource group containing the AKS cluster.
20 . PARAMETER Cluster
21 AKS cluster name.
22 . PARAMETER Namespace
23 Optional namespace for an extra pod overview.
24 . PARAMETER Subscription
25 Optional Azure subscription id. Defaults to the current subscription.
26 . EXAMPLE
27 .\aks-baseline.ps1 -ResourceGroup my-rg -Cluster my-cluster
28 . EXAMPLE
29 .\aks-baseline.ps1 -ResourceGroup my-rg -Cluster my-cluster -Namespace payments
30 #>
31 param (
32 [ Alias ( "g" )][ string ]$ResourceGroup ,
33 [ Alias ( "n" )][ string ]$Cluster ,
34 [ string ]$Namespace ,
35 [ string ]$Subscription
36 )
37
38 function Show-Usage ($exitCode) {
39 Write-Host "Usage: .\aks-baseline.ps1 -ResourceGroup <resource-group> -Cluster <cluster> [-Namespace <ns>] [-Subscription <id>]"
40 exit $exitCode
41 }
42
43 if ( -not $ResourceGroup) {
44 Write-Error "Missing required -ResourceGroup. Provide the resource group containing the AKS cluster."
45 Show-Usage 1
46 }
47 if ( -not $Cluster) {
48 Write-Error "Missing required -Cluster. Provide the AKS cluster name."
49 Show-Usage 1
50 }
51
52 $azSubArgs = @ ()
53 if ($Subscription) { $azSubArgs = @ ( "--subscription" , $Subscription) }
54
55 function Write-Section ($title) {
56 Write-Host ""
57 Write-Host "=============================================================="
58 Write-Host "== $title "
59 Write-Host "=============================================================="
60 }
61
62 function Invoke-Step ($description , [ scriptblock ]$action) {
63 try {
64 & $action
65 if ( $LASTEXITCODE -ne 0 ) {
66 Write-Host " [!] Could not gather: $description (command failed or unavailable)"
67 }
68 } catch {
69 Write-Host " [!] Could not gather: $description ( $( $_ .Exception.Message ) )"
70 }
71 }
72
73 Write-Host "AKS baseline diagnostic sweep (read-only)"
74 Write-Host "Resource group: $ResourceGroup "
75 Write-Host "Cluster: $Cluster "
76 if ($Namespace) { Write-Host "Namespace: $Namespace " }
77
78 # 1. Cluster provisioning state ------------------------------------------------
79 Write-Section "1. Cluster provisioning state"
80 Invoke-Step "cluster provisioning state" {
81 az aks show - g $ResourceGroup - n $Cluster @azSubArgs `
82 -- query "{name:name, provisioningState:provisioningState, powerState:powerState.code, k8sVersion:currentKubernetesVersion, fqdn:fqdn}" `
83 - o table
84 }
85
86 # 2. Node pool summary ---------------------------------------------------------
87 Write-Section "2. Node pool summary"
88 Invoke-Step "node pool summary" {
89 az aks nodepool list - g $ResourceGroup -- cluster - name $Cluster @azSubArgs `
90 -- query "[].{name:name, mode:mode, count:count, vmSize:vmSize, state:provisioningState, powerState:powerState.code, k8sVersion:orchestratorVersion}" `
91 - o table
92 }
93
94 # 3. Recent Azure activity -----------------------------------------------------
95 Write-Section "3. Recent Azure activity (last 20 events)"
96 Invoke-Step "recent activity log" {
97 az monitor activity - log list - g $ResourceGroup @azSubArgs `
98 -- max - events 20 `
99 -- query "[].{time:eventTimestamp, operation:operationName.value, status:status.value, resource:resourceId}" `
100 - o table
101 }
102
103 # 4. Node readiness ------------------------------------------------------------
104 Write-Section "4. Node readiness"
105 Invoke-Step "node readiness" { kubectl get nodes - o wide }
106
107 # 5. Unhealthy pods ------------------------------------------------------------
108 # Filter on the READY and STATUS columns (not just pod phase) so container-level
109 # failures such as CrashLoopBackOff / ImagePullBackOff — which stay in phase
110 # "Running" — are caught. Terminal pods (Completed/Succeeded) are excluded so
111 # finished jobs are not falsely flagged.
112 Write-Section "5. Unhealthy pods (CrashLoopBackOff, not Ready, restarting, or bad status)"
113 $allPods = @ (kubectl get pods - A - o wide 2> $null )
114 if ( -not $allPods) {
115 Write-Host " No pods reported (or cluster unreachable)."
116 } else {
117 $header = $allPods[ 0 ]
118 $unhealthy = foreach ($line in ($allPods | Select-Object - Skip 1 )) {
119 $cols = $line -split '\s+'
120 if ($cols.Count -lt 5 ) { continue }
121 $readyParts = $cols[ 2 ] -split '/'
122 $status = $cols[ 3 ]
123 $restarts = 0 ; [ void ][ int ]::TryParse($cols[ 4 ] , [ ref ]$restarts)
124 $terminalOk = ($status -eq 'Completed' -or $status -eq 'Succeeded' )
125 $notReady = ($status -eq 'Running' -and $readyParts.Count -eq 2 -and $readyParts[ 0 ] -ne $readyParts[ 1 ])
126 $badStatus = ($status -ne 'Running' -and -not $terminalOk)
127 $highRestarts = (( -not $terminalOk) -and $restarts -ge 5 )
128 if ($notReady -or $badStatus -or $highRestarts) { $line }
129 }
130 if ($unhealthy) {
131 $header
132 $unhealthy
133 } else {
134 Write-Host " All pods are Running/Succeeded and Ready with low restart counts."
135 }
136 }
137
138 # 6. kube-system health --------------------------------------------------------
139 Write-Section "6. kube-system health"
140 Invoke-Step "kube-system pods" { kubectl get pods - n kube - system - o wide }
141
142 # 7. Recent warning events -----------------------------------------------------
143 Write-Section "7. Recent warning events (last 40, sorted by time)"
144 Invoke-Step "warning events" {
145 $events = kubectl get events - A -- field - selector = "type=Warning" -- sort - by = .lastTimestamp 2> $null
146 if ($events) { $events | Select-Object - Last 40 }
147 }
148
149 # 8. Namespace pod overview (optional) ----------------------------------------
150 if ($Namespace) {
151 Write-Section "8. Pods in namespace ' $Namespace '"
152 Invoke-Step "pods in namespace $Namespace " { kubectl get pods - n $Namespace - o wide }
153 }
154
155 # Summary ----------------------------------------------------------------------
156 Write-Section "Summary"
157 Write-Host "Gathered the read-only AKS baseline for cluster ' $Cluster ' in resource group"
158 Write-Host "' $ResourceGroup ': Azure-side cluster/node-pool state and recent activity, then"
159 Write-Host "Kubernetes-side node readiness, unhealthy pods, kube-system health, and recent"
160 Write-Host "warning events. Review the sections above for anomalies (non-Succeeded"
161 Write-Host "provisioning state, NotReady nodes, unhealthy or restarting pods, warning events)"
162 Write-Host "before deep-diving with 'kubectl describe' / 'kubectl logs' on a specific pod."
163 Write-Host ""
164 Write-Host "No changes were made to any resource."