用于kubernetes的python客户端库
pykube-ng的Python项目详细描述
pykube(pykube ng)是一个用于kubernetes的轻量级python 3.6+客户端库。
这是kelproject/pykube的分叉,不再维护(存档)。这里是pykube自述文件的原始文本:
Kel is an open source Platform as a Service (PaaS) from Eldarion, Inc. that makes it easy to manage web application deployment and hosting through the entire lifecycle from development through testing to production. It adds components and tools on top of Kubernetes that help developers manage their application infrastructure. Kel builds on Eldarion’s 7+ years experience running one of the leading Python and Django PaaSes. For more information about Kel, see kelproject.com or follow us on Twitter @projectkel.
功能
- 使用kubeconfig请求进行身份验证的http接口
- kubernetes api对象的python本地查询
安装
要安装pykube,请使用pip:
pip install pykube-ng
交互式控制台
pykube库模块可以作为交互式控制台在本地运行,以进行快速探索。 它将自动加载~/.kube/config以提供api对象,并将pykube类(Deployment,Pod,..)加载到本地上下文:
python3 -m pykube >>> [d.name for d in Deployment.objects(api)]
用法
在自定义命名空间中查询所有就绪的播客:
importoperatorimportpykubeapi=pykube.HTTPClient(pykube.KubeConfig.from_file("~/.kube/config"))pods=pykube.Pod.objects(api).filter(namespace="gondor-system")ready_pods=filter(operator.attrgetter("ready"),pods)
访问kubernetes对象的任何属性:
pod=pykube.Pod.objects(api).filter(namespace="gondor-system").get(name="my-pod")pod.obj["spec"]["containers"][0]["image"]
选择器查询:
pods=pykube.Pod.objects(api).filter(namespace="gondor-system",selector={"gondor.io/name__in":{"api-web","api-worker"}},)pending_pods=pykube.objects.Pod.objects(api).filter(field_selector={"status.phase":"Pending"})
观看查询:
watch=pykube.Job.objects(api,namespace="gondor-system")watch=watch.filter(field_selector={"metadata.name":"my-job"}).watch()# watch is a generator:forwatch_eventinwatch:print(watch_event.type)# 'ADDED', 'DELETED', 'MODIFIED'print(watch_event.object)# pykube.Job object
创建展开:
obj={"apiVersion":"apps/v1","kind":"Deployment","metadata":{"name":"my-deploy","namespace":"gondor-system"},"spec":{"replicas":3,"selector":{"matchLabels":{"app":"nginx"}},"template":{"metadata":{"labels":{"app":"nginx"}},"spec":{"containers":[{"name":"nginx","image":"nginx","ports":[{"containerPort":80}]}]}}}}pykube.Deployment(api,obj).create()
删除展开:
obj={"apiVersion":"apps/v1","kind":"Deployment","metadata":{"name":"my-deploy","namespace":"gondor-system"}}pykube.Deployment(api,obj).delete()
检查服务器版本:
api=pykube.HTTPClient(pykube.KubeConfig.from_file())api.version
要求
- Python3.6+
- 请求(包含在install_requires中)
- pyyaml(包含在install_requires中)
地方发展
您可以对当前的kubeconfig上下文运行pykube,例如localMinikube:
pipenv install --dev pipenv run python3 >>> import pykube >>> config= pykube.KubeConfig.from_file() >>> api= pykube.HTTPClient(config) >>> list(pykube.Deployment.objects(api))
运行pep8(flake8)检查和单元测试,包括覆盖率报告:
make test
许可证
此项目中的代码是根据apache许可证2.0版授权的 (根据许可证包含在此存储库中)。
贡献
最简单的贡献方式就是提供反馈!我们很想知道你喜欢什么,你认为缺少什么。 创建问题或ping try_except_ on Twitter。
欢迎使用减贫战略。也请看一下issues labeled with “help wanted”。
行为准则
为了培养一个友好、包容和无骚扰的社区,这个项目遵循Contributor Covenant Code of Conduct。