Skip to main content

Library for simplifying liveness checks for python based infrastructure

Reason this release was yanked:

Fixing import issues

Project description

K8S-liveness Python Library

This is the K8s-liveness project; it contains a basic library for creating and referncing timestamps in order to determine if a piece of code is running or not, from a k8s perspective.

The base class, Timestamp, is intended to be inherited. Each project implementation must specify its own maximum_age attribute within the defined subclass.

Usage

This entrypoint is used to determine if this service is still active/alive from a kubernetes liveness probe perspective.

This service is deemed to be 'alive' and healthy if the main loop has executed relatively recently. The period of time for how frequently the agent checks for operational work is defined as a function of event frequency from kubernetes, so this liveness probe needs a larger than normal window to account for periods of time without a recent liveness cycle.

Example

See the example in main

This main routine exists as an example; each project should implement their own main routine within their own liveness project. Then, a helm chart deployment may reference the projects newly created module by adding this yaml snippet to your helm chart or PodSpec.

Helm Chart or PodSpec

      livenessProbe:
        exec:
         command:
         - python3
         - "-m"
         - "myservice.liveness"
        initialDelaySeconds: 10
        periodSeconds: 30

Python Code

import sys
import logging
import os

from liveness import TIMESTAMP_PATH
from liveness.timestamp import Timestamp


LOGGER = logging.getLogger('liveness.main')
DEFAULT_LOG_LEVEL = logging.INFO


def setup_logging():
    log_format = "%(asctime)-15s - %(levelname)-7s - %(name)s - %(message)s"
    requested_log_level = os.environ.get('LOG_LEVEL', DEFAULT_LOG_LEVEL)
    log_level = logging.getLevelName(requested_log_level)
    logging.basicConfig(level=log_level, format=log_format)


if __name__ == '__main__':
    setup_logging()
    timestamp = Timestamp.byref(TIMESTAMP_PATH)
    if timestamp.alive:
        LOGGER.info("%s is considered valid; the application is alive!", timestamp)
        sys.exit(0)
    else:
        LOGGER.warning("Timestamp is no longer considered valid.")
        sys.exit(1)

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

liveness-1.1.2.tar.gz (3.9 kB view details)

Uploaded Source

File details

Details for the file liveness-1.1.2.tar.gz.

File metadata

  • Download URL: liveness-1.1.2.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.1

File hashes

Hashes for liveness-1.1.2.tar.gz
Algorithm Hash digest
SHA256 927592bce0dd6c356cab6710511b8f9e11c5070ed9762b14deceda2bc9965a85
MD5 853b9af9542630cc25609a41e08f92b7
BLAKE2b-256 e39bfcae4d9d051f3ebddcd012a5cfeea381fa5ee449c005ae9f56c7e3350f58

See more details on using hashes here.

Supported by

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