Skip to content

Commit

Permalink
Merge pull request #12 from promhippie/fix-static-binaries
Browse files Browse the repository at this point in the history
Fix static binaries
  • Loading branch information
tboerger committed Jun 22, 2020
2 parents b7e0585 + 1d70cb1 commit 55654d6
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 9 deletions.
15 changes: 15 additions & 0 deletions .drone.star
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,21 @@ def docker(ctx, arch):
'arch': agent,
},
'steps': [
{
'name': 'version',
'image': 'webhippie/golang:1.14',
'pull': 'always',
'environment': environment,
'commands': [
'go env',
],
'volumes': [
{
'name': 'gopath',
'path': '/srv/app',
},
],
},
{
'name': 'generate',
'image': 'webhippie/golang:1.14',
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ else
endif

ifeq ($(UNAME), Darwin)
GOBUILD ?= go build -i
GOBUILD ?= CGO_ENABLED=0 go build -i
else
GOBUILD ?= go build
GOBUILD ?= CGO_ENABLED=0 go build
endif

PACKAGES ?= $(shell go list ./...)
SOURCES ?= $(shell find . -name "*.go" -type f -not -path "./node_modules/*")
GENERATE ?= $(PACKAGES)

TAGS ?=
TAGS ?= netgo

ifndef OUTPUT
ifneq ($(DRONE_TAG),)
Expand All @@ -48,7 +48,7 @@ ifndef SHA
SHA := $(shell git rev-parse --short HEAD)
endif

LDFLAGS += -s -w -X "$(IMPORT)/pkg/version.String=$(VERSION)" -X "$(IMPORT)/pkg/version.Revision=$(SHA)" -X "$(IMPORT)/pkg/version.Date=$(DATE)"
LDFLAGS += -s -w -extldflags "-static" -X "$(IMPORT)/pkg/version.String=$(VERSION)" -X "$(IMPORT)/pkg/version.Revision=$(SHA)" -X "$(IMPORT)/pkg/version.Date=$(DATE)"
GCFLAGS += all=-N -l

.PHONY: all
Expand Down
8 changes: 8 additions & 0 deletions changelog/0.4.1_2020-06-22/static-linked.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Bugfix: Binaries are not static linked

We fixed building properly static linked binaries, since the last release and a
major refactoring of the binaries and the CI pipeline we introduced binaries
which had been linked to muslc by mistake. With this change applied all binaries
will be properly static linked again.

https://github.com/promhippie/prometheus-hetzner-sd/issues/11
103 changes: 98 additions & 5 deletions docs/content/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,116 @@ We won't cover further details how to properly setup [Prometheus](https://promet

First of all we need to prepare a configuration for [Prometheus](https://prometheus.io) that includes the service discovery which simply maps to a node exporter.

{{< gist tboerger ce77494a0c24012a95e22b3691d15b7c "prometheus.yml" >}}
{{< highlight yaml >}}
global:
scrape_interval: 1m
scrape_timeout: 10s
evaluation_interval: 1m

scrape_configs:
- job_name: node
file_sd_configs:
- files: [ "/etc/sd/hetzner.json" ]
relabel_configs:
- source_labels: [__meta_hetzner_public_ipv4]
replacement: "${1}:9100"
target_label: __address__
- source_labels: [__meta_hetzner_dc]
target_label: datacenter
- source_labels: [__meta_hetzner_name]
target_label: instance
- job_name: hetzner-sd
static_configs:
- targets:
- hetzner-sd:9000
{{< / highlight >}}

After preparing the configuration we need to create the `docker-compose.yml` within the same folder, this `docker-compose.yml` starts a simple [Prometheus](https://prometheus.io) instance together with the service discovery. Don't forget to update the envrionment variables with the required credentials. If you are using a different volume for the service discovery you have to make sure that the container user is allowed to write to this volume.

{{< gist tboerger ce77494a0c24012a95e22b3691d15b7c "docker-compose.yml" >}}
{{< highlight yaml >}}
version: '2'

volumes:
prometheus:

services:
prometheus:
image: prom/prometheus:v2.6.0
restart: always
ports:
- 9090:9090
volumes:
- prometheus:/prometheus
- ./prometheus.yml:/etc/prometheus/prometheus.yml
- ./service-discovery:/etc/sd

hetzner-sd:
image: promhippie/prometheus-hetzner-sd:latest
restart: always
environment:
- PROMETHEUS_HETZNER_LOG_PRETTY=true
- PROMETHEUS_HETZNER_OUTPUT_FILE=/etc/sd/hetzner.json
- PROMETHEUS_HETZNER_USERNAME=octocat
- PROMETHEUS_HETZNER_PASSWORD=p455w0rd
volumes:
- ./service-discovery:/etc/sd
{{< / highlight >}}

Since our `latest` Docker tag always refers to the `master` branch of the Git repository you should always use some fixed version. You can see all available tags at our [DockerHub repository](https://hub.docker.com/r/promhippie/prometheus-hetzner-sd/tags/), there you will see that we also provide a manifest, you can easily start the exporter on various architectures without any change to the image name. You should apply a change like this to the `docker-compose.yml`:

{{< gist tboerger ce77494a0c24012a95e22b3691d15b7c "tag.diff" >}}
{{< highlight diff >}}
hetzner-sd:
- image: promhippie/prometheus-hetzner-sd:latest
+ image: promhippie/prometheus-hetzner-sd:0.2.0
restart: always
environment:
- PROMETHEUS_HETZNER_LOG_PRETTY=true
- PROMETHEUS_HETZNER_OUTPUT_FILE=/etc/sd/hetzner.json
- PROMETHEUS_HETZNER_USERNAME=octocat
- PROMETHEUS_HETZNER_PASSWORD=p455w0rd
volumes:
- ./service-discovery:/etc/sd
{{< / highlight >}}

Depending on how you have launched and configured [Prometheus](https://prometheus.io) it's possible that it's running as user `nobody`, in that case you should run the service discovery as this user as well, otherwise [Prometheus](https://prometheus.io) won't be able to read the generated JSON file:

{{< gist tboerger ce77494a0c24012a95e22b3691d15b7c "userid.diff" >}}
{{< highlight diff >}}
hetzner-sd:
image: promhippie/prometheus-hetzner-sd:latest
restart: always
+ user: '65534'
environment:
- PROMETHEUS_HETZNER_LOG_PRETTY=true
- PROMETHEUS_HETZNER_OUTPUT_FILE=/etc/sd/hetzner.json
- PROMETHEUS_HETZNER_USERNAME=octocat
- PROMETHEUS_HETZNER_PASSWORD=p455w0rd
volumes:
- ./service-discovery:/etc/sd
{{< / highlight >}}

Finally the service discovery should be configured fine, let's start this stack with [docker-compose](https://docs.docker.com/compose/), you just need to execute `docker-compose up` within the directory where you have stored `prometheus.yml` and `docker-compose.yml`.

{{< gist tboerger ce77494a0c24012a95e22b3691d15b7c "output.log" >}}
{{< highlight txt >}}
# docker-compose up
Creating network "hetzner-sd_default" with the default driver
Creating volume "hetzner-sd_prometheus" with default driver
Creating hetzner-sd_prometheus_1 ... done
Creating hetzner-sd_hetzner-sd_1 ... done
Attaching to hetzner-sd_prometheus_1, hetzner-sd_hetzner-sd_1
prometheus_1 | level=info ts=2018-10-07T16:01:22.6155953Z caller=main.go:238 msg="Starting Prometheus" version="(version=2.4.3, branch=HEAD, revision=167a4b4e73a8eca8df648d2d2043e21bdb9a7449)"
prometheus_1 | level=info ts=2018-10-07T16:01:22.6157307Z caller=main.go:239 build_context="(go=go1.11.1, user=root@1e42b46043e9, date=20181004-08:42:02)"
prometheus_1 | level=info ts=2018-10-07T16:01:22.6160183Z caller=main.go:240 host_details="(Linux 4.9.93-linuxkit-aufs #1 SMP Wed Jun 6 16:55:56 UTC 2018 x86_64 a0bb50ec35d5 (none))"
prometheus_1 | level=info ts=2018-10-07T16:01:22.6162848Z caller=main.go:241 fd_limits="(soft=1048576, hard=1048576)"
prometheus_1 | level=info ts=2018-10-07T16:01:22.6163093Z caller=main.go:242 vm_limits="(soft=unlimited, hard=unlimited)"
prometheus_1 | level=info ts=2018-10-07T16:01:22.6180128Z caller=main.go:554 msg="Starting TSDB ..."
prometheus_1 | level=info ts=2018-10-07T16:01:22.6182055Z caller=web.go:397 component=web msg="Start listening for connections" address=0.0.0.0:9090
prometheus_1 | level=info ts=2018-10-07T16:01:22.6256146Z caller=main.go:564 msg="TSDB started"
prometheus_1 | level=info ts=2018-10-07T16:01:22.6270596Z caller=main.go:624 msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml
prometheus_1 | level=info ts=2018-10-07T16:01:22.6308837Z caller=main.go:650 msg="Completed loading of configuration file" filename=/etc/prometheus/prometheus.yml
prometheus_1 | level=info ts=2018-10-07T16:01:22.6315546Z caller=main.go:523 msg="Server is ready to receive web requests."
hetzner-sd_1 | level=info ts=2018-10-07T16:01:22.6646358Z msg="Launching Prometheus Hetzner SD" version=0.0.0-master revision=9e14c57 date=20180924 go=go1.11
hetzner-sd_1 | level=info ts=2018-10-07T16:01:22.6648328Z msg="Starting metrics server" addr=0.0.0.0:9000
{{< / highlight >}}

That's all, the service discovery should be up and running. You can access [Prometheus](https://prometheus.io) at [http://localhost:9090](http://localhost:9090).

Expand Down

0 comments on commit 55654d6

Please sign in to comment.