Skip to main content

KubraGen Builder: Loki

Project description

KubraGen Builder: Loki

PyPI version Supported Python versions

kg_loki is a builder for KubraGen that deploys a Grafana Loki service in Kubernetes.

KubraGen is a Kubernetes YAML generator library that makes it possible to generate configurations using the full power of the Python programming language.

Example

from kubragen import KubraGen
from kubragen.consts import PROVIDER_GOOGLE, PROVIDERSVC_GOOGLE_GKE
from kubragen.object import Object
from kubragen.option import OptionRoot
from kubragen.options import Options
from kubragen.output import OutputProject, OD_FileTemplate, OutputFile_ShellScript, OutputFile_Kubernetes, \
    OutputDriver_Print
from kubragen.provider import Provider

from kg_loki import LokiBuilder, LokiOptions, LokiConfigFile, LokiConfigFileOptions

kg = KubraGen(provider=Provider(PROVIDER_GOOGLE, PROVIDERSVC_GOOGLE_GKE), options=Options({
    'namespaces': {
        'mon': 'app-monitoring',
    },
}))

out = OutputProject(kg)

shell_script = OutputFile_ShellScript('create_gke.sh')
out.append(shell_script)

shell_script.append('set -e')

#
# OUTPUTFILE: app-namespace.yaml
#
file = OutputFile_Kubernetes('app-namespace.yaml')

file.append([
    Object({
        'apiVersion': 'v1',
        'kind': 'Namespace',
        'metadata': {
            'name': 'app-monitoring',
        },
    }, name='ns-monitoring', source='app', instance='app')
])

out.append(file)
shell_script.append(OD_FileTemplate(f'kubectl apply -f ${{FILE_{file.fileid}}}'))

shell_script.append(f'kubectl config set-context --current --namespace=app-monitoring')

#
# SETUP: loki
#
lokiconfigfile = LokiConfigFile(options=LokiConfigFileOptions({
}))

loki_config = LokiBuilder(kubragen=kg, options=LokiOptions({
    'namespace': OptionRoot('namespaces.mon'),
    'basename': 'myloki',
    'config': {
        'loki_config': lokiconfigfile,
    },
    'kubernetes': {
        'volumes': {
            'data': {
                'persistentVolumeClaim': {
                    'claimName': 'loki-storage-claim'
                }
            }
        },
        'resources': {
            'statefulset': {
                'requests': {
                    'cpu': '150m',
                    'memory': '300Mi'
                },
                'limits': {
                    'cpu': '300m',
                    'memory': '450Mi'
                },
            },
        },
    }
}))

loki_config.ensure_build_names(loki_config.BUILD_CONFIG, loki_config.BUILD_SERVICE)

#
# OUTPUTFILE: loki-config.yaml
#
file = OutputFile_Kubernetes('loki-config.yaml')
out.append(file)

file.append(loki_config.build(loki_config.BUILD_CONFIG))

shell_script.append(OD_FileTemplate(f'kubectl apply -f ${{FILE_{file.fileid}}}'))

#
# OUTPUTFILE: loki.yaml
#
file = OutputFile_Kubernetes('loki.yaml')
out.append(file)

file.append(loki_config.build(loki_config.BUILD_SERVICE))

shell_script.append(OD_FileTemplate(f'kubectl apply -f ${{FILE_{file.fileid}}}'))

#
# Write files
#
out.output(OutputDriver_Print())
# out.output(OutputDriver_Directory('/tmp/build-gke'))

Output:

****** BEGIN FILE: 001-app-namespace.yaml ********
apiVersion: v1
kind: Namespace
metadata:
  name: app-monitoring

****** END FILE: 001-app-namespace.yaml ********
****** BEGIN FILE: 002-loki-config.yaml ********
apiVersion: v1
kind: Secret
metadata:
  name: myloki-config-secret
  namespace: app-monitoring
type: Opaque
data:
  loki.yaml: YXV0aF9lbmFibGVkOiBmYWxzZQppbmdlc3RlcjoKICBjaHVua19pZGxlX3BlcmlvZDogM20KICBjaHVua19ibG9ja19zaXplOiAyNjIxNDQKICBjaHVua19yZXRhaW5fcGVyaW9kOiAxbQogIG1heF90cmFuc2Zlcl9yZXRyaWVzOiAwCiAgbGlmZWN5Y2xlcjoKICAgIHJpbmc6CiAgICAgIGt2c3RvcmU6IHtzdG9yZTogaW5tZW1vcnl9CiAgICAgIHJlcGxpY2F0aW9uX2ZhY3RvcjogMQpsaW1pdHNfY29uZmlnOiB7ZW5mb3JjZV9tZXRyaWNfbmFtZTogZmFsc2UsIHJlamVjdF9vbGRfc2FtcGxlczogdHJ1ZSwgcmVqZWN0X29sZF9zYW1wbGVzX21heF9hZ2U6IDE2OGh9CnNjaGVtYV9jb25maWc6CiAgY29uZmlnczoKICAtIGZyb206IDIwMjAtMTAtMjQKICAgIHN0b3JlOiBib2x0ZGItc2hpcHBlcgogICAgb2JqZWN0X3N0b3JlOiBmaWxlc3lzdGVtCiAgICBzY2hlbWE6IHYxMQogICAgaW5kZXg6IHtwcmVmaXg6IGluZGV4XywgcGVyaW9kOiAyNGh9CnNlcnZlcjoge2h0dHBfbGlzdGVuX3BvcnQ6IDMxMDB9CnN0b3JhZ2VfY29uZmlnOgogIGJvbHRkYl9zaGlwcGVyOiB7YWN0aXZlX2luZGV4X2RpcmVjdG9yeTogL2RhdGEvbG9raS9ib2x0ZGItc2hpcHBlci1hY3RpdmUsIGNhY2hlX2xvY2F0aW9uOiAvZGF0YS9sb2tpL2JvbHRkYi1zaGlwcGVyLWNhY2hlLAogICAgY2FjaGVfdHRsOiAyNGgsIHNoYXJlZF9zdG9yZTogZmlsZXN5c3RlbX0KICBmaWxlc3lzdGVtOiB7ZGlyZWN0b3J5OiAvZGF0YS9sb2tpL2NodW5rc30KY2h1bmtfc3RvcmVfY29uZmlnOiB7bWF4X2xvb2tfYmFja19wZXJpb2Q6IDBzfQp0YWJsZV9tYW5hZ2VyOiB7cmV0ZW50aW9uX2RlbGV0ZXNfZW5hYmxlZDogZmFsc2UsIHJldGVudGlvbl9wZXJpb2Q6IDBzfQpjb21wYWN0b3I6IHt3b3JraW5nX2RpcmVjdG9yeTogL2RhdGEvbG9raS9ib2x0ZGItc2hpcHBlci1jb21wYWN0b3IsIHNoYXJlZF9zdG9yZTogZmlsZXN5c3RlbX0K

****** END FILE: 002-loki-config.yaml ********
****** BEGIN FILE: 003-loki.yaml ********
apiVersion: v1
kind: Service
metadata:
  name: myloki-headless
  namespace: app-monitoring
  labels:
    app: myloki
spec:
  clusterIP: None
  ports:
  - port: 3100
    protocol: TCP
    name: http-metrics
    targetPort: http-metrics
  selector:
    app: myloki
---
apiVersion: v1
kind: Service
metadata:
  name: myloki
  namespace: app-monitoring
  labels:
    app: myloki
<...more...>
****** END FILE: 003-loki.yaml ********
****** BEGIN FILE: create_gke.sh ********
#!/bin/bash

set -e
kubectl apply -f 001-app-namespace.yaml
kubectl config set-context --current --namespace=app-monitoring
kubectl apply -f 002-loki-config.yaml
kubectl apply -f 003-loki.yaml

****** END FILE: create_gke.sh ********

Credits

based on

Install Loki with Helm

Author

Rangel Reale (rangelreale@gmail.com)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kg_loki-0.8.0.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

kg_loki-0.8.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file kg_loki-0.8.0.tar.gz.

File metadata

  • Download URL: kg_loki-0.8.0.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.3

File hashes

Hashes for kg_loki-0.8.0.tar.gz
Algorithm Hash digest
SHA256 5efaae35e492bb11054064ffb7e68ed7ea3f48b739de6bf2facb468956b65e5a
MD5 39fe5ee00443f8296bece8d58e20ae38
BLAKE2b-256 2085e49e68934daff0b253806322ade51afbd4cabd2f16585df1b847ecffba0c

See more details on using hashes here.

File details

Details for the file kg_loki-0.8.0-py3-none-any.whl.

File metadata

  • Download URL: kg_loki-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.3

File hashes

Hashes for kg_loki-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aa605f6ae3303c2aaa724173b3d01e1e0d7d96cac8761c6d0a45556906f4ec1b
MD5 5a0be15ee79c915018de6f92b87a141c
BLAKE2b-256 4eb8262eb25ef614d036738340936a67777608879593c497c63e1c2e57a0ccf4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page