From a1fde756105453df0e124201e593d2a40f82dd29 Mon Sep 17 00:00:00 2001 From: hjet Date: Thu, 22 Jul 2021 18:46:53 -0400 Subject: [PATCH 1/4] Use Deployment for traces quickstart --- production/kubernetes/agent-tempo.yaml | 79 ++----------------- .../build/templates/tempo/main.jsonnet | 9 ++- .../grafana-agent/v1/lib/deployment.libsonnet | 61 +++++++++++--- 3 files changed, 65 insertions(+), 84 deletions(-) diff --git a/production/kubernetes/agent-tempo.yaml b/production/kubernetes/agent-tempo.yaml index 9daad11008c1..bab61d6fe8ee 100644 --- a/production/kubernetes/agent-tempo.yaml +++ b/production/kubernetes/agent-tempo.yaml @@ -2,69 +2,7 @@ apiVersion: v1 kind: ServiceAccount metadata: name: grafana-agent-traces - namespace: ${NAMESPACE} ---- -apiVersion: v1 -data: - agent.yaml: | - server: - http_listen_port: 8080 - log_level: info - tempo: - configs: - - batch: - send_batch_size: 1000 - timeout: 5s - name: default - receivers: - jaeger: - protocols: - grpc: null - thrift_binary: null - thrift_compact: null - thrift_http: null - remote_sampling: - insecure: true - strategy_file: /etc/agent/strategies.json - opencensus: null - otlp: - protocols: - grpc: null - http: null - zipkin: null - remote_write: - - basic_auth: - password: ${TEMPO_PASSWORD} - username: ${TEMPO_USERNAME} - endpoint: ${TEMPO_ENDPOINT} - retry_on_failure: - enabled: false - scrape_configs: - - bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token - job_name: kubernetes-pods - kubernetes_sd_configs: - - role: pod - relabel_configs: - - action: replace - source_labels: - - __meta_kubernetes_namespace - target_label: namespace - - action: replace - source_labels: - - __meta_kubernetes_pod_name - target_label: pod - - action: replace - source_labels: - - __meta_kubernetes_pod_container_name - target_label: container - tls_config: - ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt - insecure_skip_verify: false - strategies.json: '{"default_strategy": {"param": 0.001, "type": "probabilistic"}}' -kind: ConfigMap -metadata: - name: grafana-agent-traces - namespace: ${NAMESPACE} + namespace: YOUR_NAMESPACE --- apiVersion: rbac.authorization.k8s.io/v1beta1 kind: ClusterRole @@ -99,7 +37,7 @@ roleRef: subjects: - kind: ServiceAccount name: grafana-agent-traces - namespace: ${NAMESPACE} + namespace: YOUR_NAMESPACE --- apiVersion: v1 kind: Service @@ -107,7 +45,7 @@ metadata: labels: name: grafana-agent-traces name: grafana-agent-traces - namespace: ${NAMESPACE} + namespace: YOUR_NAMESPACE spec: ports: - name: agent-http-metrics @@ -145,12 +83,14 @@ spec: name: grafana-agent-traces --- apiVersion: apps/v1 -kind: DaemonSet +kind: Deployment metadata: name: grafana-agent-traces - namespace: ${NAMESPACE} + namespace: YOUR_NAMESPACE spec: minReadySeconds: 10 + replicas: 1 + revisionHistoryLimit: 10 selector: matchLabels: name: grafana-agent-traces @@ -200,12 +140,7 @@ spec: - mountPath: /etc/agent name: grafana-agent-traces serviceAccount: grafana-agent-traces - tolerations: - - effect: NoSchedule - operator: Exists volumes: - configMap: name: grafana-agent-traces name: grafana-agent-traces - updateStrategy: - type: RollingUpdate diff --git a/production/kubernetes/build/templates/tempo/main.jsonnet b/production/kubernetes/build/templates/tempo/main.jsonnet index c178c076ed81..93faafd10324 100644 --- a/production/kubernetes/build/templates/tempo/main.jsonnet +++ b/production/kubernetes/build/templates/tempo/main.jsonnet @@ -11,7 +11,7 @@ local newPort(name, portNumber, protocol='TCP') = { agent: - agent.new('grafana-agent-traces', '${NAMESPACE}') + + agent.newDeployment('grafana-agent-traces', 'YOUR_NAMESPACE') + agent.withConfigHash(false) + agent.withImages({ agent: (import 'version.libsonnet'), @@ -74,5 +74,10 @@ local newPort(name, portNumber, protocol='TCP') = param: 0.001, }, }) + - agent.withTempoScrapeConfigs(agent.tempoScrapeKubernetes), + agent.withTempoScrapeConfigs(agent.tempoScrapeKubernetes) + { + agent+: { + // Remove this block to generate ConfigMap + config_map:: {}, + }, + }, } diff --git a/production/tanka/grafana-agent/v1/lib/deployment.libsonnet b/production/tanka/grafana-agent/v1/lib/deployment.libsonnet index 4c9645d387da..4d965966d049 100644 --- a/production/tanka/grafana-agent/v1/lib/deployment.libsonnet +++ b/production/tanka/grafana-agent/v1/lib/deployment.libsonnet @@ -1,4 +1,9 @@ local agent = import '../internal/agent.libsonnet'; +local k = import 'ksonnet-util/kausal.libsonnet'; + +local configMap = k.core.v1.configMap; +local service = k.core.v1.service; +local container = k.core.v1.container; { // newDeployment creates a new single-replicaed Deployment of the @@ -26,25 +31,61 @@ local agent = import '../internal/agent.libsonnet'; local has_prometheus_config = std.objectHasAll(self, '_prometheus_config'), local has_prometheus_instances = std.objectHasAll(self, '_prometheus_instances'), + local has_tempo_config = std.objectHasAll(self, '_tempo_config'), + local has_sampling_strategies = std.objectHasAll(self, '_tempo_sampling_strategies'), config:: { server: { log_level: 'info', http_listen_port: 8080, }, - - prometheus: - if !has_prometheus_config then {} - else this._prometheus_config { - configs: - if has_prometheus_instances - then this._prometheus_instances - else [], + } + ( + if has_prometheus_config + then { + prometheus: + this._prometheus_config { + configs: + if has_prometheus_instances + then this._prometheus_instances + else [], + }, + } + else {} + ) + ( + if has_tempo_config then { + tempo: { + configs: [this._tempo_config { + name: 'default', + }], }, - }, + } + else {} + ), agent: agent.newAgent(name, namespace, self._images.agent, self.config, use_daemonset=false) + - agent.withConfigHash(self._config_hash), + agent.withConfigHash(self._config_hash) + ( + if has_tempo_config + then + { + container+:: container.withEnvMixin([ + k.core.v1.envVar.fromFieldPath('HOSTNAME', 'spec.nodeName'), + ]), + + // If sampling strategies were defined, we need to mount them as a JSON + // file. + config_map+: + if has_sampling_strategies + then configMap.withDataMixin({ + 'strategies.json': std.toString(this._tempo_sampling_strategies), + }) + else {}, + + // If we're deploying for tracing, applications will want to write to + // a service for load balancing span delivery. + service: + k.util.serviceFor(self.agent) + service.mixin.metadata.withNamespace(namespace) + } else {} + ) }, } From 568c5d02857c7e2df7035459c0ff4eafceb24f5c Mon Sep 17 00:00:00 2001 From: hjet Date: Fri, 23 Jul 2021 12:27:16 -0400 Subject: [PATCH 2/4] Add link to traces quickstart --- production/kubernetes/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/production/kubernetes/README.md b/production/kubernetes/README.md index 4ff2383a4fec..359462e6c38a 100644 --- a/production/kubernetes/README.md +++ b/production/kubernetes/README.md @@ -15,6 +15,7 @@ For sample configurations and detailed installation instructions, please head to - [Grafana Agent Metrics Kubernetes Quickstart](https://grafana.com/docs/grafana-cloud/quickstart/agent-k8s/k8s_agent_metrics/) - [Grafana Agent Logs Kubernetes Quickstart](https://grafana.com/docs/grafana-cloud/quickstart/agent-k8s/k8s_agent_logs/) +- [Grafana Agent Traces Kubernetes Quickstart](https://grafana.com/docs/grafana-cloud/quickstart/agent-k8s/k8s_agent_traces/) ## Manually Applying From 2ed10c8af6fe52ad3f9de560a661b27c775c988a Mon Sep 17 00:00:00 2001 From: hjet Date: Fri, 23 Jul 2021 15:10:05 -0400 Subject: [PATCH 3/4] Set HOSTNAME env var regardless of application --- production/kubernetes/agent-bare.yaml | 5 +++ .../grafana-agent/v1/lib/deployment.libsonnet | 36 +++++++++---------- 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/production/kubernetes/agent-bare.yaml b/production/kubernetes/agent-bare.yaml index 40caab9ee254..63e970d83040 100644 --- a/production/kubernetes/agent-bare.yaml +++ b/production/kubernetes/agent-bare.yaml @@ -61,6 +61,11 @@ spec: - -config.file=/etc/agent/agent.yaml command: - /bin/agent + env: + - name: HOSTNAME + valueFrom: + fieldRef: + fieldPath: spec.nodeName image: grafana/agent:v0.16.1 imagePullPolicy: IfNotPresent name: agent diff --git a/production/tanka/grafana-agent/v1/lib/deployment.libsonnet b/production/tanka/grafana-agent/v1/lib/deployment.libsonnet index 4d965966d049..1b33890b8691 100644 --- a/production/tanka/grafana-agent/v1/lib/deployment.libsonnet +++ b/production/tanka/grafana-agent/v1/lib/deployment.libsonnet @@ -64,28 +64,24 @@ local container = k.core.v1.container; agent: agent.newAgent(name, namespace, self._images.agent, self.config, use_daemonset=false) + - agent.withConfigHash(self._config_hash) + ( - if has_tempo_config - then - { - container+:: container.withEnvMixin([ - k.core.v1.envVar.fromFieldPath('HOSTNAME', 'spec.nodeName'), - ]), - - // If sampling strategies were defined, we need to mount them as a JSON - // file. - config_map+: - if has_sampling_strategies - then configMap.withDataMixin({ - 'strategies.json': std.toString(this._tempo_sampling_strategies), - }) - else {}, - + agent.withConfigHash(self._config_hash) + { + container+:: container.withEnvMixin([ + k.core.v1.envVar.fromFieldPath('HOSTNAME', 'spec.nodeName'), + ]), + // If sampling strategies were defined, we need to mount them as a JSON + // file. + config_map+: + if has_sampling_strategies + then configMap.withDataMixin({ + 'strategies.json': std.toString(this._tempo_sampling_strategies), + }) + else {}, // If we're deploying for tracing, applications will want to write to // a service for load balancing span delivery. service: - k.util.serviceFor(self.agent) + service.mixin.metadata.withNamespace(namespace) - } else {} - ) + if has_tempo_config + then k.util.serviceFor(self.agent) + service.mixin.metadata.withNamespace(namespace) + else {}, + }, }, } From 9c022e478ff8acb8e422d2d2b192bcbb121dffe0 Mon Sep 17 00:00:00 2001 From: Robert Fratto Date: Fri, 23 Jul 2021 15:14:27 -0400 Subject: [PATCH 4/4] format, move container env to agent.libsonnet --- production/kubernetes/agent-loki.yaml | 4 +++ .../grafana-agent/v1/internal/agent.libsonnet | 5 ++- .../grafana-agent/v1/lib/deployment.libsonnet | 33 +++++++++---------- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/production/kubernetes/agent-loki.yaml b/production/kubernetes/agent-loki.yaml index cddee33ae8a0..da9ba92dcb89 100644 --- a/production/kubernetes/agent-loki.yaml +++ b/production/kubernetes/agent-loki.yaml @@ -60,6 +60,10 @@ spec: command: - /bin/agent env: + - name: HOSTNAME + valueFrom: + fieldRef: + fieldPath: spec.nodeName - name: HOSTNAME valueFrom: fieldRef: diff --git a/production/tanka/grafana-agent/v1/internal/agent.libsonnet b/production/tanka/grafana-agent/v1/internal/agent.libsonnet index 7afcd7e232fa..a41bb9a68918 100644 --- a/production/tanka/grafana-agent/v1/internal/agent.libsonnet +++ b/production/tanka/grafana-agent/v1/internal/agent.libsonnet @@ -45,7 +45,10 @@ local serviceAccount = k.core.v1.serviceAccount; container.withCommand('/bin/agent') + container.withArgsMixin(k.util.mapToFlags({ 'config.file': '/etc/agent/agent.yaml', - })), + })) + + container.withEnvMixin([ + k.core.v1.envVar.fromFieldPath('HOSTNAME', 'spec.nodeName'), + ]), agent: ( diff --git a/production/tanka/grafana-agent/v1/lib/deployment.libsonnet b/production/tanka/grafana-agent/v1/lib/deployment.libsonnet index 1b33890b8691..67651adfc857 100644 --- a/production/tanka/grafana-agent/v1/lib/deployment.libsonnet +++ b/production/tanka/grafana-agent/v1/lib/deployment.libsonnet @@ -40,7 +40,7 @@ local container = k.core.v1.container; http_listen_port: 8080, }, } + ( - if has_prometheus_config + if has_prometheus_config then { prometheus: this._prometheus_config { @@ -65,23 +65,20 @@ local container = k.core.v1.container; agent: agent.newAgent(name, namespace, self._images.agent, self.config, use_daemonset=false) + agent.withConfigHash(self._config_hash) + { - container+:: container.withEnvMixin([ - k.core.v1.envVar.fromFieldPath('HOSTNAME', 'spec.nodeName'), - ]), - // If sampling strategies were defined, we need to mount them as a JSON - // file. - config_map+: - if has_sampling_strategies - then configMap.withDataMixin({ - 'strategies.json': std.toString(this._tempo_sampling_strategies), - }) - else {}, - // If we're deploying for tracing, applications will want to write to - // a service for load balancing span delivery. - service: - if has_tempo_config - then k.util.serviceFor(self.agent) + service.mixin.metadata.withNamespace(namespace) - else {}, + // If sampling strategies were defined, we need to mount them as a JSON + // file. + config_map+: + if has_sampling_strategies + then configMap.withDataMixin({ + 'strategies.json': std.toString(this._tempo_sampling_strategies), + }) + else {}, + // If we're deploying for tracing, applications will want to write to + // a service for load balancing span delivery. + service: + if has_tempo_config + then k.util.serviceFor(self.agent) + service.mixin.metadata.withNamespace(namespace) + else {}, }, }, }