python3 kubernetes api 使用
一、安装
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 使用的更多相关文章
- Centos7部署kubernetes API服务(四)
1.准备软件包 [root@linux-node1 bin]# pwd /usr/local/src/kubernetes/server/bin [root@linux-node1 bin]# cp ...
- Gravitational Teleport 开源的通过ssh && kubernetes api 管理linux 服务器集群的网关
Gravitational Teleport 是一个开源的通过ssh && kubernetes api 管理linux 服务器集群的网关 支持以下功能: 基于证书的身份认证 ssh ...
- kubernetes API Server 权限管理实践
API Server权限控制方式介绍 API Server权限控制分为三种:Authentication(身份认证).Authorization(授权).AdmissionControl(准入控制). ...
- Kubernetes API server工作原理
作为Kubernetes的使用者,每天用得最多的命令就是kubectl XXX了. kubectl其实就是一个控制台,主要提供的功能: 1. 提供Kubernetes集群管理的REST API接口,包 ...
- 资深专家深度剖析Kubernetes API Server第3章(共3章)
在本系列的前两部分中我们介绍了API Server的总体流程,以及API对象如何存储到etcd中.在本文中我们将探讨如何扩展API资源. 在一开始的时候,扩展API资源的唯一方法是扩展相关API源代码 ...
- 资深专家深度剖析Kubernetes API Server第2章(共3章)
欢迎来到深入学习Kubernetes API Server的系列文章的第二部分.在上一部分中我们对APIserver总体,相关术语及request请求流进行探讨说明.在本部分文章中,我们主要聚焦于探究 ...
- 资深专家深度剖析Kubernetes API Server第1章(共3章)
欢迎来到深入学习Kubernetes API Server的系列文章,在本系列文章中我们将深入的探究Kubernetes API Server的相关实现.如果你对Kubernetes的内部实现机制比较 ...
- 深度剖析Kubernetes API Server三部曲 - part 3
在本系列的前两部分中我们介绍了API Server的总体流程,以及API对象如何存储到etcd中.在本文中我们将探讨如何扩展API资源. 在一开始的时候,扩展API资源的唯一方法是扩展相关API源代码 ...
- 深度剖析Kubernetes API Server三部曲 - part 2
欢迎来到深入学习Kubernetes API Server的系列文章的第二部分.在上一部分中我们对APIserver总体,相关术语及request请求流进行探讨说明.在本部分文章中,我们主要聚焦于探究 ...
随机推荐
- JMeter——分布式压测
一.Jmeter4.0分布式压测准备工作 压测注意事项 the firewalls on the systems are turned off or correct ports ...
- matplotlib 条形图
一.特点 离散数据,数据之间没有直接的关系 二.分类 1.垂直条形图 bar(x, height, width=0.8) # x 为x轴 # height 为y轴 # width 为 条形图的宽度 例 ...
- python 进程Queue
1.作用:进程之间的数据交互 2.常用方法 """ 对象.put() 作用:放入队列一个数据 对象.get() 作用:取队列一个数据,若队列没有值,则阻塞 对象.empt ...
- mysql中更改字符集为utf8&&mysql中文输入不了问题解决
写给TT:对不起啦!! 嗯,输入不了中文,大多数问题是mysql的字符集设置的问题,当然,别的问题也有可能, 这里我们用两种方法设置mysql的字符集,图形化工具和命令行的方式(一种操作完即可) 一, ...
- atx测试框架实现手机应用UI自动化测试
最近工作中遇到游戏APP需要实现UI自动化测试,这个app中真的是典型的混合App,有Android原生控件,有webview控件,以及游戏操作页面.研究了Appium,发现appium实现跨应用操作 ...
- Python3实现发送邮件和发送短信验证码
Python3实现发送邮件和发送短信验证码 Python3实现发送邮件: import smtplib from email.mime.text import MIMEText from email. ...
- SqlServer 利用游标批量更新数据
SqlServer 利用游标批量更新数据 Intro 游标在有时候会很有用,在更新一部分不多的数据时,可以很方便的更新数据,不需要再写一个小工具来做了,直接写 SQL 就可以了 Sample 下面来看 ...
- SparkSQL 如何自定义函数
1. SparkSql如何自定义函数 2. 示例:Average 3. 类型安全的自定义函数 1. SparkSql如何自定义函数? spark中我们定义一个函数,需要继承 UserDefinedAg ...
- Git详解之安装
前言 是时候动手尝试下 Git 了,不过得先安装好它.有许多种安装方式,主要分为两种,一种是通过编译源代码来安装:另一种是使用为特定平台预编译好的安装包. 从源代码安装 若是条件允许,从源代码安装有很 ...
- spring cloud oauth2搭建认证中心与资源中心
一 认证中心搭建 添加依赖,如果使用spring cloud的话,不管哪个服务都只需要这一个封装好的依赖即可 <dependency> <groupId>org.springf ...