From 4a930b284303979fd47b7ee6ac18647e2c2b06c6 Mon Sep 17 00:00:00 2001 From: Brian On Date: Sun, 2 Jan 2022 22:35:06 -0800 Subject: [PATCH] sample hpa --- deploy/Makefile | 2 +- deploy/addepar/custom-metrics-namespace.json | 10 --- deploy/addepar/prometheus/Makefile | 5 ++ deploy/addepar/prometheus/prometheus.yaml | 63 +++++++++++++++++++ deploy/addepar/sample-hpa/Makefile | 25 ++++++++ .../addepar/sample-hpa/sample-app.deploy.yaml | 22 +++++++ deploy/addepar/sample-hpa/sample-app.hpa.yaml | 24 +++++++ .../sample-hpa/sample-app.service.yaml | 15 +++++ .../addepar/sample-hpa/service-monitor.yaml | 12 ++++ 9 files changed, 167 insertions(+), 11 deletions(-) delete mode 100644 deploy/addepar/custom-metrics-namespace.json create mode 100644 deploy/addepar/prometheus/Makefile create mode 100644 deploy/addepar/prometheus/prometheus.yaml create mode 100644 deploy/addepar/sample-hpa/Makefile create mode 100644 deploy/addepar/sample-hpa/sample-app.deploy.yaml create mode 100644 deploy/addepar/sample-hpa/sample-app.hpa.yaml create mode 100644 deploy/addepar/sample-hpa/sample-app.service.yaml create mode 100644 deploy/addepar/sample-hpa/service-monitor.yaml diff --git a/deploy/Makefile b/deploy/Makefile index e249f819..e1573d12 100644 --- a/deploy/Makefile +++ b/deploy/Makefile @@ -7,7 +7,7 @@ delete-adapter-local: kubectl delete -f manifests/ namespace: - kubectl create -f addepar/custom-metrics-namespace.json + kubectl create namespace custom-metrics raw: kubectl get --raw /apis/custom.metrics.k8s.io/v1beta1 diff --git a/deploy/addepar/custom-metrics-namespace.json b/deploy/addepar/custom-metrics-namespace.json deleted file mode 100644 index 472b9f11..00000000 --- a/deploy/addepar/custom-metrics-namespace.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "apiVersion": "v1", - "kind": "Namespace", - "metadata": { - "name": "custom-metrics", - "labels": { - "name": "custom-metrics" - } - } -} diff --git a/deploy/addepar/prometheus/Makefile b/deploy/addepar/prometheus/Makefile new file mode 100644 index 00000000..2f772ede --- /dev/null +++ b/deploy/addepar/prometheus/Makefile @@ -0,0 +1,5 @@ +deploy: + kubectl create -n prom -f prometheus.yaml + +namespace: + kubectl create namespace prom diff --git a/deploy/addepar/prometheus/prometheus.yaml b/deploy/addepar/prometheus/prometheus.yaml new file mode 100644 index 00000000..f9df65f1 --- /dev/null +++ b/deploy/addepar/prometheus/prometheus.yaml @@ -0,0 +1,63 @@ +apiVersion: v1 +kind: ServiceAccount +metadata: + name: prometheus +--- +apiVersion: rbac.authorization.k8s.io/v1beta1 +kind: ClusterRole +metadata: + name: prometheus +rules: + - apiGroups: [""] + resources: + - nodes + - services + - endpoints + - pods + verbs: ["get", "list", "watch"] + - apiGroups: [""] + resources: + - configmaps + verbs: ["get"] + - nonResourceURLs: ["/metrics"] + verbs: ["get"] +--- +apiVersion: rbac.authorization.k8s.io/v1beta1 +kind: ClusterRoleBinding +metadata: + name: prometheus +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: ClusterRole + name: prometheus +subjects: + - kind: ServiceAccount + name: prometheus + namespace: prom +--- +apiVersion: monitoring.coreos.com/v1 +kind: Prometheus +metadata: + name: prometheus +spec: + # Match all service monitors in all namespaces + serviceMonitorNamespaceSelector: {} + serviceMonitorSelector: {} + resources: + requests: + memory: 400Mi +--- +apiVersion: v1 +kind: Service +metadata: + name: prometheus +spec: + type: NodePort + ports: + - name: web + nodePort: 30900 + port: 9090 + protocol: TCP + targetPort: web + selector: + prometheus: prometheus diff --git a/deploy/addepar/sample-hpa/Makefile b/deploy/addepar/sample-hpa/Makefile new file mode 100644 index 00000000..995c5070 --- /dev/null +++ b/deploy/addepar/sample-hpa/Makefile @@ -0,0 +1,25 @@ +sample-app: + kubectl create -n default -f sample-app.deploy.yaml + kubectl create -n default -f sample-app.service.yaml + +delete-sample-app: + kubectl delete -n default -f sample-app.deploy.yaml + kubectl delete -n default -f sample-app.service.yaml + +monitor: + kubectl create -n default -f service-monitor.yaml + +hpa: + kubectl delete -f sample-app.hpa.yaml + kubectl create -f sample-app.hpa.yaml + +test: + kubectl get --raw "/apis/custom.metrics.k8s.io/v1beta1/namespaces/default/pods/*/http_requests?selector=app%3Dsample-app" + +get-ip: + kubectl get service sample-app -o jsonpath='{ .spec.clusterIP }' + +busybox: + # run wget -qO- IP_ADDRESS + kubectl run -it --rm --restart=Never busybox --image=gcr.io/google-containers/busybox sh + diff --git a/deploy/addepar/sample-hpa/sample-app.deploy.yaml b/deploy/addepar/sample-hpa/sample-app.deploy.yaml new file mode 100644 index 00000000..3a0ad13e --- /dev/null +++ b/deploy/addepar/sample-hpa/sample-app.deploy.yaml @@ -0,0 +1,22 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: sample-app + labels: + app: sample-app +spec: + replicas: 1 + selector: + matchLabels: + app: sample-app + template: + metadata: + labels: + app: sample-app + spec: + containers: + - image: luxas/autoscale-demo:v0.1.2 + name: metrics-provider + ports: + - name: http + containerPort: 8080 diff --git a/deploy/addepar/sample-hpa/sample-app.hpa.yaml b/deploy/addepar/sample-hpa/sample-app.hpa.yaml new file mode 100644 index 00000000..674ffbde --- /dev/null +++ b/deploy/addepar/sample-hpa/sample-app.hpa.yaml @@ -0,0 +1,24 @@ +kind: HorizontalPodAutoscaler +apiVersion: autoscaling/v2beta1 +metadata: + name: sample-app +spec: + scaleTargetRef: + # point the HPA at the sample application + # you created above + apiVersion: apps/v1 + kind: Deployment + name: sample-app + # autoscale between 1 and 10 replicas + minReplicas: 1 + maxReplicas: 10 + metrics: + # use a "Pods" metric, which takes the average of the + # given metric across all pods controlled by the autoscaling target + - type: Pods + pods: + # use the metric that you used above: pods/http_requests + metricName: http_requests + # target 500 milli-requests per second, + # which is 1 request every two seconds + targetAverageValue: 20m diff --git a/deploy/addepar/sample-hpa/sample-app.service.yaml b/deploy/addepar/sample-hpa/sample-app.service.yaml new file mode 100644 index 00000000..04710a8c --- /dev/null +++ b/deploy/addepar/sample-hpa/sample-app.service.yaml @@ -0,0 +1,15 @@ +apiVersion: v1 +kind: Service +metadata: + labels: + app: sample-app + name: sample-app +spec: + ports: + - name: http + port: 80 + protocol: TCP + targetPort: 8080 + selector: + app: sample-app + type: ClusterIP diff --git a/deploy/addepar/sample-hpa/service-monitor.yaml b/deploy/addepar/sample-hpa/service-monitor.yaml new file mode 100644 index 00000000..2b4b4efe --- /dev/null +++ b/deploy/addepar/sample-hpa/service-monitor.yaml @@ -0,0 +1,12 @@ +kind: ServiceMonitor +apiVersion: monitoring.coreos.com/v1 +metadata: + name: sample-app + labels: + app: sample-app +spec: + selector: + matchLabels: + app: sample-app + endpoints: + - port: http