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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: kg_loki-0.7.6.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.6.tar.gz
Algorithm Hash digest
SHA256 3051cb4d7e543d1842933e4116c04ce1d4b00401e782aa5a5b285c0296d682c9
MD5 136e4942f388d8723c10e7e38d397f73
BLAKE2b-256 7e88bce05b2c55962a350fa6077526d3e185b101e050520d50d9470430d7f579

See more details on using hashes here.

File details

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

File metadata

  • Download URL: kg_loki-0.7.6-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.6-py3-none-any.whl
Algorithm Hash digest
SHA256 077b04d9f5cd4a975809bd53139dc6ac33c51b1293e561e478a76941d5c95a66
MD5 d9b18267d66afeda1269e94c57643568
BLAKE2b-256 f50559e7220b382117b2e3cd0af1ff9d4dbf2b54bf873504de65be693d7aa6d1

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