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 (rangelspam@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.7.9.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

kg_loki-0.7.9-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: kg_loki-0.7.9.tar.gz
  • Upload date:
  • Size: 10.9 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.6

File hashes

Hashes for kg_loki-0.7.9.tar.gz
Algorithm Hash digest
SHA256 168b94a70cfe88fe3d0ab9ce158bc8f96bcd245bbe34c0c9fe9c9e089ac80fb3
MD5 d7e218e7f05f9101bf1858ebdefa053f
BLAKE2b-256 5b773225e75ea2cc7d363d8dcf109efe922e61b6280111cd5317c83bd2e91065

See more details on using hashes here.

File details

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

File metadata

  • Download URL: kg_loki-0.7.9-py3-none-any.whl
  • Upload date:
  • Size: 10.4 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.6

File hashes

Hashes for kg_loki-0.7.9-py3-none-any.whl
Algorithm Hash digest
SHA256 7056246d2edd214d97b4f5edbeff413bd1b0600451f29209d70fd12994fa6ad0
MD5 bed5f116dae2282d98c9b462c13144cc
BLAKE2b-256 d40d6728ac55f2fbd54b6db8603b4c3a9accd02ce8b7d8abd5162c131ec520e3

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