一、安装

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 / Running 40s
mydeploy-6946c867dc-rdnvj / Running 40s
[root@k8s-m ~]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
mydeploy / 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 / Running 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[].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(, , , , , , 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[].image = "nginx:alpine"
#修改镜像
new_resp = k8s_core_v1.patch_namespaced_pod(namespace="default",name='nginx-pod',body=old_resp)
print(new_resp.spec.containers[].image)
if __name__ == '__main__':
main() [root@k8s-m ~]# python3 pp.py
nginx:alpine

python3 kubernetes api 使用的更多相关文章

  1. Centos7部署kubernetes API服务(四)

    1.准备软件包 [root@linux-node1 bin]# pwd /usr/local/src/kubernetes/server/bin [root@linux-node1 bin]# cp ...

  2. Gravitational Teleport 开源的通过ssh && kubernetes api 管理linux 服务器集群的网关

    Gravitational Teleport 是一个开源的通过ssh && kubernetes api 管理linux 服务器集群的网关 支持以下功能: 基于证书的身份认证 ssh ...

  3. kubernetes API Server 权限管理实践

    API Server权限控制方式介绍 API Server权限控制分为三种:Authentication(身份认证).Authorization(授权).AdmissionControl(准入控制). ...

  4. Kubernetes API server工作原理

    作为Kubernetes的使用者,每天用得最多的命令就是kubectl XXX了. kubectl其实就是一个控制台,主要提供的功能: 1. 提供Kubernetes集群管理的REST API接口,包 ...

  5. 资深专家深度剖析Kubernetes API Server第3章(共3章)

    在本系列的前两部分中我们介绍了API Server的总体流程,以及API对象如何存储到etcd中.在本文中我们将探讨如何扩展API资源. 在一开始的时候,扩展API资源的唯一方法是扩展相关API源代码 ...

  6. 资深专家深度剖析Kubernetes API Server第2章(共3章)

    欢迎来到深入学习Kubernetes API Server的系列文章的第二部分.在上一部分中我们对APIserver总体,相关术语及request请求流进行探讨说明.在本部分文章中,我们主要聚焦于探究 ...

  7. 资深专家深度剖析Kubernetes API Server第1章(共3章)

    欢迎来到深入学习Kubernetes API Server的系列文章,在本系列文章中我们将深入的探究Kubernetes API Server的相关实现.如果你对Kubernetes的内部实现机制比较 ...

  8. 深度剖析Kubernetes API Server三部曲 - part 3

    在本系列的前两部分中我们介绍了API Server的总体流程,以及API对象如何存储到etcd中.在本文中我们将探讨如何扩展API资源. 在一开始的时候,扩展API资源的唯一方法是扩展相关API源代码 ...

  9. 深度剖析Kubernetes API Server三部曲 - part 2

    欢迎来到深入学习Kubernetes API Server的系列文章的第二部分.在上一部分中我们对APIserver总体,相关术语及request请求流进行探讨说明.在本部分文章中,我们主要聚焦于探究 ...

随机推荐

  1. Java Linked集合的简单介绍和常用方法的使用

    LinkedList的简单介绍 java.util.LinkedList 集合数据存储的结构是链表结构.LinkedList是一个双向链表在实际开发中,对一个集合元素的添加和删除,经常涉及到首尾操作, ...

  2. 【转载】Notepad++源码分析

    在网上发现了一个哥们写了关于Notepad++源码的文章,不过就写了一就没有了,我就接着他的工作再说说吧! 大三了,也写了一点儿程序了,但是如果只是按照自己的思路写下去恐怕难以提高,于是准备开始阅读一 ...

  3. python3学习笔记一

    install 安装软件包download 下载安装包uninstall 卸载安装包freeze 按照req uirements 格式输出安装包,可以到其他服务器上执行pip install -r r ...

  4. 定义可选URL片段 定义自定义片段变量 精通ASP-NET-MVC-5-弗瑞曼

  5. 个人第四次作业——Alpha项目测试

    一.格式描述 这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/GeographicInformationScience/ 这个作业要求在哪里 https: ...

  6. 开发工具篇:Git和Github

    开发工具篇:Git和Github Git是什么? Git是目前世界上最先进的分布式版本控制系统.工作原理 / 流程: Workspace:工作区 Index / Stage:暂存区 Repositor ...

  7. Python中将变量按行写入txt文本中

    案例一: 讲数组a 循环写入名称为2.txt的文档中 # -*-coding:utf8-*- import requests from lxml import etree a=[1,2,3,4,5,6 ...

  8. Python的条件控制及循环

    一.条件控制: 1.If语句的使用: Python中if语句的一般形式如下所示: 上图中: 如果 "score>=90" 为 True 将执行 "print(‘优秀 ...

  9. PlayCanvas PBR材质shader代码分析(vertex shader)

    顶点shader主要对顶点坐标变换,将顶点坐标从local->world->view->clip 空间变换 local空间:模型物体坐标系 world空间:世界空间坐标系 view空 ...

  10. php--->查询超大文件(12G)

    今天遇到一个要在一个12G日志中查询数据的需求,手中暂时没有查询这种超大文件的工具,于是自己写了一个程度来读这个超大文件 其整体思路就是一行一行地去读取超大文件中的数据,然后将拿出的一行数据做相应的查 ...