Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
378 changes: 378 additions & 0 deletions labs/onprem-lab/kubernetes-arc/README.md

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions labs/onprem-lab/kubernetes-arc/ama-metrics-settings-configmap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: ama-metrics-settings-configmap
namespace: kube-system
data:
config: |-
[prometheus-collector-settings]
[prometheus-collector-settings.cluster-metrics]
[prometheus-collector-settings.cluster-metrics.pod-annotation-based-scraping]
enabled = true
pod-annotation-based-scraping-interval = "30s"
pod-annotation-based-scraping-namespace-regex = "sre-demo"
30 changes: 30 additions & 0 deletions labs/onprem-lab/kubernetes-arc/load-generator.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: load-generator
namespace: sre-demo
spec:
replicas: 1
selector:
matchLabels:
app: load-generator
template:
metadata:
labels:
app: load-generator
spec:
containers:
- name: load-generator
image: curlimages/curl
command: ["/bin/sh", "-c"]
args:
- |
while true; do
TIMESTAMP=$(date -Iseconds)
RESULT=$(curl -s \
-o /dev/null \
-w "%{http_code}" \
http://sample-app/api/data)
echo "$TIMESTAMP status=$RESULT"
sleep 2
done
16 changes: 16 additions & 0 deletions labs/onprem-lab/kubernetes-arc/sample-app-servicemonitor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: azmonitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
name: sample-app
namespace: sre-demo
spec:
labelLimit: 63
labelNameLengthLimit: 511
labelValueLengthLimit: 1023
selector:
matchLabels:
app: sample-app
endpoints:
- port: http
path: /metrics
interval: 30s
122 changes: 122 additions & 0 deletions labs/onprem-lab/kubernetes-arc/sample-app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: sample-app-code
namespace: sre-demo
data:
app.py: |
from flask import Flask, Response, jsonify
import os
import time

app = Flask(__name__)

request_count = 0
error_count = 0

@app.route("/")
def root():
return "SRE Agent on-prem Kubernetes demo\n"

@app.route("/health")
def health():
return jsonify(status="healthy")

@app.route("/api/data")
def data():
global request_count, error_count
request_count += 1

if os.path.exists("/fault/fail"):
error_count += 1
return jsonify(
status="error",
message="Simulated application failure"
), 500

return jsonify(
status="ok",
timestamp=time.time()
)

@app.route("/metrics")
def metrics():
body = f"""# HELP demo_requests_total Total requests
# TYPE demo_requests_total counter
demo_requests_total {request_count}

# HELP demo_errors_total Total errors
# TYPE demo_errors_total counter
demo_errors_total {error_count}
"""
return Response(body, mimetype="text/plain")

app.run(host="0.0.0.0", port=8080)
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: sample-app
namespace: sre-demo
spec:
replicas: 1
selector:
matchLabels:
app: sample-app
template:
metadata:
labels:
app: sample-app
annotations:
prometheus.io/scrape: "true"
prometheus.io/path: "/metrics"
prometheus.io/port: "8080"
spec:
containers:
- name: sample-app
image: python:3.12-slim
command: ["/bin/sh", "-c"]
args:
- |
pip install --no-cache-dir flask &&
python /app/app.py
ports:
- containerPort: 8080
volumeMounts:
- name: app-code
mountPath: /app
- name: fault
mountPath: /fault
readinessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 5
periodSeconds: 5
livenessProbe:
httpGet:
path: /health
port: 8080
initialDelaySeconds: 10
periodSeconds: 10
volumes:
- name: app-code
configMap:
name: sample-app-code
- name: fault
emptyDir: {}
---
apiVersion: v1
kind: Service
metadata:
name: sample-app
namespace: sre-demo
labels:
app: sample-app
spec:
selector:
app: sample-app
ports:
- name: http
port: 80
targetPort: 8080
96 changes: 96 additions & 0 deletions labs/onprem-lab/kubernetes-arc/scripts/configure-azure.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string]$SubscriptionId,

[Parameter(Mandatory = $true)]
[string]$ResourceGroupName,

[Parameter(Mandatory = $true)]
[string]$Region,

[string]$ClusterName = $env:COMPUTERNAME.ToLowerInvariant(),

[string]$WorkspaceName
)

$ErrorActionPreference = 'Stop'

if (-not $WorkspaceName) {
$WorkspaceName = "$ClusterName-amw"
}

if (-not (Get-Command az -ErrorAction SilentlyContinue)) {
throw 'Azure CLI (az) is required.'
}
if (-not (Get-Command kubectl -ErrorAction SilentlyContinue)) {
throw 'kubectl is required.'
}

az account show 1>$null 2>$null
if ($LASTEXITCODE -ne 0) {
az login
if ($LASTEXITCODE -ne 0) { throw 'az login failed.' }
}

az account set --subscription $SubscriptionId
if ($LASTEXITCODE -ne 0) { throw 'Unable to select the subscription.' }

$providers = @(
'Microsoft.Kubernetes',
'Microsoft.KubernetesConfiguration',
'Microsoft.ExtendedLocation',
'Microsoft.Insights',
'Microsoft.Monitor',
'Microsoft.AlertsManagement',
'Microsoft.OperationalInsights',
'Microsoft.Dashboard'
)

foreach ($provider in $providers) {
Write-Host "Registering $provider..."
az provider register --namespace $provider --wait
if ($LASTEXITCODE -ne 0) { throw "Failed to register $provider." }
}

az extension add --name connectedk8s --upgrade --yes
if ($LASTEXITCODE -ne 0) { throw 'Unable to install connectedk8s extension.' }
az extension add --name k8s-extension --upgrade --yes
if ($LASTEXITCODE -ne 0) { throw 'Unable to install k8s-extension extension.' }

az group create --name $ResourceGroupName --location $Region --output none
if ($LASTEXITCODE -ne 0) { throw 'Unable to create or update the resource group.' }

az connectedk8s show --name $ClusterName --resource-group $ResourceGroupName 1>$null 2>$null
if ($LASTEXITCODE -eq 0) {
Write-Host "Arc-enabled cluster $ClusterName already exists."
} else {
az connectedk8s connect --name $ClusterName --resource-group $ResourceGroupName --location $Region --yes
if ($LASTEXITCODE -ne 0) { throw 'Unable to connect the cluster to Azure Arc.' }
}

az monitor account show --name $WorkspaceName --resource-group $ResourceGroupName 1>$null 2>$null
if ($LASTEXITCODE -eq 0) {
Write-Host "Azure Monitor workspace $WorkspaceName already exists."
} else {
az monitor account create --name $WorkspaceName --resource-group $ResourceGroupName --location $Region --output none
if ($LASTEXITCODE -ne 0) { throw 'Unable to create the Azure Monitor workspace.' }
}

$workspaceId = az monitor account show --name $WorkspaceName --resource-group $ResourceGroupName --query id -o tsv
if ($LASTEXITCODE -ne 0 -or -not $workspaceId) { throw 'Unable to retrieve the Azure Monitor workspace ID.' }

az k8s-extension show --name azuremonitor-metrics --cluster-type connectedClusters --cluster-name $ClusterName --resource-group $ResourceGroupName 1>$null 2>$null
if ($LASTEXITCODE -eq 0) {
az k8s-extension update --name azuremonitor-metrics --cluster-type connectedClusters --cluster-name $ClusterName --resource-group $ResourceGroupName --configuration-settings "azure-monitor-workspace-resource-id=$workspaceId" --output none
} else {
az k8s-extension create --name azuremonitor-metrics --extension-type Microsoft.AzureMonitor.Containers.Metrics --cluster-type connectedClusters --cluster-name $ClusterName --resource-group $ResourceGroupName --configuration-settings "azure-monitor-workspace-resource-id=$workspaceId" --release-train stable --auto-upgrade-minor-version true --output none
}
if ($LASTEXITCODE -ne 0) { throw 'Unable to configure the Azure Monitor metrics extension.' }

Write-Host ''
Write-Host 'Azure Arc and Managed Prometheus configuration completed.'
Write-Host "Cluster: $ClusterName"
Write-Host "Workspace: $WorkspaceName"
az connectedk8s show --name $ClusterName --resource-group $ResourceGroupName --query connectivityStatus -o tsv
az k8s-extension show --name azuremonitor-metrics --cluster-type connectedClusters --cluster-name $ClusterName --resource-group $ResourceGroupName --query provisioningState -o tsv
Loading
Loading