

Kubernetes(以下简称k8s)是一个用于自动化部署、扩展和管理容器化应用的开源系统。
图片来源于网络
Python的k8s客户端是以python包的形式提供的,开源地址(https://github.com/kubernetes-client/python),可以通过pip命令下载安装。
直接通过 PyPI下载安装
pip install kubernetes从源码安装
git clone –-recursive https://github.com kubernetes-client/python.git cd python python setup.py install
配置kubernetes api
//导入包:
from kubernetes import client
from pprint import pprint //优化输出
k8s_api_url="YOUR_API_URL"
k8s_api_token="YOUR_API_TOKEN"
k8s_api_conf=kubernetes.client.Configuration()
k8s_api_conf.host=k8s_api_url //配置url
k8s_api_conf.verify_ssl=False //关闭SSL验证,可选择
k8s_api_conf.api_key = {"authorization": "Bearer " + k8s_api_token} //配置token
k8s_api_client=client.ApiClient(k8s_api_conf) //创建客户端
常用资源接口实例化
kubernetes的api接口极为丰富,本次仅介绍常用操作和会涉及到的类和方法,全部的接口文档可以在官方API文档(https://github.com/kubernetes-client/python/blob/master/kubernetes/README.md)处找到。
namespace、pod、node和service相关,使用CoreV1Api类实例
k8s_api_client_core=client.CoreV1Api(k8s_api_clientdeployment相关,使用AppsV1Api类实例
k8s_api_client_apps=client.AppsV1Api(k8s_api_clientingress相关,使用NetworkingV1beta1Api类实例
k8s_api_client_networking=client.NetworkingV1beta1Api(k8s_api_client)
常用操作
1)查看namespace
使用list_namespace方法
//可选参数:
//pretty str 如果值为"true",则美化输出
//limit int 限制输出数量
//timeout_second int 超时时间
try:
api_response=k8s_api_client_core.list_namespace(pretty="true")
pprint(api_response)
exception Exceptions as e:
print(e)
//使用read_namespace方法
//必要参数:
//name str namespace名
//可选参数:
//pretty str 如果值为"true",则美化输出
try:
api_response=k8s_api_client_core.read_namespace(name,pretty="true")
pprint(api_response)
exception Exceptions as e:
print(e)
2)创建namespace
//使用create_namespace方法
//必要参数:
//body V1Namespace 创建的namespace描述
//可选参数:
//pretty str 如果值为"true",则美化输出
//dry_run str 如果存在此参所则表示试运行,有效值为"ALL"
//field_manager str 该参数是与进行这些更改的参与者或实体关联的名称。该值的长度必须小于或等于128个字符,并且仅包含可打印字符
body=client.V1Namespace() 创建body
try:
api_response=k8s_api_client_core.create_namespace(body,pretty="true")
pprint(api_response)
exception Exceptions as e:
print(e)
3)删除namespace
//使用delete_namespace方法 //必要参数: //name str 删除的namespace名 //可选参数: //pretty str 如果值为"true",则美化输出 //dry_run str 如果存在此参所则表示试运行,有效值为"ALL" //grace_period_seconds int 表示延迟多少秒执行,非负数,当为0时立即执行 try:
api_response=k8s_api_client_core.delete_namespace(name,pretty="true")
pprint(api_response)
exception Exceptions as e:
print(e)
4)查看pod
//使用list_namespaced_pod方法
//必要参数:
//namespace str namespace名
//可选参数:
//pretty str 如果值为"true",则美化输出
//limit int 限制输出数量
//timeout_second int 超时时间
try:
api_response=k8s_api_client_core.list_namespaced_pod(namespace,pretty="true")
pprint(api_response)
exception Exceptions as e:
print(e)
//使用list_pod_for_all_namespaces方法 //可选参数: //pretty str 如果值为"true",则美化输出 //limit int 限制输出数量 //timeout_second int 超时时间 try:
api_response=k8s_api_client_core.list_pod_for_all_namespaces(pretty="true")
pprint(api_response)
exception Exceptions as e:
print(e)
//使用read_namespaced_pod方法 //必要参数: //name str pod名 //namespace str namespace名 //可选参数: //pretty str 如果值为"true",则美化输出 try:
api_response=k8s_api_client_core.read_namespaced_pod(name,namespace,pretty="true")
pprint(api_response)
exception Exceptions as e:
print(e)
5)查看node
//使用list_node方法
//可选参数:
//pretty str 如果值为"true",则美化输出
//limit int 限制输出数量
//timeout_second int 超时时间
try:
api_response=k8s_api_client_core.list_node(pretty="true")
pprint(api_response)
exception Exceptions as e:
print(e)
//使用read_node方法 //必要参数: //name str node名 //可选参数: //pretty str 如果值为"true",则美化输出 try:
api_response=k8s_api_client_core.read_node(name,pretty="true")
pprint(api_response)
exception Exceptions as e:
print(e)
6)查看deployment
//使用list_namespaced_deployment方法 //必要参数: //namespace str namespace名 //可选参数: //pretty str 如果值为"true",则美化输出 //limit int 限制输出数量 //timeout_second int 超时时间 try:
api_response=k8s_api_client_apps.list_namespaced_deployment(namespace,pretty="true")
pprint(api_response)
exception Exceptions as e:
print(e)
//使用list_deployment_for_all_namespaces方法 //可选参数: //pretty str 如果值为"true",则美化输出 //limit int 限制输出数量 //timeout_second int 超时时间 try:
api_response=k8s_api_client_apps.list_deployment_for_all_namespaces(pretty="true")
pprint(api_response)
exception Exceptions as e:
print(e)
//使用read_namespaced_deployment方法
//必要参数:
//name str deployment名
//namespace str namespace名
//可选参数:
//pretty str 如果值为"true",则美化输出
try:
api_response=k8s_api_client_apps.read_namespaced_deployment(name,namespace,pretty="true")
pprint(api_response)
exception Exceptions as e:
print(e)
7)删除deployment
//使用delete_namespaced_deployment方法 //必要参数: //name str deployment名 //namespace str namespace名 //可选参数: //pretty str 如果值为"true",则美化输出 //dry_run str 如果存在此参所则表示试运行,有效值为"ALL" //grace_period_seconds int 表示延迟多少秒执行,非负数,当为0时立即执行 try:
api_response=k8s_api_client_apps.delete_namespaced_deployment(name,namespace,pretty="true")
pprint(api_response)
exception Exceptions as e:
print(e)
8)删除pod/重启pod
//使用delete_namespaced_pod方法 //必要参数: //name str pod名 //namespace str namespace名 //可选参数: //pretty str 如果值为"true",则美化输出 //dry_run str 如果存在此参所则表示试运行,有效值为"ALL" //grace_period_seconds int 表示延迟多少秒执行,非负数,当为0时立即执行 try:
api_response=k8s_api_client_core.delete_namespaced_pod(name,namespace,pretty="true")
pprint(api_response)
exception Exceptions as e:
print(e)
9)修改deployment配置
//使用replace_namespaced_deployment方法 //必要参数: //name str deployment名 //namespace str namespace名 //body V1Deployment 修改后的配置 //可选参数: //pretty str 如果值为"true",则美化输出 //dry_run str 如果存在此参所则表示试运行,有效值为"ALL" //field_manager str 该参数是与进行这些更改的参与者或实体关联的名称。该值的长度必须小于或等于128个字符,并且仅包含可打印字符 try:
//先获取当前的deployment配置
body=k8s_api_client_apps.read_namespaced_deployment(name,namespace,pretty="true")
//修改deployment
some_change()
//替换deployment配置
api_response=k8s_api_client_apps.replace_namespaced_deployment(name,namespace,body,pretty="true")
pprint(api_response)
exception Exceptions as e:
print(e)

本文作者:李双修(上海新炬王翦团队)
本文来源:“IT那活儿”公众号





