圆月山庄资源网 Design By www.vgjia.com
一、安装
github:https://github.com/kubernetes-client/python
安装
pip install kubernetes
二、认证
1、kubeconfig文件认证
首先引入SDK支持库。然后将 ~/.kube 的config文件的内容复制到本地目录,保存为文件kubeconfig.yaml,然后运行下面的python代码。
[root@k8s-m ~]# cp .kube/config kubeconfig.yaml #使用 from kubernetes import client, config config.kube_config.load_kube_config(config_file="/root/kubeconfig.yaml")
三、api使用
1、列出资源信息
from kubernetes import client, config config.kube_config.load_kube_config(config_file="/root/kubeconfig.yaml") #获取API的CoreV1Api版本对象 v1 = client.CoreV1Api() #列出 namespaces for ns in v1.list_namespace().items: print(ns.metadata.name) #列出所有的services ret = v1.list_service_for_all_namespaces(watch=False) for i in ret.items: print("%s \t%s \t%s \t%s \t%s \n" % (i.kind, i.metadata.namespace, i.metadata.name, i.spec.cluster_ip, i.spec.ports )) #列出所有的pod ret = v1.list_pod_for_all_namespaces(watch=False) for i in ret.items: print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name)) #列出所有deploy ret = v1.list_deployments_for_all_namespaces(watch=False) for i in ret.items: print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name)) ##列出其他资源和以上类似,不懂可以查看(kubectl api-resources)
2、创建k8s资源对象
github:https://github.com/kubernetes-client/python/tree/master/examples
创建资源(提前写好yaml资源清单)
#创建deploy [root@k8s-m ~]# cat create_deploy.py from os import path import yaml from kubernetes import client, config def main(): config.load_kube_config() with open(path.join(path.dirname(__file__), "/root/deploy.yaml")) as f: dep = yaml.safe_load(f) k8s_apps_v1 = client.AppsV1Api() resp = k8s_apps_v1.create_namespaced_deployment( body=dep, namespace="default") print("Deployment created. status='%s'" % resp.metadata.name) main() [root@k8s-m ~]# kubectl get pod NAME READY STATUS RESTARTS AGE mydeploy-6946c867dc-bgcld 1/1 Running 0 40s mydeploy-6946c867dc-rdnvj 1/1 Running 0 40s [root@k8s-m ~]# kubectl get deploy NAME READY UP-TO-DATE AVAILABLE AGE mydeploy 2/2 2 2 44s #创建pod例子(其它资源得自己查源码自己找对应的API) [root@k8s-m ~]# cat create_pod.py from os import path import yaml from kubernetes import client, config def main(): config.load_kube_config() with open(path.join(path.dirname(__file__), "/root/pod.yaml")) as f: dep = yaml.safe_load(f) k8s_core_v1 = client.CoreV1Api() resp = k8s_core_v1.create_namespaced_pod( body=dep, namespace="default") print("Pod created. status='%s'" % resp.metadata.name) if __name__ == '__main__': main() ## [root@k8s-m ~]# python3 create_pod.py Pod created. status='nginx-pod' [root@k8s-m ~]# kubectl get pod nginx-pod NAME READY STATUS RESTARTS AGE nginx-pod 1/1 Running 0 8s
3、删除资源(我这里展示pod例子,其它资源删除差不多)
参考地址:/usr/local/python3/lib/python3.6/site-packages/kubernetes/client/
[root@k8s-m ~]# cat dp.py from os import path import yaml from kubernetes import client, config def main(): config.load_kube_config() k8s_core_v1 = client.CoreV1Api() resp = k8s_core_v1.delete_namespaced_pod(namespace="default",name='nginx-pod') print("delete Pod ") [root@k8s-m ~]# python3 dp.py delete Pod
4、查看资源(类似kubectl get pod xxx -o json)
#查看(read) [root@k8s-m ~]# cat rp.py from os import path import yaml from kubernetes import client, config def main(): config.load_kube_config() k8s_core_v1 = client.CoreV1Api() resp = k8s_core_v1.read_namespaced_pod(namespace="default",name='nginx-pod') print("read Pod ") #详细信息 print(resp) #指定信息 print(resp.spec.containers[0].image) if __name__ == '__main__': main() [root@k8s-m ~]# python3 rp.py |tail 'host_ip': '172.31.250.229', 'init_container_statuses': None, 'message': None, 'nominated_node_name': None, 'phase': 'Running', 'pod_ip': '10.244.167.134', 'qos_class': 'BestEffort', 'reason': None, 'start_time': datetime.datetime(2019, 8, 30, 9, 13, 49, tzinfo=tzutc())}} nginx
5、修改
[root@k8s-m ~]# cat pp.py from os import path import yaml from kubernetes import client, config def main(): config.load_kube_config() k8s_core_v1 = client.CoreV1Api() old_resp = k8s_core_v1.read_namespaced_pod(namespace="default",name='nginx-pod') old_resp.spec.containers[0].image = "nginx:alpine" #修改镜像 new_resp = k8s_core_v1.patch_namespaced_pod(namespace="default",name='nginx-pod',body=old_resp) print(new_resp.spec.containers[0].image) if __name__ == '__main__': main() [root@k8s-m ~]# python3 pp.py nginx:alpine
以上就是python3 kubernetes api的使用示例的详细内容,更多关于python3 kubernetes api的使用的资料请关注其它相关文章!
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
更新日志
2024年11月01日
2024年11月01日
- 群星《时光正好 电视剧影视原声带》[FLAC/分轨][234.11MB]
- 英雄联盟s14韩国队种子怎么排名 s14韩国队种子队伍排名一览
- 英雄联盟s14四强开始时间 英雄联盟s14四强赛程表一览
- 英雄联盟s14四强赛在哪举办 英雄联盟s14半决赛举办地点一览
- 《三国志8重制版》全女将一览
- 《英雄联盟》Faker第七次晋级决赛
- 《历史时代3》下载方法
- EchoVocalEnsemble-Innocence(2024)[WAV]
- BuceadorVoltio-Satelite(2024)[24-48]FLAC
- VilmPribyl-SmetanaDalibor(2024)[24Bit-WAV]
- 高通骁龙8至尊版亮相:性能领先A18 Pro达到40%,更有多项首发
- 2024骁龙峰会:自研Oryon CPU登陆手机、汽车丨骁龙8至尊版、骁龙至尊版汽车平台
- 稀有度拉满!首款小马宝莉背卡引爆网络热梗
- 群星《全糖少爷1 影视原声带》[320K/MP3][98.36MB]
- 群星《全糖少爷1 影视原声带》[FLAC/分轨][420.97MB]