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
| kubectl create ns app-metrics
cat <<EOF | kubectl apply -f - apiVersion: apps/v1 kind: Deployment metadata: name: metrics-app namespace: app-metrics labels: app: metrics-app spec: replicas: 2 selector: matchLabels: app: metrics-app template: metadata: labels: app: metrics-app annotations: prometheus.io/scrape: "true" prometheus.io/port: "9113" spec: containers: - name: nginx image: nginx:alpine ports: - containerPort: 80 - name: exporter image: nginx/nginx-prometheus-exporter:0.11 args: - -nginx.scrape-uri=http://localhost/nginx_status ports: - containerPort: 9113 name: metrics EOF
kubectl expose deploy metrics-app -n app-metrics --port=9113 --name=metrics-app-svc
cat <<EOF | kubectl apply -f - apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: name: metrics-app-monitor namespace: monitoring spec: selector: matchLabels: app: metrics-app namespaceSelector: matchNames: - app-metrics endpoints: - port: metrics interval: 30s EOF
kubectl port-forward -n monitoring svc/monitoring-prometheus 9090:9090 &
|