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.7.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

kg_loki-0.7.7-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: kg_loki-0.7.7.tar.gz
  • Upload date:
  • Size: 10.8 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.7.7.tar.gz
Algorithm Hash digest
SHA256 fb808633b28ae420afba5b96f0dac1c4a950c1076e350db9b0b5c1b59c8542df
MD5 08c7f6877b42f9dac7c4203f10d05d07
BLAKE2b-256 a02933bbe1fbef5d5b4845ac00fa6f21b6c45b36ee45910358fc1ddb99113e4a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: kg_loki-0.7.7-py3-none-any.whl
  • Upload date:
  • Size: 10.2 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.7.7-py3-none-any.whl
Algorithm Hash digest
SHA256 2dd94519e68388ed123a0e19a8771ec139fbe04af4e63f0039d6d390c0e2ada1
MD5 bef6f6eb191e26e97bf21ed1078b6e30
BLAKE2b-256 e82380c94127dc6afa0b2d6fe169971dc6f691ce1f1ec1e4ff69b78efd76e469

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