# In-cluster Prometheus for Lab 7 bonus (AnalysisTemplate). # Why you need this: the docker-compose Prometheus from Lab 3 runs on the host # and can't reach pod IPs inside the k3d bridge network. Argo Rollouts' # AnalysisTemplate queries Prometheus from *inside* the cluster, so Prometheus # must live there too. # # Apply with: kubectl apply -f labs/lab7/prometheus.yaml # Verify: kubectl get pods -n monitoring # Query UI: kubectl port-forward -n monitoring svc/prometheus 9091:9090 apiVersion: v1 kind: Namespace metadata: name: monitoring --- apiVersion: v1 kind: ServiceAccount metadata: name: prometheus namespace: monitoring --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: prometheus rules: - apiGroups: [""] resources: [pods, services, endpoints] verbs: [get, list, watch] --- apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: prometheus roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: prometheus subjects: - kind: ServiceAccount name: prometheus namespace: monitoring --- apiVersion: v1 kind: ConfigMap metadata: name: prometheus-config namespace: monitoring data: prometheus.yml: | global: scrape_interval: 5s evaluation_interval: 5s scrape_configs: - job_name: gateway kubernetes_sd_configs: - role: pod namespaces: names: [default] relabel_configs: # Only scrape pods labeled app=gateway. - source_labels: [__meta_kubernetes_pod_label_app] regex: gateway action: keep # Use the pod IP:8080 directly (bypass the Service — we want per-pod series). - source_labels: [__meta_kubernetes_pod_ip] regex: (.+) target_label: __address__ replacement: ${1}:8080 - source_labels: [__meta_kubernetes_pod_name] target_label: pod # CRITICAL for Argo Rollouts canary analysis: copy the # rollouts-pod-template-hash label (set automatically by Argo Rollouts) # into rs_hash so the AnalysisTemplate query can filter canary vs stable. - source_labels: [__meta_kubernetes_pod_label_rollouts_pod_template_hash] target_label: rs_hash --- apiVersion: apps/v1 kind: Deployment metadata: name: prometheus namespace: monitoring spec: replicas: 1 selector: matchLabels: app: prometheus template: metadata: labels: app: prometheus spec: serviceAccountName: prometheus containers: - name: prometheus image: prom/prometheus:v3.11.2 args: - --config.file=/etc/prometheus/prometheus.yml - --storage.tsdb.path=/prometheus - --web.listen-address=:9090 ports: - containerPort: 9090 volumeMounts: - name: config mountPath: /etc/prometheus volumes: - name: config configMap: name: prometheus-config --- apiVersion: v1 kind: Service metadata: name: prometheus namespace: monitoring spec: selector: app: prometheus ports: - port: 9090 targetPort: 9090