跳转到主要内容

Python封装Kubernetes,其接口与Docker兼容

项目描述

兼容性

PyPI version

此软件包公开了Kubernetes Python客户端的一部分,其接口与Docker SDK for Python兼容。

示例

首先,检出此项目,并安装依赖项: pip install -r requirements-dev.txt。然后,确保您已安装并启动 Docker 和/或 Minikube

有了这些工具,您可以选择 import docker as sdkimport kompatible as sdk,以下示例将正常工作,尽管在第一种情况下是Docker容器启动,而在第二种情况下是Kubernetes pods。

"Hello World!": 运行、列出、删除

>>> client = sdk.from_env()

>>> [client.containers.run(
...     "alpine", "echo hello world",
...     name='foobar',
...     labels={'foo': 'bar'})]
[...'hello world\n']

>>> def not_kube(containers):  # Only needed for Docker, on Travis, with k8s started.
...     return [c for c in containers if 'kube' not in c.name]

>>> containers = not_kube(client.containers.list(all=True, filters={}))
>>> [c.name for c in containers]
[...'foobar']
>>> c = containers[0]

>>> c.remove(force=True, v=True)
>>> containers = not_kube(client.containers.list(all=True, filters={}))
>>> assert len(containers) == 0

容器属性

>>> assert c.id
>>> assert c.image
>>> c.labels
{...'foo': ...'bar'}
>>> assert c.short_id
>>> assert c.status

containers.run 参数


子客户端

>>> assert client.api
>>> assert client.containers
>>> assert client.images
>>> assert client.volumes

待办事项

client.api.base_url

client.containers.run(image_name,
    name=container_spec.container_name,
    ports={'{}/tcp'.format(container_spec.container_port): None},
    detach=True,
    labels=labels,
    volumes=volumes,
    nano_cpus=int(container_spec.cpus * 1e9),
    environment=environment,
    mem_reservation='{}M'.format(new_mem_reservation_mb))
client.containers.get(name_or_id)
    container.logs(timestamps=True)
    container.attrs['NetworkSettings']
    container.attrs['Config']['Labels']
    container.remove(force=True, v=True)
client.containers.list(all=True, filters=filters)

client.images
client.images.pull  # Used by script, but not used by runtime.
# For Kubernetes, consider https://kubernetes.ac.cn/docs/concepts/containers/images/#pre-pulling-images
# We would need to make sure that every image is on every node we start up.
# Mayber other Container registries are faster than DockerHub, in which
# case pre-caching might be less important?

client.volumes
client.volumes.create(driver='local').name

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关 安装软件包 的更多信息。

源代码分发

kompatible-0.0.2.tar.gz (5.0 kB 查看哈希)

上传时间: 源代码

构建分发

kompatible-0.0.2-py2.py3-none-any.whl (6.2 kB 查看哈希值)

上传时间 Python 2 Python 3

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面