from lightkube import Client
from lightkube.resources.core_v1 import Pod
with Client() as client:
pod = client.get(Pod, name="my-pod", namespace="default")
print(pod.namespace.uid)
List nodes
from lightkube import Client
from lightkube.resources.core_v1 import Node
client = Client()
for node in client.list(Node):
print(node.metadata.name)
Create
Create a config map
from lightkube import Client
from lightkube.resources.core_v1 import ConfigMap
from lightkube.models.meta_v1 import ObjectMeta
client = Client()
config = ConfigMap(
metadata=ObjectMeta(name='my-config', namespace='default'),
data={'key1': 'value1', 'key2': 'value2'}
)
client.create(config)
Create resources defined in a file
from lightkube import Client, codecs
client = Client()
with open('deployment.yaml') as f:
for obj in codecs.load_all_yaml(f):
client.create(obj)
Modify
Replace the previous config with a different content
# When using PatchType.MERGE, setting a value of a key/value to None, will remove the current item
patch = {'metadata': {"key3": None}}
client.patch(ConfigMap, name='my-config', namespace='default', obj=patch, patch_type=PatchType.MERGE)
Note:field_manager is required for server-side apply. You can specify it once in the client constructor
or when calling apply(). Also apiVersion and kind need to be provided as part of
the object definition.
from lightkube.resources.core_v1 import ConfigMap
from lightkube.models.meta_v1 import ObjectMeta
client = Client(field_manager="my-manager")
config = ConfigMap(
# note apiVersion and kind need to be specified for server-side apply
apiVersion='v1', kind='ConfigMap',
metadata=ObjectMeta(name='my-config', namespace='default'),
data={'key1': 'value1', 'key2': 'value2'}
)
res = client.apply(config)
print(res.data)
# prints {'key1': 'value1', 'key2': 'value2'}
del config.data['key1']
config.data['key3'] = 'value3'
res = client.apply(config)
print(res.data)
# prints {'key2': 'value2', 'key3': 'value3'}
from lightkube import Client
from lightkube.resources.apps_v1 import Deployment
client = Client()
for op, dep in client.watch(Deployment, namespace="default"):
print(f"{dep.namespace.name} {dep.spec.replicas}")
Stream pod logs
from lightkube import Client
client = Client()
for line in client.log('my-pod', follow=True):
print(line)
Execute a command inside a pod
from lightkube import Client
client = Client()
# Capture stdout or raise ApiError if error code is != 0
res = client.exec('my-pod', namespace='default', container='main', command=['ls', '-l', '/'],
stdout=True, raise_on_error=True)
print(res.stdout)
# Send data to stdin and capture output
res = client.exec('my-pod', namespace='default', container='main', command=['cat'],
stdin='hello\n', stdout=True)
print(res.stdout)
print(res.exit_code)
Unsupported features
The following features are not supported at the moment:
Special subresources attach, portforward and proxy.
auth-provider authentication method is not supported. The supported authentication methods are token, tokenFile, username + password and exec.
Alternatives
Use instead of lightkube: tools that do the same job, closest first.