Kubernetes leader election
Project description
k8s-leader-election-py
Client-go has a very easy to use leader-election package for kubernetes controllers to utilize leader-election. The pyhton kubernetes-client doesn't have such a method, so this will have to do for now.
Usage
Your controller must have a pod and namespace environment variable defined:
env:
- name: POD
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
Sample code:
from threading import Thread
from leaderelection import Elect
# Init leader election class. Configmap is the name of the configmap to create to store leader election information
leaderelection = Elect(configmap='sample-controller-leader-election')
# Run leader election in new thread
th = Thread(target=leaderelection.run)
th.setDaemon(True)
th.start()
#start main controller loop
while True:
# Check if pod is the leader. If so continue on with controller logic.
leader = leaderelection.check_leader()
if leader:
logger.info("I am the leader!!")
else:
logger.info("I am NOT the leader")
An example of this can be found in the sample-controller directory.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
leaderelection-0.0.2.tar.gz
(3.1 kB
view details)
File details
Details for the file leaderelection-0.0.2.tar.gz
.
File metadata
- Download URL: leaderelection-0.0.2.tar.gz
- Upload date:
- Size: 3.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.14.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 89607c88a993c10cbc3c931372a38f6aa41fc4f45b741a07e27e0188a63683d3 |
|
MD5 | 50aaddd12d4c4e8f7b0f16ebdb132b64 |
|
BLAKE2b-256 | 8099aeff2a9e3bf003e2a9cfa7e3567d40011c69ad5d640958602bc15609a303 |