Skip to content

Commit

Permalink
Added CORS headers to cmd/costmodel/main.go.
Browse files Browse the repository at this point in the history
This uses the rs/cors package, which has been added to go.mod and
go.sum.

Updated the kubecost-exporter.md doc to explain that the prometheus URI
must be manually supplied.

Added the CLUSTER_ID env var to exporter.yaml.
  • Loading branch information
nealormsbee committed Apr 26, 2021
1 parent 5e37d2f commit 4dfe2cf
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
4 changes: 3 additions & 1 deletion cmd/costmodel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/kubecost/cost-model/pkg/costmodel"
"github.com/kubecost/cost-model/pkg/errors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rs/cors"
"k8s.io/klog"
)

Expand All @@ -23,5 +24,6 @@ func main() {
a.Router.GET("/healthz", Healthz)
rootMux.Handle("/", a.Router)
rootMux.Handle("/metrics", promhttp.Handler())
klog.Fatal(http.ListenAndServe(":9003", errors.PanicHandlerMiddleware(rootMux)))
handler := cors.AllowAll().Handler(rootMux)
klog.Fatal(http.ListenAndServe(":9003", errors.PanicHandlerMiddleware(handler)))
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/prometheus/client_golang v1.0.0
github.com/prometheus/client_model v0.2.0
github.com/rs/cors v1.7.0 // indirect
github.com/satori/go.uuid v1.2.0 // indirect
github.com/shopspring/decimal v0.0.0-20180709203117-cd690d0c9e24 // indirect
go.etcd.io/bbolt v1.3.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,8 @@ github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R
github.com/prometheus/procfs v0.0.2 h1:6LJUbpNm42llc4HRCuvApCSWB/WfhuNo9K98Q9sNGfs=
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rs/cors v1.7.0 h1:+88SsELBHx5r+hZ8TCkggzSstaWNbDvThkVK8H6f9ik=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
Expand Down
35 changes: 26 additions & 9 deletions kubecost-exporter.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Running Kubecost as a Prometheus metric exporter

Running Kubecost as a Prometheus metric exporter allows you to export various cost metrics to Prometheus without setting up any other Kubecost dependencies. Doing so lets you write PromQL queries to calculate the cost and efficiency of any Kubernetes concept, e.g. namespace, service, label, deployment, etc. You can also calculate the cost of different Kubernetes resources, e.g. nodes, PVs, LoadBalancers, and more. Finally, you can do other interesting things like create custom alerts via AlertManager and custom dashboards via Grafana.
Running Kubecost as a Prometheus metric exporter allows you to export various cost metrics to Prometheus without setting up any other Kubecost dependencies. Doing so lets you write PromQL queries to calculate the cost and efficiency of any Kubernetes concept, e.g. namespace, service, label, deployment, etc. You can also calculate the cost of different Kubernetes resources, e.g. nodes, PVs, LoadBalancers, and more. Finally, you can do other interesting things like create custom alerts via AlertManager and custom dashboards via Grafana.

## Installing

Expand All @@ -11,9 +11,26 @@ If you would prefer to not use the recommended install option and just deploy th

1. Apply the combined YAML:

```
kubectl apply -f https://raw.githubusercontent.com/kubecost/cost-model/develop/kubernetes/exporter/exporter.yaml --namespace cost-model
```
1.a.

```
wget https://raw.githubusercontent.com/kubecost/cost-model/develop/kubernetes/exporter/exporter.yaml
```

1.b.
On the line

```
value: "{{prometheusEndpoint}}" # The endpoint should have the form http://<service-name>.<namespace-name>.svc
```

of `exporter.yaml`, substitute your own Prometheus URI for `{{prometheusEndpoint}}`

1.c.

```
kubectl apply -f exporter.yaml --namespace cost-model
```

> If you want to use a namespace other than `cost-model`, you will have to edit the `ClusterRoleBinding` after applying the YAML to change `subjects[0].namespace`. You can do this with `kubectl edit clusterrolebinding cost-model`.
Expand All @@ -38,7 +55,7 @@ Add Kubecost scrape config to Prom ([more info](https://prometheus.io/docs/intro

Done! Kubecost is now exporting cost metrics. See the following sections for different metrics available and query examples.

## Available Prometheus Metrics
## Available Prometheus Metrics

| Metric | Description |
| ------------ | ------------------------------------------------------------------------------------------------------ |
Expand All @@ -51,13 +68,13 @@ Done! Kubecost is now exporting cost metrics. See the following sections for dif
| container_cpu_allocation | Average number of CPUs requested over last 1m |
| container_memory_allocation_bytes | Average bytes of RAM requested over last 1m |

By default, all cost metrics are based on public billing APIs. See the Limitations section below about reflecting your precise billing information. Supported platforms are AWS, Azure, and GCP. For on-prem clusters, prices are based on configurable defaults.
By default, all cost metrics are based on public billing APIs. See the Limitations section below about reflecting your precise billing information. Supported platforms are AWS, Azure, and GCP. For on-prem clusters, prices are based on configurable defaults.

More metrics are available in the recommended install path and are described in [PROMETHEUS.md](PROMETHEUS.md).

## Dashboard examples

Here’s an example dashboard using Kubecost Prometheus metrics:
Here’s an example dashboard using Kubecost Prometheus metrics:

![sample dashboard](https://grafana.com/api/dashboards/8670/images/5480/image)

Expand Down Expand Up @@ -98,7 +115,7 @@ avg(avg_over_time(node_ram_hourly_cost[1d] )) by (node)

## Setting Cost Alerts

Custom cost alerts can be implemented with a set of Prometheus queries and can be used for alerting with AlertManager or Grafana alerts. Below are example alerting rules.
Custom cost alerts can be implemented with a set of Prometheus queries and can be used for alerting with AlertManager or Grafana alerts. Below are example alerting rules.

#### Determine in real-time if the monthly cost of all nodes is > $1000

Expand All @@ -114,4 +131,4 @@ Running Kubecost in exporter-only mode by definition limits functionality. The f
- For large clusters, these Prometheus queries might not scale well over large time windows. We recommend using [Kubecost APIs](https://github.com/kubecost/docs/blob/master/apis.md) for these scenarios.
- Allocation metrics, like `container_cpu_allocation` only contain _requests_ and do not take usage into account.
- Related to the previous point, efficiency metrics are not available.
- Public billing costs on default. The standard Kubecost install and a cloud integration gives you accurate pricing based on your bill.
- Public billing costs on default. The standard Kubecost install and a cloud integration gives you accurate pricing based on your bill.
4 changes: 3 additions & 1 deletion kubernetes/exporter/exporter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,11 @@ spec:
memory: "55M"
env:
- name: PROMETHEUS_SERVER_ENDPOINT
value: "{{prometheusEndpoint}}" #The endpoint should have the form http://<service-name>.<namespace-name>.svc
value: "{{prometheusEndpoint}}" # The endpoint should have the form http://<service-name>.<namespace-name>.svc
- name: CLOUD_PROVIDER_API_KEY
value: "AIzaSyD29bGxmHAVEOBYtgd8sYM2gM2ekfxQX4U" # The GCP Pricing API requires a key. This is supplied just for evaluation.
- name: CLUSTER_ID
value: "cluster-one" # Default cluster ID to use if cluster_id is not set in Prometheus metrics.
imagePullPolicy: Always

---
Expand Down

0 comments on commit 4dfe2cf

Please sign in to comment.